Devantech SRF04 to BrainStem Example
Last Modified: 2009-01-13
find:

basket

Acroname Robotics PDF webpage version Devantech SRF04 to BrainStem Example PDF

Related
Products

Product image for BrainStem GP 2.0
BrainStem GP 2.0
Product image for 2600mAh AA NiMH Battery
2600mAh AA NiMH Battery
Product image for Devantech SRF04 Ranger
Devantech SRF04 Ranger
Product image for 3 Conductor Cable w/ Connector
3 Conductor Cable w/ Connector
Product image for 4X AA Battery Pack w/Connector
4X AA Battery Pack w/Connector

Contents

Photo of interface between BrainStem GP 2.0 and Devantech SRF04 ranging module

Introduction

In this example, a BrainStem GP microcontroller uses a SRF04 Ultrasonic Range Finder to measures distances to obstacles.  The BrainStem GP tells the sonar module to emit a "ping" and measure the time it takes to receive an echo.  It returns the distance as a raw time in increments of the timer's resolution, which is 1.6 uSec for the BrainStem GP 2.0. 

Circuit Schematic

In the above schematic, two digital I/O pins of the BrainStem GP 2.0 are configured to handle the SRF04 ranging module's ECHO and INIT.  The ECHO line must be wired into a digital pin that is capable of logic event timing.  Please review the Digital Input/Output hardware documentation for the BrainStem board you are using.  The circuit is powered by 4 NiMH AA batteries. 

Schematic Diagram of connections between BrainStem GP 1.0 and Devantech SRF04

Source Code

This code uses the TEA language, which is very similar to ANSI C.  It is compiled using steep command.  Once compiled, the program can be loaded onto a BrainStem using the Console application. 

This program utilizes the built-in library for handling Devantech SRF04 Rangers and specifies which BrainStem digital I/O lines are being used before including the library for the SRF04 drivers as pre-processor macros.  The program then sets up the digital pins using the aSRF04_Setup routine before taking multiple readings using the aSRF04_ReadInt command. 

Once the program running, it will return distance measurements (in usec) to the Console every second.  Explore the sensor range (by aiming it down a hallway, for instance) and moving your hand toward the SRF04 to find the minimum range. 

Note

This example uses the Acroname SRF04 library functions and procedure that is optimized for when a single SRF04 ranger is used.  This approach is important when code size matters.  When code size is not a concern, generalized library functions are available in the Acroname SRF05 library.  Please see the related SRF05 Mode 1 application example and the Reference documentation to learn how to alternatively use this function. 

/* srf04_example.tea */ /* included for the display routines */ #include <aCore.tea> #include <aPrint.tea> /* Define which pins control the SRF04 */ /* The ECHO pin must be capable of pulse width timing measurement */ #define aSRF04_INIT 1 #define aSRF04_ECHO 2 /* now include the SRF04 driving routines */ #include <aSRF04.tea> void main() { int num = 0; int reading = 0; /* Configure the digital pins to control the SRF04 */ aSRF04_Setup(); while (1) { /* Take a reading */ reading = aSRF04_ReadInt(); /* Output the result to the Console */ aPrint_String("Reading "); aPrint_IntDec(num++); aPrint_String(" = "); aPrint_IntDec(reading); aPrint_Char('\n'); /* Delay for 1 second */ aCore_Sleep(10000); } }

Revision History:

  • 2001-07-13: Example Created
  • 2009-01-14: Updated example with a BrainStem GP 2.0. Added wiring diagram and test rig pictures. Included notes about using multiple rangers and SRF05 library routines.
 

Related Links:

Brainstem Software: Console Overview

The Devantech Sonic Range Finders Comparison and Examples

Related Examples:

Devantech SRF05 Interface to BrainStem GP Example

Devantech SRF04 Sonar Interface to a Handyboard Example

Example code and schematic for Devantech SRF04 sensor interface to an OOPic

Devantech SRF04 Sonar Interface to BASIC Stamp II Example

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