| SRF04 to BS II Example |
Last Modified: 2006-10-25
In this example, a Parallax BASIC Stamp II microcontroller uses a Devantech SRF04 Ultrasonic Range Finder to measures distances to obstacles. The BASIC Stamp II tells the sonar module to emit a "ping" and measures the time it takes to receive an echo. It converts this time to a distance in inches and displays the value using the debug feature in the Basic Stamp II development software.
In the above schematic, the BASIC Stamp II and SRF04 are powered by an external regulated 5V power supply. According to the Devantech specs, the SRF04 sonar module may require up to 50ma. The voltage regulator on a BASIC Stamp II is rated at 50ma. The Stamp itself uses 8ma so it is necessary to have a power supply rated significantly higher than 50ma to power the two devices.
This code is written in PBASIC.
' ' Devantech SRF04 Example ' wDist var word INIT con 0 ECHO con 1 ' CONVERSION FACTORS: ' ' The PULSIN command returns the round-trip echo time in 2us units ' which is equivalent to the one-way trip time in 1us units. ' ' Filename: srf04-1.bs2 ' distance = (echo time) / (conversion factor) ' ' use 74 for inches (73.746us per 1 in) ' use 29 for centimeters (29.033us per 1 cm) ' convfac con 74 ' use inches '--------------------------------------------- main gosub sr_sonar debug dec wDist, cr pause 200 goto main sr_sonar: pulsout INIT,5 ' 10us init pulse pulsin ECHO,1,wDist ' measure echo time wDist=wDist/convfac ' convert to inches pause 10 return sr_sonar_2: pulsout INIT,5 ' 10us init pulse output INIT ' dummy command (delay) rctime ECHO,1,wDist ' measure echo time wDist=wDist/convfac ' convert to inches pause 10 return
The controller must raise and lower the INIT pin on the sonar module to begin a reading. After the INIT input pin goes low, the sonar module emits a ping. After the ping, the ECHO output goes high. It goes low again when the module receives an echo.
The PBASIC pulsout command sends a high pulse to the INIT pin on the sonar module. The PBASIC pulsin command measures the time it takes for the echo to return. After recording the reading, the Stamp waits 10ms to give the module time to reset properly.
| voice: 720-564-0373, email: email@example.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy |
© Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved.