Interfacing with a Devantech SRF04 Sonar

copyright, Jesus Hernadez and Peter H Anderson, Baltimore, MD, May, '04


Introduction.

This note deals with interfacing the PICAXE-18X with the Devantech SRF04 Sonar Ranging Unit. The SRF04 is a small unit, well built, easy to mount and interface with and I have found to be quite accurate over the range of slightly less than one inch to nominally 36 inches. It is marketed by Rev-Ed and a number of vendors in the US for a bit over $30.00.

Operation.

The Trigger input to the SRF04 is momentarily brought high for nominally 10 us using the PulsOut command. The SRF04 then immediately pings and lifts the Echo lead to a logic one and when the echo is received, it is returned to a logic zero. The width of the echo pulse may then be measured by the PICAXE and the range calculated.

However, when using the PulsIn command, the transition from a zero to a one must be seen by the PICAXE in order to measure the width of the positive echo pulse. The original design of the SRF04 provided virtually no delay between the receipt of the trigger pulse and the lifting of the echo lead to a logic one and thus was not compatible with the PICAXE as the positive transition occurred before the PulsIn command was executed.

This problem was corrected by Devantech by modifying the SRF04 firmware such that a sufficient delay is inserted between the receipt of the trigger and the launching of the sound pulse (and lifting of the echo lead) when the previously unused "Do not connect lead" is connected to ground. This firmware change was made about April, '03 and I assume most SRF04 distributors in the US have the new firmware. But, it is worth asking prior to purchasing.

Pulse Width to Range in Inches

Sound travels at nominally 1100 feet per sec or nominally 1.1 feet per ms. However, as the sound bounces off the distant object and returns to the SONAR receiver the distance to the target this is 0.55 feet / ms or 6.6 inches per ms or 0.066 inches per 10 us. Thus, the range in inches is the pulse width Tm in 10us * 0.066.

Another way of looking at this is the range in inches times ten is the echo pulse width in 10us times a factor of 0.54.

Thus, the pulse width may be measured;

	PulsIn EchoPin, 1, Tm
and the range in inches times 10 may then be calculated as;
    RInches_10 = Tm * 66 / 100
or
    RInches_10 = Tm * 33 / 50


' SRF04.Bas (PICAXE-18X)
'
' Illustrates an interface with the Devantech SRF04 Sonar
'
' Launches pulse using PulsOut Command and measures the width of the Echo
' pulse.
'
' PICAXE-18X                      SRF04
'
' Trig (Out7) --------------------- Trigger
' Echo (In1) <--------------------- Echo
'
'                       GRD ------- Do Not Connect Terminal
'                       GRD ------- GRD
'                       +5 VDC ---- 5VDC
'
' copyright, Jesus Hernandez, Peter Anderson, Baltimore, MD, May, '04


Symbol Tm = W0
Symbol RInches_10 = W1

Symbol TrigPin = 7    ' PICAXE Output
Symbol EchoPin = 1    ' PICAXE input


   Pause 1000			' allow system to settle when booting

TOP:
   PulsOut TrigPin, 1	' sonar output, 10 us
   PulsIn EchoPin, 1, Tm
   If Tm = 0 Then OverRange
   If Tm > 1000 Then OverRange
   RInches_10 = Tm *  33 / 50   ' assumes 10 us per tick on pulsin

   SerTxD (#Tm, "  ", #RInches_10, 13, 10)
   Pause 1000
   Goto TOP

OverRange:
   SerTxD ("Target Not Found", 13, 10)
   Pause 1000
   Goto TOP