I posted a question regarding the DS18B20 digital temperature sensor a while back and wanted to share what worked for me. I didn’t want to daisy-chain multiple sensors on a single bus due to complexities with figuring out which address belonged to which chip on the bus. I had previously been having trouble getting consistently accurate readings. This code, while perhaps not the most elegant, is working reliably.
#include <DS18B20.h>
const int MAXRETRY = 5 ;
int delayTime = 16000 ;
String sout = " ";
String tempData = " ";
float temp_Supply ;
float temp_Return1 ;
float temp_Return2 ;
float temp_Return3 ;
float ToFahrenheit(float TempIn) ;
DS18B20 Supply_obj(D0,TRUE) ;
DS18B20 Return_obj1(D1,TRUE) ;
DS18B20 Return_obj2(D2,TRUE) ;
DS18B20 Return_obj3(D3,TRUE) ;
void setup() {
pinMode(D0, INPUT) ;
pinMode(D1, INPUT) ;
pinMode(D2, INPUT) ;
pinMode(D3, INPUT) ;
}
void loop() {
int i = 0 ;
do {
delay(1000) ;
temp_Supply = Supply_obj.getTemperature() ;
temp_Supply = ToFahrenheit(temp_Supply) ;
} while (!Supply_obj.crcCheck() || MAXRETRY > i++) ;
// Note above the while condition from another post mistakedly used && when || was needed
i = 0 ;
do {
delay(1000) ;
temp_Return1 = Return_obj1.getTemperature() ;
temp_Return1 = ToFahrenheit(temp_Return1) ;
} while (!Return_obj1.crcCheck() || MAXRETRY > i++) ;
i = 0 ;
do {
delay(1000) ;
temp_Return2 = Return_obj2.getTemperature() ;
temp_Return2 = ToFahrenheit(temp_Return2) ;
} while (!Return_obj2.crcCheck() || MAXRETRY > i++) ;
i = 0 ;
do {
delay(1000) ;
temp_Return3 = Return_obj3.getTemperature() ;
temp_Return3 = ToFahrenheit(temp_Return3) ;
} while (!Return_obj3.crcCheck() || MAXRETRY > i++) ;
// I have omitted the output code because it's not central to the topic, but put it here
delay(delayTime) ;
}
float ToFahrenheit(float TempIn) { // Doing the conversion loacally
float RetVal ;
RetVal = ((TempIn*1.8)+32) ;
return RetVal ;
}