Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano-Based Ultrasonic Distance Sensor with Buzzer and Vibration Motor

View and Edit Project In Cirkit DesignerImage of Arduino Nano-Based Ultrasonic Distance Sensor with Buzzer and Vibration Motor

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

  1. 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
  2. Buzzer

    • Description: An electronic device that emits sound when powered.
    • Pins: PIN, GND
  3. 3.7V Battery

    • Description: A rechargeable lithium-ion battery.
    • Pins: +, -
  4. HC-SR04 Ultrasonic Sensor

    • Description: A sensor that uses ultrasonic waves to measure distance.
    • Pins: VCC, TRIG, ECHO, GND
  5. Vibration Motor

    • Description: A small motor that vibrates when powered.
    • Pins: +, -
  6. Rocker Switch

    • Description: A switch that can be toggled between on and off positions.
    • Pins: output, input

Wiring Details

Arduino Nano

Buzzer

3.7V Battery

HC-SR04 Ultrasonic Sensor

Vibration Motor

Rocker Switch

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.