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
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
9V Battery
- Description: A standard 9V battery used to power the Arduino.
- Pins: -, +
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic sensor used for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
Rocker Switch (SPST)
- Description: A single-pole single-throw (SPST) switch used to control the power supply.
- Pins: 1, 2
Wiring Details
Arduino UNO
- 5V connected to VCC of the HC-SR04 Ultrasonic Sensor.
- GND connected to GND of the HC-SR04 Ultrasonic Sensor.
- GND connected to Pin 1 of the Rocker Switch (SPST).
- Vin connected to + of the 9V Battery.
- D9 connected to TRIG of the HC-SR04 Ultrasonic Sensor.
- D8 connected to ECHO of the HC-SR04 Ultrasonic Sensor.
9V Battery
- + connected to Vin of the Arduino UNO.
- - connected to Pin 2 of the Rocker Switch (SPST).
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V of the Arduino UNO.
- GND connected to GND of the Arduino UNO.
- TRIG connected to D9 of the Arduino UNO.
- ECHO connected to D8 of the Arduino UNO.
Rocker Switch (SPST)
- Pin 1 connected to GND of the Arduino UNO.
- Pin 2 connected to - of the 9V Battery.
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.
