Pages: [1]   Go Down
Author Topic: SOLVED : DS18B20 returns Zero - sensor or code - or normal ?  (Read 769 times)
0 Members and 1 Guest are viewing this topic.
South Africa
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

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);
    }
  }
}
}
« Last Edit: July 27, 2011, 03:52:18 pm by DaveO » Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see you are using pin 13 for your I/O. If it has the normal resistor-LED to ground still attached. That could be a problem. Moving your sensors to a different pin would be a quick test of that. 8 m is not a short cable run. You could try a lower value for your pull-up resistor to help compensate for the additional cable capacitance.

 I'll have to visit the one-wire library again because it has been 2 years since I used it and there were many problems/inconsistencies with it then. So, if the above doesn't work, we might want to take a look at the code.
Logged

South Africa
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

EmilyJane - Thank You

On your suggestion, I moved the temp. sensor data wire from pin 13 to pin 11, modified the code, and never got another zero reading again.

Great solution. Thanks again.
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're quite welcome! I'm glad it was something simple.
Logged

South Africa
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm glad it was something simple.

not as glad as I was.
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I hear that! 1-wire problems can be difficult to troubleshoot.
Logged

Pages: [1]   Go Up
Jump to: