Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple sensors measurements #43

Open
smjacques opened this issue Sep 22, 2024 · 0 comments
Open

Multiple sensors measurements #43

smjacques opened this issue Sep 22, 2024 · 0 comments

Comments

@smjacques
Copy link

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant