i2c-moisture-sensor icon indicating copy to clipboard operation
i2c-moisture-sensor copied to clipboard

Multiple sensors measurements

Open smjacques opened this issue 1 year ago • 0 comments

Hi,

I bought some sensors to connect them to a ESP32 and test the measurements of multiple sites.

I've changed the code adding two sensors, but the result of the second (sensor2) is always 'Soil Moisture Capacitance: 65535, Temperature: -0.10, Light: 65535',

I'm afraid I'm missing something here. Is there an example which I could use as a reference for multiple moisture sensors?

My code right now is as follows:

#include <I2CSoilMoistureSensor.h>
#include <Wire.h>

// Initialize two sensor objects with different I2C addresses
I2CSoilMoistureSensor sensor1(0x20);  // First sensor with address 0x20
I2CSoilMoistureSensor sensor2(0x21);  // Second sensor with address 0x21

void setup() {
  Wire.begin();
  Serial.begin(9600);

  // Initialize the first sensor
  sensor1.begin();  // reset sensor 1
  delay(1000);      // give some time to boot up
  Serial.print("I2C Soil Moisture Sensor 1 Address: ");
  Serial.println(sensor1.getAddress(), HEX);
  Serial.print("Sensor 1 Firmware version: ");
  Serial.println(sensor1.getVersion(), HEX);
  Serial.println();

  // Initialize the second sensor
  sensor2.begin();  // reset sensor 2
  delay(1000);      // give some time to boot up
  Serial.print("I2C Soil Moisture Sensor 2 Address: ");
  Serial.println(sensor2.getAddress(), HEX);
  Serial.print("Sensor 2 Firmware version: ");
  Serial.println(sensor2.getVersion(), HEX);
  Serial.println();
}

void loop() {
  // Read data from sensor 1
  while (sensor1.isBusy()) delay(50);  // available since FW 2.3
  Serial.print("Sensor 1 - Soil Moisture Capacitance: ");
  Serial.print(sensor1.getCapacitance());
  Serial.print(", Temperature: ");
  Serial.print(sensor1.getTemperature() / (float)10);
  Serial.print(", Light: ");
  Serial.println(sensor1.getLight(true));  // request light measurement, wait and read
  sensor1.sleep();                         // put sensor 1 to sleep

  // Read data from sensor 2
  while (sensor2.isBusy()) delay(50);  // available since FW 2.3
  Serial.print("Sensor 2 - Soil Moisture Capacitance: ");
  Serial.print(sensor2.getCapacitance());
  Serial.print(", Temperature: ");
  Serial.print(sensor2.getTemperature() / (float)10);
  Serial.print(", Light: ");
  Serial.println(sensor2.getLight(true));  // request light measurement, wait and read
  sensor2.sleep();                         // put sensor 2 to sleep

  delay(2000);  // wait a bit before the next reading
}

smjacques avatar Sep 22 '24 13:09 smjacques