//https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
//https://forum.arduino.cc/t/changing-i2c-address-of-sensors/910535

#include <Wire.h>
 
void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}
 
void loop() {
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknow error at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
  delay(3000);
  int error2 = Wire.endTransmission();
  if( error2 != 0)
  {
    Serial.println( "The sensor is not at 0x24");
  }
  else
  {
    Serial.println( "The sensor is found, changing I2C address");
    Wire.beginTransmission( 0x24);
    Wire.write( 0x53);  // password register
    Wire.write( 0xAA);  // password
    Wire.endTransmission();

    delay(10);    // not described somewhere, just for safety

    Wire.beginTransmission( 0x24);
    Wire.write( 0x00);  // I2C address register
    Wire.write( 0x50 << 1);  // new I2C address
    Wire.endTransmission();
  }  
}
