HC-SR04 Ping Sensor Hardware Mod


HC-SR04 ultrasonic sensor modules, also commonly called ping sensors, are practically a dime a dozen, with many Asia online retailers selling them in the $2 to $3 dollar range each.  While possibly not intended for an end solution or product, these sensors offer a more affordable option for prototyping and application development compared to the original and possibly more well-known ping sensor sold by Parallax, or transducers sold by Maxbotix.

If you intend to do your prototyping with the HC-SR04 module, you may find yourself having to write slightly customized code to drive the HC-SR04, which has one more pin compared to the Parallax ping sensor.  This is because the HC-SR04 has separate pins for the trigger and echo function, where the Parallax module combines this functionality into one pin.  For example, the Arduino “ping” example sensor sketch was written for the Parallax ping sensor and will not work “out-of-the-box” with the HC-SR04 as it only has provisions in the code for one signal pin.

The Arduino “ping” example sketch works by setting the “pingPin”, which connects to the Parallax “Sig” pin, to an output, sending the pulse to generate the 40kHz sonar ping and then immediately setting the “pingPin” to an input and using the pulseIn() function to wait for the resulting pulse width generated by the time taken for the echo to be received.

To drive the HC-SR-04 you must modify the sketch to use two of the Arduino IO pins, one for the ‘Trig’ signal and one for the ‘Echo’ signal.  The Arduino IO pin connected to the ‘Trig’ pin must be set to an output at all times and is used only for generating the trigger pulse.  The Arduino IO pin connected to the ‘Echo’ signal must be set to an input at all times and is only used for receiving the pulse width signal for the echo return. Here is an illustration of this:

The code snippet below shows a modified version of the Arduino “ping” example sketch for driving the HC-SR04 with a separate trigger and echo pin. Notice the pinMode() functions are relocated to the setup() routine since they never have to change once they have been set.

But what if you intend to use the Parallax sensor in the end, or one of the high-end Maxbotix sensors, but don’t want to make any code changes when you implement the final sensor? it would be nice if the same software and connections worked across all of these sensors without making frequent changes when going back and forth between sensors.

Well, there is a hardware hack you can perform on the HC-SR04 that will make if function virtually identical to the Parallax ping sensor. Simply connect a 10k ohm resistor across the ‘Trig’ and ‘Echo’ pin and remove the 10k pullup on the HC-SR04 attached to the ‘Trig’ pin (circled in red below).  The ‘Trig’ pin now becomes a multipurpose pin similar to the ‘Sig’ pin on the Parallax module, and the ‘Echo’ pin must then remain unconnected.

This modification allows the ‘Echo’ pin to drive both the HC-SR04 ‘Trig’ pin and the Arduino ‘pingPin’ when it is in a high-impedance input mode during echo receive.  Conversely it also allows the Arduino ‘pingPin’ to drive the HC-SR04 ‘Trig’ pin in output mode when triggering a ping. Note: make sure you do not enable the internal pullup on the microcontroller input pin, or you will effectively have a voltage divider and the ‘Trig’ pin on the HC-SR04 will only get pulled down to 2.5V instead of the minimum voltage threshold it needs to see a logic ‘low’.

Leave a comment

Your email address will not be published. Required fields are marked *