Circuit Documentation
Summary
This circuit consists of an Arduino UNO microcontroller, an HC-SR04 Ultrasonic Sensor, and a buzzer. The purpose of the circuit is to measure the distance using the ultrasonic sensor and trigger the buzzer if the measured distance is less than 50 cm. The Arduino UNO processes the sensor data and controls the buzzer accordingly.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
Buzzer
- Description: An audio signaling device.
- Pins: PIN, GND
Wiring Details
Arduino UNO
- GND: Connected to GND of HC-SR04 Ultrasonic Sensor and GND of Buzzer
- 5V: Connected to VCC of HC-SR04 Ultrasonic Sensor
- D2: Connected to PIN of Buzzer
- D5: Connected to ECHO of HC-SR04 Ultrasonic Sensor
- D6: Connected to TRIG of HC-SR04 Ultrasonic Sensor
HC-SR04 Ultrasonic Sensor
- GND: Connected to GND of Arduino UNO
- VCC: Connected to 5V of Arduino UNO
- TRIG: Connected to D6 of Arduino UNO
- ECHO: Connected to D5 of Arduino UNO
Buzzer
- PIN: Connected to D2 of Arduino UNO
- GND: Connected to GND of Arduino UNO
Documented Code
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
new_delay = (distance * 3) + 30;
Serial.print(distance);
Serial.println(" cm");
if (distance < 50)
{
digitalWrite(buzzer, HIGH);
delay(new_delay);
digitalWrite(buzzer, LOW);
}
else
{
digitalWrite(buzzer, LOW);
}
delay(200);
}
Code Explanation
- setup(): Initializes the serial communication and sets the pin modes for the TRIG, ECHO, and buzzer pins.
- loop(): Continuously measures the distance using the HC-SR04 sensor. If the distance is less than 50 cm, the buzzer is activated with a delay proportional to the distance. The distance is also printed to the serial monitor.
