DS18B20 Via address

I using x2 ds18b20's, 1 is a water proof and the other is To92. The water proof is for the aquarium and the other is on the circuit board monitoring the pcb temp as its in a sealed enclosure. The pcb ds18b20 runs a small 12v pc fan, mapped is sketch between a temp range. both sensors are identified via Index.
The issue I have is should a sensor fail, the index swaps theyre numbers and my displays swap around.
Id like to identify both sensors as theyre individual address should a sensor fail. Ive looked online for examples but im really confused.
Is there someone that could help me understand how to identify them via adresses please.

Always show us a good schematic of your proposed circuit.

Show us a good image of your ‘actual’ wiring.

Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.


What are the sensor addresses ?

yeah I know. I havent because the pcb was made on Fritzing and the schem is all up the shit. Itll take me some time to redo it. Ive attached my code however.

[code]
/*
 ******************************************
 ********* DOLPHIN TANK ******************
 ******************************************
*/

//#include "Arduino.h"
#include <uRTCLib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd1(0x26,16,2);  // time/date LCD
LiquidCrystal_I2C lcd2(0x27,16,2);  // temp/fan LCD

uRTCLib rtc(0x68);

int PCBfanPin = 10;   // digital PCB fan o/p
int PWMfanPin = 9;    // digital fan o/p
int buzzerPin = 2;    // digital alarm o/p
int sensorCount = 0;
int PWMval;
int PCBfanPWMval;
int fanSpeed;
int fanState;
int PCBfanSpeed;

float CorrectedTemp;
float PCBtemp;

//******************************** MAIN SETTINGS **********************************************************************

float PCBfanTempMin = 28;         // temp when PCB fan begins
float PCBfanTempMax = 35;         // temp when PCB fan at 100%

int tempMin = 26;                 // temp when fans begin
int tempMax = 28;                 // temp when fans at 100%

float lowTMPalarm = 24.5;         // min temp alarm sounds
float highTMPalarm = 28;          // max temp alarm sounds

int PCBFanPWMvalMin = 20;         // adjustment for MIN PCB fan speed
int PCBFanPWMvalMax = 255;        // adjustment for MAX PCB fan speed

int mainFanPWMvalMin = 50;        // adjustment for MIN fan speed
int mainFanPWMvalMax = 255;       // adjustment for MAX fan speed

float CalibrateTemp = 2.2;        // manual temp calibration +2.2deg

//******************************** ALARM SETTINGS **********************************************************************

// morning purge time
int AlarmHour1 = 5;               
int AlarmMinute1 = 30;      

// evening purge time
int AlarmHour2 = 17;
int AlarmMinute2 = 30;

// The following 2 lines create a window due to the time lag of the temp sensors.
// It ensures the alarm goes off without missing the selected time.
// Note: PrimePurge must be longer than "timeSecond" window to eliminate double purging.

int timeSecondMin = 0;
int timeSecondMax = 10;

//******************************** INITIAL TIME SETTINGS *************************************************************

void SetTime()
{  
//  (Seconds,Minute,Hour,Weekday,Date,Month,Year);

//  highlight following line to initialize time  
//  rtc.set(00,58,05,6,20,6,23);
}

//********************************************************************************************************************

void PrintLCDTimeDisplay()
{
  lcd1.setCursor(0,0);
  lcd1.print("Time:");
  lcd1.setCursor(0,1);
  lcd1.print("PCB Temp: ");
}

//********************************************************************************************************************

void PrintLCDtempDisplay()
{
  lcd2.setCursor(0,0);
  lcd2.print("Temp: ");
  lcd2.setCursor(0,1);
  lcd2.print("Fan Speed: ");
}

//********************************************************************************************************************

void TimePCBtempResultsLCD()
{
  rtc.refresh();
  lcd1.setCursor(6,0);
  if (rtc.hour() <10) lcd1.print("0"); 
  lcd1.print(rtc.hour()); lcd1.print(":"); 
  if (rtc.minute() <10) lcd1.print("0");
  lcd1.print(rtc.minute()); lcd1.print(" "); 
  lcd1.setCursor(10,1);
  lcd1.print(PCBtemp,1);
  lcd1.print((char)223); 
  lcd1.print("C");
}

