SRF04 to BS II Example
Last Modified: 2006-10-25
find:

basket

Acroname Robotics PDF webpage version SRF04 to BS II Example PDF

Related
Products

Product image for Devantech SRF04 Ranger
Devantech SRF04 Ranger

Contents

Hardware setup for SRF04 to Basic Stamp II example.

Introduction

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. 

Circuit Schematic

Wiring diagram between the Stamp II and the SRF04.

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. 

Source Code

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. 

Note

The execution time of the pulsin instruction will be slightly different for different pins.  If you change the pin assignments, the pulsin command in the sr_sonar subroutine above may miss the rising edge of the ECHO input and return some readings of 0.  Using rctime instead of pulsin may fix this problem.  Another trick is to introduce a small delay with a dummy command before measuring the echo time with the rctime command.  This is illustrated in the alternate subroutine sr_sonar_2 .  You may need to experiment to determine the best code for your application. 

Revision History

  • 1/24/2001 - First posted. 
  • 2/8/2001 - Added note about using rctime instead of pulsin when using different IO pins.  Thanks go to Jim Frye for finding this problem. 
  • 3/8/2001 - Added new info regarding rctime and pulsin timing issues. 
 

Related Links:

The Devantech Sonic Range Finders Comparison and Examples

Related Examples:

Example code and schematic for Devantech SRF04 sensor interface to BrainStem GP

voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved.