Osama's Lab

Home‎ > ‎Projets list‎ > ‎

Ping))) Ultrasonic


Using Parallax Ping ultrasonic sensor is very easy, the main advantage of this sensor is that only one pin is needed.

Communication protocol:

The PING))) sensor detects objects by emitting a short ultrasonic burst and then "listening" for the echo.
Under control of a host microcontroller (trigger pulse), the sensor emits a short 40 kHz (ultrasonic) burst.
This burst travels through the air, hits an object and then bounces back to the sensor.   The PING)))
sensor provides an output pulse to the host that will terminate when the echo is detected, hence the
width of this pulse corresponds to the distance to the target
.



As the velocity of sound waves in air is known (about 340 m/s), the width of the pulse represents the twice of the time it takes the signal to hit the nearest object that can be seen by the sensor (locates in its field of view), hence the distance equals:

Distance =
(Tin/2)*340    meters

where T
in is the echo return time

Note that the bold black pulse is sent by the microcontroller, and the gray pulse is the response of the sensor, both of them is transmitted on the same pin.

Circuit:

The circuit is very simple, ATmega32 microcontroller is used:

  •  Pin 15 is connected to the max232 to send serial data to the computer
  • Pin 20 is connected to the SIG pin of the ping))) ultrasonic sensor as shown bellow
                   

Program:

This is the algorithm followed:

Initialization:
  1. Set SIG pin as output
  2. Disable Timer1
  3. Activate rising edge ICP interrupt
  4. Disable Input Capture Interrupt (ICP)
Loop:
  1. Wait 500 m sec
  2. Make sure that ICP is disabled
  3. Make sure that SIG pin is set as output
  4. Send burst signal with 5 microsecond duration
  5. Set SIG pin as input
  6. Disable pull-up (enable Tri-state) for SIG pin
  7. Enable ICP
  8. ........... wait for echo signal rising edge ............
  9. Start Timer1
  10. Set falling edge of ICP for interrupt
  11. ..........wait for echo signal falling edge .............
  12. Save Timer1 value in a variable
  13. Disable and clear Timer1
  14. Set SIG pin as output
  15. Set rising edge of ICP for interrupt
  16. Calculate the distance from the stored value of Timer1
  17. Go to 1
The distance can be calculated as following:
Distance = TimerValue * 340 / (crystalFreqency * 2)

Operation:
Timer1 is used to make use of Input Capture (ICP) facility, only Timer1 is used, no external interrupts are needed, it is recommended to keep as many as resources available for future use.

What can I do if Timer1 is already used for another purpose ?

If Timer1 is used for another purpose, any other timer can be used along with an external interrupt, this method is illustrated in an attached fileTimer1 and INT0 are used together. ICP is not used, so Timer1 can be replaced by any other timer.
 
Project files:
Complete project with simulation
All project files, created using CodeVisionAVR,and simulation files using Proteus,this is the original project, Input Capture is used here.

Using an interrupt rather than ICP
If you want to test this project using an interrupt with any timer rather than ICP with Timer1,replace Ultrasonic.c and Ultrasonic.DSN in the original project with those two files.

Using simulation:
Ping))) sensor is not modeled in Proteus, so I added another ATmega32 microcontroller to model it.

Run the simulator and observe the virtual terminal window, whenever "GET" comes into screen, you can push the button connected to Ping))) microcontroller to send the echo signal. The duration of echo signal is set to simulate 2 meters distance.

Note: you can change this distance by modifying line 33, in (project's directory/tester/tester.c) according to formula commented in line 32.