Ultrasonic Range Sensor
January 27th, 2010
3 comments
Today I recieved a Ultrasonic Range Sensor bought on eBay.
It’s much like the Parallax Ping))), except that it has got a Trig and a Echo pin, instead of the Ping)))’s multipin (Trig and Echo on the same pin)
I quickly made some code in the Arduino IDE and got it running quick…
Just a sidenode from the physics class; as the sound is travelling thru air with a speed of 340 m/s, this can be recalculated to 0.034 cm/microsecond, which is the same as 29.411 microsecond/cm
For those who may be interested the code is here:
/* Ultrasonic Sensor This sketch reads a ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return.� The length of the returning pulse is proportional to the distance of the object from the sensor. The circuit: * +V connection of the Ultrasonic Sensor attached to +5V * GND connection of the Ultrasonic Sensor attached to ground * Trig connection of the Ultrasonic Sensor attached to digital pin 2 * Echo connection of the Ultrasonic Sensor attached to digital pin 3 created 25. Januar 2010 by Thomas Jespersen */ // this constant won't change.� It's the pin number // of the sensor's output: const int TrigPin = 2; const int EchoPin = 3; void setup() { // initialize serial communication: Serial.begin(9600); pinMode(TrigPin, OUTPUT); pinMode(EchoPin, INPUT); } void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm, mm; // The Ultrasonic Sensor is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(TrigPin, LOW); delayMicroseconds(2); digitalWrite(TrigPin, HIGH); delayMicroseconds(5); digitalWrite(TrigPin, LOW); // Read the signal from the Ultrasonic Sensor a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the Trig to the reception of its echo off of an object. duration = pulseIn(EchoPin, HIGH); // convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); mm = microsecondsToMillimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm, "); Serial.print(mm); Serial.print("mm"); Serial.println(); delay(100); } long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))) // (another ultrasonic range sensor), // there are 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second).� This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. // 340 m/s // 34000 cm/s // 34 cm/ms // 0.034 cm/micros // 1/0.034 cm/micros = 29.411 micros/cm return microseconds / 29.411 / 2; } long microsecondsToMillimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter, // and then we multiply with 10 to get in in millimeters. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. // 340 m/s // 34000 cm/s // 340000 mm/s // 340 mm/ms // 0.34 mm/micros // 1/0.34 mm/micros = 2.9411 micros/mm return microseconds / 2.9411 / 2; }
Recent Comments