CP316: Microprocessor Systems and Interfacing
Ultrasonic distance sensors
Objectives
This lab will introduce several options for interfacing with an
ultrasonic distance sensor.
Equipment
Procedure
Since the ultrasonic sensors require the measurement of the width of a
pulse, it makes sense to start by considering different ways to measure
pulse widths.
It also makes it easy to separate any problems
detecting pulses from problems
creating
pulses.
Pulse width by polling
- Connect the function generator to the oscilloscope, and produce a
square wave which goes from 0 to 5V with a frequency of about 200Hz.
If the pulse produced represents the echo pulse from an
ultrasonic sensor, what distance does that correspond to?
What pulse width corresponds to a distance of 10 cm?
- Connect the Arduino board. Attach the input from the frequency
generator to Arduino pin 2.
- Create a new sketch, and download the
PulseWidthPoll
sketch.
- Compile and run the sketch, and open the serial monitor to
see the results.
Do the times make sense?
- Increase the frequency (i.e. decrease the pulse width)
gradually towards the time you determined for a distance of 10
cm.
Do the times change consistently?
Is there any point at which the times become erratic?
If so, where, and what does limitation does that place on use of
this code?
Demonstration - demonstrate what you have learned.
Pulse width using pulseIn()
- Create a new sketch, and download the
PulseWidthPulseIn
sketch.
- Compile and run the sketch, and open the serial monitor to
see the results.
Do the times make sense?
- Increase the frequency (i.e. decrease the pulse width)
gradually towards the time you determined for a distance of 10
cm.
Do the times change consistently?
Is there any point at which the times become erratic?
If so, where, and what does limitation does that place on use of
this code?
Demonstration - demonstrate what you have learned.
Pulse width using external interrupt
- Create a new sketch, and download the
PulseWidthInt
sketch.
- Compile and run the sketch, and open the serial monitor to
see the results.
Do the times make sense?
- Increase the frequency (i.e. decrease the pulse width)
gradually towards the time you determined for a distance of 10
cm.
Do the times change consistently?
Is there any point at which the times become erratic?
If so, where, and what does limitation does that place on use of
this code?
Demonstration - demonstrate what you have learned.
Pulse width using external interrupt using low level
access
- Create a new sketch, and download the
PulseWidthIntLL
sketch.
- Compile and run the sketch, and open the serial monitor to
see the results.
Do the times make sense?
- Increase the frequency (i.e. decrease the pulse width)
gradually towards the time you determined for a distance of 10
cm.
Do the times change consistently?
Is there any point at which the times become erratic?
If so, where, and what does limitation does that place on use of
this code?
Demonstration - demonstrate what you have learned.
Ultrasonic timer- time by polling
Now that you can reliably measure pulse widths, it should be easy to
adapt the code for the sensor.
- Disconnect the function generator, and connect the
echo pin of the sensor
to Arduino pin 2.
(You may want to keep the oscilloscope attached just to see that
you are getting pulses as expected.)
- Pick an unused Arduino pin to use for the
trigger pin of the sensor.
Are there any pins that seem like better choices for this? Why?
- Open the
PulseWidthPoll
sketch.
-
Look at the datasheet for the sensor to determine appropriate
parameters for the trigger pulse.
Modify the sketch to produce an appropriate pulse on the trigger pin.
- Place an object in front of the sensor so that you test your code.
- Compile and run the sketch, and open the serial monitor to
see the results.
Is the code working?
- If you haven't already done so, modify the sketch so that it
prints out distances rather than just times.
Is there an easy way to avoide floating point math?
-
Vary the distance to see the minimum and maximum distances that you
can consistently measure.
Does this depend much on the object size?
How consistent are the measurements if the object stays
stationary? Does it depend on the distance?
Demonstration - demonstrate what you have learned.
Ultrasonic timer- time using pulseIn()
- Open the
PulseWidthPulseIn
sketch.
-
If you keep the same pin for the trigger pulse, you should be able to
cut and paste the code from your previous sketch to adapt this one for
the sensor.
Modify the sketch as needed.
- Compile and run the sketch, and open the serial monitor to
see the results.
Is the code working?
-
Vary the distance to see the minimum and maximum distances that you
can consistently measure.
Are the maximum and minimum distances consistent with the
previous sketch? If
not, why might that be?
Are the distances measured consistent with the previous sketch?
If not, why might that be?
How consistent are the measurements if the object stays
stationary? Does it depend on the distance?
Demonstration - demonstrate what you have learned.
Ultrasonic timer- time by interrupt
- Open the
PulseWidthInt
sketch.
-
If you keep the same pin for the trigger pulse, you should be able to
cut and paste the code from your previous sketch to adapt this one for
the sensor.
Modify the sketch as needed.
- Compile and run the sketch, and open the serial monitor to
see the results.
Is the code working?
-
Vary the distance to see the minimum and maximum distances that you
can consistently measure.
Are the maximum and minimum distances consistent with the
previous sketches? If
not, why might that be?
Are the distances measured consistent with the previous
sketches?
If not, why might that be?
How consistent are the measurements if the object stays
stationary? Does it depend on the distance?
Demonstration - demonstrate what you have learned.
Ultrasonic timer- time by interrupt using low level
access
- Open the
PulseWidthIntLL
sketch.
-
If you keep the same pin for the trigger pulse, you should be able to
cut and paste the code from your previous sketch to adapt this one for
the sensor.
Modify the sketch as needed.
- Compile and run the sketch, and open the serial monitor to
see the results.
Is the code working?
-
Vary the distance to see the minimum and maximum distances that you
can consistently measure.
Are the maximum and minimum distances consistent with the
previous sketches? If
not, why might that be?
Are the distances measured consistent with the previous
sketches?
If not, why might that be?
How consistent are the measurements if the object stays
stationary? Does it depend on the distance?
Demonstration - demonstrate what you have learned.
Wilfrid Laurier University
© 2019 Wilfrid Laurier University