Arbi Abdul Jabbaar
Published

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.

BeginnerProtip1 hour913,840
Ultrasonic Sensor HC-SR04 with Arduino Tutorial

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
You can use any version of Arduino, but i'll use Arduino UNO R3
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
RGB Backlight LCD - 16x2
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
Jumper wires (generic)
Jumper wires (generic)
10cm or longer Male to Male Jumper Wires
×4
Solderless Breadboard Full Size
Solderless Breadboard Full Size
You can use any breadboards you have.
×1

Software apps and online services

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

Hand tools and fabrication machines

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

Story

Read more

Schematics

Wiring Diagram 1 Fritzing File

Wiring Diagram 2 (with LCD) Fritzing File

Wiring diagram of Arduino and Ultrasonic Sensor HC-SR04

Wiring diagram of Arduino LCD and Ultrasonic Sensor HC-SR04

Code

Ultrasonic Sensor HC-SR04 with Arduino Code for Ranging Test

C/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 Code

C/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");
}

Credits

Arbi Abdul Jabbaar

Arbi Abdul Jabbaar

3 projects • 42 followers
I am a student of National Technology of Institute, Indonesia majoring in Electrical Engineering. I've developed hardware for about 3 years.

Comments