| SRF04 to Handyboard Example Last Modified: 2006-10-30 | | |
| Acroname Robotics | PDF webpage version | ||
| ![]() Introduction In this example, a Handy Board microcontroller uses a Devantech SRF04 Ultrasonic Range Finder to measures distances to obstacles. The Handy Board tells the sonar module to emit a "ping" and measures the time it takes to receive an echo. It displays the echo return time (in clock ticks) using its built-in LCD display. It also sounds a tone proportional to each measurement. A high-pitched tone means an object is far away. A low-pitched tone means an object is very close. Circuit Schematic In the following schematic, the Handy Board supplies power to the SRF04 from its own regulated 5V power supply. According to the Devantech specifications, the SRF04 sonar module may require up to 50mA. The Handy Board voltage regulation can easily supply this much current. ![]() Source Code This code is written in Interactive C (IC) for the Handy Board. 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 Handy Board measures the echo delay with an internal timer. The timer stops running at the falling edge of the ECHO signal. The controller waits 10ms to give the module time to reset properly. An added feature is a tone that gives an audible indication of object distance. Wave your hand over the sensor to generate different tones. /*********************************************/
/* srf04-3.c */
/* HANDY BOARD EXAMPLE FOR */
/* DEVANTECH SRF04 ULTRASONIC RANGING MODULE */
/* */
/* This code is based heavily on the */
/* Polaroid Sonar Ranging code at: */
/* http://handyboard.com/software/sonar.html */
/* developed by Fred Martin */
/* fredm@media.mit.edu */
/*********************************************/
void main()
{
sonar_init();
while (1) {
int result;
result= sonar_sample();
if (result != -1)
printf("%d\n", result);
else
printf("@@@@@@@\n");
tone((float)(result/8),0.2);
}
}
void sonar_init() {
bit_set(0x1026, 0x80); /* set Digital Input #9 as output */
bit_clear(0x1021, 1); /* at TCTL2, */
bit_set(0x1021, 2); /* set TIC3 for falling edge */
}
int sonar_sample() {
int start_time;
int dist;
poke(0x1023, 1); /* clear tic3 flag */
start_time= peekword(0x100e); /* capture start time */
bit_set(0x1000, 0x80); /* send init pulse */
bit_clear(0x1000, 0x80);
/* wait until receive echo */
while (peek(0x1000) & 0x1) {
if ((peekword(0x100e) - start_time) < 0) {
/* if too much time has elapsed, abort */
return -1;
}
defer(); /* let others run while waiting */
}
msleep(10L); /* give unit time to reset */
dist= peekword(0x1014)-start_time;
return dist; /* tic3 has time of echo */
}
Additional discussion is beyond the scope of this example. Please visit the Handy Board Web Site for more information regarding the Handy Board hardware and Motorola 68HC11 processor. Revision History:
| |||
Related Links: | ||||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2008 Acroname, Inc., Boulder, Colorado. All rights reserved. |