SRF04 Source Code
Anyone please help, i need a source code for SRF04 that will continousely check distance not more than 2.5 meters, im just new to micro controllers and i need to apply this on my project using a bs2sx with boe. Any help will be appreciated.
Comments
Here is a link to a sample program for the Stamp BS-2 for the Devantech SRF04:
http://www.acroname.com/robotics/info/examples/srf04-1/srf04-1.html
Please also note the comments about the SRF04 power requirements on the above web page as well. This is a most important issue which is often overlooked.
You will have to modify the duration parameter of the PULSOUT command to reflect the increased speed of the BS-2SX over the BS-2. The PULSOUT command requires the specification of some number of timer units (TU) to set the duration of the outgoing pulse. For the BS-2 the TU's are of 2.0 uS duration and on the BS-2SX the TU's are of .8 uS duration. There may be other modifications necessary as well, as I didn't inspect the program thoroughly.
There is an additional application note (R93-SRF04p.PDF) about the SRF04, written by Parallax, which can be downloaded from here:
http://www.acroname.com/robotics/parts/R93-SRF04.html
Regards,
Bruce Bates
We didn't write the application on the Acroname site.
But Nhoyb can get our documentation here:
http://www.parallax.com/detail.asp?product_id=28015-SRF04
Ken Gracey
Parallax, Inc.
I beg to differ with you. If you download the file that I indicated you will see that the information was produced by Parallax. Perhaps it's being used without your permission, but it is a Parallax document. I actually tried to find such a document on the Parallax web site first but wasn't able to find it, thus the reference to the Acroname copy (R93-SRF04p.PDF).
If you'd like me to send you a copy, I'd be happy to do so.
Regards,
Bruce Bates
' =========================================================================
'
' File....... SRF04_Demo.BS2
' Purpose.... Devantech SRF04 Ultrasonic Range Finder
' Author..... Parallax, Inc. (Copyright 2003 - All Rights Reserved)
' E-mail..... support@parallax.com
' Started.... 06 MAR 2002
' Updated.... 01 OCT 2003
'
' {$STAMP BS2sx}
' {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]
'
' This program uses the Devantech SRF04 to measure the distance between the
' unit and a target. Display is raw value, centimeters, and inches.
'
[noparse][[/noparse] Revision History ]
'
' 01 OCT 2003 : Updated for PBASIC 2.5 and for any BASIC Stamp module
'
[noparse][[/noparse] I/O Definitions ]
'
Trigger PIN 0
Echo PIN 1
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
#CASE BS2, BS2E
Trig10 CON 5 ' trigger pulse = 10 uS
ToCm CON 30 ' conversion factor to cm
#CASE BS2SX, BS2P
Trig10 CON 13
ToCm CON 78
#CASE BS2PE
Trig10 CON 5
ToCm CON 31
#ENDSELECT
'
[noparse][[/noparse] Variables ]
samples VAR Nib ' loop counter
pWidth VAR Word ' pulse width from sensor
rawDist VAR Word ' filtered measurment
cm VAR Word ' centimeters
inches VAR Word
'
[noparse][[/noparse] EEPROM Data ]
'
[noparse][[/noparse] Initialization ]
Setup:
LOW Trigger
DEBUG CLS,
"Devantech SRF04 Demo", CR,
"
", CR,
"Raw........... ", CR,
"Centimeters... ", CR,
"Inches........ "
'
[noparse][[/noparse] Program Code ]
Main:
DO
GOSUB Get_Sonar ' take sonar reading
DEBUG CRSRXY, 15, 2, DEC rawDist, CLREOL
cm = rawDist / ToCm ' convert to centimeters
DEBUG CRSRXY, 15, 3, DEC cm, CLREOL
inches = cm */ $03EF ' x 3.937 (to 0.1 inches)
DEBUG CRSRXY, 15, 4,
DEC inches / 10, ".", DEC1 inches,
CLREOL
PAUSE 250 ' delay between readings
LOOP
END
'
[noparse][[/noparse] Subroutines ]
Get_Sonar:
rawDist = 0 ' clear measurement
FOR samples = 1 TO 5 ' take five samples
PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
#SELECT $STamp
#CASE BS2, BS2E
RCTIME Echo, 1, pWidth ' measure pulse
#CASE #ELSE
PULSIN Echo, 1, pWidth ' measure pulse
#ENDSELECT
rawDist = rawDist + (pWidth / 5) ' simple digital filter
PAUSE 10 ' minimum period between
NEXT
RETURN
It works fine and its great, Im confuse on adding a new command line that will make my piezzo speaker and LED, that will light up and buzz when the taken measurement is; lets say under 100 inches. This project is suppose to be an alarm. And I'd like to trigger a pin for a led and piezzo speaker when a certain value is less than indicated. The "What is Microcontroller" doesnt teach me well in programming. Specially the CASE and IF THEN statement.
Perhaps you're rushed, and will go back.· Until then, this is what you should have come up with:
Check_Alarm:
· IF (inches < 1000) THEN·········' distance less that 100.0 inches?····
··· HIGH AlarmLed·················' yes -- alarm on·
··· FREQOUT·Piezo, 1250, 180····· '········sound beeper
· ELSE
··· LOW AlarmLed················· ' no -- clear alarm
· ENDIF
I will leave it to you to create the pin definitions for the·piezo and alarm LED, and then·insert the code into the proper place in the program.· Since this is the Stamps In Class forum I'm assuming that this is a class project ... so I won't give you all the answers (maybe so other soul will, but that would rob you of the learning experience....).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
Post Edited (Jon Williams) : 3/13/2005 4:01:07 PM GMT
Thanks, It took me 3 weeks to review the What's a Microcontroller and figured where to insert the code you've given. It works great. But I still have some questions, like; how can BS2 measure distance from SRF04? I still dont understand it, where does the RAW value comes from? And from how to convert RAW from Inches. This is very very confusing. The manual sez that the ultra sonic sound travels at 1.12feet per ms, but I dont see that value on the source code.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Soul Still Bruns!!
The BS2 triggers the SRF-04 to send a sonic burst. The SRF-04 will create an echo pulse that has a width that corresponds to the distance to the target. The BS2 actually measures the width of the echo pulse and derives the distance from it. Remember that this distance measurement, initially, is in units of time (microseconds). Since sound moves at about one inch in 74 micoseconds, if we divide the time value by 74 we can get inches.
Acroname sells the SRF-04 (we don't carry them anymore) and has lots of info on the SRF-04.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
I have inserted this code on my project.
IF (inches < 300) THEN
HIGH 14
FREQOUT 9, 1250, 1800
ELSE
LOW 14·····································
LOW 12
LOW 11
ENDIF
IF (inches < 300) THEN
LOW 14
ENDIF
IF (inches < 200) THEN
LOW 12
HIGH 11
ENDIF
IF (inches < 100) THEN
HIGH 12
LOW 11
FREQOUT 3, 1250, 1800
ENDIF
NEXT
RETURN
It works but there is a delay and I think its also conflicting with the Pin 14 which is a led, i cant make it shut off, when the other endif statement meet its condition. I'v also tried to use the goto and gosub command but could not make it work. Also I used 2 piezo speaker, which not seemed logical, I just want the piezo speaker to beep faster when object is getting nearer with a reasonable delay. Please Jon Williams, Im just new to programming. Im trying my best here to learn, but I still need help.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Soul Still Bruns!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA