Engine detonation, which occurs when the air/fuel mixture occurs outside the combustion cylinder, is more than just a nuisance due to the pinging noise it makes -- the detonation produces severe shocks that can easily ruin engine components like pistons, spark plugs, head gaskets, and others. For this reason, you should prevent detonation by:
Using High-Octane Fuel
The octane rating of a fuel is an indicator of how well it resist auto ignition when it is subjected to high pressure and heat. Thus, fuel with a high octane rating is less likely to ignite before the spark plug fires (as compared to one with a lower rating).
A high octane rating is especially useful if you are overloading the engine, for example, by towing a heavy load. Under ideal circumstances, the switch should eliminate the problem, but this is not always the case.
Reduce Engine Compression
As hinted above, an overly compressed air/fuel mixture is more likely to detonate than an uncompressed one. Therefore, one way of preventing the problem is to reduce your engine's compression ratio. The recommended compression ratio depends on your engine type. For example, some newer engines with knock sensors can handle higher compressions than conventional naturally aspirated engines. Reducing the engine compression ratio is a professional mechanic's job.
Use the Correct Spark Plugs
Damaged or wrong spark plugs may also cause engine detonation. This may happen, for example, if your spark plugs have a higher heat range or have damaged insulators making them too hot for your combustion system. This is something you can check for yourself: spark plugs heat ratings are given in numbers, with lower numbers representing colder spark plugs. Confirm with your engine's manufacturer or mechanic the right spark plug for your car. You can also examine the individual plugs for burn marks or blistering near the electrodes, which means they are damaged and should be replaced.
Have the Exhaust Gas Recirculation (EGR) System Optimized
The EGR system is meant to reduce the amount of dangerous gases, mainly oxides of nitrogen, escaping into the atmosphere. It does this by recirculating some of the exhaust gases back to the engine to help with the adjustment of the air/fuel ratio. It does this by mixing with the air/fuel mixture, an action that also lowers the mixtures temperature. If the EGR system is damaged and the requisite sample of the exhaust gases do not get back to the engine, then this cooling effect disappears, and the mixture may detonate. Calibration of the EGR system is also a professional mechanic's job.
As you can see, there are several possible causes of engine detonation. Thus, if you have tried lowering the car's load, switching to high octane fuel and other changes you can do on your own, then it is time to visit the mechanic like AutoRx Auto Repair for a professional diagnosis and fix.