Solved: DS18B20 sometimes no measurements?


I'm using some DS18B20 sensors to monitor the temperature of my boiler and some other temperatures.

Sometimes the measurement drops to 0 (I've changed the programming of the EmonTX, otherwise it would drop to -127 but that kills all the nice diagrams).

Some of the sensors are connected to the EmonTX via a 10m ethernet cable, using orange/green and blue/brown as supply and data line and the rest (white cables) for ground. Could this be a problem ? I've red about problems because of the high slew rate of this sensors but why they are ok most of the time and only have problems sometimes?

Any ideas? You can have a look at it here:, for example: errors starting today 9:40 in the morning.

Robert Wall's picture

Re: Solved: DS18B20 sometimes no measurements?

I can't quite understand your wiring, but I suspect that a problem could well be there. CAT5 Ethernet cable is made up of twisted pairs, the twists are designed in such a way as to reduce crosstalk between pairs. Therefore by using more than one pair you may in fact be adding to the problems rather than reducing them. I don't see the need for the reduced voltage drop that paralleled conductors might give you. I suggest you put the supply on one pair only (say Orange + White/Orange) and the data on one pair (say Blue + White/Blue) and leave the remaining two pairs unconnected.

A second suggestion is to check the value of the pull-up resistor. The Maxim data sheet specifies 4.7 kΩ.

Thirdly, you don't say how many sensors are on a single input and whether they are connected in star or daisy-chained. The cable acts as a transmission line and if the configuration is 'star', reflections in the other cables could be distorting the wanted signal. "Daisy-chain" is the preferred configuration for long cable runs, but of course it all depends on the requirements of your installation.

jb79's picture

Re: Solved: DS18B20 sometimes no measurements?

Ok, i've drawn a litte picture to illustrate how I connected the sensors. At total there are 6 sensors connected to the EmonTX at the moment. One of them directly with 1m cable on the EmonTX, the next 4 via about 10m ethernet cable and then with their own 1m cables and a last one with another 5m ethernet cable and with it#s own 1m cable.

So it's a combination of daisy chain and star, but there is no other possibility

At the top of the picture you can see the connections of the two parts with the ethernet cable.

The pull-up resistor is 4,7k.

Robert Wall's picture

Re: Solved: DS18B20 sometimes no measurements?

Which sensor is close to the emonTx and which is the one 15 m distant?  As you say there's no possibility in practice of wiring the sensors any other way, it seems you are stuck with what you have. It may be a silly question, is it possible that the cause is interference from some equipment nearby?

I see the software polls the sensors in turn every 20 s approximately, but when I look carefully, I think I see patterns. T5 fails frequently on the measurement after T6 fails, and T3 often fails at the same time as T6 or T4.

It is rare that you have two bad readings in sequence. Instead of converting the -127 value to zero, would it be better (= less bad) for the emonTx to send the last reading again? I know the data would be false, but it would give a better graph.

jb79's picture

Re: Solved: DS18B20 sometimes no measurements?

Hello Robert!

I'll try to correct the software next week, I've also thought about sending the old values again if a sensor fails. To be informed about a sensor outage even if old values are transmitted I think I'll use another feed. If sensor 1 fails, I'll add 1 to the feed value, sensor 2 adds 2, sensor 3 adds 4 and so on. So I can check the data consistency of up to 16 sensors (integer has 16bit) by only one additional feed.

T_Technikraum is the sensor nearest to the EmonTX.

Then there is the 10m cable and T3-T6 mounted on the boiler.

From there about 5m to T-Solar, mounted on the copper pipe coming down from the thermal solar system that heats up the boiler when the sun is shining.

All sensors are in a room with a heat pump, the 800l boiler, the ventilation system, the decalcifying plant and the power distributor. But I couldn't find a relation during the periodes when the sensors fail and one of this systems start or stop working. For example: The up/down of T4 is caused by the heat pump switching on and off (at the moment about once an hour). The ventilation system is running all the time, directly next to the EmonTX.


jb79's picture

Re: Solved: DS18B20 sometimes no measurements?

Hello again!

It took a little bit more time to correct the software. Now I store the latest valid temperature for each sensor. Here is my code example for one of the sensors.

emontx.T2 = sensors.getTempC(address_T2) * 100;
if (emontx.T2 < -5000) emontx.T2=emontx_old.T2;
else emontx_old.T2=emontx.T2;

I repeat this for all 6 sensors and it is working fine, I tested it by disconnecting the room temperature sensor for some seconds, hold the sensor in my hand, so that it got about 27°C (normal room temperature is about 19°C) and connected it again. In the measurement the value didn't change until it was reconnected, then the value jumped to the new measurement.


Does anybody know how my code could be written in an easier way for multiple sensors?

In my definitions I used T2-T7 (I bought 10 sensors, red out the addresses and labled them, T2-T7 are for this emonTx). I'd like to use a if loop, like this with a small function that only needs the sensor number and automatically does the same as the short code in the beginning but I don't know how to do it (I'm not the best in programming C)

for (i=2; i<=7; i++) sensors_check (i);

Robert Wall's picture

Re: Solved: DS18B20 sometimes no measurements?

Probably the best way around the software problem is to make an object called "sensor" and then create 6 instances of it. "sensor_check" then becomes a method of that class that does the calculation. If that's all very strange to you, take a look again at emonLib, because that's exactly what EnergyMonitor is, and calcVI is a method that does the calculation. So you'd copy the parts you need of the overall structure of EnergyMonitor but almost none of the detail. For example, you'd have a method to initialise it with the sensor address in the same way that currentTX or voltageTX plug in the calibration coefficients. You could take a look at which is a C++ tutorial, and if you understand the basics it should be helpful.

If you want to stay with plain C (not C++), then you probably need several arrays to hold the address, present temperature. old temperature, etc and use the parameter into sensors_check (i) as an index into those arrays.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.