HC-SR04 Ultrasonic Distance Sensor and Arduino (Lesson #9)
Table of Contents
Introduction
In this tutorial, you will learn how to use the HC-SR04 ultrasonic distance sensor with an Arduino to measure the distance to nearby objects. This project is a great introduction to working with sensors and can be applied in various robotics and automation projects.
Step 1: Gather Your Materials
To get started, make sure you have the following components:
- HC-SR04 ultrasonic distance sensor
- Arduino board (e.g., Arduino Uno)
- Jumper wires
- Breadboard (optional)
- USB cable to connect Arduino to your computer
Step 2: Make the Connections
Connect the HC-SR04 to the Arduino as follows:
- Connect the VCC pin of the HC-SR04 to the 5V pin on the Arduino.
- Connect the GND pin of the HC-SR04 to a GND pin on the Arduino.
- Connect the TRIG pin of the HC-SR04 to digital pin 9 on the Arduino.
- Connect the ECHO pin of the HC-SR04 to digital pin 10 on the Arduino.
Tip: Ensure all connections are secure to avoid any interruptions in data transmission.
Step 3: Review the Datasheet
- Familiarize yourself with the HC-SR04 datasheet. It provides critical information on the sensor's specifications, including its operating voltage, range, and timing requirements.
Step 4: Write the Code
Use the following Arduino code to set up the HC-SR04 sensor:
#define TRIG_PIN 9
#define ECHO_PIN 10
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration, distance;
// Clear the trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Set the trigger high for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
// Print the distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Tip: Make sure to upload the code to your Arduino using the Arduino IDE.
Step 5: Test the Sensor
- Open the Serial Monitor in the Arduino IDE to view the distance readings.
- Hold an object in front of the sensor to see how the distance changes in real-time.
Step 6: Compare HC-SR04 with PING Sensor
- Understand the differences between the HC-SR04 and the PING))) ultrasonic sensors.
- The HC-SR04 is typically more affordable and widely used for basic applications.
- The PING sensor provides a simpler interface but may not have the same range as the HC-SR04.
Common Pitfalls:
- Ensure that the HC-SR04 is not too close to the object you are measuring, as it has a minimum range.
- Avoid using the sensor in areas with excessive noise, which may interfere with the ultrasonic signals.
Conclusion
You have successfully set up the HC-SR04 ultrasonic distance sensor with an Arduino. This basic project can serve as a foundation for more complex applications, such as obstacle detection in robots or automatic distance measuring devices. Explore additional projects and tutorials to expand your understanding of sensors and Arduino programming.