//Sketch for Arduino UNO using 16X2 LCD Display //and HC-SR04 Ultrasonic module #include "LiquidCrystal.h" // POWERTIP LCD PC1602A // LiquidCrystal display with: // rs lcd pin 4 on arduino pin 12 // rw lcd pin 5 to ground // enable lcd pin 6 arduino on pin 11 / // d4,d5,d6,d7 lcd pins 11, 12, 13, 14 on arduino pins 5, 4, 3, 2 // Save pin 10 & 9 for the Ultrasonic Sensor #define trigPin 10 #define echoPin 9 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { //Setup Ultrasonic Detector //Measured distance will be output on USB serial link //and displayed on LCD Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); lcd.begin(16, 2); lcd.setCursor(0,0); lcd.print("Distance"); lcd.setCursor(0,1); lcd.print("Measuring"); } void loop() { //Get distance long duration, distance; // Trig should go Low-Wait 2 usec-High wait 100 usec-Low digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(100); digitalWrite(trigPin, LOW); //Get Echo pulse duration duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; // extended range checked upto 10 Meters // Write to serial and LCD if (distance >= 1000 || distance <= 0){ Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm "); } delay(500); lcd.clear(); lcd.setCursor(0,0); lcd.print("Distance"); lcd.setCursor(0, 1); // print distance lcd.print(distance); lcd.print(" cm"); }