Skip to content

Instantly share code, notes, and snippets.

@kirkjoserey
Created April 4, 2013 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkjoserey/5306581 to your computer and use it in GitHub Desktop.
Save kirkjoserey/5306581 to your computer and use it in GitHub Desktop.
20x4 LCD display (Hitachi HD44780 driver) + HY-SRF05 Sensor
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 20x4 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
/*
Tested with HY-SRF05, HC-SR04
Assuming a room temp of 20 degrees centigrade
The circuit:
* VVC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 6
* ECHO connection of the sensor attached to digital pin 7
*/
// include the library code:
#include <LiquidCrystal.h>
const int TRIG_PIN = 6;
const int ECHO_PIN = 7;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
// Print a message to the LCD.
lcd.print(" HY-SRF05 Sensor");
delay(3000);
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Distance");
lcd.setCursor(0,2);
lcd.print("IN");
lcd.setCursor(0,3);
lcd.print("CM");
}
void loop() {
long duration, distanceCm, distanceIn;
//Read the Sensor
duration = pulseDuration();
// convert the time into a distance
distanceCm = duration / 29.1 / 2 ;
distanceIn = duration / 74 / 2;
lcd.setCursor(4, 2);
lcd.print(" ");
lcd.setCursor(4, 2);
if (distanceIn <= 0){
lcd.print("Out");
} else {
lcd.print(distanceIn);
}
lcd.setCursor(4, 3);
lcd.print(" ");
lcd.setCursor(4, 3);
if (distanceCm <= 0){
lcd.print("Out");
} else {
lcd.print(distanceCm);
}
}
long pulseDuration(){
long duration;
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN,HIGH);
return duration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment