Library for interfacing the SRF08 ultrasonic range sensor. Most functions of the SRF08 are covered, including interrupt-based waiting for the ranging process to finish

Dependents:   DISCO-F746NG_LCDTS_demo Srf08Test

/media/uploads/brentdekker/ultrasonic-range-finder-srf08.jpg

SRF08

Warning

Don't forget to add the pullup resistors from 5v to SDA and SCL!

Information on the SRF08 can be found here: http://www.robot-electronics.co.uk/htm/srf08tech.shtml

Example program

#include "mbed.h"
#include "SRF08.h"

Serial PC(USBTX, USBRX);       //Debug port to PC
SRF08 rangeMod1(p28, p27, 0xE4); //SRF08 ranging module 1
SRF08 rangeMod2(p28, p27, 0xE2); //SRF08 ranging module 2

int main() {
    PC.printf("Start ranging test \n");
    rangeMod1.setAddress(0xE4); //Factory default is 0xE0
    while(1) {
        rangeMod1.startRanging();
        while (!rangeMod1.rangingFinished() ) wait(0.01);
        int range1 = rangeMod1.getRange();
        int light1 = rangeMod1.getLightIntensity();
        rangeMod2.startRanging();
        while (!rangeMod2.rangingFinished() ) wait(0.01);
        int range2 = rangeMod2.getRange();
        PC.printf(" Range_1: %i", range1);
        PC.printf(" Range_2: %i", range2);
        PC.printf(" Light_1: %i", light1);
        PC.printf("\n");
    }
}

Library

Import library

Public Member Functions

SRF08 (PinName SDA, PinName SCL, int i2cAddress)
Create a SRF08 object connected to the specified I2C pins and address.
void startRanging ()
Send the "Start ranging in cm" command via I2C.
bool rangingFinished ()
Checks if the module has finished ranging.
int getRange ()
Gets the measured range from the module.
int getLightIntensity ()
Gets the measured light intensity from the module.
void setRangeRegister (unsigned char rangeVal)
Sets the range register of the SRF08 for faster ranging.
void setMaxGainRegister (unsigned char gainVal)
Sets the max gain register of the SRF08 .
void setAddress (int i2cAddress)
Changes the I2C address of the SRF08 .

Files at this revision

API Documentation at this revision

Comitter:
brentdekker
Date:
Wed Jul 11 08:11:34 2012 +0000
Parent:
3:c43cd24ec7e7
Commit message:
Removed debug code

Changed in this revision

SRF08.cpp Show annotated file Show diff for this revision Revisions of this file
SRF08.h Show annotated file Show diff for this revision Revisions of this file
diff -r c43cd24ec7e7 -r a11bd4ea3c18 SRF08.cpp
--- a/SRF08.cpp	Wed Jul 11 07:56:49 2012 +0000
+++ b/SRF08.cpp	Wed Jul 11 08:11:34 2012 +0000
@@ -32,8 +32,7 @@
         i2cMod(SDA, SCL), 
         i2cAddress(i2cAddress), 
         rangeTimeout(), 
-        rangingBusy(false), 
-        debugPC(USBTX, USBRX) {
+        rangingBusy(false) {
 
 }
 
diff -r c43cd24ec7e7 -r a11bd4ea3c18 SRF08.h
--- a/SRF08.h	Wed Jul 11 07:56:49 2012 +0000
+++ b/SRF08.h	Wed Jul 11 08:11:34 2012 +0000
@@ -105,10 +105,6 @@
     bool rangingBusy;
     
     void setRangingFinished();
-    
-private:
-
-    Serial debugPC;
 };
 
 #endif
\ No newline at end of file