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



Showing posts with label HC-SR04. Show all posts
Showing posts with label HC-SR04. Show all posts

Saturday, 27 December 2014

Measuring Distance with HC-SR04 Ultrasonic Module on 16X2 LCD PART-2 [ARDUINO]



So, I hope you might have got the idea of the working of the HC-SR04 Ultrasonic Distance Module. If not, read the part - 1 of the tutorial here: Measuring Distance with HC-SR04 Ultrasonic Ping Sensor and Arduino PART-1 [ARDUINO]

Also, I would recommend you to check out the previous article which used an LCD to display the temperature and humidity. Because I haven't changed the LCD connection. I only changed the HC-SR04 connection scheme.

Well, this is a simple continuation of the the tutorial. In the previous part we used a serial monitor to see the distance. This is quite inconvenient if you want to make the project more portable and small. Also, at many times, you'll need to show data on the 16X2 LCD. This project will be quite helpful to you t see how the data is displayed on the 16X2 LCD.

What this project does?
Calculate the distance of the object from the sensor and print the distance in centimeters on an 16X2 LCD.

I'm skipping the "Let's Know The Basics" part, because in the beginning I've already mentioned the links to previous posts. I've mentioned everything in those 2 posts.

Materials Required:
1. An HC-SR04 Ultrasonic Ping Sensor
2. Arduino
3. Jumpers
4. One 16X2 LCD
5. Three 1K and one 10K resistor.
6. Breadboard
7. and of course, a PC with Arduino IDE installed!

Procedure:
Hardware part:
1. Make the connections as shown:


REFERENCE:



Software part:
1. Upload the sketch given below.
2. Power on the Arduino.


Sketch:

/* 
 * ON PUBLIC DOMAIN
 * Author         : Nitish Dash
 * Name           : Measuring Distance with HC-SR04 on 16X2 LCD 
 * Created        : 12/27/2014
 * Webpage        : http://goo.gl/qcpuCa
 * Author Email   : nitishdash95@gmail.com
 * Author Website : http://www.nitishdash.com/
 **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS ****
 */
 
#include 
#define trig 10
#define echo 9
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() { //initialise for at least 2s
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  lcd.begin(16, 2);
  lcd.print("INITIALISING");
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
}
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 >= 200 || dist <= 0)
 {
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Distance: ");
   lcd.setCursor(0, 1);
   lcd.print("Out of range!");
 }
 else 
 {
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Distance: ");
   lcd.setCursor(0, 1);
   lcd.print((int)dist);
   lcd.print(" cms");
  }
 delay(700); //modify but don't go below 500 ms
}

NOTE:Please remove the line 61 before uploading.

How this works (Algorithm):
Refer to the previous two posts here to know more:
1. Measuring Distance with HC-SR04 Ultrasonic Ping Sensor and Arduino PART-1 [ARDUINO]
2. Temperature and Humidity display on 16X2 LCD with DHT11 Sensor [ARDUINO]

Video:
Coming soon.....

Downloads:

Something for you!

Try these ideas to enhance your knowledge and application:
1. Display the distance in feet and inches
2. Combine Part-1 with Part-2 and glow a red led when distance is less than 20cms.


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.



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!