TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
Introduction
This tutorial is about the commonly use HC-SR04 ultrasonic sensor. We will give some details about it, explain how it works, and at the last part we will create an Arduino sketch to test its functionality.
HOW HC-SR04 WORKS:
The HC-SR04 ultrasonic sensor uses a sound frequency to determine distance of an object just like how dolphins and bats senses its surroundings for navigation. Ultrasonic sound frequency can be found just above the upper limit of human hearing capacity (20 Hz to 20 KHz).
Ultrasonic sensors is composed of 2 sound transducers, one for transmitter and another one for receiver. So, how does it work?
- The transmitter sends a signal and when the signal hits an object, the signal is reflected.
- The reflected signal is then receive by the receiver.
The time it takes for the transmitted signal to be reflected back to the sensor is proportionally equivalent to the distance it travels according to the speed of sound.
To determine the distance of an object, we will use the following formula:
distance = duration x speed of sound
But, we need to consider that the signal is sent, hits an object, then reflected back. So taking it in consideration, we will use the following formula instead: The speed of sound is 343 m/sec or 0.0343 cm/µsec since the returned value of ultrasonic sensor is in micro seconds.
Pin Assignment
- VCC: +5V DC
- Trig: Trigger pin is an input signal used to activate the sending of ultrasonic signal burst.
- Echo: Echo pin is an output signal which returns a TTL HIGH equivalent to the duration of travel from transmitting to receiving.
- GND: Ground pin
The HC-SR04 provides a distance measurement between 2 cm to 400 cm with an accuracy of 3mm at a maximum angle of 15 degrees. For more details, you may check the datasheet (links will be provided n the description).
[https://www.electroschematics.com/wp-content/uploads/2013/07/HCSR04-datasheet-version-1.pdf]
Bill Of Materials
For this example, we will need the following materials to test its funtionality:
- An Arduino Uno board as the main controller.
- The HC-SR04 Ultrasonic sensor.
- Some jumper wires.
Hardware Instruction
So lets build our circuit,
- Connect the VCC pin of HC-SR04 to 5V pin of Arduino Uno.
- Connect the Trig pin of HC-SR04 to digital pin 9 of Arduino Uno.
- Connect the Echo pin of HC-SR04 to digital pin 10 of Arduino Uno.
- Connect the GND pin of HC-SR04 to GND pin of Arduino Uno.
Now, let us prepare our Arduino code,
- Copy and paste the provided Arduino code to your Arduino IDE.
- Connect the Arduino Uno to your computer and make sure that the correct board is selected under Tools > Board. Also make sure that the correct serial port is selected under Tools > Port.
- Upload the sketch and if everything is ok, you should be able to see the measured distance of the HC-SR04 ultrasonic sensor in your Serial Monitor.
Video Demonstration
Source Code
1#define ECHO_PIN 10
2#define TRIG_PIN 9
3
4long duration, distance;
5
6void setup() {
7 Serial.begin (9600);
8 pinMode(ECHO_PIN, INPUT);
9 pinMode(TRIG_PIN, OUTPUT);
10 Serial.println("SETUP Complete");
11}
12
13void loop() {
14 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
15 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
16 digitalWrite(TRIG_PIN, LOW);
17 delayMicroseconds(5);
18 digitalWrite(TRIG_PIN, HIGH);
19 delayMicroseconds(10);
20 digitalWrite(TRIG_PIN, LOW);
21
22 // Read the signal from the sensor: a HIGH pulse whose
23 // duration is the time (in microseconds) from the sending
24 // of the ping to the reception of its echo off of an object.
25 duration = pulseIn(ECHO_PIN, HIGH);
26
27 // Convert the time into a distance
28 distance = (duration/2) * 0.0343; // speed of sound is 0.0343 cm/us
29
30 // Now send it to Serial monitor
31 Serial.print("Distance is ");
32 Serial.print(distance);
33 Serial.println(" cm.");
34 delay(1000);
35}
Call To Action
If you find this tutorial helpful, please consider helping me by sharing this to your friends. If you have any question, please do not hesitate to leave it in the comments section.
Thank you and have a good day.
Happy tinkering!
Posts in this series
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!