Arduino DS18B20 1Wire

A ideia surgiu como meio para verificar se a temperatura de um quarto estava correcta perante um termómetro de mercúrio.

Arranjou-se um LCD Alfanumérico de 20×4 caracteres, de um FAX danificado , mas por sorte o LCD estava OK e obtiveram-se algumas amostras de sensores de temperatura 1Wire. O objectivo seria portanto visualizar informação da temperatura no LCD.

Este foi o resultado obtido, usando as bibliotecas do Arduino :

As bibliotecas do LCD e 1Wire podem ser obtidas aqui:

LiquidCrystal Source

One Wire Library
E este é o código-fonte:




/*
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);
}

Arduino DS18B20 1Wire

Deixe uma resposta

Voltar ao topo