I just got an DS18B20 Temperature sensor and am trying to modify the example code to use a function instead. Below is my getTemp
function. It's working but returning results with a 0 value part of the time for example:
Temperature = 72.39
Temperature = 0.00
If I add Serial.print(fahrenheit);
within the function I am always provided with the correct temperature. I though this may have to do with needing a delay but adding delay(5000);
in my loop didn't help.
I'm assuming it's something simple I'm not understanding about how floats
return.
#include <OneWire.h>
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
void setup(void) {
Serial.begin(9600);
}
float getTemp(char tScale) {
byte i;
byte present = 0;
byte type_s = 0;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return false;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
if (tScale == 'C') {
return celsius;
}
else if (tScale == 'F') {
return fahrenheit;
}
}
void loop(void) {
Serial.print("Temperature = ");
Serial.println( getTemp( 'F' ) );
}
Edit:
Interesting enough, adding an if
statement to the loop fixes this, however. I'd still like to understand what is going on:
void loop(void) {
if ( getTemp( 'F' ) == 0 ){
Serial.print( "Temperature = " );
Serial.println( getTemp( 'F' ) );
}
}
0
, it's clear that you filter for zero by checking forgetTemp('F') == 0
.getTemp
is set to 0 then the correct temperature is being displayed to serial (never showingTemperature = 0.00
It only returns the actual temperature.!ds.search(addr)
(by just polling the sensor twice in close succession), ignoring the underlying problem. :)