Kronos Robotics and Electronics
Site Map
 
Home Zeus Projects App Notes Downloads Dios Athena Forums
 

DAN149

SRF04 Interface

  By Michael Simpson

 

A couple of you have requested an interface to the some sort of sonar device.  I chose the SRF04 because its cheaper then the SRF08 and a bit faster.   The SRF04 will cost you between $30-$40.   

How it works

you supply a pulse from low to high and back low again on the trigger lead to start the SRF04.  This sends out a pulse.  The SRF04 will then pause for a few ms then deliver a pulse on the output line.  To read the range we measure the length of this pulse.  I wont go into the speed of sound or the formulas used as the Dios has a command built-in that take care of all that for you.  This command is called sonar.

sonor pingport,echoport,variable

The command will populate the variable given with the distance to the detected object in 1/64" units.  If you want just inches just divide by 64.  I will give you a couple program examples later.

Hookup

Before you hookup the unit you will have to attach wires/header or some other connector to the SRF04 as it comes with 5 holes that must be soldered.

Here I attached a 5 pin right angle header.

 

Schematic 1

I used a Dios Mini ultra but any Dios/DLC product will work.

This first program is just a real simple example of how to read the SRF04.  It will display the distance in inches in the debug window.

Program 1 (download it here)

'sonar example
func main()
   dim SRFcount

loop:
   sonar 0,1,SRFcount
   print "Distance= ",SRFcount /64," Inches"
   pause 20
goto loop

endfunc
 

This program is a bit more complicated.  I took one of my generic bot programs and added a collision avoidance system using the SRF04.  If you are telling the bot to move forward with the IR remote and it detects an object it will slam on the breaks and attempt to back up until its back out of range.

Program 2 (download it here)

'Kronos Crawler IR control program
'Use the CH+ and CH- for forward and reverse movement
'Use the Vol keys for the left and right movement
'Use the Key pad to set the speed 2-9
'Sonar device is connected to ports 0 and 1
'IR is connected to port 0
func main()
   print "reset"
   pause 300 ' Give CoProc time to resync
   dim cmd
   dim lcmd,range
   clear

   CPinit()
   CPMotorReset(0)

   CPMotor1speed(0,200)
   CPMotor2speed(0,200)
   pause 100

   CPMotorDamperoff(0) 'Turn off damper so we can slam off breaks.

loop:
   cmd = IRreadverify(0,1,10000)

   if cmd = 0 then
     CPMotorBotstop(0)
     goto loop
   endif

   'Test the Sonar here
   if lcmd = 17 or cmd = 17 then
     sonar 1,2,range
     range = range / 64
     if range <16 then
         CPMotorBotrev(0)
         pause 20 'Go in revers for short period
         CPMotorBotstop(0)
         lcmd = 0
      goto loop
      endif
  endif

'Forward
   if cmd = 17 then
      CPMotorBotfwd(0)
      lcmd = 17
      goto loop
    endif

'Reverse
    if cmd = 18 then
      CPMotorBotrev(0)
     goto loop
    endif

'Left
   if cmd = 20 then
      CPMotorBotleft(0)
     goto loop
   endif

'Right
   if cmd = 19 then
     CPMotorBotright(0)
     goto loop
   endif

   goto loop

endfunc
 

Parts

DiosPro 40 Pin Chip

Dios Workboard Deluxe

 

Easy RS232 Driver  

DiosPro 28 Pin Chip

Dios 32 Pin Carrier (Carrier #1)

 

9 Pin Cable

Breadboard Regulator

 

SRF04 Module:  Sorry you will have to do an internet search on this one.

 

 

 

Copyright © 2001 - 2007 Kronos Robotics