Connecting an ultrasonic sensor like the common HC-SR04 to an Arduino Uno (or any similar board) is a fundamental project. The process for an "Arduino Uno32" is identical to a standard Arduino Uno, as the pin functionality and voltage levels are the same.
Here is a clear, step-by-step guide.
1. How an Ultrasonic Sensor (HC-SR04) Works
The HC-SR04 has four pins:
- VCC: Power (5V)
- Trig: Trigger Input Pin
- Echo: Echo Output Pin
- GND: Ground
It operates by:
- You send a short 10µs HIGH pulse to the Trig pin.
- The sensor sends out an ultrasonic burst (8 pulses of 40kHz).
- The sensor then sets the Echo pin HIGH.
- It keeps the Echo pin HIGH until the burst reflects back and is detected.
- You measure the duration the Echo pin was HIGH. The longer the duration, the farther the object.
You then calculate distance using the speed of sound.
2. Components Needed
- Arduino Uno (or Uno32)
- HC-SR04 Ultrasonic Sensor
- Breadboard and Jumper Wires
3. Wiring Diagram & Connections
Connect the components as follows:
Important Note on Voltage (For 3.3V Boards):
- The standard HC-SR04 module is designed to run at 5V. The Echo pin outputs a 5V signal.
- The Arduino Uno's digital pins are 5V-tolerant, meaning you can safely connect the Echo pin directly to Pin 10.
- If you were using a 3.3V board (like an ESP32 or Arduino Due), you must use a voltage divider (e.g., two resistors: 1kΩ and 2kΩ) on the Echo pin to step the 5V signal down to 3.3V and avoid damaging the microcontroller. This is not needed for the Arduino Uno.
4. Arduino Code
Here is a basic code to read the distance and print it to the Serial Monitor.
cpp
// Defines pins for the sensor
const int trigPin = 9;
const int echoPin = 10;
// Defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
// Speed of sound wave divided by 2 (go and back)
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Add a delay between readings for stability
}
5. How the Code Works
1. Initialization (setup):
- The trigPin is set as an OUTPUT because the Arduino sends the trigger signal.
- The echoPin is set as an INPUT because the Arduino listens for the return signal.
- Serial communication is started to display the results.
2. Taking a Measurement (loop):
- Clear Trigger: The trigPin is set LOW to ensure a clean pulse.
- Send Pulse: The trigPin is set HIGH for exactly 10 microseconds, telling the sensor to send out a sound wave.
- Listen for Response: The pulseIn() function is used to wait for the echoPin to go HIGH and then measure exactly how long it stays HIGH. This duration is stored in the duration variable.
- Calculate Distance: The distance is calculated using the formula based on the speed of sound (~340 m/s, or 0.034 cm/µs). We divide by 2 because the sound wave traveled to the object and back.
6. Troubleshooting Common Issues
Strange Large Numbers (e.g., 0 cm or 500 cm):
- This usually means the sensor isn't detecting anything or the echo is being missed. Check your wiring, especially the GND connection.
- Make sure you are measuring in a well-lit room. HC-SR04 sensors can be unreliable in direct sunlight or with very soft, sound-absorbing objects.
Consistent Zero Readings:
You likely have the Trig and Echo pins swapped. Double-check your connections.
Inconsistent Readings:
- Add a small delay (delay(50)) between readings to allow the sensor to settle.
- Ensure the object is within the sensor's range (typically 2cm - 400cm).
By following these steps, you should have your ultrasonic sensor successfully measuring distance with your Arduino Uno.
Top comments (0)