Detecting obstacles and warning – Arduino, ultrasonic and buzzer


Detecting obstacles and warning – Arduino, ultrasonic and buzzer

Arduino, ultrasonic and buzzer

Understand ultrasonic and Buzzer and you can go deep towards learning Arduino.

Here are some few steps:

Step 1:

Material Required

+5V buzzer

Arduino cable

Test Board

Arduino UNO board

  1. Arduino cable
  2. +5V buzzer
  3. Arduino UNO board
  4. Ultrasonic sensor
  5. Male to male pins
  6. Test Board

Step 2:

How to connect your circuit

Connection Diagram

Connect Material

Connected Ardiano with laptop

The negative terminal of the Buzzer should be connected to the GND and the positive terminal to be connected to the Arduino pin 2.

To +5v pin VCC pin of ultrasonic should be connected and Gnd to the ground.

Echo pin to be connected with pin 9 and Trig pin to be connected to pin 10

Connection is visible in the pin.

Step 3:

To the Arduino code should be loaded

Arduino code loaded

Find the code in here; it is well explained and remarked.

Below is the copy of the code anyhow.


This code works in order to give a warning cross the buzzer when something is closer than that of 0.5 meter.

Circuit is ultrasonic sensor and buzzer +5v and Arduino UNO is required:

+201153300223 */

// Define pins for ultrasonic and buzzer

int const trigPin = 10; int const echoPin = 9;

int const buzzPin = 2;

void setup() {

pinMode(trigPin, OUTPUT);

// trig pin will have pulses output

pinMode(echoPin, INPUT);

// echo pin should be input to get pulse width

pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering }

void loop() {

// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters

int duration, distance;

// Output pulse with 1ms width on trigPin

digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

// Measure the pulse input in echo pin

duration = pulseIn(echoPin, HIGH);

// Distance is half the duration devided by 29.1 (from datasheet)

distance = (duration/2) / 29.1;

// if distance less than 0.5 meter and more than 0 (0 or less means over range)

if (distance <= 50 && distance >= 0) {

// Buzz

digitalWrite(buzzPin, HIGH);

} else {

// Don’t buzz

digitalWrite(buzzPin, LOW);


// Waiting 60 ms won’t hurt any one



Now you should Try it.


8.0 Arduino


  • Learning 8
  • User Ratings (0 Votes) 0


About Author

Leave A Reply