Introduction: Ultrasonic Ruler With LCD and Arduino
You'll need
- An LCD
- An Arduino Uno (I have an ethernet shield on mine, it doesn't do any thing in this project, but it's a pain to take on and off)
- An Ultrasonic Sensor Module
- 20 - 30 Jumper Wires
Step 1: The Ultrasonic Module
I'm using the HC - SR04 Ultrasonic but it doesn't really matter, the pins are probably the same on all of them.
Put the ultrasonic in your breadboard and hook up
* Ultrasonic GND to A0
* Ultransonic Echo to A1
* Ultrasonic Trigger to A3
* Ultrasonic VCC to A4
Now upload this code to the board and replace lcd.print with serial.print
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 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)
* Ultrasonic GND to A0
* Ultransonic Echo to A1
* Ultrasonic Trigger to A3
* Ultrasonic VCC to A4
#include <LiquidCrystal.h>
*/
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
pinMode (A4,OUTPUT);//attach pin 2 to vcc
pinMode (A0,OUTPUT);//attach pin 5 to GND
// initialize serial communication:
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Ultrasonic Ruler with LCD");
lcd.setCursor(0, 1);
lcd.print("by Alex Willis");
}
void loop() {
digitalWrite(A4, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, 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:
pinMode(A3, OUTPUT);// attach pin 3 to Trig
digitalWrite(A3, LOW);
delayMicroseconds(2);
digitalWrite(A3, HIGH);
delayMicroseconds(5);
digitalWrite(A3, LOW);
// The same 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.
pinMode (A1, INPUT);//attach pin 4 to Echo
duration = pulseIn(A1, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(0, 2);
lcd.print(inches);
lcd.print("in, ");
lcd.print(cm);
lcd.print("cm");
delay(1000);
lcd.clear();
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
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;
}
Replacing the lcd.print with serial.print will ensure that your ultrasonic is working and then we can move onto the LCD.
Put the ultrasonic in your breadboard and hook up
* Ultrasonic GND to A0
* Ultransonic Echo to A1
* Ultrasonic Trigger to A3
* Ultrasonic VCC to A4
Now upload this code to the board and replace lcd.print with serial.print
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 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)
* Ultrasonic GND to A0
* Ultransonic Echo to A1
* Ultrasonic Trigger to A3
* Ultrasonic VCC to A4
#include <LiquidCrystal.h>
*/
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
pinMode (A4,OUTPUT);//attach pin 2 to vcc
pinMode (A0,OUTPUT);//attach pin 5 to GND
// initialize serial communication:
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Ultrasonic Ruler with LCD");
lcd.setCursor(0, 1);
lcd.print("by Alex Willis");
}
void loop() {
digitalWrite(A4, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, 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:
pinMode(A3, OUTPUT);// attach pin 3 to Trig
digitalWrite(A3, LOW);
delayMicroseconds(2);
digitalWrite(A3, HIGH);
delayMicroseconds(5);
digitalWrite(A3, LOW);
// The same 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.
pinMode (A1, INPUT);//attach pin 4 to Echo
duration = pulseIn(A1, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(0, 2);
lcd.print(inches);
lcd.print("in, ");
lcd.print(cm);
lcd.print("cm");
delay(1000);
lcd.clear();
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
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;
}
Replacing the lcd.print with serial.print will ensure that your ultrasonic is working and then we can move onto the LCD.
Step 2: The LCD
Start by attaching
* 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 pot:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3),
Now this is the same circuit as the hello world example for Arduino and it would probably be best to test that program to ensure you have your LCD setup properly (if you didn't notice, I like to test as I go along).
* 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 pot:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3),
Now this is the same circuit as the hello world example for Arduino and it would probably be best to test that program to ensure you have your LCD setup properly (if you didn't notice, I like to test as I go along).
Step 3: The Code
You should have your circuit all setup now, so now it's time for the code.
/*
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)
* Ultrasonic GND to A0
* Ultransonic Echo to A1
* Ultrasonic Trigger to A3
* Ultrasonic VCC to A4
#include <LiquidCrystal.h>
*/
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
pinMode (A4,OUTPUT);//attach pin 2 to vcc
pinMode (A0,OUTPUT);//attach pin 5 to GND
// initialize serial communication:
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Ultrasonic Ruler with LCD");
lcd.setCursor(0, 1);
lcd.print("by Alex Willis");
}
void loop() {
digitalWrite(A4, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, 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:
pinMode(A3, OUTPUT);// attach pin 3 to Trig
digitalWrite(A3, LOW);
delayMicroseconds(2);
digitalWrite(A3, HIGH);
delayMicroseconds(5);
digitalWrite(A3, LOW);
// The same 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.
pinMode (A1, INPUT);//attach pin 4 to Echo
duration = pulseIn(A1, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(0, 2);
lcd.print(inches);
lcd.print("in, ");
lcd.print(cm);
lcd.print("cm");
delay(1000);
lcd.clear();
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
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;
}
Upload this code and it should work, although it's not very accurate, but hey! It works!
/*
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)
* Ultrasonic GND to A0
* Ultransonic Echo to A1
* Ultrasonic Trigger to A3
* Ultrasonic VCC to A4
#include <LiquidCrystal.h>
*/
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
pinMode (A4,OUTPUT);//attach pin 2 to vcc
pinMode (A0,OUTPUT);//attach pin 5 to GND
// initialize serial communication:
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Ultrasonic Ruler with LCD");
lcd.setCursor(0, 1);
lcd.print("by Alex Willis");
}
void loop() {
digitalWrite(A4, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, 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:
pinMode(A3, OUTPUT);// attach pin 3 to Trig
digitalWrite(A3, LOW);
delayMicroseconds(2);
digitalWrite(A3, HIGH);
delayMicroseconds(5);
digitalWrite(A3, LOW);
// The same 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.
pinMode (A1, INPUT);//attach pin 4 to Echo
duration = pulseIn(A1, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(0, 2);
lcd.print(inches);
lcd.print("in, ");
lcd.print(cm);
lcd.print("cm");
delay(1000);
lcd.clear();
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
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;
}
Upload this code and it should work, although it's not very accurate, but hey! It works!

