Below is a nice article on how to prevent detonation
A Dozen Ways to Prevent Detonation
Try a higher octane fuel. The octane rating of a given grade of gasoline is a measure of its detonation resistance. The higher the octane number, the better able the fuel is to resist detonation. Most engines in good condition will run fine on regular grade 87 octane fuel. But engines with high compression ratios (over 9:1), turbochargers, superchargers, or with accumulated carbon deposits in the combustion chamber may require 89 or higher octane fuel.
How a vehicle is used can also affect its octane requirements. If a vehicle is used for towing or some other application where the engine is forced to work hard under load, a higher octane fuel may be necessary to prevent detonation.
If switching to a higher octane fuel fails to eliminate a persistent detonation problem, it probably means something else is amiss. Anything that increases normal combustion temperatures or pressures, leans out the air/fuel mixture, or causes the engine to run hotter than normal can cause detonation.
Check for loss of EGR. The Exhaust Gas Recirculation (EGR) system is one of the engine's primary emission controls. Its purpose is to reduce oxides of nitrogen (NOX) pollution in the exhaust. It does this by "leaking" (re-circulating) small amounts of exhaust into the intake manifold through the EGR valve. Though the gases are hot, they actually have a cooling effect on combustion temperatures by diluting the air/fuel mixture slightly. Lowering the combustion temperature reduces the formation of NOX as well as the octane requirements of the engine.
If the EGR valve is not opening, either because the valve itself is defective or because its vacuum supply is blocked (loose, plugged or misrouted vacuum hose connections, or a defective vacuum control valve or solenoid), the cooling effect is lost. The result will be higher combustion temperatures under load and an increased chance of detonation.
Keep compression within reasonable limits. A static compression ratio of 9:1 is usually the recommended limit for most naturally aspirated street engines (though some newer engines with knock sensors can handle higher compression ratios).
Compression ratios over 10.5:1 may create a detonation problem even with 93 octane premium gasoline. So unless an engine is being built to run on racing fuel, keep the compression ratio within a reasonable range for pump gasoline. This, in turn, may require using lower compression pistons and/or cylinder heads with larger combustion chambers. Another option would be to use a copper head gasket shim with the stock head gasket to reduce compression.
Retarding the cam timing can also lower cylinder pressures to reduce detonation at low r.p.m., but doing so hurts low speed torque which is not recommended for street engines or cars with automatics. For supercharged or turbocharged applications, a static compression ratio of 8:1 or less may be required depending on the amount of boost pressure.
Another point to keep in mind is that boring an engine's cylinders to accept oversized pistons also increases the static compression ratio. So too does milling the cylinder heads. If such modifications are necessary to compensate for cylinder wear, head warpage or damage, you may have to use a thicker head gasket if one is available for the application or a head gasket shim (a dead soft copper spacer shim) to offset the increase in compression.
Check for over-advanced ignition timing. Too much spark advance can cause cylinder pressures to rise too rapidly. If resetting the timing to stock specifications doesn't help, retarding the timing a couple of degrees and/or recalibrating the distributor advance curve may be necessary to keep detonation under control.
Check for a defective knock sensor. Many late model engines have a "knock sensor" on the engine that responds to the frequency vibrations characteristically produced by detonation (typically 6-8kHz). The knock sensor produces a voltage signal that signals the computer to momentarily retard ignition timing until the detonation stops.
If the "check engine" light is on, check the vehicle's onboard computer system using the prescribed procedure for a "trouble code" that would correspond to a bad knock sensor (code varies to manufacturer's specifications). A knock sensor can usually be tested by rapping a wrench on the manifold near the sensor (never hit the sensor itself!) and watching for the timing change while the engine is idling. If the timing fails to retard, the sensor may be defective -- or the problem may be within the electronic spark timing control circuitry of the computer itself. To determine the cause, you'll have to refer to the appropriate diagnostic chart in a service manual and follow the step-by-step test procedures to isolate the cause.
Sometimes a knock sensor will react to sounds other than those produced by detonation. A noisy mechanical fuel pump, a bad water pump or alternator bearing, or a loose rod bearing can all produce vibrations that can trick a knock sensor into retarding timing.
"Read" your spark plugs. The wrong heat range plug can cause detonation as well as pre ignition. If the insulators around the electrodes on your plugs appear yellowish or blistered, they may be too hot for the application. Try the next heat range colder spark plug. Copper core spark plugs generally have a broader heat range than ordinary plugs, which lessens the danger of detonation.
Check for engine overheating. A hot engine is more likely to suffer spark knock than one which runs at normal temperature. Overheating can be caused by a low coolant level, a slipping fan clutch, too small a fan, too hot a thermostat, a bad water pump, or even a missing fan shroud. Poor heat conduction in the head and water jackets can be caused by a buildup of lime deposits or steam pockets (which can result from trapped air pockets).
Check the operation of the heated air intake system. The thermostatically controlled air cleaner's job is to provide a carbureted engine with hot air when the engine is cold started. This aids fuel vaporization during engine warm-up. If the air control door sticks shut or is slow to open so that the carburetor continues to receive heated air after the engine is warm, the added heat may be enough to cause a detonation problem -- especially during hot weather. Check the operation of the air flow control door in the air cleaner to see that it opens as the engine warms up. No movement may mean the vacuum motor or thermostat is defective. Also, check the heat riser valve to make sure it's opening properly, as it, too, can affect the air intake system.
Check for a lean fuel mixture. Rich fuel mixtures resist detonation while lean ones do not. Air leaks in vacuum lines, intake manifold gaskets, carburetor gaskets or the induction plumbing downstream of a fuel injection throttle can all admit extra air into the engine and lean out the fuel mixture. Lean mixtures can also be caused by dirty fuel injectors, carburetor jets clogged with fuel deposits or dirt, a restricted fuel filter or a weak fuel pump.
If the fuel mixture becomes too lean, "lean misfire" may occur as the load on the engine increases. This can cause a hesitation, stumble and/or rough idle problem as well. The air/fuel ratio can also be affected by changes in altitude. As you go up in elevation, the air becomes less dense.
A carburetor that's calibrated for high altitude driving will run too lean if driven at a lower elevation. Altitude changes are generally not a problem with engines that have electronic feedback carburetors or electronic fuel injection because the oxygen and barometric pressure sensors compensate for changes in air density and fuel ratios.
Remove carbon deposits. An accumulation of carbon deposits in the combustion chamber and on the top of the pistons can increase compression to the point where detonation becomes a problem. Carbon deposits are a common cause of detonation in high-mileage engines, and can be especially thick if the engine consumes oil because of worn valve guides and seals, worn or broken piston rings and/or cylinder wear. Infrequent driving and not changing the oil often enough can also accelerate the buildup of deposits.
In addition to increasing compression, carbon deposits also have an insulating effect that slows the normal transfer of heat away from the combustion chamber into the head. A thick layer of deposits can therefore raise combustion temperatures and contribute to "pre ignition" as well as detonation.
Carbon deposits can often be removed from an engine that's still in service by using a chemical "top cleaner." This type of product is poured into an idling engine through the carburetor or throttle body. The engine is then shut off so the solvent can soak into and loosen the deposits. When the engine is restarted the deposits are blown out of the combustion chamber.
If chemical cleaning fails to remove the deposits, it may be necessary to pull the cylinder head and scrape the deposits off with a wire brush or scraper (be careful not to scratch the face of the cylinder head or engine deck!).