Ultrasonic sensors are a popular choice for distance measurement in robotics and other DIY electronics projects. They’re simple to use, reliable, and provide accurate measurements over a short range. In this post, we’ll guide you through connecting and programming an ultrasonic sensor with your Arduino to measure distance.
What is an Ultrasonic Sensor?
An ultrasonic sensor, like the popular HC-SR04 module, measures distance by emitting ultrasonic waves and measuring the time it takes for the echo to return after bouncing off an object. This time is then converted into a distance measurement.
Key Features:
- Range: Typically between 2 cm to 400 cm.
- Accuracy: Usually within 3 mm.
- Operating Voltage: 5V.
Components Needed
- Arduino (Uno, Nano, or any compatible board)
- HC-SR04 Ultrasonic Sensor
- Jumper wires
- Breadboard (optional)
Wiring the Ultrasonic Sensor
The HC-SR04 ultrasonic sensor has four pins:
- VCC: Connect this to the 5V pin on the Arduino.
- GND: Connect this to the GND pin on the Arduino.
- Trig: Connect this to a digital pin on the Arduino (e.g., D9).
- Echo: Connect this to another digital pin on the Arduino (e.g., D10).
Wiring Diagram:
rustKód másolása HC-SR04 Arduino
---------------------
VCC -> 5V
GND -> GND
Trig -> D9
Echo -> D10
Arduino Code Example
Here’s a simple Arduino sketch to measure distance using the HC-SR04 sensor:
cppKód másolása// Define pins
const int trigPin = 9;
const int echoPin = 10;
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() {
// Variable to store duration and distance
long duration;
int distance;
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (time) of the pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Add a short delay before next reading
delay(500);
}
Code Explanation:
- Triggering the Sensor: The
trigPin
is set HIGH for 10 microseconds to start the measurement. This sends out an ultrasonic pulse. - Measuring the Echo: The
echoPin
listens for the pulse to return andpulseIn()
measures how long it takes for the echo to return. - Calculating Distance: The duration is then used to calculate the distance. Since sound travels at approximately 343 meters per second (or 0.034 cm/μs), the distance is calculated using the formula:
Distance = (Duration x 0.034)/2
The division by 2 accounts for the fact that the pulse travels to the object and back. - Output: The distance is printed to the Serial Monitor in centimeters.
Testing the Setup
- Connect your Arduino to your computer and upload the code.
- Open the Serial Monitor (Ctrl+Shift+M) in the Arduino IDE.
- Place an object in front of the sensor and observe the distance readings on the Serial Monitor.
Practical Applications
Ultrasonic sensors can be used in various projects, such as:
- Obstacle Avoidance: In robotics, to detect and avoid obstacles.
- Distance Measurement: Measure the level of a liquid in a tank or the distance to an object.
- Security Systems: Detect the presence of objects or people.
Troubleshooting Tips
- Incorrect Readings: Ensure the sensor is connected properly and that the object is within the sensor’s range.
- No Output: Double-check your wiring and code, ensuring the correct pins are used.
- Unstable Readings: Make sure the sensor is not too close to the object and that it’s mounted securely to avoid vibrations affecting the readings.
Conclusion
Using an ultrasonic sensor with Arduino is a simple and effective way to measure distance in your projects. With just a few components and some basic code, you can add distance sensing to your next project. Whether you’re building a robot, creating a distance meter, or developing an interactive installation, the HC-SR04 ultrasonic sensor is a versatile tool that’s easy to integrate and use.
Happy coding!