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



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.


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!