DIY: Arduino Thermostat With the DS18B20

 

 

UPDATE 2/26/13

We have added a forum to our site for Q and A. Please post your questions and comments there.

 

UPDATE 1/30/13

Fixed download links for sketches

 

UPDATE 1/13/13

Updated tutorial using an I2C to interface the LCD to the Arduino, saving some of those much needed digital ports for other useful expansions and improvements. Actually, I just added to this one. Read on and I hope you find it helpful. Leave me some comments or reach me on facebook, or google +.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 

UPDATE:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Here is an updated breadboard design. Very easy to read. I hope it helps. If you have questions…. Please Ask!

Added Code for finding the address of a DS18B20 Temerature sensor.

UPDATE 12/8/2012

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

I was going to carry this tutorial further but have decided go a slightly different route and use a IIC/I2C/TWI/SP​​I Serial Interface to reduce the number of digital ports used on my Arduino for future expansion. Read further for the Updated code and improvements.

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

I built this using the Arduino Uno Rev 3, relay shield from seeedstudio, Dallas DS18B20, and a 16×2 LCD.

Hardware Required:

Arduino Uno Rev 3

Relay Shield

Dallas DS18B20 One Wire Temperature Sensor (Datasheet)

16×2 LCD (Datasheet)

I2C interface ( you could just buy an LCD that already has an I2C interface on it)

10 K Ohm Potentiometer

2 Single Pole Single Throw Rocker Switches

Red LED indicator for heating mode

Blue LED indicator for cooling mode

Green LED indicator for whether the thermostat is on or off.

2 10 K Ohm Resistors

1 4.7 K Ohm Resistor

1 10 K Ohm Trimmer Potentiometer

Wire

You need to install the libraries for the Dallas DS18B20 and also for using the LCD with an I2C. You can get them both here. You will also need to rename or move the original LiquidCrystal library if you are going to use an I2C with your LCD. If you don’t know how to work with libraries please visit arduino.cc.

This is all I have for now but I will be updating very soon!

Use this code to find the address of your DS18B20 Sensor.Download the Sketch Here

DS18B20 Address Finder::::

// This sketch looks for 1-wire devices and

// prints their addresses (serial number) to

// the UART, in a format that is useful in Arduino sketches

// Tutorial:

// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

#include <OneWire.h>

OneWire ds(3); // Connect your 1-wire device to pin 3

void setup(void) {

Serial.begin(9600);

discoverOneWireDevices();

}

void discoverOneWireDevices(void) {

byte i;

byte present = 0;

byte data[12];

byte addr[8];

Serial.print(“Looking for 1-Wire devices…\n\r”);

while(ds.search(addr)) {

Serial.print(“\n\rFound \’1-Wire\’ device with address:\n\r”);

for( i = 0; i < 8; i++) {

Serial.print(“0x”);

if (addr[i] < 16) {

Serial.print(’0′);

}

Serial.print(addr[i], HEX);

if (i < 7) {

Serial.print(“, “);

}

}

if ( OneWire::crc8( addr, 7) != addr[7]) {

Serial.print(“CRC is not valid!\n”);

return;

}

}

Serial.print(“\n\r\n\rThat’s it.\r\n”);

ds.reset_search();

return;

}

void loop(void) {

// nothing to see here

}

 

Thermostat Code without the I2C interface:::: Download the Sketch Here

 

/* A fair portion of the code here is from Arduinotronics and I would have been lost with out it. This is my first “big” Arduino project. So, there is probably a ton of things that can be improved on and tweeked. I would love to know your thoughts and see your improvements! If you would like to share your thoughts with me on this please email me at modsbyus at modsbyus dot com and make the subject line RE: Arduino Thermostat Thoughts, or write a comment on the tutorial at http://www.modsbyus.com/diy-arduino-thermostat. Required library for the DS18B20 can be downloaded here.. http://63.160.63.15/public.php?service=files&file=/admin/files/Modsbyus/libraries.rar/ */

#include <OneWire.h> /*This temperature sensor requires a 4.7k Ohm resistor across its pins 2 and three!!!! */

#include <DallasTemperature.h>

#include <LiquidCrystal.h>

int sensorPin = A0; // select the input pin for the 10K potentiometer

int sensorValue = 0; // variable to store the value coming from the sensor

int setTemp = 0; // variable to store temp desired

int SSRCPin = 5; //Turn on A/C unit

int SSRHPin = 6; //Turn on heat (electric or gas)

int hcLED = 4; //indicator for Cooling mode

int SwitchPin = 1; // To switch between Cooling and Heating

int SSRFan = 7; // To turn on and off the air handler fan

float currentTemp = 0; //Store the current tempurature

// LCD Wires from LCD/to Arduino and other components

// 1 to GND

// 2 to 5V

// 3 to Trimmer Pot Reference Pin

// 4 to Arduino Pin 9

// 5 to GND

// 6 to Arduino Pin 10

// 11 to Arduino Pin 11

// 12 to Arduino Pin 12

// 13 to Arduino Pin 13

// 14 to Arduino Pin 8

// 15 to 5V

// 16 to GND

//Heat relay to Arduino pin 4

//Cooling relay to Arduino pin 5

//Fan relay to Arduino pin 6

//LEDs relay to Arduino pin 7

//DS18B20 to Arduino pin 2

//Heat/Cool switch to Arduino pin 1

LiquidCrystal lcd(9, 10, 11, 12, 13, 8);

/*This temperature sensor requires a 4.7k Ohm resistor across its pins 2 and three!!!! Thats the middle pin and the VDD pin */

// Data wire is plugged into pin 2 on the Arduino

#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.

DallasTemperature sensors(&oneWire);

DeviceAddress insideThermometer = { 0×28, 0xF0, 0xEA, 0×73, 0×03, 0×00, 0×00, 0xCF };

// DeviceAddress outsideThermometer = { 0×28, 0×20, 0×04, 0xA8, 0×02, 0×00, 0×00, 0x4D };

void setup(void)

{

// Start up the library

sensors.begin();

// set the resolution to 9 bit (good enough?)

sensors.setResolution(insideThermometer, 9);

// sensors.setResolution(outsideThermometer, 9);

lcd.begin(16,2); // columns, rows. use 16,2 for a 16×2 LCD, etc.

pinMode(SSRFan, OUTPUT); //set air handler fan pin as output

digitalWrite(SSRFan, LOW);//Set the fan relay pin normally low (off)

pinMode(SSRHPin, OUTPUT);//Set the heat relay pin as an output

digitalWrite(SSRHPin, LOW);//Set the heat relay pin as normally low (off)

pinMode(SSRCPin, OUTPUT);//Set the cooling relay pin as an output

digitalWrite(SSRCPin, LOW);//Set the cooling relay pin as normally low (off)

pinMode(hcLED, OUTPUT);//Set the indicator LED pin as an output

digitalWrite(hcLED, LOW);//Set the LED indicator pin as normally low (cooling indication)

pinMode(SwitchPin, INPUT);//Set the switch pin as an input

}

void printTemperature(DeviceAddress deviceAddress)

{

sensors.requestTemperatures(); //Get temperature from DS18B20

float tempC = sensors.getTempC(deviceAddress);

if (tempC == -127.00) {

lcd.print(“Error”);

} else {

// lcd.print(tempC);

// lcd.print(“/”);

currentTemp = (DallasTemperature::toFahrenheit(tempC));

lcd.print(currentTemp);

}

}

void loop(void)

{

delay(500);

sensorValue = analogRead(sensorPin); //Set the variable ‘sensorValue’ as and analog read of the sensor pin

Page 1 of 4 | Next page