//********************************************************************************************************************

void TempFanResultsLCD()
{
  lcd2.setCursor(6,0);
  lcd2.print(CorrectedTemp);
  lcd2.print((char)223); 
  lcd2.print("C   ");
  lcd2.setCursor(11,1); 
  lcd2.print(fanSpeed);
  lcd2.print("%   ");
}

//********************************************************************************************************************

void PurgeFans()
{
  Serial.println();
  Serial.print("Drying fans");
  Serial.println();
  lcd2.clear();
  lcd2.setCursor(0,0); 
  lcd2.print("Drying Fans");
  
    for (int t=5; t>0; t--)
    {
      digitalWrite(PWMfanPin, HIGH);
      delay(3000);
      digitalWrite(PWMfanPin, LOW);
      delay(3000);
    }
  
  PrintLCDtempDisplay();
}

//********************************************************************************************************************

void CheckPurgeTimes()
{
if ((rtc.hour() == AlarmHour1) && 
      (rtc.minute() == AlarmMinute1) && 
          (rtc.second() >= timeSecondMin) && 
            (rtc.second() <= timeSecondMax) && 
              (fanState == 0))
  {
    PurgeFans();
  }

  else

  if ((rtc.hour() == AlarmHour2) && 
       (rtc.minute() == AlarmMinute2) && 
         (rtc.second() >= timeSecondMin) && 
           (rtc.second() <= timeSecondMax) && 
             (fanState == 0))
  {
    PurgeFans();
  }
}

//********************************************************************************************************************

void checkTMPerror()
{
if (CorrectedTemp <= lowTMPalarm)
  { 
    Serial.println("LOW WATER TEMP");
      for (int t=2; t>0; t--)
      {
        lcd2.setCursor(0,1);
        lcd2.print("LOW TEMP!       ");
        digitalWrite(buzzerPin, HIGH);
        delay(500);
        digitalWrite(buzzerPin, LOW);
        lcd2.setCursor(0,1);
        lcd2.print("               "); 
        delay(500);
      }
     lcd2.setCursor(0,1);
     lcd2.print("LOW TEMP!       ");
     digitalWrite(buzzerPin, HIGH);
     delay(500);
     digitalWrite(buzzerPin, LOW);
     delay(1000);
     lcd2.setCursor(0,1);
     lcd2.print("               ");  
  }
  
  else

  if (CorrectedTemp >= highTMPalarm)
    {
     Serial.println("HIGH WATER TEMP");
       for (int t=2; t>0; t--)
       {
         lcd2.setCursor(0,1);
         lcd2.print("HIGH TEMP      ");
         digitalWrite(buzzerPin, HIGH);
         delay(500);
         digitalWrite(buzzerPin, LOW);
         lcd2.setCursor(0,1);
         lcd2.print("               ");   
         delay(500);
       } 
     lcd2.setCursor(0,1);
     lcd2.print("HIGH TEMP!       ");
     digitalWrite(buzzerPin, HIGH);
     delay(500);
     digitalWrite(buzzerPin, LOW);
     delay(1000);
     lcd2.setCursor(0,1);
     lcd2.print("               ");      
    }

    else
    
      PrintLCDtempDisplay();
}

//********************************************************************************************************************

void MainFanControl()
{

if (CorrectedTemp < tempMin)
  {
    fanState = 0;       // meets purge fans requirements
    PWMval = 0;
    fanSpeed = 0;
    digitalWrite(PWMfanPin, LOW);
  } 

  else

  if (CorrectedTemp > tempMax)
    {
      fanState = 1;     // does not meet purge fans requirements
      PWMval = mainFanPWMvalMax;
      fanSpeed = 100;
      digitalWrite(PWMfanPin, HIGH);
    }

    else

    {
      fanState = 1;     // does not meet purge fans requirements
      PWMval = map (CorrectedTemp * 10, tempMin * 10, tempMax * 10, mainFanPWMvalMin, mainFanPWMvalMax);
      fanSpeed = map (CorrectedTemp * 10, tempMin * 10, tempMax * 10, 1, 100);
      analogWrite(PWMfanPin, PWMval);
    }


    Serial.println();
    Serial.print(rtc.day()); Serial.print('/'); Serial.print(rtc.month()); Serial.print('/'); Serial.print(rtc.year());
    Serial.print(" ");
    Serial.print(rtc.hour()); Serial.print(':');
    
    if (rtc.minute() <10) Serial.print("0");
    
    Serial.print(rtc.minute()); Serial.print(':');
    
    if (rtc.second() <10) Serial.print("0");
    
    Serial.print(rtc.second());
    Serial.println();
    
    Serial.print("Water Temp: "); Serial.print(CorrectedTemp); Serial.print("°C");
    Serial.print(", Fan Speed: "); Serial.print(fanSpeed); Serial.print("%, ");
    Serial.print("PWMval: "); Serial.print(PWMval);
    Serial.print(", Fan State "); Serial.print(fanState);
    Serial.println();

}

