Circuit Documentation
Summary
This circuit is designed to interface an Arduino UNO with an HC-SR04 Ultrasonic Sensor, a red two-pin LED, and a buzzer. The Arduino UNO is programmed to trigger the ultrasonic sensor to measure distance and based on the measured distance, it controls the state of the LED and buzzer. The LED and buzzer act as indicators for a certain distance threshold.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central processing unit for the circuit, controlling the LED, buzzer, and reading data from the ultrasonic sensor.
LED: Two Pin (red)
- Description: A basic red light-emitting diode.
- Purpose: Serves as a visual indicator when a certain condition is met in the circuit.
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic ranging module that provides 2cm to 400cm non-contact measurement functionality.
- Purpose: Measures the distance to an object and sends this data to the Arduino.
Buzzer
- Description: An electronic buzzer that can produce a tone when powered.
- Purpose: Acts as an audible indicator when a certain condition is met in the circuit.
Wiring Details
Arduino UNO
- Digital Pin 8: Connected to the anode of the red LED.
- Digital Pin 7: Connected to the PIN of the buzzer.
- Digital Pin 4: Connected to the ECHO pin of the HC-SR04 Ultrasonic Sensor.
- Digital Pin 2: Connected to the TRIG pin of the HC-SR04 Ultrasonic Sensor.
- 5V: Provides power to the VCC pin of the HC-SR04 Ultrasonic Sensor.
- GND: Connected to the cathode of the red LED, GND of the buzzer, and GND of the HC-SR04 Ultrasonic Sensor.
LED: Two Pin (red)
- Anode: Connected to Digital Pin 8 of the Arduino UNO.
- Cathode: Connected to GND of the Arduino UNO.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V of the Arduino UNO.
- TRIG: Connected to Digital Pin 2 of the Arduino UNO.
- ECHO: Connected to Digital Pin 4 of the Arduino UNO.
- GND: Connected to GND of the Arduino UNO.
Buzzer
- PIN: Connected to Digital Pin 7 of the Arduino UNO.
- GND: Connected to GND of the Arduino UNO.
Documented Code
#define trigPin 2
#define echoPin 4
#define ledPin 8
#define buzzerPin 7
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration / 74 / 2;
if (distance < 5) {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
}
Code Explanation
trigPin
and echoPin
are defined for interfacing with the HC-SR04 Ultrasonic Sensor.
ledPin
and buzzerPin
are defined for controlling the LED and buzzer respectively.
- In the
setup()
function, the pins are configured as input or output as required.
- The
loop()
function triggers the ultrasonic sensor and reads the distance. If the distance is less than 5 cm, the LED is turned on and the buzzer is activated. If the distance is greater or equal to 5 cm, both the LED and buzzer are turned off.