Introduction: Obstacle Detector - Using Ultrasonic Distance Sensor to Turn on Buzzer and LED

About: I'm a high school senior who is very interested in coding, robotics, drawing, dancing, video making, and photography.

Hey guys! Want to learn how to detect obstacle and react to it? Well here you've got the perfect tutorial on how to do so! In this instructable, I will guide you through the simple steps needed to turn a buzzer and LED on when an obstacle is detected and turn it back off when it moves away.

Step 1: Step 1: Downloading the Arduino IDE

Download and Install the Arduino IDE (Interactive Development Environment) using the link below: Choose and save the version that best suits your operating system and configuration.

Step 2: Step 2: Hardware Materials

  1. 1 Arduino board
  2. 1 breadboard
  3. 1 LED
  4. 1 piezo speaker
  5. 1 Ultrasonic sensor
  6. Jumper wires
  7. Resistors

Step 3: Step 3: Building the Hardware

1) Add the LED to the breadboard. Connect the short leg of the LED to Ground, long leg of LED to pin 7 with jumper wires and resistors shown in the diagram.

2) Add the piezo speaker to the breadboard. Connect the short leg of the piezo speaker to Ground, long leg to pin 9 with jumper wires and resistors shown in the diagram.

3) Add the ultrasonic sensor to the breadboard. There are 4 pins in the ultrasonic sensor. They are Vcc (5V power supply), Trig (Trigger), Echo, Gnd (Groud). Connect Vcc to 5V power supply, Gnd to Ground, Echo to pin 13, Trigger to pin 11 with jumper wires and resistors shown in the diagram.

4) Above picture shows how the connections should look like.

Step 4: Step 4: Downloading and Running the Program

Download the attached arduino program to your laptop. Connect the arduino to your laptop, and run the program. When you bring an obstacle closer to the ultrasonic sensor, you will see that the piezo speaker starts buzzing and LED glows. When you move the obstacle away the buzzing stops and the LED turns off.

Step 5: Step 5: Understanding the Program

LED has two methods:

  • digitalWrite(ledPinNumber, HIGH) which sends a HIGH signal to LED making it glow.
  • digitalWrite(ledPinNumber, LOW) which sends a LOW signal to LED turning it off.

Piezo has two methods:

  • tone(piezoPinNumber, delay) which makes piezo to buzz
  • noTone(piezoPinNumber) which makes piezo to switch off

Next let's understand how ultrasonic sensor works. An Ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It measures distance by sending out a sound wave at a specific frequency and listening for that sound wave to bounce back.It works by sending out a burst of ultrasound and listening for the echo when it bounces off of an object. It pings the obstacles with ultrasound. The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the same pin using the pulseIn() function. pulseIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the timing. At the end the function will return the length of the pulse in microseconds. For getting the distance we will multiply the duration by 0.034 and divide it by 2 as explained in this equation.

When the distance is less than 10 cm, the tone method is called causing the piezo speaker to buzz. Otherwise the noTone method is called causing the piezo speaker to turn off.