Guru (talk) 10:33, 14 October 2015 (IST)


In this simple tutorial, I will show you how to make a distance meter. It uses a ultrasonic sensor to read distance and display it in custom big fonts on a LCD display. It is powered up by a battery, so that you can take it around for a ride. We also plan to put up a case around it and make a kit. How does that sound?


1-1-DSC04314.JPG


Components/Boards Used

You'll require the components below to make it happen. Please consider buying them from the Explore Embedded Store to help us make all the awesome stuff.

1 Explore One for Arduino
2 Character LCD 16 x 2 - White on Blue
3 HC-SR04 Ultrasonic Sensor Module
4 LCD Breakout Board for 16x1 16x2 20x4 128x64
5 Large Breadboard

Wiring diagram

Explore Distance meter.png

Arduino code for Distance Meter

/*
A set of custom made large numbers for a 16x2 LCD using the
LiquidCrystal librabry. Works with displays compatible with the
Hitachi HD44780 driver.
The Cuicuit:
LCD RS pin to D12
LCD Enable pin to D11
LCD D4 pin to D5
LCD D5 pin to D4
LCD D6 pin to D3
LCD D7 pin to D2
LCD Vee tied to a pot to control brightness
LCD Vss and R/W tied to ground
LCD Vcc to +5V
*/
// include the library
#include <LiquidCrystal.h>
#include <Ultrasonic.h>
// initialize the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Ultrasonic ultrasonic(A0,A1);
// the 8 arrays that form each segment of the custom numbers
byte bar1[8] =
{
B11100,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11100
};
byte bar2[8] =
{
B00111,
B01111,
B01111,
B01111,
B01111,
B01111,
B01111,
B00111
};
byte bar3[8] =
{
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte bar4[8] =
{
B11110,
B11100,
B00000,
B00000,
B00000,
B00000,
B11000,
B11100
};
byte bar5[8] =
{
B01111,
B00111,
B00000,
B00000,
B00000,
B00000,
B00011,
B00111
};
byte bar6[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte bar7[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00111,
B01111
};
byte bar8[8] =
{
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
void setup()
{
// assignes each segment a write number
lcd.createChar(1,bar1);
lcd.createChar(2,bar2);
lcd.createChar(3,bar3);
lcd.createChar(4,bar4);
lcd.createChar(5,bar5);
lcd.createChar(6,bar6);
lcd.createChar(7,bar7);
lcd.createChar(8,bar8);
// sets the LCD's rows and colums:
lcd.begin(16, 2);
}
void custom0(int col)
{ // uses segments to build the number 0
lcd.setCursor(col, 0);
lcd.write(2);
lcd.write(8);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(1);
}
void custom1(int col)
{
lcd.setCursor(col,0);
lcd.write(32);
lcd.write(32);
lcd.write(1);
lcd.setCursor(col,1);
lcd.write(32);
lcd.write(32);
lcd.write(1);
}
void custom2(int col)
{
lcd.setCursor(col,0);
lcd.write(5);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(6);
}
void custom3(int col)
{
lcd.setCursor(col,0);
lcd.write(5);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(6);
lcd.write(1);
}
void custom4(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(6);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(32);
lcd.write(32);
lcd.write(1);
}
void custom5(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(3);
lcd.write(4);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(6);
lcd.write(1);
}
void custom6(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(3);
lcd.write(4);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(1);
}
void custom7(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(8);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(32);
lcd.write(32);
lcd.write(1);
}
void custom8(int col)
{
lcd.setCursor(col, 0);
lcd.write(2);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(1);
}
void custom9(int col)
{
lcd.setCursor(col, 0);
lcd.write(2);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(6);
lcd.write(1);
}
void printNumber(int value, int col) {
if (value == 0) {
custom0(col);
} if (value == 1) {
custom1(col);
} if (value == 2) {
custom2(col);
} if (value == 3) {
custom3(col);
} if (value == 4) {
custom4(col);
} if (value == 5) {
custom5(col);
} if (value == 6) {
custom6(col);
} if (value == 7) {
custom7(col);
} if (value == 8) {
custom8(col);
} if (value == 9) {
custom9(col);
}
}
void printDist(int dist) {
int m , c, d, u, number;
number = dist;
if (number > 999) {
m = (number - (number % 1000)) / 1000;
number = number % 1000;
} else {
m = 0;
}
if (number > 99) {
c = (number - (number % 100)) / 100;
number = number % 100;
} else {
c = 0;
}
if (number > 9) {
d = (number - (number % 10)) / 10;
number = number % 10;
} else {
d = 0;
}
u = number;
lcd.setCursor(0, 0);
lcd.print("Dist: ");
printNumber(m, 4);
printNumber(c, 7);
printNumber(d, 10);
printNumber(u, 13);
lcd.setCursor(0,1);
lcd.print("cm");
}
void loop()
{
printDist(ultrasonic.Ranging(CM));
delay(200);
}

Downloads

References and Credits