0% found this document useful (0 votes)
76 views1 page

Detecting Obstacles and Warning - Arduino and Ultrasonic

This Arduino code uses an ultrasonic sensor and buzzer to detect obstacles and provide a warning. It defines pins for the ultrasonic trigger, echo, and buzzer. In the setup, it configures the trigger pin as output and echo and buzzer pins as input. The main loop sends a pulse on trigger, measures the echo duration to calculate distance, and triggers the buzzer if the distance is less than 0.5 meters.

Uploaded by

Zasni Zaxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views1 page

Detecting Obstacles and Warning - Arduino and Ultrasonic

This Arduino code uses an ultrasonic sensor and buzzer to detect obstacles and provide a warning. It defines pins for the ultrasonic trigger, echo, and buzzer. In the setup, it configures the trigger pin as output and echo and buzzer pins as input. The main loop sends a pulse on trigger, measures the echo duration to calculate distance, and triggers the buzzer if the distance is less than 0.5 meters.

Uploaded by

Zasni Zaxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Detecting Obstacles and Warning - Arduino and Ultrasonic

/* This code should work to get warning cross the buzzer when
something be closer than 0.5 meter Circuit is ultrasonic sensor and
buzzer +5v and Arduino uno is used
[Link]
obstacles-and-warning-arduino-and-ultrasonic-13e5ea?
ref=tag&ref_id=arduino&offset=4 */

// 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);
delay(1);
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
delay(60);
}

You might also like