Author Topic: SRF08 - SRF02 and some AXON I2C issues  (Read 2675 times)

0 Members and 1 Guest are viewing this topic.

Offline teoxanTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 2
SRF08 - SRF02 and some AXON I2C issues
« on: May 14, 2010, 12:08:48 PM »

I'm trying to write some code to use the AXON2 and the SRF02-SRF08 ultrasonic rangers ( they both have the same i2C address and commands).

I have writen some code for the Wiring microcontroller ( the father of Arduino - they use the same "language") and it works great.

I used the same algorithms and steps in AXON2 using the Webbotlib, but I have a couple of issues:

1. The command to start a reading is not actually working, although it seems that code-wise it's accepted. I don't know what seems to be the problem, but in some point
    I need to stop the I2C transmission and start it again so I can read the registers that hold the actuall reading.
    I'm not very familiar with the inner works of Webbotlib since i'm new to AXON, but I think there must be a "start"-"stop" commands for I2C interface.
2. The reading results come in two bytes ( high-low) , I overpassed this by using  unsigned char types, since there's no "byte" type, but there's nothing to read there
    since the I2C reading command is not working.

A couple of months ago, I interfaced the TPA81 and submitted the code here, and everything works fine with this sensor.

But the SRF02 gives me a real headache.

Below is the code.

Any help will be appreciated!!

Code: [Select]
// SRF02 - SRF08 Test

#include "sys/axon2.h"
#include "rprintf.h" //use for UART
#include "i2c_master.h" //use for I2C
#include "myhardware.h" // user includes

uint8_t address=0xE0; // I2C address for SRF02 - same as the SRF08
uint8_t command[1]={0x00}; // command to start ranging
unsigned char rangebyte_high[1]={0x02}; //ranging data high byte
unsigned char rangebyte_low[1]={0x03}; // ranging data low byte
uint8_t unitbyte[1]={0x51}; // ranging unit in cm -use 0x50 for inches -and 0x52 for ms

unsigned int range;
unsigned char result[2]={0,0};

void appInitHardware(void)


TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
rprintf("\nAxon Initiated.\n\n");

return 0;

TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
i2cMasterSend(address,1,command); //command to initialize module
i2cMasterSend(address,1,unitbyte);// start ranging in cm
delay_ms(70);                    // wait to complete ranging

if(i2cMasterReceive(address,1,unitbyte)) // if it's ok
rprintf("Command for cm 0x51 sent\n");
i2cMasterSend(address,1,rangebyte_high);   // request high byte first
     i2cMasterSend(address,1,rangebyte_low);   // request  low byte
rprintf("range: ");
range=result[0]<<8;  // shift high byte left to make room for low byte
range+=range+result[1]; // and add low byte
         rprintf("%d\n", range);
     rprintf("No responce\n");


Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
Re: SRF08 - SRF02 and some AXON I2C issues
« Reply #1 on: May 15, 2010, 09:41:10 AM »
Think it should be more like this:-

Code: [Select]
// Define device I2C address
#define ADDR 0xe0

uint8_t range[] = { 0, 0x51 }; // Set command register to 0x51 to start ranging in cm
uint8_t response[4];               // Read 4 register values back

-- in appControl --

Code: [Select]
i2cMasterSend(ADDR, sizeof(range), range); // start ranging

delay_ms(70); // Wait till its done

if( i2cMasterTransfer( ADDR, 1, range, sizeof(response), response) ){
  // response[0] = software version or 255 if still ranging
  // response[1] = the light level or 0x80 on SRF02
  // response[2] = high byte of range
  // response[3] = low byte of range
  uint16_t value = response[2];
  value <<= 8;  // Move to high byte
  value |= response[3]; // add the low byte
  // Distance is now in the 'value' variable

Once you got it working then I'd recommend removing the 'delay' and instead test if response[0] == 255 which indicates the ranging is still happening. Once you get a valid value back then store it in a global variable and start a new ranging. Therefore the device will constantly be ranging and you will have stored the last 'received' value into the global variable.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline teoxanTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 2
Re: SRF08 - SRF02 and some AXON I2C issues
« Reply #2 on: May 17, 2010, 09:07:52 AM »

I tested the coade and it's working ok! :)




Get Your Ad Here