I have an Arduino Mini Pro that has a simple sketch that simply reads the temp from A DS18B20. The sketch uploads and runs fine the first time printing out the correct temp until you restart or reset the Arduino. Then the temp prints out as -196.60 I have tried multiple sensors with the same result.
The DS18B20 is connected Red to +5v, Black to Ground and Yellow (signal) is connected to Red by 4.7K resistor and to pin 6 on the Mini.
Thanks for the reply. That didn't do the trick. After looking through the DallasTemperature.cpp source I found this:
// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_F is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
and in DallasTemperature.h I found this: #define DEVICE_DISCONNECTED_F -196.6
It sounds like you are not allowing the sensor time to do its conversion after power-up. In short, your sketch is too simple. Just add
delay(1000) in the setup
I think it is working the first time because it manages to scratch its bum while you are uploading. This is not a problem, merely a charcteristic of the sensor, and the type of Arduino is immaterial.
This issue will go away as your setup gets more comprehensive. Something like a display comment or testing an SD card card will probably cover it.
Thank you Nick. I tried that first. I've put delays ranging from 100ms to 5000ms at the beginning and during each loop and I still get the same response.
1000 should suffice for both. If it is left out of the setup, you might get -85 for the first reading but subsequent readings will be OK. I'm afraid I misread your original, and I now think something else is going on.
It only reads correctly after uploading the sketch. Any time I power up after that I get the -196.60 reading.
Are you saying that the only time you get a reading is when you upload?
And you never get a reading at any other time?
If yes to the first, does it then continue to work OK?
If yes to the second, what is going on that is different, other than merely uploading the programme?
I can't help feeling this has something to do with the power supply, but I would expect a -127 error. I don't know how those error messages are generated. Perhaps they are done by the library.
I've tried your code using a DS28B20 on pin 6 of a NANO and I cannot make it fail.
Actually, you are getting -127 for a temperature reading. The DS18B20 reports temperature in Celsius (-127) but you have then asked the library to convert to Fahrenheit (-196.6).
-127 is always an indication of bad wiring (or a dead DS18B20). If you sometimes get a reading, the DS18B20 is good so your wiring is bad. Make sure that you have solid connections to all three leads.
I have now tried this with 2 of the Pro Micro ATmega32U4 (Leonardo) units with the same results. I have also tried grounding pins 1 & 3 (VCC and GND) as suggested. Grounding 1 & 3 immediately returns the -196.60 value even after reloading the sketch.
You must have a bad, intermittent connection. When the temperature prints as -196.6 it means that your code is not seeing the DS18B20 at all.
Try running this onewire scanner a few times. It prints out the rom address of any DS18B20 it finds on pin 6.
// This sketch looks for 1-wire devices and
// prints their addresses (serial number) to
// the UART, in a format that is useful in Arduino sketches
// Pete El_Supremo
#include <OneWire.h>
#define ONEWIRE_PIN 6
OneWire ds(ONEWIRE_PIN);
void setup(void) {
Serial.begin(9600);
while(!Serial);
delay(2000);
discoverOneWireDevices();
}
void discoverOneWireDevices(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
Serial.print("Looking for 1-Wire devices on pin ");
Serial.println(ONEWIRE_PIN);
while(ds.search(addr)) {
// Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i] < 16) {
Serial.print('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
Serial.println("");
}
Serial.print("\nThat's all.\n");
ds.reset_search();
}
void loop(void) {
// nothing to see here
}
Here's what it prints when I have two DS18B20 on pin 6 of a NANO.
Looking for 1-Wire devices on pin 6
0x28, 0xE1, 0xA3, 0x00, 0x04, 0x00, 0x00, 0xE3
0x28, 0x25, 0xAC, 0x00, 0x04, 0x00, 0x00, 0xCF
That's all.
I'm thinking that the DS chips are not reseting.
The specs say that one can hold the clock ( or is it data? )
and all the devices are requited to reset. I don't think the
library code is doing that.
In your init, before anything else, force this type of reset
to the DS chips.
If that doesn't work, you may have to add an external
FET or relay to power the chips down.
Dwight