Getting Started with the HC-SR04 Ultrasonic sensor © CC BY-SA

A basic introduction to the low cost and accurate HC-SR04!

  • 13,198 views
  • 0 comments
  • 15 respects

Components and supplies

Apps and online services

About this project

In this project I will introduce you to the HC-SR04 Ultrasonic sensor. It works by sending sound waves from the transmitter, which then bounce off of an object and then return to the receiver. You can determine how far away something is by the time it takes for the sound waves to get back to the sensor. Let's get right to it!

Connections

The connections are very simple:

  • VCC to 5V
  • GND to GND
  • Trig to pin 9
  • Echo to pin 10

You can actually connect Trig and Echo to whichever pins you want, 9 and 10 are just the ones I'm using.

Code

First we define the pins that Trig and Echo are connected to.

const int trigPin = 9; 
const int echoPin = 10;

Then we declare 2 floats, duration and distance, which will hold the length of the sound wave and how far away the object is.

float duration, distance; 

Next, in the setup, we declare the Trig pin as an output, the Echo pin as an input, and start Serial communications.

void setup() { 
 pinMode(trigPin, OUTPUT); 
 pinMode(echoPin, INPUT); 
 Serial.begin(9600); 
} 

Now, in the loop, what we do is first set the trigPin low for 2 microseconds just to make sure that the pin in low first. Then, we set it high for 10 microseconds, which sends out an 8 cycle sonic burst from the transmitter, which then bounces of an object and hits the receiver(Which is connected to the Echo Pin).

void loop() { 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH); 
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW); 

When the sound waves hit the receiver, it turns the Echo pin high for however long the waves were traveling for. To get that, we can use a handy Arduino function called pulseIn(). It takes 2 arguments, the pin you are listening to(In our case, the Echo pin), and a state(HIGH or LOW). What the function does is waits for the pin to go whichever sate you put in, starts timing, and then stops timing when it switches to the other state. In our case we would put HIGH since we want to start timing when the Echo pin goes high. We will store the time in the duration variable. (It returns the time in microseconds)

duration = pulseIn(echoPin, HIGH); 

Now that we have the time, we can use the equation speed = distance/time, but we will make it time x speed = distance because we have the speed. What speed do we have? The speed of sound, of course! The speed of sound is approximately 340 meters per second, but since the pulseIn() function returns the time in microseconds, we will need to have a speed in microseconds also, which is easy to get. A quick Google search for "speed of sound in centimeters per microsecond" will say that it is .0343 c/μS. You could do the math, but searching it is easier. Anyway, with that information, we can calculate the distance! Just multiply the duration by .0343 and the divide it by 2(Because the sound waves travel to the object AND back). We will store that in the distance variable.

distance = (duration*.0343)/2; 

The rest is just printing out the results to the Serial Monitor.

 Serial.print("Distance: "); 
 Serial.println(distance); 
 delay(100); 
} 

Cover image by: Sparkfun

Code

HC-SR04 Full Code

Schematics

HC-SR04 Diagram
Hc sr04 m9qtzfpmuo 1esb0fuolf

Comments

Similar projects you might like

Turn Lights On When Approaching Home

Project tutorial by Andrei Florian

  • 2,491 views
  • 5 comments
  • 11 respects

Arduino Oscilloscope (6-Channel)

by Meeker6751

  • 2,397 views
  • 5 comments
  • 17 respects

Gesture Control Robot

Project tutorial by Raunak Oberai

  • 1,810 views
  • 6 comments
  • 9 respects

Matrix Orbital GTT TFT Thermometer Stage 1

Project tutorial by MatrixOrbital

  • 3,985 views
  • 0 comments
  • 11 respects

Air Quality Monitoring

Project tutorial by Ruchir Sharma

  • 499 views
  • 0 comments
  • 6 respects

Temperature and Humidity Sensor

by Amarantowy

  • 460 views
  • 1 comment
  • 4 respects
Add projectSign up / Login