(HC-SR04) Ultrasonic Arduino? Easy!

1.9K263

Intro: (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].

3 Comments

Thank you. You definitely helped me to easily get my HC-SR04 up and running quickly!

Can you explain where you derived the following:

cm=duration/58;
inches=duration/148;

and why you limited the readout to between 3 - 100cm?

How do you view the data please?

Cool! I've always liked to include ultrasonic sensors in my projects!