Introduction: Very Simple HC-SR04 Connection to Arduino Example

This example shows HC-SR04 connection to Arduino. This example is basic setup and can be integrated into any other project and get the distance as final from either the cm or inch variables.

The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module. This example uses Arduino Uno, still, it can work for any Arduino model.

Step 1: HC-SR04 Wiring

First implement the HC-SR04 connection to Arduino Uno as depicted in the image above.

Step 2: The Arduino Sketch

This is a self explanatory sketch:

// HC-SR04 Sesnsor Example from TechDepot Egypt
// http://www.tdegypt.com/

const int trigPin = 7;

const int echoPin = 8;

long reply, cm, inch;

void setup() {

pinMode(trigPin, OUTPUT); // Prepare the trigger pin

pinMode(echoPin, INPUT); // Prepare the echo pin

Serial.begin(9600);

}

void loop() {

// Reset the trigger pin and get ready for a clean trigger pulse

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Generate and send to trigger pin the trigger pulse

digitalWrite(trigPin, HIGH); // You need to keep it high for 10 micro seconds length

delayMicroseconds(10); // This is the 10 microseconds we mentioned above :)

digitalWrite(trigPin, LOW); // Stop the trigger pulse after the 10 microseconds

// Now let's see how long did it take the sound wave to travel

// It will take a time depending on the distance to the obstacle

// This time may be up to 38 millisecond in case of no obstacle

// If you never saw pulseIn before please check:

// https://www.arduino.cc/en/Reference/PulseIn

reply = pulseIn(echoPin, HIGH);

// Now we have the reply in microseconds, but we need a distance!

time2Distance(reply);

// Let's print the distance to the Arduino's Serial Monitor. Tools --> Serial Monitor

Serial.print("Distance in Inches ");

Serial.print(inch);

Serial.print(" - Distance in cm ");

Serial.print(cm);

Serial.println();

}

void time2Distance(long rawReply) {

// The data sheet says that it takes sound 73.746 (almost 74) microseconds to travel 1 inch.

// So if we divide rawReply which is in microseconds by the above we will get how many

// inches the sound travelled forward and travelled back to the sensor.

// The rawReply is the sound travelling to obstacle and back, then we need to divide by 2

inch = rawReply/74/2;

// According to Google, 1 inch eaquales 2.54 cm, accordingly and to get distance in cm

cm = inch*2.54;

}

You are all done. Enjoy :)

Comments

author
MrLunk made it!(author)2017-02-22

Thanks a lot for this great explanation ! Much apreciated...
Q: Would you be so kind and do the same for a good setup with Two Ultrasonic sensors ?
Greetings, Peter Lunk

author
Slime+Eel made it!(author)2016-03-11

I've had one of these sensors sitting around in my drawers for years (I would have gotten to it eventually...) so thanks for giving me an excuse to finally hook it up to something.

author
TechDepot+Egypt made it!(author)2016-03-11

Most welcome Slime Eel :)

author
Verga made it!(author)2016-03-09

Several of my students are designing a project that calls for this circuit. Thank you.

author
TechDepot+Egypt made it!(author)2016-03-09

Thanks Verga :), Glad to know that.
Actually our next tutorial will be about DC Motors drivers, then we will consolidate all of instructables into a little obstacle detecting, solar powered, Android controlled little robot :).
If you think this would be beneficiary for your students then follow us :)
Cheers!

About This Instructable

1,567views

30favorites

License:

Bio: TechDepot Egypt is an online retail store that sells the components to bring your electronics projects ideas to life. We are not just components sellers ... More »
More by TechDepot Egypt:Add motion detection to your Arduino home security project using HC-SR505Receive alerts and messages from your Arduino MegaVery Simple HC-SR04 Connection to Arduino Example
Add instructable to: