Skip to content

Instantly share code, notes, and snippets.

@buildcircuit
Created October 24, 2012 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buildcircuit/3945670 to your computer and use it in GitHub Desktop.
Save buildcircuit/3945670 to your computer and use it in GitHub Desktop.
Ultrasonic range finder using Amarino Evaluation shield
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 10, 9, 8, 7);
int TrigPin = 4;
int EchoPin = 2;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
}
void loop()
{
int distance,duration;
digitalWrite(TrigPin,HIGH);//TrigPin prepare high of more than 10us
delayMicroseconds(11);
digitalWrite(TrigPin,LOW);
duration = pulseIn(EchoPin, HIGH);//EchoPin received high start counting until the receiver to the low,return to the count valu
duration = duration/29/2;//Calculating the distance cm
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
lcd.clear();
lcd.print("Distance:");
lcd.print(duration);
Serial.print(duration);//Serial display distance
lcd.print("cm");
Serial.print("cm");
Serial.println();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment