Project3 Ultrasonicwith LCD
Project3 Ultrasonicwith LCD
In this project you are going to create a system which is supposed to measure the distance and also
display some information on the LCD and set on led light whever necessary. If the distance is in the
range of 10 to 50 cm then you will display “the risk is high” and set the red LED on, if the distance is
greater than 50cm and less or equal to 100cm then the risk is medium and set the yellow LED on, if the
distance is greater than 100cm then display no risk and set the green light on
2. Materials
1. Arduino.
2. ultrasonic sensor ( Hc-sr04 ).
3. LCD display with I2C
4. Resistor 220ohm.
5. LED.
6. Breadboard.
7. Jumper wire.
3. Program
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor-lcd
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x3F, 16 column and 2 rows
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
pinMode(trigPin, OUTPUT); // config trigger pin to output mode
pinMode(echoPin, INPUT); // config echo pin to input mode
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Distance: ");
lcd.print(distance_cm);
delay(500);
}
4. Wiring