Pages: [1]   Go Down
Author Topic: Requesting data from SRF02 Range finder via I2C  (Read 1538 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to request data from a SRF02 ultrasonic range finder. The sensor data is here http://www.robot-electronics.co.uk/htm/srf02techI2C.htm but the examples listed here http://www.robot-electronics.co.uk/htm/arduino_examples.htm#SRF02,%20SRF08,%20SRF10,%20SRF235 are outdated.

I've tried to alter it to give the following, but I'm getting the error "34: error: call of overloaded 'write(int)' is ambiguous". I can't work out where abouts the error is coming from.

Code:
#include <Wire.h>

#define srfAddress (0x70)    //USV device address
#define cmdByte (0x00)       //Command byte
#define rangeByte (0x02)


//SETUP
//-----------------------------------------------------------------------------

byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  delay(1000);
}


//MAIN PROGRAM
//----------------------------------------------------------------------
void loop()
{
int rangeData = getRange();
Serial.println(rangeData, DEC); 
}

int getRange(){                                   // This function gets a ranging from the SRF08
 
  int range = 0;
 
  Wire.beginTransmission(srfAddress);             // Start communticating with SRF08
  Wire.write(cmdByte);                             // Send Command Byte
  Wire.write(0x51);                                // Send 0x51 to start a ranging
  Wire.endTransmission();
 
  delay(100);                                     // Wait for ranging to be complete
 
  Wire.beginTransmission(srfAddress);             // start communicating with SRFmodule
  Wire.write(rangeByte);                           // Call the register for start of ranging data
  Wire.endTransmission();
 
  Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
  while(Wire.available() < 2);                    // Wait for data to arrive
  highByte = Wire.read();                      // Get high byte
  lowByte = Wire.read();                       // Get low byte

  range = (highByte << 8) + lowByte;              // Put them together
 
  return(range);                                  // Returns Range
}
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19342
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Change the line in question to:

Code:
  Wire.write((byte) cmdByte);                             // Send Command Byte

Or better still, change:

Code:
#define cmdByte (0x00)       //Command byte

to:

Code:
const byte cmdByte = 0x00;       //Command byte
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Nick smiley I've updated the program with your suggestion and it compiles now. For some reason it's not sending data; I've added in some text at different stages of the loop like so:

Code:
#include <Wire.h>

#define srfAddress (0x70)    //USV device address
const byte cmdByte = 0x00;     //Command byte
#define rangeByte (0x02)


//SETUP
//-----------------------------------------------------------------------------

byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  delay(1000);
}


//MAIN PROGRAM
//----------------------------------------------------------------------
void loop()
{
Serial.println("Program starting");
int rangeData = getRange();
}

int getRange(){                                   // This function gets a ranging from the SRF08
  Serial.println("starting getRange");
  int range = 0;
 
  Wire.beginTransmission(srfAddress);             // Start communticating with SRF08
  Wire.write(cmdByte);                             // Send Command Byte
  Wire.write(0x51);                                // Send 0x51 to start a ranging
  Wire.endTransmission();
  Serial.println("requested range sample");
  delay(100);                                     // Wait for ranging to be complete
 
  Wire.beginTransmission(srfAddress);             // start communicating with SRFmodule
  Wire.write(rangeByte);                           // Call the register for start of ranging data
  Wire.endTransmission();
  Serial.println("requesting range data");
  Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
  while(Wire.available() < 2);                    // Wait for data to arrive
  highByte = Wire.read();                      // Get high byte
  lowByte = Wire.read();                       // Get low byte
  Serial.println("finishing range data transmission");
  range = (highByte << 8) + lowByte;              // Put them together
  Serial.println(range);
  return(range);                                  // Returns Range
}

The serial monitor shows up to:
Code:
Serial.println("requesting range data");


Does that mean that I'm correct in saying that when trying to exectute this:

Code:
  Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
  while(Wire.available() < 2);                    // Wait for data to arrive
  highByte = Wire.read();                      // Get high byte
  lowByte = Wire.read();       

the program must get stuck because Wire never becomes available? i.e something wrong with the wiring or addressing of my sensor?
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19342
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't need the second line here:

Code:
  Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
  while(Wire.available() < 2);                    // Wait for data to arrive

requestFrom "blocks" until the requested number of bytes arrives, or it times out. So the second line achieves nothing except to potentially put it into a loop. In fact requestFrom returns the number of bytes. So a better plan would be:

Code:
  if (Wire.requestFrom(srfAddress, 2) != 2)
    {
      // some sort of error
    }
  else
    {
    highByte = Wire.read();                      // Get high byte
    lowByte = Wire.read();       
    }

Try the "I2C bus scanner" on this page. That will confirm whether or not the device is responding and if so, what address it has:

http://www.gammon.com.au/i2c
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Nick, I now have a fully functional sensor  smiley-cool The article on your website about the pullup resistor values also answered alot of questions that were in the back of my mind about what effect different values had on the signal smiley
Logged

Pages: [1]   Go Up
Jump to: