I have two DS18B20 temperature sensors connected to my Mega 2560.
Both are 'external' powered using the 5v and Gnd from the Arduino board.
Both sensor data pins are connected together, to a single 4K7 resistor tied to 5v.
They are both working, but often return a zero value. My serial monitor displays (example) :
Reading One-Wire Sensors
Outside temperature is: 16.00
Inside temperature is: 0.00
Reading One-Wire Sensors
Outside temperature is: 16.00
Inside temperature is: 19.50
Reading One-Wire Sensors
Outside temperature is: 0.00
Inside temperature is: 19.50
Reading One-Wire Sensors
Outside temperature is: 16.00
Inside temperature is: 19.50
Reading One-Wire Sensors
Outside temperature is: 16.00
Inside temperature is: 0.00
As you can see, it is not specific to an individual sensor.
Length of the wires to the sensors is about 1m (inside) and 8m (outside).
My code is reading the sensors and ( IF the temp. changes and IF the temp. > 0 ) displaying it on an LCD screen.
So the question : is this a normal situation for these sensors, is it likely a hardware problem, or an error in the following code :
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
//pin numbers for LCD display
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//special LCD character for degrees symbol
byte newChar[8] = {B00111,B00101,B00111,B00000,B00000,B00000,B00000,B00000};
#define ONE_WIRE_BUS 13 // Temperature Data wire is plugged into pin 13 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress outsideThermometer = { 0x28, 0x3C, 0xC0, 0x08, 0x03, 0x00, 0x00, 0x54 };
DeviceAddress insideThermometer = { 0x28, 0xCC, 0xD1, 0xE1, 0x02, 0x00, 0x00, 0xE6 };
float Tmax = 0, Tmin = 99, LastTempC1 = -1, LastTempC2 = -1;
float tempC1 = 0, tempC2 = 0;
long intervalTemp = 5000;
long pMilTemp = 0;
void LCDpos(int Lcol, int Lrow) {
lcd.setCursor(Lcol, Lrow);
if(Lrow > 1) lcd.setCursor(Lcol + 16, Lrow - 2);
}
void LCDtemps(float tempC1, float tempC2, float Tmax, float Tmin){
LCDpos(0,0);
lcd.print("Inside = ");
if (tempC2 > 1){
lcd.print(tempC2); lcd.write(0); lcd.print(" ");
}
LCDpos(0,1);
lcd.print("Outside = ");
if (tempC1 > 1){
lcd.print(tempC1); lcd.write(0); lcd.print(" ");
}
LCDpos(0,2);
lcd.print("Max = ");
if (Tmax > 1){
lcd.print(Tmax); lcd.write(0); lcd.print(" ");
}
LCDpos(0,3);
lcd.print("Min = ");
if (Tmin > 1){
lcd.print(Tmin); lcd.write(0); lcd.print(" ");
}
}
float readTemperature(DeviceAddress deviceAddress)
{
float tempC = 0;
tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
return 0;
} else {
return tempC;
}
}
void setup(void)
{
lcd.createChar(0, newChar);
lcd.begin(16, 4);
Serial.begin(9600);
sensors.begin(); // Start up the library
// set the resolution to 10/11/12 bit
sensors.setResolution(outsideThermometer, 11);
sensors.setResolution(insideThermometer, 11);
LCDtemps (0, 0, 0, 0);
}
void loop(void)
{
unsigned long cMil = millis();
if(cMil - pMilTemp >= intervalTemp) {
pMilTemp = cMil;
Serial.print("Reading One-Wire Sensors\n\r");
sensors.requestTemperatures();
float tempC1 = readTemperature(outsideThermometer);
float tempC2 = readTemperature(insideThermometer);
Serial.print("Outside temperature is: ");
Serial.print(tempC1);
Serial.print("\n\r");
Serial.print("Inside temperature is: ");
Serial.print(tempC2);
Serial.print("\n\r");
if (tempC1 != LastTempC1){
if (tempC1 > 1){
LastTempC1 = tempC1;
if (tempC1 < Tmin) Tmin = tempC1;
if (tempC1 > Tmax) Tmax = tempC1;
LCDtemps (tempC1, tempC2, Tmax, Tmin);
}
}
if (tempC2 != LastTempC2){
if (tempC2 > 1){
LastTempC2 = tempC2;
LCDtemps (tempC1, tempC2, Tmax, Tmin);
}
}
}
}