Wrong temp with DS18B20 sensor

I have 2 projects with a waterproof DS18B20 sensor, but if I use it like this, everything works perfectly. But when I put the sensor in the water, the temperature keeps switching between -127 and the actual temperature. When I take it out of the water, I only see the actual temperature.

Have used various sensors, but always the same problem. How is this possible??
I even put a sensor with sealant in a little tube to possibly make it extra waterproof.

Leakage?

Does it occur if you test with one sensor?

Can you share a photo of your setup?
And post your code?
-127 means bad connection.

Yes

I use it with a sensor module.

The temperature is fluctuating. Sometimes the actual temperature, and then suddenly -127. If I take the sensor out of the water, it still shows the correct temperature. Here's the diagram.

```
//MAX7219
//din-d7
//cs-d8
//clk-d5
//board esp8266 (nodecmu)

#include <MD_Parola.h>
//#include <MD_MAX72xx.h>
#include <SPI.h>

#include <DIYables_LED_Matrix.h>
//======================temp=================
/*

#include <OneWire.h>
#include <DallasTemperature.h>

#define SENSOR_PIN D2 // The ESP8266 pin connected to DS18B20 sensor's DQ pin

OneWire oneWire(SENSOR_PIN);
DallasTemperature DS18B20(&oneWire);

float temperature_C; // temperature in Celsius
float temperature_F; // temperature in Fahrenheit
//=========================

// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW

// Defining size, and output pins
#define NUM_MATRICES 4 // Number of cascaded MAX7219 modules
//#define CS_PIN 10 //15 voor ESP32 10 voor UNO
#define CS_PIN D8 // Chip Select pin for MAX7219
#define SPACING 2 // Spacing between characters

DIYables_Max7219 display(CS_PIN, NUM_MATRICES);

void setup() {
//=================================temp
Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor.
DS18B20.begin(); // initialize the DS18B20 sensor
//======================================
// Initialize the LED matrix display
display.setBrightness(10); // Brightness level: 0 to 15
display.clear();
display.show();
}

void loop() {
// Display.setTextAlignment(PA_CENTER);
// Display.print("TEMP");
// delay(2000);
//==============temp

DS18B20.requestTemperatures(); // send the command to get temperatures
temperature_C = DS18B20.getTempCByIndex(0); // read temperature in °C
// temperature_F = temperature_C * 9 / 5 + 32; // convert °C to °F

// Serial.print("Temperature: ");
// Serial.print(temperature_C); // print the temperature in °C
// Serial.print("°C");

// Display.setTextAlignment(PA_CENTER);
// Display.print(temperature_C);
// delay(2000);

display.clear();
// Convert to C-style string before passing to the print() function
//String tempStr = String(temperature_C, 1); // 1 decimal place
String tempStr = String(temperature_C, 1) + "°C"; // 1 decimal place
display.print(tempStr.c_str(), SPACING, 0);
display.show();

delay(2000);
}

```

Add a proper 4.7kΩ pull-up resistor between the DQ line and VCC if you haven't already, right at the sensor end rather than just at the microcontroller, this helps with signal integrity over longer wire runs, especially in noisy/wet environments.

Check the seal at the wire-entry point of the sensor probe itself, not just the tip. Manufacturers vary a lot in build quality here, and even "waterproof" DS18B20 probes can have a weak seal exactly where the three wires enter the metal tube, that's usually the actual leak point, not the sensor tip.

Try wrapping that wire-entry junction with heat shrink plus a dab of silicone/hot glue sealant if it isn't already, this is a very common fix reported by others who've had the identical issue.

Since it happens with multiple different sensors for you, this makes it more likely to be a shared design flaw in that batch/type of probe (weak seal at entry point) rather than a one-off defective unit, worth trying a different sensor brand or add extra sealing to confirm.

I have used DS18B20 on a river monitoring project for years without problems
if you remove the matrix and test just the DS18B20 do you have the same problem?

how long is the lead to the DS18B20?

how is the ES8266 powered?

what temperature range are you looking at?

How are the wires to the sensor connected? The sensor may be moving enough when the sensor is in the water to cause an intermittent bad connection.

On the sensor module

And i use a matrix display to show temp

I try to use DS18S20 in TO92 form from reputable suppliers and where necessary, waterproof them myself.

If using heat shrink, use the adhesive lined type to get a good seal.

It's a tight squeeze in the usual "waterproof" metal tubes you see on eBay etc. and insulating the legs is important.

If need be, make up a tube from thin wall copper tube, flatten and solder one end and backfill with your preferred adhesive,

@horace Agree, never had any problems with them, always a good idea to read the datasheet.

i use 1,5m of sensor

i powered 5v on usb

range 10-50gr

The fact that the sensor works normally in air, starts alternating between the actual temperature and -127 °C when immersed, and immediately works normally again when removed from the water seems significant to me.

-127 °C indicates a communication/read error, but if water had actually entered the probe, I would not expect the problem to disappear immediately when the probe is taken out of the water. The water would still be inside.

Since this also happens with several sensors, I wonder whether immersion itself is affecting the 1-Wire signal, for example through leakage or additional capacitive coupling to the water.

A simple test might help narrow this down:

Immerse only the metal tip, keeping the cable entry completely dry. Then gradually immerse the probe deeper, including the transition between the metal tube and the cable, and finally part of the cable.

If the -127 errors start only at a particular immersion depth, that would be a very useful clue.

I would also test the DS18B20 alone, without the MAX7219 display, connected directly to the ESP8266 with a proper 4.7 kΩ pull-up from DQ to 3.3 V. That should help distinguish a waterproofing problem from a 1-Wire signal/power issue.

Stainless tubes are readily available from sites like eBay.
5 for $1.18
https://www.ebay.com/itm/191692293280

I read that some (fake) DS18B20 don't perform well outside normal temps when powered from 3V3. I power my DS18B20 from 5volt, and pull up to 3.3volt.

I've tried several sensors. The strange thing is when you put them in the water, this sensor gives immediately -127. As soon as you take it out of the water, it shows immediately the actual temperature.
I also use the sensor module that came with it.
If I really just hang the metal in the water with a new sensor, no problem, further in water, immediately -127
But I sealed 2 others completely with silicone caulk. And as soon as they get in the water, I immediately get -127. Out of water no problem.
I bought this as a waterproof sensor

Does running a ground wire from GND to the water change anything?

What is the temperature of the water?
Ice cold, boiling hot, room temp?