AE EP#53 a DS18B20 based Temperature Sensor Module

Hello, its been a while 🙂 Fortunately the friendly people from icstation.com contacted me to have a look at this DS18B20 breakout board:

Use the code “claics” until the 20th Feb, 2014 to get a 10 % discount on your order!

Basically it is nothing more than that, a breakout board but it is very conveniet to have the filter-cap and pull-up resistor ready in place. The board has vias besides the DS18B20 which should allow it to be soldered to a plate. Also it comes with a handy hole to to screw it down. The package comes with a three-lead jumper wire and is childsplay to connect with arduino.

To get rolling I put this library in my “/usr/share/arduino/libraries/” (Ubuntu) found at playground.arduino.cc/Learning/OneWire. The lib already comes with a fitting example in its examples folder.
But for your copy & paste easy here a stripped-down version:

///based on http://playground.arduino.cc/Learning/OneWire

#include

OneWire ds(2); // initialize onewire to data/out connected on pin 2

int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
byte i;
byte present = 0;//is it there?
byte data[12];//temperature, max resolution
byte addr[8]; //the devices address

void setup(void) {
Serial.begin(9600); //initialize the serial

if ( !ds.search(addr)) {
Serial.print("No more addresses.\n");
ds.reset_search();
return;
}
Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr[i], HEX);//device found at
Serial.print(" ");
}

}

void loop(void) {

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}

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

Serial.print("P=");
Serial.print(present,HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print( OneWire::crc8( data, 8), HEX);
Serial.println();

LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;

if (SignBit) // If its negative
{
Serial.print("-");
}
Serial.print(Whole);
Serial.print(".");
if (Fract < 10)
{
Serial.print("0");
}
Serial.print(Fract);

Serial.print("\n");

}

Just paste into the Arduino IDE and hit that upload button, if connected correctly, data should show up a second or so after you opened Tools/Serial Monitor. Thanks for watching 😀 and give them a shot!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image