This is a Beginner's Guide to Arduino Ultrasonic Sensor usage - Measuring Distances with HC-SR04

Introduction

Arduino Ultrasonic sensors: The world of Arduino offers endless possibilities for creative projects, and one of the most fascinating tools at your disposal is the ultrasonic sensor. This amazing little device allows you to measure distances accurately and reliably, opening doors to a wide range of applications.

Whether you're building a robot that avoids obstacles, a parking assistant for your car, or a water level monitor, the ultrasonic sensor is your key to success.

Ultrasonic sensors are useful for proximity detection and measuring distance. In this tutorial, you will look at using an ultrasonic sensor with an Arduino to measure distance. You will connect an HC-SR04 ultrasonic sensor to an Arduino Uno and write code to trigger ultrasound pulses and measure the echo return time to calculate distance.

The HC-SR04 ultrasonic sensor uses sonar to determine distance to the nearest object by emitting high frequency sound pulses and listening for the echo. By measuring the time between the pulse emission and echo reception, you can calculate the distance using the speed of sound. Ultrasonic sensors are commonly used in robotics, presence detection, and measurement applications.

What is an HC-SR04 Ultrasonic Sensor?

The HC-SR04 is an ultrasonic range finder that uses sound waves to measure distances. It emits a short burst of ultrasonic sound and then listens for the echo to return. By measuring the time it takes for the echo to return, the sensor can calculate the distance to the object it's facing.

The HC-SR04 is a popular choice for Arduino projects due to its low cost, ease of use, and high accuracy. It has a measuring range of 2cm to 400cm with a resolution of 0.3cm. It also operates at a frequency of 40kHz, making it safe for use around humans and pets.

Required Components

  • Arduino Uno
  • HC-SR04: Arduino Ultrasonic Sensor
  • Breadboard
  • Jumper wires

Arduino Ultrasonic Sensor: Circuit Diagram

The circuit diagram shows how to connect the HC-SR04 sensor to the Arduino Uno.

  • Connect the VCC pin of the HC-SR04 to the 5V pin of the Arduino.
  • Connect the GND pin of the HC-SR04 to the GND pin of the Arduino.
  • Connect the Trig pin of the HC-SR04 to digital pin 13 of the Arduino.
  • Connect the Echo pin of the HC-SR04 to digital pin 12 of the Arduino.

arduino ultrasonic sensor layout diagram

Diagram using Fritzing

Arduino Ultrasonic Sensor: Schematic

arduino ultrasonic sensor schematic

Diagram using Fritzing


Libraries needed

The library you need to install is "NewPing".

Installing an Arduino Library with IDE

Install the library as follows:

1st method:

    Click the menu bar and follow the menu items:

Sketch-->Include Library-->Manage Libraries...

2nd method:

    Click the ICON on the left that looks like a set of books.
The library manager appears on the left of the screen.

In the search box type NewPing. Now click the install button.

Arduino Ultrasonic Sensor: Example Sketch

Here's an example sketch to get you started with the HC-SR04 sensor:

You can copy and paste the code below into the Arduino IDE (in a new sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).

#include <NewPing.h>

#define TRIG_PIN 13
#define ECHO_PIN 12
#define MAX_DISTANCE 200

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(50);
  int distance = sonar.ping_cm();
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println("cm");
}

Short Code Explanation

  • The code includes the NewPing library for working with the HC-SR04 sensor.
  • The TRIG_PIN and ECHO_PIN constants define the digital pins connected to the sensor.
  • The MAX_DISTANCE constant sets the maximum distance the sensor can measure.
  • The sonar object is created with the NewPing library.
  • In the loop function, the ping_cm() function is called to measure the distance.
  • The measured distance is printed to the serial monitor.

Detailed Code Explanation

  1. The #include line includes the NewPing library, which provides functions for working with the HC-SR04 sensor.
  2. The #define TRIG_PIN 9 and #define ECHO_PIN 10 lines define the digital pins connected to the sensor's Trig and Echo pins, respectively.
  3. The #define MAX_DISTANCE 200 line sets the maximum distance the sensor can measure to 200cm.
  4. The NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); line creates a NewPing object named sonar with the specified trigger pin, echo pin, and maximum distance.
  5. The void setup() function initializes the serial communication with the computer.
  6. The void loop() function is the main loop of the program. It runs continuously.
  7. The delay(50); line delays the program for 50 milliseconds to prevent the sensor from being overwhelmed with requests.
  8. The int distance = sonar.ping_cm(); line calls the ping_cm() function of the sonar object to measure the distance in centimeters.
  9. The Serial.print("Distance: "); line prints the text "Distance: " to the serial monitor.
  10. The Serial.print(distance); line prints the measured distance to the serial monitor.
  11. The Serial.println("cm"); line prints the text "cm" to the serial monitor, indicating the unit of measurement.

Arduino Ultrasonic Sensor: Uploading the Code

  1. Connect the Arduino Uno to the PC with a USB cable.
  2. Select the Arduino Uno hardware from the Tools > Board menu.
  3. Open a new sketch.
  4. Paste the code above into the new page (overwrite everything).
  5. Press the upload button (right arrow at top).

You can find a more detailed tutorial on the Arduino IDE page.

Testing the Circuit

Once you've uploaded the code, open the serial monitor (Tools > Serial Monitor) and you should see the distance readings being printed every 50 milliseconds. You can test the sensor by placing your hand or an object in front of it and observing the changes in the distance readings.

Conclusions

Congratulations! You've now successfully built and tested an ultrasonic sensor circuit with Arduino. This is just the beginning of your journey into the world of distance measurement. With the HC-SR04 sensor, you can create countless projects that will amaze and inspire you.

If you want to go a bit more in-depth on ultrasonic sensors you can find, on this page, a project that uses transistor amplifiers to get the result from "raw" sensors. This uses a smoothing circuit and comparator to get the final echo return result. It shows essentially what the HC SR04 board has to do i.e. its circuit operation.


Written by John Main who has a degree in Electronic Engineering.

Note: Parts of this page were written using claude-instant as a research assistant.




Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use