Smart Home: The ultrasonic sensor for our Arduino project

Hello everyone. This week, we will talk about the detection sensor used for our Smart Home project and its implementation in the Arduino software.


The detection sensor studied is an ultrasonic sensor HC-SR04. We will now define the characteristics of this sensor:


Ultrasonic ranging module HC – SR04 provides 2cm – 400cm non-contact measurement function, the ranging accuracy can reach to 3mm.

The module includes ultrasonic transmitter, receiver and control circuit. The basic principle of work:

1) Using IO trigger for at least 10us high level signal,

2) The Module automatically sends eight 40 kHz signals and detects whether there is a pulse signal back.

3) IF the signal comes back, through high level, time of high output IO duration is the time from sending ultrasonic to returning.

You only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8-cycle-burst of ultrasound at 40 kHz and raise its echo. The Echo is a distance object that is pulse width and the range in proportion .You can calculate the range through the time interval between sending trigger signal and receiving echo signal. Formula: uS / 58 = centimeters and the range = high level time * velocity (340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.

To study this sensor and learn how to use it, we made a prototype using one sensor and one LED. By coding with Arduino, we are able to receive in real time the distance between the sensor and any other object in front of it. When an object is under the distance of 50mm, the light is on.


Here is the code:

#define trigPin 13   //Arduino card output for the trigger

#define echoPin 12  //Arduino card intput for the echo

#define led 11   //Arduino card output for the led

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);


void loop() {

long duration, distance;

digitalWrite(trigPin, LOW);  // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin, HIGH);

//  delayMicroseconds(1000); – Removed this line

delayMicroseconds(10); // Added this line

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/58);  //see datasheet

if (distance >= 400 || distance <= 0){

Serial.println(“Out of range”);


else {


Serial.println(” cm”);


if (distance <= 50)


digitalWrite (led, HIGH);




digitalWrite (led,LOW);





This prototype and the code allow us to learn how this sensor works. Thank you for following us.

The Smart Home Team.

One thought on “Smart Home: The ultrasonic sensor for our Arduino project

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s