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

Arduino UNO Battery-Powered Ultrasonic Distance Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Battery-Powered Ultrasonic Distance Sensor

Circuit Documentation

Summary

This circuit utilizes an Arduino UNO microcontroller, a 9V battery, an HC-SR04 ultrasonic sensor, and a rocker switch (SPST). The purpose of the circuit is to measure the distance using the ultrasonic sensor and display the results via the Arduino's serial monitor. The 9V battery powers the Arduino, and the rocker switch is used to control the power supply.

Component List

  1. 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
  2. 9V Battery

    • Description: A standard 9V battery used to power the Arduino.
    • Pins: -, +
  3. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic sensor used for distance measurement.
    • Pins: VCC, TRIG, ECHO, GND
  4. Rocker Switch (SPST)

    • Description: A single-pole single-throw (SPST) switch used to control the power supply.
    • Pins: 1, 2

Wiring Details

Arduino UNO

9V Battery

HC-SR04 Ultrasonic Sensor

Rocker Switch (SPST)

Code Documentation

// Define pins for the HC-SR04
const int trigPin = 9;
const int echoPin = 8;

// Variables for duration and distance
long duration;
int distance;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Set the trigPin as an OUTPUT
  pinMode(trigPin, OUTPUT);
  
  // Set the echoPin as an INPUT
  pinMode(echoPin, INPUT);
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Set the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distance = duration * 0.034 / 2;
  
  // Print the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  // Wait for a short period before the next loop
  delay(1000);
}

This code initializes the pins for the HC-SR04 ultrasonic sensor and sets up the serial communication. In the loop, it sends a pulse from the trigPin, reads the echoPin to get the duration of the pulse, calculates the distance, and prints the distance to the serial monitor. The loop repeats every second.