Arduino Lab 8: Obstacle Detection
Background: An ultrasonic sensor can be used in so many ways, from obstacle detection to touchless door opening. Here we will demo one such application of the ultrasonic sensor.
Goal: Create a system that alerts the user of an obstacle in front of them using an ultrasonic sensor and a buzzer.
HC-SR04 Ultrasonic Sensor Background Info
- Works like sonar. Emits high-frequency sound waves and calculates the time until the waves are reflected backward.
- Send a trigger signal (digital out) then monitor echo signal (digital in) to see how far away the objects are.
Circuit Setup
- Materials
- 1x Arduino
- 1x HC-SR04 Ultrasonic Sensor
- 1x Active Buzzer (buzzer with black potting on the underside, not the "speaker").
- Setup
- Buzzer
- Connect “+” side of speaker to digital pin 2.
- Connect other side of speaker to GND.
- Ultrasonic Sensor
- VCC – 5V
- Trig – connected as a digital output to digital pin 3
- Echo – connected as a digital input to digital pin 4
- GND – GND
Lab Instructions
- Use the project below as your starting point.
- Configure the 2 pins of the ultrasonic sensor as either INPUT or OUTPUT based on their usage.
- The trigger pin will be configured as an OUTPUT.
- The echo pin will be configured as an INPUT.
- To read a value from the ultrasonic sensor, use the function get_distance() that I provided.
- get_distance() takes no arguments.
- get_distance() returns an integer value that is how far away an object is in inches.
- When object gets closer than the threshold, set off an alarm.
- To play a tone with the active buzzer, just turn on pin 2 using digitalWrite(). To stop playing the tone, turn off pin 2 using digitalWrite(). See the speaker/buzzer page for more info.
- You can use the passive buzzer/speaker to play a song signifying there is an intruder. See the speaker/buzzer page to learn how to use the tone() function.
https://create.arduino.cc/editor/mjdargen_ncsu/82968114-194e-41b9-9272-3c739f417a1f/preview