Archive

Posts Tagged ‘temperature’

Reading temperature with Maxim DS18x20

February 19th, 2009 aprendiz 2 comments

Its really easy to use the ds18x20 family of temperature sensors of Maxim. You can get a pair of them for free from Maxim.

The sensors use a one-wire protocol, only one wire for data and a pair more for gnd and voltage (even only one from data and other for gnd in parasit mode). Each sensor has an unic ID.
You need to search the “network” (filtering only the required sensor type) in order to get all the ids and then ask each one for the current temperature value.

You can send the data to the pc using the serial port.

Here is the example:

#include
/* OneWire Arduino
code from here */

/* DS18S20 Temperature chip i/o */

OneWire ds(10); // on pin 10

void setup(void) {
Serial.begin(57600);
}

void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
ds.reset_search();
return;
}

Serial.print("R");
for( i = 0; i < 8; i++) {
Serial.print("-");
if(addr[i]<16)
Serial.print(0);
Serial.print(addr[i], HEX);
}

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

if ( addr[0] != 0x10) {
Serial.print("Device is not a DS18S20 family device.\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

for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int HighByte=data[0];
int LowByte=data[1];

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

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

Serial.print(":");
Serial.print(HighByte/2);
Serial.print(".");
Serial.print(LowByte/2);
Serial.print(":");
Serial.print(TReading);

 

Serial.println();
}

More references: Arduino playground, arduino forum & phanderson