HC-SR04 Ultrasonic Distance Sensor Audio Alert with Arduino

With an HC-SR04 and a Piezo Buzzer you can create an audio distance sensor like the one many cars have for backing up.

Functional Parts in the Project:

#define trigPin 8
#define echoPin 9
#define buzzer 10

long duration;
float distanceInch;
int timer;

void setup() {
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(buzzer, OUTPUT);

void loop() {
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distanceInch = duration * 0.0133 /2;

digitalWrite(buzzer, HIGH);
digitalWrite(buzzer, LOW);

timer = distanceInch * 10;


Be the first to comment

Leave a Reply