I’ve always wanted to play around with Arduino Ultrasound sensor, but the idea of spending 30$ + shipping on a device that I may not use always sounded not that great. Luckily, Dealextreme.com had a copy version of it, called HC-SR04 Ultrasonic Sensor Distance Measuring Module (Affiliate link) priced at $4.60. Not so bad, shall we try it?

Plugging in the sensor

Only upon delivery have I noticed one tiny problem: the original Ping sensor had 3 pins, while my copy version has 4. So I connected the pins this way:

  • VCC -> Arduino +5V pin
  • GND -> Arduino GND pin
  • Trig -> Arduino Digital Pin 2
  • Echo -> Arduino Digital Pin 2

Connecting HC-SR04 sensor

The Trig pin will be used to send the signal and the Echo pin will be used to listen for returning signal.

After my messy wiring the end assembly looked like this:

End sensor assembly

Getting data from the sensor

The code is fairly straightforward. I took the original example code for Ping))) sensor and just modified the OUTPUT pin to be Digital Pin 2, the INPUT pin to be Digital Pin 4 and trigger duration to 10 us.

After compiling, uploading and running it, in the Serial Monitor (Tools -> Serial Monitor or Ctrl + Shift + M) the sensor was sending correct data!

Sensor sending correct data

After a bit of fooling around I found out that the range is approximately 2cm - 300cm. Which is not bad, but not perfect either.