Arduino - How to detect obstacles/objects and react?

How to detect an obstacle with an Arduino and HC-SR04 Ultrasonic Module?

In this post we will detect an obstacle using the HC-SR04 Ultrasonic Ranging module with an Arduino. Basically, we will write an embedded C program for Arduino to read the Ultrasonic sensor data every second, followed by converting the sensor data into distance and in the end run a piece of logic that if the detected object (i.e. motion due to moving object) is within 10 centimeters then a Piezo Buzzer will be beeped. Check out the Youtube video below demonstrating my experiment with detecting motion.





The breadboard setup for detecting an obstacle has been provided below as a reference. You may use this wiring scheme for hooking up your HC-SR04 along with the Piezo Buzzer to your Arduino. Be careful not to connect the wrong wires, as it might result in a short circuit.


Figure 1 - Breadboard Wiring for obstacle detection 

The figure below shows the schematic wiring diagram for detecting an obstacle.


Figure 2 - Schematic wiring for obstacle detection

Join the wiring as follows:
  • HC-SR04: Connect Vcc (on HC-SR04) to 5V (on Arduino)
  • HC-SR04: Connect Gnd (on HC-SR04) to Gnd (on Arduino)
  • HC-SR04: Connect Trig (on HC-SR04) to Pin 4 (on Arduino)
  • HC-SR04: Connect Echo (on HC-SR04) to Pin 12 (on Arduino)
  • Piezo Buzzer: Connect the + Terminal (Longer leg) to Pin 8 (on Arduino)
  • Piezo Buzzer: Connect the - Terminal (Shorter leg) to Gnd (on Arduino)

That C sketch for detecting an obstacle with the Arduino is very simple and is provided for reference below.



Hope you enjoyed this post. Please share this post if you found it useful and write your comments below this post. I would love to read your your thoughts and experiences with the HC-SR04 Ultrasonic Ranging module.

In my next post, I will show you how you can use your Arduino and HC-SR04 to create a reverse parking alarm for a car!

Cheers!
KBRC

No comments:

Post a Comment