//http://colandino.nl #include "Wire.h" #include "LiquidCrystal_I2C.h" #include "NewPing.h" LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() { lcd.init(); // initialize the lcd lcd.begin (20,4); // our LCD is a 20x4, change for your LCD if needed // LCD Backlight ON // lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.backlight(); lcd.home (); // go home on LCD lcd.print("Range Finder HC-SR04"); } void loop() { unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). unsigned int cm = sonar.convert_cm(uS); // Convert into centimeters lcd.setCursor (0,1); // go to start of 1st line lcd.print("http://colandino.nl"); lcd.setCursor (0,2); // go to start of 2nd line lcd.print("Huidige afstand:"); lcd.setCursor (0,3); // go to start of 4th line lcd.print("Ping: "); lcd.print(cm); lcd.print(" cm "); delay(250); }