Arduino DS18B20 1Wire

April 25th, 2011 by Fábio Souza | Posted under Arduino, Blog, Electronics.

The idea arose as a means to verify that the temperature of a room was correct before a Mercury thermometer.

Arranged an alphanumeric LCD 20×4 characters, a damaged FAX , but luckily the LCD was OK and there were some samples of 1Wire temperature sensors. The aim would therefore show LCD temperature information.

This was the result obtained, using the Arduino libraries :

LCD and 1Wire libraries can be obtained here:

LiquidCrystal Source

One Wire Library
And this is the source code:




/*
Thermometer Ds18B20 and LCD

*/

#include
#include

#define DS18S20_ID 0x10
#define DS18B20_ID 0x28
float temp;
LiquidCrystal lcd(11, 12, 5, 4, 3, 2);

OneWire ds(10);

boolean getTemperature(){
byte i;
byte present = 0;
byte data[12];
byte addr[8];

//find a device
if (!ds.search(addr)) {
ds.reset_search();
return false;
}
if (OneWire::crc8( addr, 7) != addr[7]) {
return false;
}
if (addr[0] != DS18S20_ID && addr[0] != DS18B20_ID) {
return false;
}
ds.reset();
ds.select(addr);
// Start conversion
ds.write(0x44, 0); //No parasite mode, Feed it
// Wait some time...
// delay(850);
present = ds.reset();
ds.select(addr);
// Issue Read scratchpad command
ds.write(0xBE);
// Receive 9 bytes
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
}
// Calculate temperature value
temp = ( (data[1] << 8 ) + data[0] )*0.0625;
return true;

}

void setup(void){

lcd.begin(20, 4);

Serial.begin(9600);

}

void loop(void)
{

if(getTemperature()==1)
{
lcd.setCursor(0, 0);
lcd.print("Temp: ");

lcd.print(temp, 2);

Serial.print("Temp: " );
Serial.println(temp, 2);

}



delay(500);
}

Tags: ,