Introduction: Very Simple HC-SR04 Connection to Arduino Example

About: 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 but we are as well technology lovers and hobbyists. We a…

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 :)