Arduino Nano-Based Ultrasonic Distance Sensor with Buzzer and Vibration Motor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to measure distance using an HC-SR04 Ultrasonic Sensor and trigger a buzzer and a vibration motor if the measured distance is below a certain threshold. The circuit is powered by a 3.7V battery and controlled by an Arduino Nano. A rocker switch is used to turn the circuit on and off.
Component List
Arduino Nano
- Description: A small, complete, and breadboard-friendly board based on the ATmega328P.
- Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
Buzzer
- Description: An electronic device that emits sound when powered.
- Pins: PIN, GND
3.7V Battery
- Description: A rechargeable lithium-ion battery.
- Pins: +, -
HC-SR04 Ultrasonic Sensor
- Description: A sensor that uses ultrasonic waves to measure distance.
- Pins: VCC, TRIG, ECHO, GND
Vibration Motor
- Description: A small motor that vibrates when powered.
- Pins: +, -
Rocker Switch
- Description: A switch that can be toggled between on and off positions.
- Pins: output, input
Wiring Details
Arduino Nano
- GND: Connected to GND of the Buzzer, HC-SR04 Ultrasonic Sensor, and Vibration Motor.
- D2: Connected to ECHO pin of the HC-SR04 Ultrasonic Sensor.
- D3: Connected to TRIG pin of the HC-SR04 Ultrasonic Sensor.
- D5: Connected to PIN of the Buzzer.
- D6: Connected to + pin of the Vibration Motor.
- VIN: Connected to input pin of the Rocker Switch.
- 5V: Connected to VCC pin of the HC-SR04 Ultrasonic Sensor.
Buzzer
- GND: Connected to GND of the Arduino Nano.
- PIN: Connected to D5 of the Arduino Nano.
3.7V Battery
- -: Connected to GND of the Arduino Nano.
- +: Connected to output pin of the Rocker Switch.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V of the Arduino Nano.
- TRIG: Connected to D3 of the Arduino Nano.
- ECHO: Connected to D2 of the Arduino Nano.
- GND: Connected to GND of the Arduino Nano.
Vibration Motor
- +: Connected to D6 of the Arduino Nano.
- -: Connected to GND of the Arduino Nano.
Rocker Switch
- input: Connected to VIN of the Arduino Nano.
- output: Connected to + of the 3.7V Battery.
Code Documentation
#define TRIG_PIN 3
#define ECHO_PIN 2
#define BUZZER_PIN 5
#define VIBRATION_MOTOR_PIN 6
#define DISTANCE_THRESHOLD 20 // Distance threshold in centimeters
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(VIBRATION_MOTOR_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
long duration, distance;
// Send ultrasonic pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the duration of the echo pulse
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance
distance = (duration / 2) / 29.1;
// Print distance to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If distance is less than the threshold, activate buzzer and vibration motor
if (distance < DISTANCE_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(VIBRATION_MOTOR_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(VIBRATION_MOTOR_PIN, LOW);
}
delay(500); // Delay 500 milliseconds
}
This code initializes the pins for the ultrasonic sensor, buzzer, and vibration motor. It continuously measures the distance using the ultrasonic sensor and activates the buzzer and vibration motor if the distance is below the defined threshold. The measured distance is also printed to the serial monitor.