DS18B20 Temp Sensor reads correctly until arduino is rebooted

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 any help!

Bill

#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>

OneWire oneWire(6);
DallasTemperature sensors(&oneWire);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(“Program Start”);

//initialized temp sensors
sensors.begin();

}

void loop() {
// put your main code here, to run repeatedly:

sensors.requestTemperatures();
float temp;
temp = sensors.getTempFByIndex(0);
Serial.print("Now sending ");
Serial.println(temp);

}

Add sensors.setWaitForConversion(true); after sensors.begin();

Pete

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

I guess it can’t read the devices scratchpad ?

An interesting question is: why does it work the first time? After uploading, the Arduino is effectively rebooted.

Do you disconnect/reconnect something to cause the reboot?

I am just removing the power, wait 30 seconds, restore power.

Just realized that this is NOT a Pro Mini as first reported. This is a Pro Micro ATmega32U4.

What do you mean by "removing the power"?

Are you unplugging it from the USB? If so, that does much more than just remove the power.

Yes, I am unplugging the USB cable from my lap top. This also happens when I hook it up to a battery or power supply.

It only reads correctly after uploading the sketch. Any time I power up after that I get the -196.60 reading.

I appreciate your interest and feedback. This is part of a larger project. I have used these temp sensors before and never had any trouble.

It sounds like you are not allowing the sensor time to do its conversion after power-up. In short, your sketch is [u]too[/u] 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.

Pete

Why is the pullup resistor connected to another pin. Why not connect it to the +5V? When you do a chip reset, you lose the pullup. Dwight

Thank you for your input Dwight. Yes, it is connected to +5 and Data which connects to Pin 6. My connections are similar to: |500x188

But on Pin 6 NOT pin 2 (also this is NOT an ATMEGA)

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.

Pete

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