Finally I was able to change the address of the sensor. I looked at the arduino forum and found the solution. I will share with everyone so that anyone who wants to buy cheap I2C sensors can use multiple of them.
changeAddress(SensorAddress,NewSensorAddress,1);
}
void loop(){
takeRangeReading(); //Tell the sensor to perform a ranging cycle
delay(100); //Wait for sensor to finish
word range = requestRange(); //Get the range from the sensor
Serial.print("Range: "); Serial.println(range); //Print to the user
}
//Commands the sensor to take a range reading
void takeRangeReading(){
Wire.beginTransmission(NewSensorAddress); //Start addressing
Wire.write(RangeCommand); //send range command
Wire.endTransmission(); //Stop and do something else now
}
//Returns the last range that the sensor determined in its last ranging cycle in centimeters. Returns 0 if there is no communication.
word requestRange(){
Wire.requestFrom(NewSensorAddress, byte(2));
if(Wire.available() >= 2){ //Sensor responded with the two bytes
byte HighByte = Wire.read(); //Read the high byte back
byte LowByte = Wire.read(); //Read the low byte back
word range = word(HighByte, LowByte); //Make a 16-bit word out of the two bytes for the range
return range;
}
else {
return word(0); //Else nothing was received, return 0
}
}
void changeAddress(byte oldAddress, byte newAddress, boolean SevenBitHuh){
Wire.beginTransmission(oldAddress); //Begin addressing
Wire.write(ChangeAddressCommand1); //Send first change address command
Wire.write(ChangeAddressCommand2); //Send second change address command
if(SevenBitHuh){ newAddress = newAddress << 1; } //The new address must be written to the sensor
Wire.write(newAddress); //Send the new address to change to
Wire.endTransmission();
}
Can you help me change the code to use multiple maxbotix sensors. I changed the addresses of each sensor. But I don’t know how to change the code so that it assigns the address I put in “RNGFND_ADDR”.
As noted in wiki, you need to map address in decimal , so 0x70= is 112 you must write in parameters for first RangeFinder and then you can use 0x68 = 110 for second unit and so on…
Hi! Could you make more than 1 sensor work? Infact the idea of I2C is to connect a lot of devices, but the rangefinder configuration only acept 2 rngfnd and rngfnd2. How I suppose to connect 6 I2C sensor for example? thanks!
Hello lucas, i’m having the same issues as you do, did you manage to connect both sensors? for me i tried to use a lidar with a sonar sensor, if i use the lidar alone it works fine via uavcan, and if it is the sonar alone it works, but when i try to use both of them, i only get the lidar data on rngfnd1, and rngfnd2 is always 0 or 20
and in order to use multiple sensors of the same type, like yours, you have to change the adress of each sensor via arduino code, but first, you must find a solution for the rngfnd2 not displaying data, i’m stuck here unfortunatly
Hello,
I use GY-us42y2. Need to use in I2C with pixhawk.
Impossible to change sensor address. With arduino code, 222 or 224 address always came back.
Pixhawk need a 0 to 127 address.
How can I change it definitively?
hello, i have same problem. i want to set two GY-us42y2 in my drone, one of them bellow for landing and another front on my drone, but i don’t know how do this;
can you help me?
could you set this?
thanks alot.