Two sensors only one works at a time

Ok, I have been working in this for some time and can’t figure it out. I have a XIAO ESP32C3 that seems to work just fine as long as only one sensor is connected. When I try to connect a second one the SEEED stops reading the first one. I am using a Dallas 18B20 temperature sensor and a Capacitive Moisture sensor. Normally the moisture sensor works fine and the temp sensor stops reading. I have had one instance where they both worked fine for a few minutes then temp sensor went offline. I have included the code here:

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-OneWire
 */

#include "OneWire.h"
#include "DallasTemperature.h"
#define ONE_WIRE_BUS 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin(); // initialize the OneWire sensor;  
  Serial.println("Initialized");
}

void loop() {
  // wait a few seconds between measurements.
  delay(1000);
  sensors.requestTemperatures();
  
  // read temperature as Fahrenheit
  
  int moist =analogRead(3);
  moist = map(moist, 790,2875,100,  0);

  // check if any reads failed
  if (isnan(isnan(sensors.getTempFByIndex(0)))) {
    Serial.println("Failed to read from OneWire sensor!");
    Serial.write("\n");
  } else {
    Serial.print("Temperature: ");
    Serial.print(sensors.getTempFByIndex(0));
    Serial.print(" \xC2\xB0"); // shows degree symbol
    Serial.println("F");
    Serial.print("Moisture: ");
    Serial.print(moist);
    Serial.println("%");
  }
}

Interesting thing is once it fails to read the sensor (temperature), no matter what I do I can’t make it read that sensor again. I have checked voltages and everything is where its supposed to be. Power for the sensors is being supplied by an external power source (breadboard power module)

I am at my whits end on this and appreciate any suggestions or ideas. I’m fairly new with the SEEED but have built many projects with arduino uno.

It may be the XIAO itself. I need to test that.

Hi there,

So the Xiao is different but only slightly. The bus basics are tricky with onewire and dallas that too. So good choice for a LEARNING opportunity… :grin:
Your close , stay at it.
The code has a bug in the NaN check
This line is wrong:


if (isnan(isnan(sensors.getTempFByIndex(0)))) {

It should be:

if (isnan(sensors.getTempFByIndex(0))) {

Right now it is calling isnan() on the result of another isnan(), which makes no sense and can hide what is really happening.

I would look at the DS18B20 wiring and the analog pin choice first, not the XIAO itself.

A few things stand out:

  1. The DS18B20 uses a 1-Wire bus, which needs a pull-up resistor on the data line. The datasheet calls for a weak pull-up, commonly 4.7k to 3.3V. Without that, the sensor may work intermittently and then disappear.
  2. Since your sensors are powered from an external breadboard supply, make sure the ground of that supply is connected to the XIAO ground. If the grounds are not common, the 1-Wire bus can fail even if the analog sensor appears to work.
  3. On the XIAO ESP32C3, the ADC pins are D0–D3, but Seeed specifically notes that A3/D3 uses ADC2 and may be unreliable for analog reads. They recommend using ADC1 pins A0/A1/A2 for more reliable analog input.

I would try this first:

  • DS18B20 data line with 4.7k pull-up to 3.3V
  • power DS18B20 from 3.3V
  • connect all grounds together
  • move the moisture sensor to A0, A1, or A2
  • avoid powering the moisture sensor from 5V unless you are sure its analog output is safe for the XIAO input

So my guess is not that the XIAO “can only read one sensor.” It is more likely:

  • a missing pull-up
  • grounding issue
  • or using the less reliable A3/ADC2 input for the moisture sensor

The strongest clues are:

  • the moisture sensor keeps working
  • the DS18B20 drops out
  • once it drops out, it usually means the 1-Wire bus is no longer healthy
  • on the XIAO ESP32C3, D0–D3 are the ADC pins, and D3/A3 uses ADC2, which Seeed warns can be less reliable for analog reads than A0/A1/A2 on ADC1.

HTH
GL :slight_smile: PJ :v:

Thank you. I did upload the wrong code. The nan check was ok in the actual code on the machine. My sensor came with a “connection module” that has the resistor already installed. I did not have the SEEED ground connected. I will check effect of that today. I will move the moisture sensor to A0 as well today. I checked the voltage on the moisture sensor data wire and it was 3.3 V. I’ll make the changes and see what happens. Many thanks for the in-depth analysis. Very much appreciated.

1 Like