DS18B20

Team,

just a short Q on the DS18B20 if I use 6 of them on one digital Port.
Are they always in the same order? Even if my device or Sketch restarted?
Or how do I make sure if I read the Temperature on one, it's the one I expected.

My current setup:
using an ESP8266WiFi
using the DallasTemperature
when using the "Tester" example it loops through all devices and prints the DeviceAddress to the serial monitor
--> but can I be certain if I do not change the cabling, the order is always the same? I already did 10 to 15 restarts and they have been in the same order up till now. Just a lucky thing or will it be that for always?

I will loop through the Sensors every 10 seconds and forward the Temperatures via MQTT to my Node-Red, there they will be shown to the user and maybe Node-Red will take an action on a certain result, e.g. turn on heating if the temperature is below x.

I thought I can just use the DeviceAddress and forward the DeviceAddress together with the temperature and then decide on Node-Red what sensor it is.

How are you guys going to do that? Open to other options or ideas :slight_smile:

Greetings, Ludwig

You need to know the individual address of each sensor. Mark each accordingly. If you use the "by index" method, I understand it reads each address in order. This might get complicated if you have to add, change, or replace a sensor. If you use the method where you nominate the addresses

You are never left in any doubt about which is which. I believe each approach is equally valid.

Thanks Nick_Pyner,

helps a lot. I've been to the mentioned URL. What I'm doing now, I extended the Tester example by adding a global variable tmpAdr and changed the function printAddress with this:
void printAddress(DeviceAddress deviceAddress) {
tmpAdr = "";
String tmpPart ="";
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress < 16) Serial.print("0");
_ Serial.print(deviceAddress*, HEX);_
_ tmpPart = String(deviceAddress);
if (deviceAddress <= 9) {
tmpAdr = tmpAdr + "0" + tmpPart;
}
else {
tmpAdr = tmpAdr + String(deviceAddress, HEX);
}
}
}*

Just needed to pay attention on a leading 0 in the address.
At the end my tmpAdr holds the full address and I can do the rest of the logic in Node-Red.
And you are right, if I need to replace a sensor I need to replace a few things on the Node-Red side of things as well. That's something I can live with - at least for now. Maybe need to find some more elegant way of doing it later.
Thanks, Ludwig_

If you want to read a lot of DS18B20s and display the reported temperatures of each sensor, take a look at my blog, Multi temperature-humidity sensing with an Arduino Nano – TFT display: sketch updated. That one reads seven DS18B20 sensors and displays on a 320x480 TFT display

(Multi temperature-humidity sensing with an Arduino Nano – TFT display: sketch updated – thesolaruniverse)

There is a sketch attached at the end of that blog..

Here is a simple multiple DS18B20 sketch (3 sensors) storing the temperature data in variables Temp1, Temp2 and Temp3.

// read_DS18B20_into_variable_OK
// this sketch uses Dallas D18B20 one wire temperature sensors
// reads temperature of each sensor and stores the temperature value in a variable 
// Floris Wouterlood 12 January 2016
// Thanks to everybody of the Arduino community

// DS18B20 Pinout (left to right, pins down, flat side toward you)
// - Left   = Ground
// - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
// - Right  = +5 or +3.3 V


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

// ================================= Bus on pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// ================================= Setup a oneWire bus 
OneWire oneWire(ONE_WIRE_BUS);

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


// ================================= Probe addresses
DeviceAddress probe_01 = { 0x28, 0xFF, 0x1C, 0x76, 0xA3, 0x15, 0x04, 0xD4 };
DeviceAddress probe_02 = { 0x28, 0xFF, 0x96, 0x74, 0xA3, 0x15, 0x04, 0xA5 };
DeviceAddress probe_03 = { 0x28, 0xFF, 0x7B, 0x74, 0xA3, 0x15, 0x04, 0x93 };

// ================================= number of temperature devices found
int numberOfDevices;

// ================================= variables used for control logic
float   Temp1, Temp2, Temp3;   //  storage variables temperature sensors


void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  Serial.println ("starting one wire sensors");
  Serial.print("initializing Temperature Control Library, version ");
  Serial.println(DALLASTEMPLIBVERSION);
  sensors.begin();
 
  // locate devices on the one wire bus
  Serial.println("");
  Serial.print("....... found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices ..........");
 
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(probe_01, 10);
  sensors.setResolution(probe_02, 10);
  sensors.setResolution(probe_03, 10);
 
  delay (2000);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    }
}
void loop(void)

   

{
  sensors.requestTemperatures();
 
// ================================= load variables with sensor values 
  Temp1 = sensors.getTempC(probe_01);
  Temp2 = sensors.getTempC(probe_02);
  Temp3 = sensors.getTempC(probe_03);

// ================================= serial monitor part ==========================  
 
  Serial.println("===================================");


      Serial.print("Probe_01 temperature:  ");
      Serial.print(Temp1,1);
      Serial.println(" *C"); 
      
      Serial.print("Probe_02 temperature:  ");
      Serial.print(Temp2,1);
      Serial.println(" *C "); 
      
      Serial.print("Probe_03 temperature:  ");
      Serial.print(Temp3,1);
      Serial.println(" *C"); 
 
      Serial.println(" ");    
     
 
      delay (4000);
}