Ultrasonic Sensor HC-SR04 with Arduino Tutorial

Ultrasonic Sensor HC-SR04 with Arduino Tutorial

You will learn how to interface Ultrasonic Sensor HC-SR04 with Arduino. It can be an Ultrasonic Range Sensor or any purposes.

  • 267,358 views
  • 9 comments
  • 56 respects

Components and supplies

Ph a000066 iso (1) ztbmubhmho
Arduino UNO
You can use any version of Arduino, but i'll use Arduino UNO R3
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
398 09
Adafruit RGB Backlight LCD - 16x2
It is OPTIONAL. If you don't have it you can use Arduino Serial Monitor to show the display.
×1
11026 02
Jumper wires (generic)
10cm or longer Male to Male Jumper Wires
×4
Adafruit industries ada239 image 75px
Solderless Breadboard Full Size
You can use any breadboards you have.
×1

Necessary tools and machines

64t7332 40
Tape Measure, Manual
You can use any measurement tools, you can also use a ruler.

Apps and online services

Ide web
Arduino IDE
You can download this app for free in https://www.arduino.cc/en/main/software

About this project

Ultrasonic Sensor HC-SR04 is a sensor that can measure distance. It emits an ultrasound at 40 000 Hz (40kHz) which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

The configuration pin of HC-SR04 is VCC (1), TRIG (2), ECHO (3), and GND (4). The supply voltage of VCC is +5V and you can attach TRIG and ECHO pin to any Digital I/O in your Arduino Board.

The materials that we need to make this project:

1. Arduino UNO R3 CH340 (you can use any Arduino Boards)

2. Ultrasonic Sensor HC-SR04

3. Male to Male Jumper Wires

4. Breadboard



The connection of Arduino and Ultrasonic Sensor HC-SR04

In order to generate the ultrasound we need to set the Trigger Pin on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo Pin. The Echo Pin will output the time in microseconds the sound wave traveled.

For example, if the object is 20 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 588 microseconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

For the programming code, first we need to define the Trigger Pin and Echo Pin that connected to Arduino board. In this project EchoPin is attached to D2 and TrigPin to D3. Then define variables for the distance (int) and duration (long).

In the loop first you have to make sure that the trigPin is clear so we have to set that pin on a LOW State for just 2 µs. Now for generating the ultrasound wave we have to set the trigPin on HIGH State for 10 µs. Using the pulseIn()function you have to read the travel time and put that value into the variable “duration”. This function has 2 parameters, the first one is the name of the echo pin and for the second one you can write either HIGH or LOW. In this case, HIGH means that the pulseIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the timing. At the end the function will return the length of the pulse in microseconds. For getting the distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation previously. At the end we will print the value of the distance on the Serial Monitor.

Steps :

1. First do the wiring as shown in the picture

2. Open Arduino IDE Software and write down your code, or download the code below and open it

3. Choose your own Arduino board (in this case Arduino Uno), by selecting Tools > Board > Arduino/Geniuno Uno

4. Choose your COM Port (usually it appears only one existing port), Tools > Port > COM.. (If there are more than one ports, try it one by one)

5. Upload your code by pressing Ctrl + U or Sketch > Upload

6. To display the measurement data you can use Serial Monitor by pressing Ctrl + Shift + M (make sure that the baudrate speed is 9600)


Results:

After uploading the code, display the data with Serial Monitor. Now try to give an object in front of the sensor and see the measurement.



For the consideration, you can use your manual tape meter to measure the distance and compare it with the distance on Serial Monitor. If you want to display it on LCD, you can follow the second wiring diagram and upload the code below.

Code

Ultrasonic Sensor HC-SR04 with Arduino Code for Ranging TestC/C++
Write your code in Arduino IDE Software, save it. Select the port (i.e COM3) and choose your Arduino board type (i.e Arduino Uno / Genuino) then compile it to your Arduino.
// ---------------------------------------------------------------- //
// Arduino Ultrasoninc Sensor HC-SR04
// Re-writed by Arbi Abdul Jabbaar
// Using Arduino IDE 1.8.7
// Using HC-SR04 Module
// Tested on 17 September 2019
// ---------------------------------------------------------------- //

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}
Ultrasonic Sensor HC-SR04 with Arduino and LCD CodeC/C++
If you want to display it on LCD, use this code and another wiring diagram (file included)
// ---------------------------------------------------------------- //
// Arduino Ultrasoninc Sensor HC-SR04 with LCD 16x2
// Re-writed by Arbi Abdul Jabbaar
// Using Arduino IDE 1.8.7
// Using HC-SR04 Module
// Tested on 17 September 2019
// ---------------------------------------------------------------- //
#include <LiquidCrystal.h>
LiquidCrystal lcd(6, 7, 8, 9, 10, 11); // RS, EN, D4, D5, D6, D7

#define echoPin 12 // attach pin D12 Arduino to pin Echo of HC-SR04
#define trigPin 13 //attach pin D13 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance_cm; // variable for centimeters measurement
int distance_inch; // variable for inches measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
  lcd.begin(16, 2); // lcd starts with resolution 16x2
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance_cm = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  distance_inch = duration * 0.0133 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  lcd.setCursor(0, 0);
  lcd.print("Distance: ");
  lcd.print(distance_cm);
  lcd.println(" cm");
  lcd.setCursor(0, 1);
  lcd.print("Distance: ");
  lcd.print(distance_inch);
  lcd.println(" inch");
}

Schematics

Wiring Diagram 1 Fritzing File
uploads2ftmp2f94ff143c-17e9-43c4-8eb0-840121a648b82farduinohc-sr04_AXHYqJubMg.fzz
Wiring Diagram 2 (with LCD) Fritzing File
uploads2ftmp2f127eafbc-218a-42ff-85d3-fa4d0d7c6ca72farduinohc-sr04lcd_agQFBGr7rl.fzz
Wiring diagram of Arduino and Ultrasonic Sensor HC-SR04
Uploads2ftmp2f1d55fc38 3158 4c72 b4e7 38e28e1a954c2farduinohc sr04 whdpy0fy2f
Wiring diagram of Arduino LCD and Ultrasonic Sensor HC-SR04
Uploads2ftmp2f55b220bf 26a3 4104 90ed 06694e6b8d432fultrasonic6 w4yu6o4wln

Comments

Similar projects you might like

Getting Started with the HC-SR04 Ultrasonic sensor

by Isaac100

  • 136,363 views
  • 35 comments
  • 89 respects

Speed Measurement Using HC-SR04 Ultrasonic Sensor

Project tutorial by Mirko Pavleski

  • 2,261 views
  • 0 comments
  • 11 respects

Door Alarm With Ultrasonic Sensor

Project showcase by MattG

  • 30,478 views
  • 10 comments
  • 31 respects

Ultrasonic Sensor with LCD Display Using 433MHz

Project tutorial by mcharrison500

  • 24,415 views
  • 1 comment
  • 32 respects

Basic Ultrasonic Tutorial

by Damien Murtagh

  • 13,169 views
  • 7 comments
  • 43 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 26,534 views
  • 10 comments
  • 36 respects
Add projectSign up / Login