//********************************************************************************************************************

void runPCBfan()
{
if (PCBtemp < PCBfanTempMin)
  {
    PCBfanPWMval = 0;
    PCBfanSpeed = 0;
    digitalWrite(PCBfanPin, LOW);
  } 

  else

  if (PCBtemp > PCBfanTempMax)
    {
      PCBfanPWMval = PCBfanTempMax;
      PCBfanSpeed = 100;
      digitalWrite(PCBfanPin, HIGH);
    }

    else

    {
      PCBfanPWMval = map (PCBtemp *10, PCBfanTempMin*10, PCBfanTempMax*10, PCBFanPWMvalMin, PCBFanPWMvalMax);
      PCBfanSpeed = map (PCBtemp * 10, PCBfanTempMin * 10, PCBfanTempMax * 10, 1, 100);
      analogWrite(PCBfanPin, PCBfanPWMval);
    }
    
    Serial.print("PCB Temp:   "); Serial.print(PCBtemp); Serial.print("°C");
    Serial.print(", Fan Speed: "); Serial.print(PCBfanSpeed); Serial.print("%, ");
    Serial.print("PWMval: "); Serial.print(PCBfanPWMval);
    Serial.println();
}

//********************************************************************************************************************

void RequestTemperatures()
{
  sensors.requestTemperatures();           
  CorrectedTemp = (sensors.getTempCByIndex(0) + CalibrateTemp);
  PCBtemp = (sensors.getTempCByIndex(1));
}
//********************************************************************************************************************

