Circuit Documentation
Summary of the Circuit
This circuit integrates an Arduino UNO microcontroller board with an HC-SR04 Ultrasonic Sensor to measure distance. The Arduino UNO is responsible for controlling the sensor, processing the distance measurements, and outputting the results to the Serial Monitor. The HC-SR04 Ultrasonic Sensor operates by emitting an ultrasonic pulse and then listening for its echo to determine the distance to an object.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central processing unit for the circuit, controlling the ultrasonic sensor and processing the distance data.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic ranging module that provides 2cm to 400cm non-contact measurement functionality.
- Purpose: Measures the distance to objects by emitting ultrasonic waves and detecting the reflected signal.
- Pins: VCC, TRIG, ECHO, GND.
Wiring Details
Arduino UNO
- 5V: Provides power to the HC-SR04 Ultrasonic Sensor.
- GND: Connected to the ground pin of the HC-SR04 Ultrasonic Sensor to complete the power circuit.
- D3: Configured as an output to trigger the HC-SR04 Ultrasonic Sensor.
- D2: Configured as an input to receive the echo signal from the HC-SR04 Ultrasonic Sensor.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to the 5V output from the Arduino UNO to power the sensor.
- GND: Connected to the ground (GND) on the Arduino UNO to complete the power circuit.
- TRIG: Connected to pin D3 on the Arduino UNO to receive trigger pulses.
- ECHO: Connected to pin D2 on the Arduino UNO to send back the echo signal.
Documented Code
/*
* This Arduino Sketch interfaces with an HC-SR04 Ultrasonic Sensor to measure
* distance. The sensor's VCC and GND are connected to the Arduino's 5V and GND
* pins, respectively. The TRIG pin is connected to D3, and the ECHO pin is
* connected to D2. The measured distance is printed to the Serial Monitor.
*/
const int trigPin = 3; // TRIG pin connected to D3
const int echoPin = 2; // ECHO pin connected to D2
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
pinMode(trigPin, OUTPUT); // Set TRIG pin as output
pinMode(echoPin, INPUT); // Set ECHO pin as input
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Ensure TRIG pin is low
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Send a 10us high pulse to TRIG pin
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Read the ECHO pin
distance = duration * 0.034 / 2; // Calculate distance in cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Wait for 1 second before next measurement
}
This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the necessary pins for communication with the HC-SR04 Ultrasonic Sensor, sends trigger pulses, reads the echo signal, calculates the distance based on the time interval between sending and receiving the pulse, and outputs the distance measurement to the Serial Monitor.
