Pages created and updated by Terry Sturtevant Date Posted: February 11, 2020

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

  1. 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?
  2. Connect the Arduino board. Attach the input from the frequency generator to Arduino pin 2.
  3. Create a new sketch, and download the PulseWidthPoll sketch.
  4. Compile and run the sketch, and open the serial monitor to see the results.
    Do the times make sense?
  5. 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()

  1. Create a new sketch, and download the PulseWidthPulseIn sketch.
  2. Compile and run the sketch, and open the serial monitor to see the results.
    Do the times make sense?
  3. 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

  1. Create a new sketch, and download the PulseWidthInt sketch.
  2. Compile and run the sketch, and open the serial monitor to see the results.
    Do the times make sense?
  3. 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

  1. Create a new sketch, and download the PulseWidthIntLL sketch.
  2. Compile and run the sketch, and open the serial monitor to see the results.
    Do the times make sense?
  3. 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.
  1. 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.)
  2. 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?
  3. Open the PulseWidthPoll sketch.
  4. 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.
  5. Place an object in front of the sensor so that you test your code.
  6. Compile and run the sketch, and open the serial monitor to see the results.
    Is the code working?
  7. 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?
  8. 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()

  1. Open the PulseWidthPulseIn sketch.
  2. 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.
  3. Compile and run the sketch, and open the serial monitor to see the results.
    Is the code working?
  4. 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

  1. Open the PulseWidthInt sketch.
  2. 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.
  3. Compile and run the sketch, and open the serial monitor to see the results.
    Is the code working?
  4. 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

  1. Open the PulseWidthIntLL sketch.
  2. 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.
  3. Compile and run the sketch, and open the serial monitor to see the results.
    Is the code working?
  4. 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.
Creative Commons License

Information on this site which is produced by Terry Sturtevant is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.

Resources

To view pdf documents, you can download Adobe Acrobat Reader .
get Acrobat Reader
If you need to update a browser, you might try Firefox which is Get Firefox!
Since this page uses cascading style sheets for its layout, it will look best with a browser which supports the specifications as fully as possible.

If you are looking for an office package, with a word processor, spreadsheet, etc., you might try LibreOffice which is Get LibreOffice!

Go to the main page for the Department of Physics and Computer Science.

Valid XHTML 1.1

Valid CSS!

WCAG
2.0
(Level AA)

Wilfrid Laurier University