void setup()
{
  Serial.begin(9600);
  URTCLIB_WIRE.begin();
  sensors.begin();
  
  Serial.println();
  Serial.print("Found ");
  sensorCount = sensors.getDeviceCount();
  
  if (sensorCount == 1)
  {
    Serial.print(sensorCount, DEC);
    Serial.print(" ds18b20 Temperature Sensor.");
  }
  else
  {
    Serial.print(sensorCount, DEC);
    Serial.print(" ds18b20 Temperature Sensors.");
    Serial.println("");
  }

  lcd1.init();lcd2.init();
  lcd1.clear();lcd2.clear();
  lcd1.backlight();lcd2.backlight();
  
  pinMode(2, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  //chnages PWM frequency to eliminate fan noise - pins D9 and D10 - 31.4kHz
  TCCR1A = 0b00000001; // 8bit
  TCCR1B = 0b00000001; // x1 phase correct

  fanState = 0;
  
  PrintLCDTimeDisplay();
  SetTime();
  TimePCBtempResultsLCD();
  PrintLCDtempDisplay();
}

//********************************************************************************************************************

void loop()
{  
  RequestTemperatures();

  MainFanControl();

  runPCBfan();
  
  TempFanResultsLCD();      // prints temp and fan speed to LCD2
  
  checkTMPerror();          // checks for Low/High temp
  
  TimePCBtempResultsLCD();  // prints time and PCB temp to LCD1 
  
  CheckPurgeTimes();

  delay(700);
}
[/code]

For example, here are a two sensors I used in a project.


//*******************************************
//Addresses of the DS18B20s
//
//Freezer = 0x1035B58A0208009B  <-----<<<<   change to the appropriate address
uint8_t freezerAddress[8] = {0x10, 0x35, 0xB5, 0x8A, 0x02, 0x08, 0x00, 0x9B};

//Outside = 0x109CC18A02080023  <-----<<<<   change to the appropriate address
uint8_t outsideAddress[8] = {0x10, 0x9C, 0xC1, 0x8A, 0x02, 0x08, 0x00, 0x23};


    //Freezer temperature
    temperatureFreezerC = sensors.getTempC(freezerAddress);
    Serial.print(temperatureFreezerC);
    Serial.println("ºC");
 
    //Outside temperature
    temperatureOutsideC = sensors.getTempC(outsideAddress);
    Serial.print(temperatureOutsideC);
    Serial.println("ºC");

    //**************************
    //start a new conversion request
    sensors.setWaitForConversion(false);
    sensors.requestTemperatures();
    sensors.setWaitForConversion(true);

@RobMormile
1. Connect DS18B20-1/Sensor-1 (the submerged one).
ds-1xyz

2. Upload the following sketch (not tested) and record its 64-bit (8-byte ROM Code. Attach the address with Sensor-1.

//12-bit default resolution; external power supplyOneWire ds(10); //signal wire
#include<OneWire.h>
OneWire ds(10);
byte addr[8];         //to hold 64-bit ROM Codes of DS1

void setup()
{
  Serial.begin(9600);

  ds.reset();
  ds.search(addr);  //collect 64-bit ROM code from sensor (DS1)

  for (int i = 0; i < 8; i++)
  {
    byte y = addr[i];
    if (y < 0x10)
    {
      Serial.print('0');  //show leading zero
    }
    Serial.print(addr[i], HEX);   //show on I2C LCD
  }
}

void loop() {}

3. Disconnect DS18B20-1 and connect DS18B20-2/Sensor-2 (the open air one)
4. Rerun sketch of Step-2 and record the address and attach it with the Sensor-2.

3. Connect both sensors and upload the following sketch (not tested). Now, you can easily figure out which sensor has failed or malfunctioning.

//12-bit default resolution; external power supply

#include<OneWire.h>
OneWire ds(10);
byte addr1[8];         //to hold 64-bit ROM Codes of DS1
byte addr2[8];        //to ho;d 64-bit ROM-code DS2
byte data[9];        //buffer to hold data coming from DS18B20
float celsius;

void setup() 
{
  Serial.begin(9600);
    
  ds.reset();
  ds.search(addr1);  //collect 64-bit ROM code from sensor (DS1)
  ds.search(addr2);
}

void loop()
{
  probe1();
  delay(1000); //sampling interval
  probe2();
  delay(1000);
}

void probe1()
{
 //----------------------------
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr1); //slect with DS-1 with address addr1
 ds.write(0x44);    //conversion command
 delay(750);   //data ready withinh DS18B20-1 or poll status word
 //---------------------------

 ds.reset();
 ds.select(addr1);  //selectimg the desired DS18B20-1
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
 //---------------------------------

  int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  celsius = (float)raw / 16.0;  //12-bit resolution
  Serial.print("Temp from Sensor-1: ");
  Serial.println(celsius, 2);
}

void probe2()
{
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr2); //slect with DS-2 with address addr1
 ds.write(0x44);    //conversion command
 delay(750);   //data ready withinh DS18B20-2 or poll status word
 //---------------------------

 ds.reset();
 ds.select(addr2);  //selectimg the desired DS18B20
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
  //---------------------------------

  int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  celsius = (float)raw / 16.0;  //12-bit resolution
  Serial.print("Temp from Sensor-2: ");
  Serial.println(celsius, 2);
}

I assume you know that only the metal part is waterproof, the heatshrink part is not.
The sensor will soon fail if you completely submerge it.
Leo..

1 Like

been in there for 2 years bud.

lol like that didnt confuse things.

Do you understand the code offered in post #5 ?

If not, what are you have trouble understanding ?


There will be an example sketch which comes with the DS18B20 library that will display the address of the sensor.

Yes. I forgot this draft from before:

The example multiple.ino is useful for gleaning the addresses of the devices on the bus.

Then you can fix the code to use a method that takes an address instead of indexing into a table built at start up, which is where you go,wrong with the sim,er way of handling multiple sensors.

getTempC((uint8_t*) deviceAddress);

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.