Lecture 11
Lecture 11
Distance
There are lots of applications where we not only need to know that an object, or a person,
in nearby, but also how far they are. Imagine a robot moving around in a room. The robot
can use a distance (or proximity) sensor to detect that it is approaching a wall or another
object. Or, you could use a proximity sensor to automatically open a door if a person is
within a meter of the sensor. You find
such sensors in cars (to help with
parking and to avoid small accidents),
and in smart phones where the
smartphone can detect, for example,
that the phone is held against the
user's head, useful so that the screen
is turned o to avoid accidental
touchscreen input.
Ultrasonic sensors are solid-state devices, very reliable and cheap. Especially in indoor
environments, and for small spaces (or measuring small distances), these sensors represent
a good choice. Anything that is solid enough to allow sounds to bounce will work with
these sensors. If you want to measure or detect things like smoke and clouds, you will need
to use something else, perhaps a microwave doppler radar.
For the Arduino, a commonly used proximity sensor is the HC-SR04. You can find them on
Ebay for less than $2 each.
Peter Dalmaris
Lecture 11
Assembly
We will need:
The Arduino
An ultrasonic sensor, like
the HC-SR04.
Sketch
#define trigPin 13
#define echoPin 12
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
2
Peter Dalmaris
Lecture 11
delay(500);
}
We define the sensor's trigger and echo pins to be 13 and 12 respectively. In the setup
function, we initialise the monitor, and set pin 13 to be the output and pin 12 to be the
input.
Through pin 13, the Arduino will ask the sensor to trigger a ping, similar to the "boing" noise
that submarines emit when they use their sonar. This ping, assuming it bounces of an
object in range, will come back and will be picked up by the sensor's receiver. The Arduino
will know when that happens because it is monitoring pin 12, which is connected to the
sensor's echo pin.
In the loop() function, we first setup two variables of type long. Long numbers are 4 bytes in
size, a total of 32 bits, and can hold very large numbers: -2,147,483,648 to 2,147,483,647.
The variable duration will hold the total number of microseconds that it took for the ping to
reach the object and return to the sensor. The variable distance will contain the distance to
that object in centimetres.
0.5
The Arduino is triggering a ping by writing to the trigger pin three pulses: first, a digital LOW
for 2 microseconds, then a digital HIGH for 10 microseconds, and finally a digital LOW
which stays low until the next iteration of the loop.
Ping 1
Ping 2
Ping 2
It then uses the function pulseIn to get the number of microseconds in takes of the ping to
come back. PulseIn accepts two parameters: a pin number (in our case it is 12, stored in
variable echoPin), and the pulse level we want to detect, in our case it is HIGH because we
3
Peter Dalmaris
Lecture 11
want to detect the 10 microsecond ping we just emitted. As soon as the Arduino calls the
pulseIn function, it starts timing. It returns the number of microseconds from the time the
function was called until it detects the ping echo.
The distance is calculated by the Arduino. It divides by two the duration that the pulseIn
function returned, since the ping travels a total of twice the distance to the object (going
there and its echo coming back). It then divides again by the "magic number" 29.1. This
number derives from this calculation:
The speed of sound at 0 degrees celsius is measured to be 331.5 meters per second. At
dierent temperatures, the speed of sound is calculated by adjusting 331.5m/s for the
temperature by multiplying by 0.6:
We need to convert the seconds to microseconds and the length from meters to
centimetres:
Limitations
If the distance to a target is over 200 centimetres, the Arduino reports that the target is out
of range, since at that distance measurements are not reliable. The same happens if the
distance is negative (question to consider, why do we need to test for negative
distance???). Any other distance condition is valid, so the monitor will print out the distance
in centimetres.