Arduino Distance meter

Arduino Distance Meter circuit
In this post, we are going to show how to build a distance meter with an Arduino. An ultrasonic sensor is used to sense the distance and the results are displayed on the LCD. The ultrasonic sensor was already introduced in:
Measuring distance with an ultrasonic sensor
For the LCD, we bought a MIDAS MC21605B6WD-BNMLW LCD from Farnell for £6.40. The LCD is not directly compatible with the Arduino’s LCD library but by doing an initialisation of the LCD, the Arduino LCD library can eventually be used to control the LCD display.

A portable battery charger is used to power the circuit as shown in the photo above; this is of course optional! 🙂

Arduino Distance Meter

The connections to the LCD and ultrasonic sensor are shown above and the code is shown below.

/* The LCD circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 7
 * LCD D5 pin to digital pin 8
 * LCD D6 pin to digital pin 9
 * LCD D7 pin to digital pin 10
 * LCD R/W pin to digital pin 2
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * wiper to LCD VO pin to D13
 by BehindTheSciences.com */
#include <LiquidCrystal.h>
#define trigPin 5
#define echoPin 6
#define RS 12
#define RW 2
#define EN 11
#define VO 13
#define D4 7
#define D5 8
#define D6 9
#define D7 10

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 8, 9, 10);

void setup() {
  Serial.begin (9600);

  /*Initialisation of the LCD */
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  
  pinMode(EN, OUTPUT); 
  digitalWrite(EN, HIGH);
  pinMode(RS, OUTPUT); 
  digitalWrite(RS, LOW);
  pinMode(VO, OUTPUT); 
  digitalWrite(VO, LOW);

  pinMode(RW, OUTPUT); 
  digitalWrite(RW, LOW);
  
  delay(20); 
  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, HIGH);
  digitalWrite(D4, HIGH);
  delay(10);
  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, HIGH);
  digitalWrite(D4, LOW);
  
  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, HIGH);
  digitalWrite(D4, LOW);
  
  digitalWrite(D7, HIGH);
  digitalWrite(D6, HIGH);

  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D4, LOW);
  
  digitalWrite(D7, HIGH);
  digitalWrite(D6, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D4, LOW);

  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D4, LOW);

  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D4, HIGH);

  digitalWrite(D7, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D4, LOW);

  digitalWrite(D7, LOW);
  digitalWrite(D6, HIGH);
  digitalWrite(D5, HIGH);
  digitalWrite(D4, HIGH);
/*-----------------------*/
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(3, 0);
  lcd.print("Behind The");
  lcd.setCursor(4, 1);
  lcd.print("Sciences");
  delay(30);
}

void loop() {
long duration, distance;

  digitalWrite(trigPin, LOW);  // Set trigger Pin to Low
  delayMicroseconds(2); // Delay for 2 microseconds
  digitalWrite(trigPin, HIGH); // Set trigger Pin to Low
  delayMicroseconds(10); // 
  digitalWrite(trigPin, LOW); 
  duration = pulseIn(echoPin, HIGH);
  distance = ((duration/2) / 29.1);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("BehindTheScience");
  if (distance >= 400 || distance <= 0){
    lcd.setCursor(0, 1);
    lcd.print("Out of range");
  }
  else {
    lcd.setCursor(0, 1);
    lcd.print("Distance = ");
    lcd.print(distance,DEC);
    lcd.print("cm");
  }
  delay(300);
}

If you have any comments, please contact us at contact@behindthesciences.com

Add a Comment

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons