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.
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.
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...
I am trying to re address this to E6 (SRF02 Ultra sonic range finder), 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!!
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.
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.
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....