Interfacing HC-SR04 (ultrasonic sensor) with Arduino

Arduino is a versatile microcontroller platform that empowers beginners and experienced makers alike to bring their ideas to life through electronics projects. A classic starting point for newcomers is the LED blinking project, where users learn to control an LED's on and off states using Arduino code. Once comfortable with this simple project, they can quickly shift focus to more exciting ventures, like interfacing an ultrasonic sensor such as the HC-SR04.

Interfacing HC-SR04 (ultrasonic sensor)

The HC-SR04 is an ultrasonic sensor that can be used with an Arduino to measure distances. It emits ultrasonic waves and measures the time it takes for the waves to bounce back after hitting an object.

By integrating the sensor with the Arduino, users can create advanced applications like distance measurement, obstacle detection, or even interactive robots that respond to their environment. This seamless transition from LED blinking to ultrasonic sensors showcases Arduino's adaptability and encourages enthusiasts to explore the vast possibilities of electronics and programming.

Hardware required

To implement the circuit for the given Arduino code, you’ll need the following components:

  • Arduino board (e.g., Arduino Uno)

  • HC-SR04 Ultrasonic Sensor

  • Breadboard and jumper wires

Circuit connections

  • Connect the VCC pin of the HC-SR04 to the 5V pin of the Arduino board.

  • Connect the GND pin of the HC-SR04 to the GND pin of the Arduino board.

  • Connect the TRIG pin of the HC-SR04 to a digital pin (e.g., pin 7) on the Arduino.

  • Connect the ECHO pin of the HC-SR04 to another digital pin (e.g., pin 6) on the Arduino.

Circuit diagram

Circuit diagram
Circuit diagram

Code

Here’s a basic example code to measure distances using the HC-SR04 ultrasonic sensor and Arduino:

const int trigPin = 7;
const int echoPin = 6;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
long duration, distance;
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10us pulse to trigger the sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse from the ECHO pin
duration = pulseIn(echoPin, HIGH);
// Calculate the distance based on the speed of sound (343 meters per second)
// and the formula: distance = (speed of sound * time) / 2
distance = (duration * 343) / 10000;
// Print the distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Delay before the next measurement
delay(1000);
}

Explanation

The code begins by defining the trigPin (the pin connected to the TRIG pin of the HC-SR04) and the echoPin (connected to the ECHO pin of the HC-SR04). In the setup function, we set these pins as OUTPUT and INPUT, respectively, and initialize the Serial communication for displaying the distance.

In the loop function, we perform the following steps:

  1. Set the trigPin to LOW to clear any previous trigger pulse.

  2. Send a 10µs trigger pulse to the HC-SR04 by setting the trigPin to HIGH and then LOW after a short delay.

  3. Use the pulseIn function to measure the pulse duration received on the echoPin.

  4. Calculate the distance based on the speed of sound (343 meters per second) and the time of flight of the ultrasonic pulse. The formula is distance = (speed of sound * time) / 2. The division by 10000 is to convert the time to centimeters.

  5. Print the calculated distance to the serial monitor.

  6. Add a delay of 1 second before the following distance measurement.

Upload the code

  1. Connect your Arduino board to the computer using the USB cable.

  2. Open the Arduino IDE and copy the code into a new sketch.

  3. Select the correct board and port under the "Tools" menu.

  4. Click on the checkmark icon (✓) to verify the error code. Fix any errors that appear.

  5. Click on the right-arrow icon (➔) to upload the code to your Arduino board.

Observations

When you upload this code to your Arduino and open the serial monitor at a baud rate of 9600, you should see the distance measurements in centimeters displayed in the serial monitor. The sensor will continuously measure and display the distance to any object within its range.

To learn about interfacing an LCD with Arduino, click here.

Copyright ©2024 Educative, Inc. All rights reserved