Contactless Distance Finder Project using Arduino

0
Contactless Distance Finder Project using Arduino

To show how a basic ultrasonic sensor may be upgraded into a sophisticated contactless distance finder, let's quickly build a prototype. In this project, a 162 Character LCD will be used, and it will show a horizontal bar to indicate the object's distance from the viewer.
Consider reading the lesson below if you are unfamiliar with 162 character LCDs.

wiring connection

The connection to the LCD must then be made as illustrated below.



Installation of LCD libraries

Installing the LCDBarGraph library is necessary before we submit the code and begin experimenting with the sensor. This library will make it easier to draw a horizontal bar on the LCD, with the length of the bar serving as a representation of the object's distance.
Navigate to Sketch > Include Libraries > Manage Libraries to install the library. Allow Library Manager to refresh the list of installed libraries and download the library index. Type "lcdbargraph" to narrow your search results. Select Install after clicking the first entry.

Code for Arduino

Once you have installed the library, try the below sketch.

// includes the LiquidCrystal Library
#include <LiquidCrystal.h> 

// includes the LcdBarGraph Library
#include <LcdBarGraph.h>

// Maximum distance we want to ping for (in centimeters).
#define max_distance 200

// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 


LcdBarGraph lbg(&lcd, 16, 0, 1); // Creates an LCD Bargraph object.

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup() 
{
  lcd.begin(16,2); // Initializes the interface to the LCD screen
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() 
{
  // Write a pulse to the HC-SR04 Trigger Pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Measure the response from the HC-SR04 Echo Pin
  duration = pulseIn(echoPin, HIGH);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  distance= duration*0.034/2;
  
  // Prints "Distance: <value>" on the first line of the LCD
  lcd.setCursor(0,0);
  lcd.print("Distance: "); 
  lcd.print(distance);
  lcd.print(" cm");

  // Draws bargraph on the second line of the LCD
  lcd.setCursor(0,1);
  lbg.drawValue(distance, max_distance);
  delay(500);
}

This is how the result appears.

Code Explanation

The Liquid Crystal Library must first be configured as normal. Next, use the LiquidCrystal instance you just made to construct a LcdBarGraph instance. The function Object() { [native code] } of LcdBarGraph should include a reference to LiquidCrystal.

 
Three more arguments are required by the LcdBarGraph function Object() { [native code] }. The second is the LCD's Character Column Number (in our case it is 16). The last two options allow you to arrange the bar anywhere you like.

// creating bargraph instance
LcdBarGraph lbg(&lcd, 16, 0, 1);

We use the drawValue(value, maxValue) method to display the bargraph after determining the distance from the sensor. With a value between 0 and maxValue, it creates a bargraph.

//display bargraph
lbg.drawValue(distance, max_distance);

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !