Re Addressing Devantech SRF02 Sensor

Hi,
I am new to Arduino, and I need to know how to re address the devantech srf02 sensor. I have 2 connected up and both are on the same address.

This is my code so far

#include <Wire.h>
int led = 13;
int E1 = 4;    //Motor Controller Pin Declarations
int M1 = 5;
int E2 = 6;                         
int M2 = 7;    //Motor Controller Pin Declarations END                       
 

void setup()
{
  Wire.begin();             // join i2c bus (address optional for master)
  Serial.begin(9600);      // start serial communication at 9600bps
  pinMode(led, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
}

int reading = 0;
void loop()
{
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(112); // transmit to device #112 (0x70)
                               // the address specified in the datasheet is 224 (0xE0)
                               // but i2c adressing uses the high 7 bits so it's 112
  Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)  
  Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50) 
                               // use 0x51 for centimeters
                               // use 0x52 for ping microseconds
  Wire.endTransmission();      // stop transmitting

  // step 2: wait for readings to happen
  delay(90);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
  {
    reading = Wire.read();  // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.read(); // receive low byte as lower 8 bits
    Serial.println(reading);   // print the reading
  }
if (reading < 100)
{
 digitalWrite(M1,HIGH);
 digitalWrite(M2,HIGH);
 analogWrite(E1, 200);
 analogWrite(E2, 200);
}
else
{
digitalWrite (led,HIGH);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
}
}

I also took a look at this website: http://www.robot-electronics.co.uk/htm/srf02techSer.htm, but I still dont understand how to re address it.

If someone could help me re address 1 of the 2 sensors and then show how to call the value from that second sensor that would be much appreciated.

Thanks,
Rohan

CHeck - http://forum.arduino.cc/index.php?topic=197360.0 -

if you connect one sensor at a time you should be able to detect its address quite easily

Hi, I already know the address of the 2 sensors, when it turns on a series of flashes from its LED tells me the address, I need to onow how to re address one of those sensors. For example, sensor one has address 1 and sensor 2 has address 1, I want to change sensor 2 to address 2.

Thanks, Rohan

Changing the SRF02 Address To change the address of the SRF02 you must have only one sonar connected. Write the 3 sequence commands in the correct order followed by the address. Example; to change the address of a sonar currently at 0 (the default shipped address) to 5, write the following to address 0; (0xA0, 0xAA, 0xA5, 0x05 ). These commands must be sent in the correct sequence to change the I2C address, additionally, No other command may be issued in the middle of the sequence. The sequence must be sent as four separate commands to the current address of the sonar. i.e. 0x00, 0xA0 then 0x00, 0xAA, then 0x00, 0xA5 and finally 0x00, 0x05.

in code that should be something like

#include "Wire.h"
void setup()
{
  SRF02Address(1, 2);
}

void loop()
{
}

void set SRF02Address(byte oldAddress, byte newAddress)
{
  Wire.beginTransmission(oldAddress); 
  Wire.write(byte(0x00));       
  Wire.write(byte(0xA0)); 
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress); 
  Wire.write(byte(0x00));       
  Wire.write(byte(0xAA)); 
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress); 
  Wire.write(byte(0x00));       
  Wire.write(byte(0xA5)); 
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress); 
  Wire.write(byte(0x00));       
  Wire.write(newAddress); 
  Wire.endTransmission();
}

note that Wire.endTransmission(); does the actual transmission and that it returns a value which indicates success or not.

hmm,
strange, I applied the code, and the IDE was giving me this error:
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:258)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

This is the code:

#include <Wire.h>
int led = 13;
int E1 = 4;    //Motor Controller Pin Declarations
int M1 = 5;
int E2 = 6;                         
int M2 = 7;    //Motor Controller Pin Declarations END                       
 

void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);      // start serial communication at 9600bps
  pinMode(led, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
}

int reading = 0;
void loop()
{
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(112); // transmit to device #112 (0x70)
                               // the address specified in the datasheet is 224 (0xE0)
                               // but i2c adressing uses the high 7 bits so it's 112
  Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)  
  Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50) 
                               // use 0x51 for centimeters
                               // use 0x52 for ping microseconds
  Wire.endTransmission();      // stop transmitting

  // step 2: wait for readings to happen
  delay(90);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
  {
    reading = Wire.read();  // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.read(); // receive low byte as lower 8 bits
    Serial.println(reading);   // print the reading
  }
