picture of Bill


Building A Digital
Yardstick

In this project the Arduino micro-controller is paired with an ultrasonic sensor and Liquid Crystal Display to produce a distance measuring device.

Project #1 Building A Digital Yardstick

This project combines an ultrasonic sensor , an LCD display, and an Arduino to create a digital yardstick.

The Sensor

The ultrasonic sensor was the HCSRO4. These are fairly inexpensive, costing in the neighborhood $3.00. They consist of two parts mounted on a circuit board. One sends out a ping at ultrasonic frequency, while the other receives the reflected echo. Circuity on board measures the time from send to echo received in micro seconds and passes this along to the Arduino.

The LCD Display Backpack

In this experiment the LCD display was combined with an Adadfruit backpack which converts the display to a Serial Input device using I2C protocol. This significantly reduces the number of wires needed to connect the LCD display to the Arduino from eight to four. Two of the four wires are for power. Of the remaining two, one is for data, and the other for a clock.

The Wiring Diagram

Here is how the various components are wired together.

The completed Project

And this is what the completed project looks like.

The Script

The software program, in Arduino lingo the script, that makes it all happen follows:

// Arduino Ultrasonic Sensor HC-SR04
// Adafruit I2c LCD backpack
// Modified by Bill Johnson 5/14/21 to include LCD display
/* The LCD
* 5V to 5 v pin
* Gnd to Arduinio GND
* Clk to Analog #5
* Dat to Analog #4
*/
// -----------------------------//
#include // This library needed for I2C serial protocol
#include // This library needed for Adafruit backpack

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
float distance; // variable for the distance measurement

Adafruit_LiquidCrystal lcd(0);
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // Serial Communication is starting with 9600 of baudrate speed lc
d.begin(16,2);

}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2/2.54; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" inches");
Serial.print(" Time: ");
Serial.print(duration);
Serial.println(" microseconds");

lcd.setCursor(0,0);
lcd.print (distance);
lcd.print(" inches");

lcd.setCursor(0,1);
lcd.print("Time usec ");
lcd.print(duration);
delay(5000);
lcd.clear();
}
// -----------------------------//

Conclusion

Experiment #1 demonstrates that a digital yard stick can be created using an ultrasonic sensor, an LCD and Arduino. Measuments are accurate from about one to thirty-six inches. The unit shows significant error with distances less than one inch and greater than thirty-six inches.


Copyright 2021, William Johnson