Arduino UNO: Complete Ultrasonic Sensor Guide (HC-SR04)

This guide will teach you how to connect your HC-SR04 ultrasonic sensor to your Arduino UNO and how to calibrate it to measure distance.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Wiring Your Sensor

Connect your ultrasonic sensor to the Arduino UNO as shown in the diagram. The HC-SR04 has four pins which should be connected as follows:

  • Ultrasonic VCC → Arduino 5V
  • Ultrasonic GND → Arduino GND
  • Ultrasonic Echo → Arduino digital pin (2 is used for this tutorial)
  • Ultrasonic Trig → Arduino digital pin (4 is used for this tutorial)

Step 2: Calibrating Your Sensor

Note: Due to a bug in Instructables, please click on the external link to the code in order to copy it correctly. Otherwise, you may see some missing spaces.

Look through the following code labelled ultrasonic_callibration.ino to see how to print the duration of your ultrasonic sensor's pulses. Another term for this duration is the period of each pulse. The period is the amount of time between the sensor sending out and receiving a pulse.

To view the period of each pulse, open the Serial Monitor (Ctrl+Shift+M). We will use these values to find a correlation between the period (in microseconds) and the distance to the object the ultrasonic sensor is facing (in any unit of distance you choose).

  1. Create a new Google Sheets document. Microsoft Excel works too, but for this tutorial we will be using the former. Create two columns: the first will be for pulse duration, and the second will be for distance.
  2. Place a ruler on top of a horizontal flat surface with the end that reads zero perpendicular to a vertical flat surface. Consider, for example, a tabletop and a hardcover book.
  3. Choose intervals along the ruler at which you will be placing your ultrasonic sensor such that it is facing and parallel to the vertical flat surface.
    • Tip: Choose a small interval that spans the entirety of your ruler. The more data points you have, the more calibrated your ultrasonic sensor will be.
  4. Start your ultrasonic sensor closest to the vertical surface and turn it on. Record the period of the pulse in the first column of your Google Sheet, then record the ultrasonic sensor's distance along the ruler for that pulse.
  5. Increment the ultrasonic sensor's position by your interval and repeat steps four and five until you have a fair number of data points.
  6. In a cell in your Google Sheet, type =LINEST(. You will be prompted for a range of known_data_y. Select the second column containing your distances. Add a comma, then select the first column containing your periods when prompted for the known_data_x. Add a comma and type true, then add a closing parenthesis and hit Enter. Continue on to the next step.

ultrasonic_calibration.ino

/**
* The digital pin that receives input from the ultrasonic sensor.
*/
constint echoPin = 2;
/**
* The digital pin that sends output to the ultrasonic sensor.
*/
constint trigPin = 4;
voidsetup() {
pinMode(echoPin, INPUT); // Register echoPin for receiving input
pinMode(trigPin, OUTPUT); // Register trigPin for sending output
Serial.begin(9600); // Begin serial communication to receive data from the ultrasonic sensor
}
voidloop() {
// Send a short low pulse to ensure a clean high one.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a ten-second high pulse.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the high pulse and print out its duration.
constlong duration = pulseIn(echoPin, HIGH);
Serial.print("Period (microseconds): ");
Serial.println(duration);
}

Step 3: Measuring Distance

Note: Due to a bug in Instructables, please click on the external link to the code in order to copy it correctly. Otherwise, you may see some missing spaces.

Reference the attached code for this final step. In the method called microsecondsToDistance, set the variables m and b to the value that appeared in the cell where you typed the formula and the value in the cell to its right, respectively.

Upload the code to your Arduino UNO and you should be all set! Try measuring distances along the ruler to see if they match what you expect.

ultrasonic.ino

/**
* The digital pin that receives input from the ultrasonic sensor.
*/
constint echoPin = 2;
/**
* The digital pin that sends output to the ultrasonic sensor.
*/
constint trigPin = 4;
voidsetup() {
pinMode(echoPin, INPUT); // Register echoPin for receiving input
pinMode(trigPin, OUTPUT); // Register trigPin for sending output
Serial.begin(9600); // Begin serial communication to receive data from the ultrasonic sensor
}
voidloop() {
// Send a short low pulse to ensure a clean high one.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a ten-second high pulse.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Store the high pulse's duration.
constlong duration = pulseIn(echoPin, HIGH);
// Calculate and print the distance to the target.
constdouble distance = microsecondsToDistance(duration);
Serial.print("Distance: ");
Serial.println(distance);
}
/**
* @param microseconds a number of microseconds
* @return the conversion of the provided microseconds into a distance
*/
constdoublemicrosecondsToDistance(constlong microseconds) {
// Initialize m and b to their respective values in the formula, y = mx + b.
// y = distance, x = time (in microseconds).
constdouble m = 1.0;
constdouble b = 0.0;
return m * microseconds + b;
}
view rawultrasonic.ino hosted with ❤ by GitHub

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    Discussions