Hi all,
I have two DS18B20 digital temp sensors hooked up to my Arduino (for indoor and outdoor temps) which then displays each onto my 20x4 LCD.
All works nicely, except occasionally, very occasionally I'll get bogus readings for one or both of the sensors. Usually the reading is -127.00C but occasionally it'll jump up to 85.00C.
Now the outdoor sensor is attached to a piece of cat5 cable which is around 5 metres long, so I thought maybe this was the problem. But I'm wondering if there's any way I can code it to "ignore" these readings?
I wouldn't be so fussed if it didn't affect the LCD's readout, i.e.:
The display shows "20.50c" for instance, but when the -127.00 reading occurs, it writes these chars to the LCD which don't get 'erased' so the next correct reading will show up as "20.50c0" for example.
I hope that makes sense. I'm a total Arduino and programming noob so these questions may be very dumb. And here's my code for you to all laugh at:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
int reLED=13;
int blLED=12;
int grLED=11;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
#define CONTRAST_PIN 9
#define BACKLIGHT_PIN 7
#define CONTRAST 100
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void){
pinMode(reLED, OUTPUT);
pinMode(blLED, OUTPUT);
pinMode(grLED, OUTPUT);
pinMode(CONTRAST_PIN, OUTPUT);
pinMode(BACKLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, HIGH);
analogWrite (CONTRAST_PIN, CONTRAST);
lcd.begin(4,20); // initialize the lcd
lcd.setCursor(0,0);
lcd.print("----TEMPERATURES----");
lcd.setCursor(0,1);
lcd.print("Outdoors is");
lcd.setCursor(0,2);
lcd.print("Indoors is");
lcd.setCursor(0,3);
lcd.print("====================");
// Start up the library
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
}
void loop(void)
{
float outdoors = sensors.getTempCByIndex(0);
float indoors = sensors.getTempCByIndex(1);
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
lcd.setCursor(12,1);
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(12,2);
lcd.print(sensors.getTempCByIndex(1));
lcd.setCursor(0,1);
lcd.print("Outdoors is");
lcd.setCursor(0,2);
lcd.print("Indoors is");
lcd.setCursor(17,1);
lcd.print ("c");
lcd.setCursor(17,2);
lcd.print ("c");
if (indoors>= 25)
{
digitalWrite (reLED, HIGH);
digitalWrite (grLED, LOW);
}
else
{
digitalWrite (reLED, LOW);
digitalWrite (grLED, HIGH);
}
if (outdoors< 1.0)
{
digitalWrite (blLED, HIGH);
}
else
{
digitalWrite (blLED, LOW);
}
delay(5000);
}
Oh I should add that if I power it from USB the bogus readings are frequent, but if I power it from my usual source of a 12v wall wart, they're very infrequent. I left it running all evening and it was fine but I got up this morning and saw one