Circuit Documentation
Summary
This circuit interfaces an Arduino UNO with an HC-SR04 Ultrasonic Sensor to measure distances. The ultrasonic sensor emits sound waves and measures the time it takes for the echo to return, allowing the calculation of distance. The Arduino processes the sensor data and outputs the measured distance to the Serial Monitor.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P. It is used for various electronic projects and can be programmed to control other components.
- Purpose: Acts as the main controller for the circuit, processing inputs from the ultrasonic sensor and outputting data.
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic distance sensor that can measure distances from 2 cm to 400 cm.
- Purpose: Measures the distance to an object by emitting ultrasonic waves and calculating the time taken for the echo to return.
Wiring Details
Arduino UNO
5V: Connected to the VCC pin of the HC-SR04 Ultrasonic Sensor.
GND: Connected to the GND pin of the HC-SR04 Ultrasonic Sensor.
D3: Connected to the TRIG pin of the HC-SR04 Ultrasonic Sensor.
D2: Connected to the ECHO pin of the HC-SR04 Ultrasonic Sensor.
HC-SR04 Ultrasonic Sensor
VCC: Connected to the 5V pin of the Arduino UNO.
GND: Connected to the GND pin of the Arduino UNO.
TRIG: Connected to the D3 pin of the Arduino UNO.
ECHO: Connected to the D2 pin of the Arduino UNO.
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() {
noInterrupts();
long duration;
float 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.0343 / 2; // Calculate distance in cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Pulse duration: ");
Serial.println(duration);
interrupts();
delay(1000); // Wait for 1 second before next measurement
}
This documentation provides a comprehensive overview of the circuit, detailing the components used, their connections, and the code that drives the functionality of the ultrasonic distance measurement system.
