Running two I2C soil moisture sensors on Arduino UNO

Hi all,

I am new to coding and Arduino so please bare with me.

I am looking to upgrade my first Arduino UNO project. I have an auto watering setup for my garden and looking to upgrade my current soil moisture sensors to a more durable and accurate capacative sensor. In particular the I2C Soil moisture sensor from Tindie.

I have done a search around and I cant seem to find anything around directly related to running two of these sensors.

I will be running two sensors with each sensor controlling its own water pump.
Currently my code is quiet simple compared to the example code provided for the i2c sensors.

I am having troubles getting my head around how to run two sensors off the i2c, From my understanding I will need to change the address on one of my sensors.

I have the code for one sensor with the address 0x20

#include <Wire.h>

void writeI2CRegister8bit(int addr, int value) {
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
}

unsigned int readI2CRegister16bit(int addr, int reg) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.endTransmission();
  delay(20);
  Wire.requestFrom(addr, 2);
  unsigned int t = Wire.read() << 8;
  t = t | Wire.read();
  return t;
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  writeI2CRegister8bit(0x20, 6); //reset
}

void loop() {
  Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register
  Serial.print(", ");

If i was to change my second sensors address to 0x21 would i simply just need to add in the following?

#include <Wire.h>

void writeI2CRegister8bit(int addr, int value) {
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
}

unsigned int readI2CRegister16bit(int addr, int reg) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.endTransmission();
  delay(20);
  Wire.requestFrom(addr, 2);
  unsigned int t = Wire.read() << 8;
  t = t | Wire.read();
  return t;
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  writeI2CRegister8bit(0x20, 6); //reset
  writeI2CRegister8bit(0x21, 6); //reset
}

void loop() {
  Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register sensor one
  Serial.print(", ");

delay(5000);

Serial.print(readI2CRegister16bit(0x21, 0)); //read capacitance register sensor two
  Serial.print(", ");

It would probably be a lot easier if I had the sensors in front of me, but i would like to understand the code before purchasing them as they are slightly costly for the rugged version.

Thanks for reading, any help would be greatly appreciated.

Link to sensor: https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/
Link to example code: i2c-moisture-sensor/README.md at master · Miceuz/i2c-moisture-sensor · GitHub

Indeed, set int addr to the correct value.
Apparently this sensor can be set to any address, the first time I see this kind of flexibility:

To change the I2C address of the sensor, write a new address (one byte [1..127]) to register 1; the new address will take effect after reset.

Usually I2C devices have one or more addresses hard coded, that can be selected by jumpers or connecting pins to GND or Vcc.

Also check out the I2C scanner sketch, this is very helpful in making sure your I2C bus is working in the first place, and to see the addresses of all connected devices.

Do mind that I2C is not meant for long distances. It's designed for intra-PCB communication. With some tricks (stronger pull-ups, twisted pair wiring, slower bus speed) you can get to 5 meters and I have seen references to even longer distances but if you want to go for that kind of wire lenghts you can better go for the RS485 version of these sensors (that's a protocol that's designed to do up to 1200 meters).