Introduction: (HC-SR04) Ultrasonic Arduino? Easy!

Here is how you can get your Ultrasonic Sensor (HC-SR04) working in less than 5 minutes!

You will need:

Connections:

  • Gnd -> pin 13
  • Echo -> pin 12
  • Trig -> pin 11
  • Vcc -> pin 10

Code:

Done:

The measured distance is sent to your computer through the serial port, in centimeters and inches!

Troubleshooting:

  • Make sure you connected the module properly
  • Make sure that the Serial Monitor is set to 9600 Baud
  • You may need to install additional drivers if using a non-genuine Arduino

Disclaimer/Improvements:

I am aware that it is not ideal to use digital pins for VCC and Ground, but works well for this example plus it eliminates the need for using a breadboard.Once you've got the code up and running, you can experiment with different ranges or connecting multiple modules. For more precision you can try disabling Interrupts and digging deeper into the lower-level functionality of the Atmega chip. You can check the sensor's datasheet for detailed specifications [HC-SR04].