Interfacing Ultrasonic Rangefinder with AVR MCUs – AVR Tutorial Using Atmega

Interfacing Ultrasonic Rangefinder

Obstacle detecting sensors are one of the most basic type of sensors that electronic hobbyists use. There are several methods to make cheap obstacle sensors. These simple sensors are made using a IR Rx/Tx pair or Normal LED and LDR pair(this design is most basic and is heavily affected by environment lighting conditions). These sensor may be useful for simple requirement but they have following drawbacks :-

Interfacing Ultrasonic Rangefinder

  • Can’t say anything about the real distance of obstacle.
  • Give different result for different coloured obstacles.
  • Need calibration (like setting up a variable resistor).

To solve these problems we have IR Range Finder Module (like one made by Sharp) but they have small range.

  • Sharp GP2D12 Distance Measurement Sensor has a maximum range of 80cm
  • Sharp GP2D120 Distance Measurement Sensor has a maximum range of 30cm only.

To solve all these problem we can use an Ultrasonic Range Finder Module. An Ultrasonic Range Finder Module uses ultrasonic waves (inaudible to humans) to measure distance. These module consist of an Ultrasonic Transmitter (Tx) that emits the ultrasonic wave, the waves after striking any obstacle bounces back and reach the Ultrasonic Receiver (Rx). By measuring the time it take for the whole process to complete and using simple arithmetic we can measure the distance to the obstacle. The Ultrasonic Range Finder Modules has a wide operating range of 1cm to 400cm with an accuracy of 1cm. These specifications makes it ideal for distance measurement application. These can be used for :-

    • Contact less measurement of liquid level in tanks (even 4m deep tank!)
    • Radars for robot.
    • Obstacle sensing in Robotics.
    • Speed check in roads.
      • Handheld units that can be pointed on vehicles to measure their speed.
      • Fixed unit installed in check booths that can click pictures of over speeding vehicles (Remember NFS Most Wanted?)

The reason for using ultrasonic wave are:-

  • The speed of Ultra Sonic waves is 343m/s (Speed of Sound) which is not too fast for MCUs to measure accurately. Compare this with speed of electromagnetic waves (like light or radio waves) which is 30,00,00,000 m/s! So it takes only 20ns (nano second) to go and bounce back from an obstacle which is 3m away! An AVR running at 16MIPS(maximum for most AVRs) takes 62ns to execute a single instruction.
  • Ultrasonic waves travels more narrow, like a beam than normal sound wave. This property helps the sensor detect the obstacles that are exactly in line with it only. The sensors can be rotated with steppers or servo motors to get a “image” of obstacle in the surrounding area (like a radar).
  • Finally the wave do not disturb any humans nearby!

Ultrasonic Range Finder Interface.

These modules are designed to be used for microcontroller based applications hence optimized for it. The interface is a single pin called SIG (signal). The MCU is connected to the Ultrasonic Range Finder Module by a single i/o line. The steps required to read distance are :-

  1. Microcontroller make the i/o line output. (by using the DDRx Register in AVR or TRISx Register in PIC)
  2. The i/o line is made low (this may be the default state of i/o pin)
  3. Wait for 10uS
  4. Make the i/o line high.
  5. Wait for 15uS
  6. Make the i/o line low
  7. Wait for 20uS
  8. Now make it input (by using the DDRx Register in AVR or TRISx Register in PIC)
  9. Module will keep it low. Wait till it is low, as soon as it becomes high start the timer.
  10. After that wait till it is high, as soon as it becomes low copy the timer value and stop the timer.
  11. Finally we have the time required for the wave to go hit the obstacle and come back to the module.
Interfacing Ultrasonic Rangefinder Schematic

If the pulse width is in microseconds, the distance can be calculated by the following formula :-

  • Distance in cm = Pulse width/58
  • Distance in inches = Pulse width/148

Read More: Interfacing Ultrasonic Rangefinder with AVR MCUs – AVR Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *