How to connect multiple DS20B18 sensors with Arduino MEGA

Hi guys,

I want to make a data logger. i am using 8 probe sensors.

The problem is when i connect 8 sensors and while the operation i disconnect one of them the Arduino still working and give a value for it!.

Also if any problem happen to one of them ( water leakage inside a sensor) all reads become -127 why is that?

i connect all sensors to one digital pin. and this is my code.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
#include <DS1302.h>

// RST,DAT,CLK
DS1302 rtc(31, 30, 33 );
File myFile;
int pinCS = 53; // Pin 10 on Arduino Uno

// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;

void setup(void)
{
sensors.begin(); // Start up the library
Serial.begin(9600);

if (SD.begin())
{
Serial.println(“SD card is ready to use.”);
} else
{
Serial.println(“SD card initialization failed”);
return; }
rtc.halt(false);
rtc.writeProtect(false);
pinMode(pinCS, OUTPUT);
// SD Card Initialization

Serial.print(“Locating devices…”);
Serial.print(“Found “);
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(” devices.”);
Serial.println("");
sensors.requestTemperatures();
rtc.setTime(2, 36, 00); // Set the time to 12:00:00 (24hr format)
rtc.setDate(22, 12, 2020); // Set the date to August 6th, 2010
}
void loop(void)
{

// Send command to all the sensors for temperature conversion
for (int j=0; j<1; j++)

{
Serial.print(rtc.getDateStr());
Serial.print(",");
Serial.print(rtc.getTimeStr());
Serial.print(",");
if (SD.begin())
{
myFile = SD.open(“TestT9.txt”, FILE_WRITE);
if (myFile) {
myFile.println(" “);
myFile.print(rtc.getDateStr());
myFile.print(”,");
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.close(); // close the file
}
else {
Serial.println(“error opening test.txt”);
}
} else
{
Serial.println(“SD card initialization failed”);
return; }
// Display temperature from each sensor
for (int i = 0; i < deviceCount; i++)
{
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print(" , ");

if (SD.begin())
{
myFile = SD.open(“TestT9.txt”, FILE_WRITE);
if (myFile) {
myFile.print(tempC);
myFile.print(",");
myFile.close(); // close the file
}
else {
Serial.println(“error opening test.txt”);
}
} else
{
Serial.println(“SD card initialization failed”);
return; }
}
delay(5000);
}
Serial.println("");
}

You retrieve the temperature “by index”. However, in this situation I prefer to use the unique ID of the temperature sensors. You need an array to store them.

The disadvantage is that when you replace a sensor, then you have to add the new ID in the sketch.

You have to give them a good name in the sketch, and put a label with that name on each DS18B20 sensor.

I made an example in 2016. Is this helpful ? https://forum.arduino.cc/index.php?topic=393653.msg2711178#msg2711178.

Please use code-tags for the sketch instead of a quote. The </> button is for code-tags.

If water gets into your circuit, yes you have a problem! Water can conduct, then corrode and
electrolyze away parts of the circuit. Water and electricity should never mix!

I've never understood why people use the by-index method
https://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
-127 means no connection.

Koepel:
You retrieve the temperature “by index”. However, in this situation I prefer to use the unique ID of the temperature sensors. You need an array to store them.

The disadvantage is that when you replace a sensor, then you have to add the new ID in the sketch.

You have to give them a good name in the sketch, and put a label with that name on each DS18B20 sensor.

I made an example in 2016. Is this helpful ? https://forum.arduino.cc/index.php?topic=393653.msg2711178#msg2711178.

Please use code-tags for the sketch instead of a quote. The </> button is for code-tags.

You are a hero man. i used it and added RTC and SDcard it working very well.

but i have some questions:
1- some time the vaule become -127 for different sensors and returned back quickly why is that happening?
2- What is the resolution? is your code the value is 10

MarkT:
If water gets into your circuit, yes you have a problem! Water can conduct, then corrode and
electrolyze away parts of the circuit. Water and electricity should never mix!

Yes but the value for the 8 sensors become -127 if anyone of them have a water inside.

Nick_Pyner:
I've never understood why people use the by-index method
Arduino 1-Wire Tutorial
-127 means no connection.

Thank you so much.

hamzatay:
Yes but the value for the 8 sensors become -127 if anyone of them have a water inside.

They're all connected to the same bus. If that bus is messed with by one sensor, you can't read any of the others either.

The sensors you buy as “ water proof” are not - the metal end is waterproof but where the cable exits is not - so if you submerge them , they will fail.

You can probably improve them with the right silicone sealant though. The other problem is that the
cheap one on eBay are probably fake anyway.

wvmarle:
They're all connected to the same bus. If that bus is messed with by one sensor, you can't read any of the others either.

So how can i fix this problem?

hammy:
The sensors you buy as “ water proof” are not - the metal end is waterproof but where the cable exits is not - so if you submerge them , they will fail.

Yes you are right.

if i immerged it vertically can the water get inside the probe?

hamzatay:
So how can i fix this problem?

Make sure no water gets into the sensors.

MarkT:
You can probably improve them with the right silicone sealant though. The other problem is that the
cheap one on eBay are probably fake anyway.

Thank you

wvmarle:
Make sure no water gets into the sensors.

There is water inside how can i dry it?

Maybe in an oven... but then the inside may have corroded already, and drying may not fix it.

Easiest and most reliable> replace the offending sensor. And make sure it doesn't get wet inside again.

If it got wet , probably best to bin it .

If you want to measure under a liquid you need a thermowell - you can make something from a length of copper tube with sealed end - push the sensor down the tube and make sure the open end is well above the surface .

hammy:
If it got wet , probably best to bin it .

If you want to measure under a liquid you need a thermowell - you can make something from a length of copper tube with sealed end - push the sensor down the tube and make sure the open end is well above the surface .

Yes i want to put the sensor inside the wet concrete and take the values until it drk what do you suggest?

wvmarle:
Maybe in an oven... but then the inside may have corroded already, and drying may not fix it.

Easiest and most reliable> replace the offending sensor. And make sure it doesn't get wet inside again.

Thank you.

I’d suggest you use the tube approach I mentioned - you could flatten the end and solder it up