/*This software is provided in an “AS IS” condition,NO WARRANTIES in any form apply to this software. picmicrolab.com 10.8.2017 *************************************************************************************************************** Arduino Ultrasonic Sensor HC-SR04 Triger Output - pin 6 Echo Input - pin 7 */ // include the library code: #include // initialize the library by associating any needed LCD interface pin // with the arduino pin number it is connected to const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); int Triger = 6; int Echo = 7; int long EchoTime = 0; int centimeters = 0; void setup() { // initialize I/O pins. pinMode(Triger, OUTPUT); //Bit_6 pinMode(Echo, INPUT); //Bit_7 // set up the LCD's number of columns and rows: lcd.begin(16, 2); } void loop() { digitalWrite(Triger, LOW); delayMicroseconds(5); digitalWrite(Triger, HIGH); delayMicroseconds(10); digitalWrite(Triger, LOW); //----Generate Trig Puls of 10us------ EchoTime = pulseIn(Echo, HIGH); //--Measure return time---HC-SR04 Datasheet--- centimeters = EchoTime / 58.0 ; //-------------------------------------------- // Turn on the display: lcd.display(); if (centimeters > 400 || centimeters < 2) { lcd.print("Range");//If outside spec print "Range" lcd.setCursor(0, 0); // top left delay(500); } else { lcd.print(centimeters); lcd.print(" cm "); lcd.setCursor(0, 0); // top left delay(500); lcd.clear(); } }