2 sensors DS18B20 on the same wire - sometimes I get result from the wrong sensor

Hello,
I have 2 sensors DS18B20 on the same wire. The wire is puled up by one resistor 4.7k.

Definitions:

OneWire oneWire(cTempSensorsIn); 
 //Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

In Setup():

sensors.setResolution(12);
sensors.setWaitForConversion(false);
sensors.begin();

Reading:

//read inside and outside temperature every second
  if(FALSE != IsTimerElapsed(u32InputReadTimer, cTempReadPeriod))
  {
    if(sensors.isConversionComplete())
    {
    
      f32InsideTempL = sensors.getTempCByIndex(0); 
      f32OutsideTempL = sensors.getTempCByIndex(1);
      Serial.print(f32InsideTempL);Serial.print(",");
      Serial.println(f32OutsideTempL);
      
      sensors.requestTemperatures(); 
    }
    StartTimer(&u32InputReadTimer);
  }

End the result is:

|2023-08-02 12:38:55.197|27.56|15.25|
|2023-08-02 12:38:56.273|27.56|15.25|
|2023-08-02 12:38:57.349|27.56|15.25|
|2023-08-02 12:38:58.423|27.56|15.31|
|2023-08-02 12:38:59.498|27.56|15.25|
|2023-08-02 12:39:00.573|27.56|27.56|
|2023-08-02 12:39:01.647|27.56|15.31|
|2023-08-02 12:39:02.728|27.62|15.25|
|2023-08-02 12:39:03.800|27.56|15.25|
|2023-08-02 12:39:04.876|27.56|15.25|
|2023-08-02 12:39:05.952|27.56|15.25|
|2023-08-02 12:39:07.028|27.56|15.25|
|2023-08-02 12:39:08.103|27.56|27.56|
|2023-08-02 12:39:09.178|27.56|15.31|
|2023-08-02 12:39:10.253|27.56|15.25|
|2023-08-02 12:39:11.329|27.56|15.25|
|2023-08-02 12:39:12.405|27.56|15.25|
|2023-08-02 12:39:13.481|27.56|15.25|
|2023-08-02 12:39:14.556|27.62|15.25|
|2023-08-02 12:39:15.631|27.56|15.31|

You can see, that sometimes it reads the outside sensor instead of the inside. It never happens the other way.
I try it with it 2 seconds, but the result is the same.

Can you give more information please ?

Which Arduino board do you use ? How long are the wires ? Do you use two or three wires for the DS18B20 ? Can you give a full sketch that has the problem ?

If you bought your DS18B20 on Ebay/Amazon/AliExpress, then they are counterfeit. Those don't work well at 3.3V or at high or low temperatures or with long wires or with 2 wires to the DS18B20 (parasitic power).

The most common way it so get the temperatures every 2 seconds or every 5 seconds. Are you trying to get the temperatures every second ?

Sensors are in 3 wires mode, powered by 4.6V, wires are long about 70 cm. Arduino Nano, but this doesn't have any connection with the problem. The problem persists at both 1 and 2 seconds reading interval. It is present at 5 seconds too. Apparently it is not connected to measurement readiness, but with messing up addresses. It doesn't read wrong value, it reads correct value, but from the wrong sensor.

Post your full code.

I added the definitions. There is no more sensors connected code, so there is no need to post the full code.

That's your opinion. I'm out. Good luck.

Instead of using getTempCByIndex(), use the sensor address to specify which sensor to read the temperature from.

Probably a good idea, but I decided to resolve the problem in more radical way - separating the sensors on different inputs. No more problems :slightly_smiling_face:

my opinion is based on 17 years of working experience in embedded programming.

Isn’t that the decision for volunteers to make ? :thinking:


When those who are trying to help you ask for more information, it’s your job to supply it.

Of course anyone can ask anything. But I'm the one who decides to fulfill the request or no. Let's imagine that you are working on a commercial product and someone ask you for the whole code :). Or let imagine that you have thousands lines of code.
Or your experience is telling you that the asking person doesn't have enough experience and is asking unrelated things?

If you were making a commercial product, then tell us.

If you are going to decide what information we need then you should be able to fix your own problem.


This volunteer and others will be placing you on the ignore list.


BTW, there is a forum on this web site where you can request paid help.


And, what’s stopping you from showing a scaled down version of the sketch that still demonstrates your problem ?


Good luck.

2 Likes

Please do not waste the time and effort of the volunteers on the Arduino forum.

And yet you resorted to a rookie fix for your error.

An experienced designer would excerpt a standalone program from their thousands of lines that exhibited the problem and troubleshoot that.

1 Like

No, it is not commercial in my case, I'm just giving examples.

I think one of the the purposes of such forums is not to fix one (mine) problem, but to leave a trace for the problem, so others can find a solution without asking again.

I just cannot grasp why anyone would want to loose his time reading unrelated to the problem code.

My problem was illustrated by the title of the post. Then the code showed how I use the sensors, and this is the only code that has anything to do with these measurements. The log showed the results. I don't have a schematic, but I have described how the sensors are connected and powered. A just cannot give more meaningful information.

When such kind of question is asked I don't expect that someone will spend his time reading my whole code and looking my schematics. I expect that someone who had encountered similar problem in the past will see it and give some information.

This is not a rookie fix :slight_smile: This is a fast and working fix. I have free pins, so for the moment it is easier, faster and safer to use another one than to spend hours trying to catch an elusive random problem, which could be caused by wire interference, a problem in the library or whatever other reason that is not caused by my code.

Correct. And your solution was to give up, therefore useless to anyone reading the thread in the future.

70cm is trivial wire length. 4K7 is the recommended pull-up for 5V operation. The library you used is a mystery since you didn’t include it in your code snippet.

Based on your description of your setup, the problem most likely resides in your code.

The library is the standard one that everyone is using for DS18B20. - DallasTemperature. It could be suggested from the snippets: DallasTemperature sensors(&oneWire); If not, you could ask about it without the need to post all the irrelevant code.
And If the problem is in my code, it should be visible, because the posted code snippet is all that is connected to the sensors.

Okay, Ken, you know best. Enjoy your hobby.

If you have quality problems with the information provided free of charge, then you should look for another forum to solve your technical problems.