Testing the SRF08 ultrasonic rangefinder

// mbed test program using I2C and PWMout: WGM 6/2010
// Uses SRF08 Ultrasonic Range Finder

#include "mbed.h"

I2C sonar(p9, p10);                         // Define SDA, SCL pins
Serial pc(USBTX, USBRX);                    // Define Tx, Rx for PC
PwmOut motor(p21);                          // Define PWM pin
const int addr = 0xE0;                      // I2C device address for SRF08
char cmd[2];
char echo[2];

int main() {

// Set up SRF08 max range and receiver sensitivity over I2C bus
    cmd[0] = 0x02;                          // Range register
    cmd[1] = 0x1C;                          // Set max range about 100cm
    sonar.write(addr, cmd, 2);
    cmd[0] = 0x01;                          // Receiver gain register
    cmd[1] = 0x1B;                          // Set max receiver gain
    sonar.write(addr, cmd, 2);

// Set up PWM frequency
    motor.period(0.01);                     // Set PWM frequency = 100Hz
   
    while (1) {
   
// Get range data from SRF08
// Send Tx burst command over I2C bus
        cmd[0] = 0x00;                      // Command register
        cmd[1] = 0x51;                      // Ranging results in cm
        sonar.write(addr, cmd, 2);          // Send ranging burst

        wait(0.07);                         // Wait for return echo

// Read back range over I2C bus
        sonar.start();                                // Start condition
        sonar.write(addr);                         // Address write
        sonar.write(0x02);                         // First echo register
        sonar.start();                                // Repeated start
        sonar.write(addr+1);                      // Address read
        echo[0] = sonar.read(I2C::ACK);     // Read MSB, with ACK
        echo[1] = sonar.read(I2C::NoACK); // Read LSB, no ACK
        sonar.stop();                                 // Stop condition

// Generate PWM mark/space ratio from range data
        float range = (echo[0]<<8)+echo[1];
        range = range/100;                       // Turn range into PWM ratio       
        motor.write(range);                        // Update PWM pulse-width
       
        pc.printf("range = %0.2f\n", range);
       
        wait(0.1);
       
    }
}

Now that a bug has been fixed in the I2C driver with version 24 of mbed.h, I can reduce the size of the code to read the SRF08 range data by replacing:

        sonar.start();                                  // Start condition
        sonar.write(addr);                           // Address write
        sonar.write(0x02);                          // First echo register
        sonar.start();                                 // Repeated start
        sonar.write(addr+1);                       // Address read
        echo[0] = sonar.read(I2C::ACK);     // Read MSB, with ACK
        echo[1] = sonar.read(I2C::NoACK); // Read LSB, no ACK
        sonar.stop();                                 // Stop condition

with:

        cmd[0] = 0x02;                            // Address of first echo
        sonar.write(addr, cmd, 1, 1);         // Send address of first echo      
        sonar.read(addr, echo, 2);            // Read two-byte echo result

 


0 comments

You need to log in to post a comment