Relay Board + Temperature Sensor (DS18B20) + LCD (with serial printing)

IMG_1366

This sketch requires OneWire  library.

You can download the library here

https://docs.google.com/file/d/0B4qM0hq-OmC1RHhEcXRNSmZ1YW8/

 

#define RELAY1  3 // Relay on digital pin 3
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,10,11,12,13);
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2

void setup(void) {
pinMode(RELAY1, OUTPUT);
Serial.begin(9600);
lcd.begin(16,2);
float temperature = getTemp();
lcd.print(“TEMP   – STATUS”);
}

void loop(void) {
pinMode(RELAY1, OUTPUT);
float temperature = getTemp();
Serial.println(temperature);
lcd.setCursor(0,1);
lcd.print(temperature);
delay(2000); //just here to slow down the output so it is easier to read
if (temperature >32) {
digitalWrite(3,LOW);lcd.print(” RELAY ON “);
} else {
digitalWrite(3,HIGH);lcd.print(” RELAY OFF”);
}
}

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print(“Device is not recognized”);
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

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

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two’s compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}

Advertisements
This entry was posted in Arduino Project. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s