if (reading < 100)
{
 digitalWrite(M1,HIGH);
 digitalWrite(M2,HIGH);
 analogWrite(E1, 200);
 analogWrite(E2, 200);
}
else
{
digitalWrite (led,HIGH);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
}
}
void changeAddress(byte oldAddress, byte newAddress)
{
  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xA0));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xAA));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xA5));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(newAddress);
  Wire.endTransmission();
}

Now when I remove the code I still get this error, this is stopping my sensor from functioning…

Thanks,
Rohan

The code I posted is a sketch in itself, should not be merged into your sketch you should run it separately.

After that you should use the I2C scanner sketch to see if the address really changed

And if so, then you can try to run your sketch with two sensors.

Hi, I did as you suggested and put that code into a separate sketch, I also ran the i2c scanner, and this is what it came up with

Scanning... I2C device found at address 0x70 ! done

How do I tell if that has been readdressed or not?

Thanks, Rohan

run the I2C scanner before and after the renumber sketch

you should get 2 different numbers

Hi,
When I wired both up, all I got was

Scanning…
I2C device found at address 0x70 !
done

I dont think it has been re addressed…

I am using this diagram to wire it up, could it be because I am using the same 2 analog pins?

Also,
I had problems running the code you provided, so I used the one I found on the arduino website

This is the code:

#include <Wire.h>
void setup()
{
}

void loop()
{
}

void changeAddress(byte oldAddress, byte newAddress)
{
  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xA0));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xAA));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0xE6));
  Wire.write(byte(0xA5));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(newAddress);
  Wire.endTransmission();
}

I am trying to re address this to E6 (http://www.robot-electronics.co.uk/htm/srf02techI2C.htm), however for some reason it refuses to re address! I upload the code and then I run the i2c scanner, and I still get address 0x70!!

Thanks,
Rohan

F5OXZE5GXSSLK4I.MEDIUM.jpg

IN your sketch you post YOU NEVER call the function ChangeAddress()
adding the function is not enough,
also missing is a call to Wire.begin()

OK, here a more elaborate version of the address changer, with debugging output and a test if the address has changed.
Code compiles but as I have no such sensor I cannot test it.

#include <Wire.h>
#include <Arduino.h>

void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("Address changer - SRF02\n\n");

  Serial.println("BEFORE");
  Serial.print("0x70:\t"); 
  Serial.println(testAddress(0x70), DEC);
  Serial.print("0xE6:\t"); 
  Serial.println(testAddress(0xE6), DEC);

  changeAddress(0x70, 0xE6);
  delay(1000);

  Serial.println("\nAFTER");
  Serial.print("0x70:\t"); 
  Serial.println(testAddress(0x70), DEC);
  Serial.print("0xE6:\t"); 
  Serial.println(testAddress(0xE6), DEC);
}

void loop()
{
}

void changeAddress(byte oldAddress, byte newAddress)
{
  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xA0));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(byte(0xAA));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(newAddress));
  Wire.write(byte(0xA5));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.write(byte(0x00));
  Wire.write(newAddress);
  Wire.endTransmission();
}

bool testAddress(int address)
{
  Wire.beginTransmission(address);
  return (Wire.endTransmission() == 0);
}

give it a try

Hi, I did as you suggested and uploaded that code to my Arduino Board, but unfortunately all the serial monitor says is:

Address changer - SRF02

BEFORE 0x70: 1 0xE6: 0

AFTER 0x70: 1 0xE6: 0

I am also talking to Devantech customer support. I think it might be worthwhile to mention that it did re address once, on the code provided by Arduino, but at that point of time I didn't need a reason for the address to change, so I set it back to default. Now when I use the same code as I did when it worked, it doesn't re address.

Thanks, Rohan

Some devices will only allow you to reprogram the address once, they have a "write once" memory to contain it.

Try leaving this device on the address you are now stuck with, and try reprogramming one of your other devices to a different address, and read the instructions carefully and try and get it right, the first time.

Hi, it is a little more effort but have you consider using a multiplexer such as the PCA9548? The main advantage of this approach is that you can immediately use upto eight of the same devices all with the same address. I have built a device using this approach and it works perfectly for me; you can also multiplex the multiplexer!

Hi, Yes I did consider that option, but I have already surpassed my budget for this project, and if I can get it done this way, it would be much more convenient.

I am currently talking with Devantech Support.

They are saying that the sensor can be readdressed any number of times, and it is because there is nothing in the void loop section my sensor isn't re addressing. Unfortunately my support session was cut short so I couldn't get much more information....

I was hoping someone could elaborate on that

Thanks, Rohan