Find fresh tutorials related to Arduino, Raspberry Pi, 555 Timer and many more microcontrollers. Learn something new everyday!



Wednesday 17 December 2014

Measuring Distance with HC-SR04 Ultrasonic Ping Sensor and Arduino PART-1 [ARDUINO]



Hope you guys enjoyed making the last project. So, today my HC-SR04 Ping sensor arrived via courier which I had ordered 2 days back. Opened it up and there was this shiny flashing (not really!) 2 eyed thing.
See for yourself.


What this project does?
Measure the distance of a distant object using the ultrasonic ping sensor. Then, light up the red led if the distance between sensor and the object is 12cms or less. And green led remains on if the distance is more than 12cms. Also, it will print the reading on the serial monitor every 0.4 seconds.
But, in this first part, we'll use LEDs to notify us the proximity. In part 2, we'll use an LCD to directly display the values.

Let's know the basics!
So, let's discuss a little bit about this sensor. So, as the title itself suggests, this is a ping sensor or in simple form, a tiny little SONAR. With an arduino, you can harness the power of this tiny sonar, which is surprising me everyday. Some quick facts:


The HC-SR04 ultrasonic sensor uses sonar to determine 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.

Features:
  • Power Supply :+5V DC
  • Quiescent Current : <2mA
  • Working Currnt: 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



Parameter
Min
Typ.
Max
Unit
Operating Voltage
4.50
5.0
5.5
V
Quiescent Current
1.5
2
2.5
mA
Working Current
10
15
20
mA
Ultrasonic Frequency
-
40
-
kHz

So, hope you have got a quick idea of this device and the power it is packed with.

Now, our common algorithm follows!:

Materials Required:
1. An HC-SR04 Ultrasonic Ping Sensor
2. Arduino
3. Jumpers
4. Two 560 OHM resistors
5. One green led and one red led
6. Breadboard

Procedure:
1. Make the connections as shown:



2. Upload sketch

3. Start the serial monitor and check the runtime distance measurements

4. Bring an object close and test the project. If the red led glows, you're done with the project!


Sketch:

/*
 HC-SR04 Ultrasonic Distance Sensor
 CODED BY: Nitish Dash [nitishdash95@gmail.com]
 More info at: http://eweekend.blogspot.com
 */

#define trig 13
#define echo 12
#define red 11
#define green 10

void setup() {
  Serial.begin (9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
}

void loop() {
  long time, dist;
  digitalWrite(trig, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trig, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig, LOW);
  time = pulseIn(echo, HIGH);
  dist = ((time/2) / 29.1); //measure in cms
  if (dist < 12) //change this to set proximity distance 
  
{  
    digitalWrite(red,HIGH); 
  digitalWrite(green,LOW);
}
  else {
    digitalWrite(red,LOW);
    digitalWrite(green,HIGH);
  }
  if (dist >= 200 || dist <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(dist);
    Serial.println(" cm");
  }
  delay(400);
}

How this works (Algorithm):
The timing diagram of HC-SR04 is shown. To start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from receiver, it will set the Echo pin to high (5V) and delay for a period (width) which proportion to distance. To obtain the distance, measure the width (Ton) of Echo pin.

>>Time = Width of Echo pulse, in uS (micro second)
>>Distance in centimeters = Time / 58
>>Distance in inches = Time / 148
>>Or you can utilize the speed of sound, which is 340m/s



Video:
Coming soon.....

Downloads:
1. Sketch.ino
2. Breadboard schematics


Homework for you!

Try these ideas to enhance your knowledge and application:
1. Add another yellow led and modify the code for testing three conditions and glow the three leds as per the distance
2. Add a speaker and change it's tone as and when the distance changes.
3. Add this near your door and turn on an alarm if someone opens the door.

Stay tuned for the next part, where we will print the readings directly to a 16X2 LCD! So much fun!

UPDATE: Guys, part-2 is online now. Check it out here: http://electro.nitishdash.com/2014/12/7-measuring-distance-with-hc-sr04.html

Any doubts? Feel free to leave a comment.



0 comments:

Post a Comment

Leave your valuable feedback and suggestions.

Upcoming projects!

1. Programming ESP8266 using NodeMCU and LUA Scripts

2. IoT Applications

3. NRF42L01 2.4 GhZ module interfacing with arduino

4. ESP8266 - Everything cool about it!

Language ain't any barrier!

Get Updates!

Enter your email address:


Get the latest projects delivered into your inbox, hot!