Built one a few days ago, this works for me:
#include <LiquidCrystal.h>
#define trigPin 8; //Define the Trigger pin of the Ultrasonic
#define echoPin 9; //Define the Echo pin of the Ultrasonic
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Define LCD pins
void setup() {
lcd.begin(16, 2); //Setup LCD
pinMode(trigPin, OUTPUT); //Setup Trigger and Echo
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH); //Write High for 1ms
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //Time pulse in
distance = (duration/2) / 29.1; //Convert time to Cm
if (distance >= 200 || distance <= 0){
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Out of Range");
}
else {
lcd.clear();
lcd.setCursor(2,0); //Print to screen
lcd.print(distance);
lcd.setCursor(5,0);
lcd.print(" Cm");
}
delay(300);
}
Hope this helps,
Owen