Interfacing HC-SR04 Ultrasonic Sensor with Arduino

hc-sr04 sensor

Introduction

HC-SR04 is an ultrasonic sensor which works on the principle of SONAR to calculate distances from a far object. The HC-SR04 can be used to detect objects in the range of 2cm to 4m. Since it works on the principle of reflection of sound waves, its operation is unaffected by sunlight as opposed to IR Sensors. As such it can be used to detect and measure distances from objects very reliably. But acoustically soft materials like cloth can be difficult to detect.

Technical Specifications

  • Power Supply : +5V
  • Quiescent Current : <2mA
  • Working Current: 15mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 400 cm/1″ ­ 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm

ultra-sonic-dimen

Pin Details

  • VCC : +5V
  • Trig : Trigger input Sensor
  • Echo : Echo output Sensor
  • GND : GND

How it works

To start measurement, Trig pin must receive a pulse of high (5V) for at least 10us, this will initiate the sensor, and the sensor will transmit out 8 cycles of ultrasonic pulses at 40kHz and wait for the reflected ultrasonic pulse. When the sensor detects reflected ultrasonic pulses in the receiver, it will set the Echo pin to high (5V) and delay for a period (width) which is proportional to the distance from the object.

To obtain the distance,  we measure the width of the pulse at the Echo pin and multiply it with the speed of sound (340 m/s) and divide it by two.

hc-sr04-timing-diagram

Interfacing HC-Sr04 with with Arduino

 

arduino_hc-sr04_interfacing_bb.png
Breadboard Layout
arduino_hc-sr04_interfacing_schem
Schematic diagram

Arduino Code

/* HC-SR04 Sensor

This sketch reads a PING))) ultrasonic range finder and lights up multiple
led s in direct proportion to the distance of the given object from the
sensor. To do this, it sends a pulse to the sensor to initiate a reading,
then listens for a pulse to return. The length of the returning pulse is
proportional to the distance of the object from the sensor.

The circuit:
* VCC connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* TRIG connection of the PING))) attached to digital pin 3
* ECHO connection of the PING))) attached to digital pin 2

Created 31 Jan 2016
by Sandhan Sarma
This example code is in the public domain.

*/

// this constant won't change.It's the pin number
// of the sensor's output:
const int trigPin = 3;
const int echoPin = 2;
const int ledPin1 = 8;
const int ledPin2 = 7;
const int ledPin3 = 6;
const int ledPin4 = 5;
const int ledPin5 = 4;

void setup()
{

//set output pins for leds
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);

//set pin modes for Trigger and Echo pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

//initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, distance_in_cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

// The echoPin pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
distance_in_cm = microsecondsToCentimeters(duration);

//This block of code will print out the distance
//to the Arduino IDE serial monitor
Serial.print(distance_in_cm);
Serial.print(&amp;quot;cm&amp;quot;);
Serial.println();

//The following if-else block will sample the distance
//at every 10 cm and light up an Led.
//The farther the object, more number of Led s glow
//nearer the object lesser led s glow
if (distance_in_cm&amp;amp;gt;=10)
{
digitalWrite(ledPin5,HIGH);
}
else
{
digitalWrite(ledPin5,LOW);
}

if (distance_in_cm&amp;amp;gt;=20)
{
digitalWrite(ledPin4,HIGH);
}
else
{
digitalWrite(ledPin4,LOW);
}

if (distance_in_cm&amp;amp;gt;=30)
{
digitalWrite(ledPin3,HIGH);
}
else
{
digitalWrite(ledPin3,LOW);
}

if (distance_in_cm&amp;amp;gt;=40)
{
digitalWrite(ledPin2,HIGH);
}
else
{
digitalWrite(ledPin2,LOW);
}

if (distance_in_cm&amp;amp;gt;=50)
{
digitalWrite(ledPin1,HIGH);
}
else
{
digitalWrite(ledPin1,LOW);
}
delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

References

Advertisements