Arduino UNO Based Ultrasonic Distance Sensor with Alert Buzzer and LED Indicator
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to utilize an Arduino UNO microcontroller to interface with an HC-SR04 Ultrasonic Sensor for distance measurement. When an object is detected within a predefined threshold of 10 cm, the circuit activates a buzzer and lights up an LED. The purpose of this setup is to provide a simple proximity alert system.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
Buzzer
- An electromechanical component that produces sound
- Typically used for audible alerts or alarms.
HC-SR04 Ultrasonic Sensor
- A sensor that measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.
- It has four pins: VCC, TRIG, ECHO, and GND.
LED: Two Pin (red)
- A simple light-emitting diode that emits red light when powered.
- It has two pins: anode and cathode.
Wiring Details
Arduino UNO
- 5V connected to HC-SR04 VCC
- GND connected to HC-SR04 GND, Buzzer GND, and LED cathode
- D9 connected to HC-SR04 TRIG
- D10 connected to HC-SR04 ECHO
- D11 connected to Buzzer PIN and LED anode
Buzzer
- PIN connected to Arduino UNO D11
- GND connected to Arduino UNO GND
HC-SR04 Ultrasonic Sensor
- VCC connected to Arduino UNO 5V
- TRIG connected to Arduino UNO D9
- ECHO connected to Arduino UNO D10
- GND connected to Arduino UNO GND
LED: Two Pin (red)
- Anode connected to Arduino UNO D11
- Cathode connected to Arduino UNO GND
Documented Code
/*
* This Arduino Sketch uses an HC-SR04 Ultrasonic Sensor to measure distance.
* If an object is detected within 10 cm, a buzzer sounds and an LED lights up.
*/
#define TRIG_PIN 9
#define ECHO_PIN 10
#define BUZZER_PIN 11
#define LED_PIN 11
#define DISTANCE_THRESHOLD 10 // Distance threshold in cm
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < DISTANCE_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
delay(500);
}
The code is written for the Arduino UNO and is responsible for controlling the HC-SR04 Ultrasonic Sensor, the buzzer, and the LED. It sets up the necessary pins as input or output, performs distance measurement in the loop function, and activates the buzzer and LED when the distance threshold condition is met. Serial communication is also established for monitoring the distance measurements.