Ultrasonic Distance Sensor
How it Works The sensor emits short bursts of sound, and listens for the echo off nearby objects By measuring the time of flight, distance can be computed
When it doesn’t work When waves are not reflected back towards the sensor
Specifications An ultrasonic sensor comes with a chart indicating how far and wide its waves will travel They are not laser beams, and objects not directly in front can still reflect waves back For this sensor the waves travel ±20° And up to 7.5 feet
Wiring An ultrasonic sensor contains 2 sensors, a speaker and a microphone The speaker is an OUTPUT The microphone is an INPUT There are also connections for 5v and GND
Distance = Speed x Time Example Miles = MPH x Hours Computing Distance The sensor will give us the time for the round trip Multiplying the time by the speed of sound, and divide by 2 to calculate for one way The speed of sound is 761 MPH, or 1/74 inches per microsecond
Code void setup() { Serial.begin(9600); pinMode(12, OUTPUT); pinMode(11, INPUT); } void loop() { long duration, inches; digitalWrite(12, LOW); delayMicroseconds(2); digitalWrite(12, HIGH); delayMicroseconds(5); digitalWrite(12, LOW); duration = pulseIn(11, HIGH); inches = duration / 74 / 2; Serial.print(inches); Serial.println("in "); } By setting pin 12 to HIGH, the speaker sends a 40kHz (ultrasonic) burst of sound out its “speaker” The sound burst reflects off something and returns to the microphone; the microphone then sets INPUT pin 11 to HIGH. The pulseIn command measures the time of flight (the time it took the wave to travel back to the microphone and turn pin 11 HIGH). Multiplying TIME x SPEED (1/74) and dividing by 2 gives us the distance traveled
Complete the Ultrasonic Distance Sensor LAB Complete the Ultrasonic Distance Sensor Lab in Unit 11