#include <LiquidCrystal.h>
#define trigPin A5
#define echoPin A4
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin,
INPUT);
lcd.begin(16,
2); // start the library
lcd.setCursor(0, 1);
lcd.print(" HC-SR04 ");
}
void loop()
{
lcd.setCursor(0, 0);
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration =
pulseIn(echoPin, HIGH);
distance =
(duration/2) / 29.1;
lcd.print(distance);
lcd.print(" cm ");
delay
(500);
}
Upload the program
Read distance on LCD Screen
Activities:
1) Set the HC-SR04 sensor 10 cm from a large flat surface.
Note the location of 0 cm on the sensor.
(Front Edge?, at PC Board?, 1/2 way to PC Board?...)
2) Measure 8 known distances and record sensor reading.
Calculate % error = 100(a-e)/a where a = accepted, e = experimental
3) What are the shortest and longest distances the Ultrasonic measures accurately?
4) Are there surfaces that are "invisible" to the sensor? (absorb the sound and give erroneous readings?)
5) Extra Credit:
Create a program to scroll an Appropriate message across the LCD display.
:-)