Professional Documents
Culture Documents
com
Modsbyus.com
ARDUINO
UPDATE 8/8/15
This post still gets quite a few views every day. If you build something using this post I would love to see
what you’ve made. Please send pictures of your completed projects to modsbyus@modsbyus.com along
with any suggestions or comments you may have. I will be making a post to show them off.
UPDATE 4/21/13
Added fan only option to thermostat in code and added a switch to control it..
Cleaned up code
http://www.modsbyus.com/diy-arduino-thermostat/ 1/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
UPDATE 1/30/13
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. Read on and I hope you nd it
helpful. Leave me some comments or reach me on facebook, or google +.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
UPDATE:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
If you have questions…. Please Ask!
Added Code for nding the address of a DS18B20 Temerature sensor.
UPDATE 12/8/2012
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I have decided to use a IIC/I2C/TWI/SPI Serial Interface to reduce the number of digital ports used on
my Arduino for future expansion. Read further for the Updated code and improvements.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
This tutorial assumes you know how to properly hook up switches to the arduino and otherwise have
some basic knowlege of how electricity works.
I will be adding a full physical connections tutorial for this thermostat in the near future.
I built this using the Arduino Uno Rev 3, relay shield from seeedstudio, Dallas DS18B20, and an 16×2
LCD.
Hardware Required:
Arduino Uno Rev 3
Relay Shield
http://www.modsbyus.com/diy-arduino-thermostat/ 2/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
I2C interface ( you could just buy an LCD that already has an I2C interface on it)
1 10 K Ohm Potentiometer
2 Single Pole double Throw Rocker Switches
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 nd the address of your DS18B20 Sensor.Download the Sketch Here
#include <OneWire.h>
void setup(void) {
Serial.begin(9600);
discoverOneWireDevices();
}
http://www.modsbyus.com/diy-arduino-thermostat/ 3/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
void discoverOneWireDevices(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
void loop(void) {
// nothing to see here
}
Thermostat Code without the I2C interface:::: Download the Sketch Here
http://www.modsbyus.com/diy-arduino-thermostat/ 4/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
/* A fair portion of the code here is from Arduinotronics and I would have been lost with out it.
This is my rst “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://cloud.modsbyus.com/public.php?
service= les&t=82892f20b1296b9557303d6dc60b2f5a/ */
#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
oat 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
http://www.modsbyus.com/diy-arduino-thermostat/ 5/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
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);
http://www.modsbyus.com/diy-arduino-thermostat/ 6/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
void printTemperature(DeviceAddress deviceAddress)
{
}
void loop(void)
{
delay(500);
sensorValue = analogRead(sensorPin); //Set the variable ‘sensorValue’ as and analog read of the
sensor pin
setTemp = sensorValue / 10.24; //Gives us a set temp range between 0 and 99 degrees
http://www.modsbyus.com/diy-arduino-thermostat/ 7/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
//Cooling Mode
int val = digitalRead(SwitchPin) ;// val represents digitalRead(SwitchPin);
/* If the value of is equal to 1 (high) then make hcLED low (off) which sets the relay in a normally
closed state. Hence, turning on the blue LED. */
if (val == 1)
{
digitalWrite(hcLED, LOW);//Set the heating/cooling LED indicator to LOW if ‘val’ is equal to 1
}
/* If the SwitchPin reads 1 and the current temperature is greater than the set temperature (if its
hot) turn on the A/C and internal fan */
if (val == 1 && (currentTemp > setTemp))
{
digitalWrite(SSRFan, HIGH);//Set the fan relay high
digitalWrite(SSRHPin, LOW);//Set the heat relay low
digitalWrite(SSRCPin, HIGH);//Set the cooling relay high
}
/* Otherwise, if the SwitchPin reads 1 and the current temperature is less than the set
temperature (the set temperature has been reached), turn off the A/C and internal fan */
else if (val == 1 && (currentTemp < setTemp))
{
digitalWrite(SSRCPin, LOW);//Set the cooling relay low
digitalWrite(SSRFan, LOW);//Set the fan relay low
}
// Heating Mode
/* If the value of is 0 then make hcLED HIGH (on) which sets the relay in a normally open state.
Hence, turning on the RED LED */
if (val == 0)
{
digitalWrite(hcLED, HIGH);//Set the heating/cooling led high
}
http://www.modsbyus.com/diy-arduino-thermostat/ 8/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
/* If the SwitchPin reads 0 and the current temperature is less than the set temperature (if its
cold) turn on the HEAT and internal fan */
if (val == 0 && (currentTemp < setTemp))
{
digitalWrite(SSRFan, HIGH);//Set the fan relay high
digitalWrite(SSRCPin, LOW);//Set the cooling relay low
digitalWrite(SSRHPin, HIGH);//Set the heat relay high
}
/* If the SwitchPin reads 0 and the current temperature is greater than the set temperature (the
set temperature has been reached) turn off the HEAT and internal fan */
else if (val == 0 && (currentTemp > setTemp))
{
digitalWrite(SSRHPin, LOW);//Set the heat relay low
digitalWrite(SSRFan, LOW);//Set the fan relay low
}
You will need to add some things to the Relay Shield. A couple of pictures should do. Add some female
headers and a couple of jumpers. The jumper wire on the right in the bottom picture is to bridge all the
common terminals together for the 24 volts coming from the HVAC unit. The longer wire is taking
ground to the common terminal for the LEDs used for indicating heat and cool mode.
http://www.modsbyus.com/diy-arduino-thermostat/ 9/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
/* A fair portion of the code here is from Arduinotronics and I would have been lost with out it.
This is my rst “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 libraries for the DS18B20 and the
I2C LCD can be downloaded here.. http://cloud.modsbyus.com/public.php?
service= les& le=/admin/ les/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 <Wire.h>
#include <LCD.h>
#include <liquidcrystal_i2c.h> // F Malpartida’s NewLiquidCrystal library
#de ne I2C_ADDR 0x20 // De ne I2C Address where the PCF8574A is
#de ne BACKLIGHT_PIN 7
#de ne En_pin 4
#de ne Rw_pin 5
#de ne Rs_pin 6
#de ne D4_pin 0
#de ne D5_pin 1
#de ne D6_pin 2
#de ne D7_pin 3
http://www.modsbyus.com/diy-arduino-thermostat/ 10/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
#de ne LED_OFF 0
#de ne LED_ON 1
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
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
/*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
#de ne ONE_WIRE_BUS 2
http://www.modsbyus.com/diy-arduino-thermostat/ 11/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
void setup(void)
{
// Start up the library
sensors.begin();//Start the DS18B20 sensor
// set the resolution to 9 bit (good enough?)
sensors.setResolution(insideThermometer, 9);
// sensors.setResolution(outsideThermometer, 9);
void printTemperature(DeviceAddress deviceAddress)
{
http://www.modsbyus.com/diy-arduino-thermostat/ 12/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
void loop(void)
{
delay(500);//Delay for 1/2 second
setTemp = sensorValue / 10.24; //Gives us a set temp range between 0 and 99 degrees
//Cooling Mode
/* If the SwitchPin reads 1 and the current temperature is greater than the set temperature (if its
hot) turn on the A/C and internal fan */
if (val == 1 && (currentTemp > setTemp))
{
digitalWrite(SSRFan, HIGH);//Set the fan relay pin high
digitalWrite(SSRHPin, LOW);//Set the heat relay pin low
http://www.modsbyus.com/diy-arduino-thermostat/ 13/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
/* Otherwise, if the SwitchPin reads 1 and the current temperature is less than the set
temperature (the set temperature has been reached), turn off the A/C and internal fan */
else if (val == 1 && (currentTemp < setTemp))
{
digitalWrite(SSRCPin, LOW);//Set the cooling relay pin low
digitalWrite(SSRFan, LOW);//Set the fan relay low
}
// Heating Mode
/* If the value of is 0 then make hcLED HIGH (on) which sets the relay in a normally open state.
Hence, turning on the RED LED */
if (val == 0)
{
digitalWrite(hcLED, HIGH);//Set the heating/cooling indicator LED high
}
/* If the SwitchPin reads 0 and the current temperature is less than the set temperature (if its
cold) turn on the HEAT and internal fan */
if (val == 0 && (currentTemp < setTemp)) { digitalWrite(SSRFan, HIGH); digitalWrite(SSRCPin,
LOW); digitalWrite(SSRHPin, HIGH); } /* If the SwitchPin reads 0 and the current temperature is
greater than the set temperature (the set temperature has been reached) turn off the HEAT and
internal fan */
/blockquote>
/* A fair portion of the code here is from Arduinotronics and I would have been lost with out it.
This is my rst “big” Arduino project. So, there is probably a ton of things that can be improved on
http://www.modsbyus.com/diy-arduino-thermostat/ 14/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
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/ */
#include <OneWire.h> //This temperature sensor requires a 4.7k Ohm resistor across its pins //2
and three!!!!
#include <DallasTemperature.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida’s NewLiquidCrystal library
#de ne I2C_ADDR 0x20 // De ne I2C Address where the PCF8574A is
#de ne BACKLIGHT_PIN 7
#de ne En_pin 4
#de ne Rw_pin 5
#de ne Rs_pin 6
#de ne D4_pin 0
#de ne D5_pin 1
#de ne D6_pin 2
#de ne D7_pin 3
#de ne LED_OFF 0
#de ne LED_ON 1
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
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 = 6; //Turn on A/C unit
int SSRHPin = 5; //Turn on heat (electric or gas)
int hcLED = 4; //indicator for Cooling mode
int SwitchPin = 3; // To switch between Cooling and Heating
int SSRFan = 7; // To turn on and off the air handler fan
oat currentTemp = 0;
int FanPin = 8; //Fan Pin Switch
int FanVal = digitalRead(FanPin);
int val = digitalRead(SwitchPin) ;
http://www.modsbyus.com/diy-arduino-thermostat/ 15/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
//This temperature sensor requires a 4.7k Ohm resistor across its pins 2 and three!!!! Thats the
middle pin and the GND pin
// Data wire is plugged into pin 2 on the Arduino
#de ne ONE_WIRE_BUS 2
DeviceAddress insideThermometer = { 0x28, 0xAF, 0x51, 0x6C, 0x04, 0x00, 0x00, 0x22 };
// DeviceAddress outsideThermometer = { 0x28, 0x20, 0x04, 0xA8, 0x02, 0x00, 0x00, 0x4D };
void setup(void)
{
Serial.begin(9600);
// 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 airhandler fan pin as output
pinMode(SSRHPin, OUTPUT);
pinMode(SSRCPin, OUTPUT);
pinMode(hcLED, OUTPUT);
pinMode(SwitchPin, INPUT);
pinMode(FanPin, INPUT);
}
http://www.modsbyus.com/diy-arduino-thermostat/ 16/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
} else {
// lcd.print(tempC);
// lcd.print(“/”);
currentTemp = (DallasTemperature::toFahrenheit(tempC));
lcd.print(currentTemp);
Serial.print(“Current Temp “);
Serial.println(currentTemp);
Serial.print(“Set Temp “);
Serial.println(setTemp);
}
void loop(void)
{
Serial.println(“Begin main Loop”);
int FanVal = digitalRead(FanPin);
int val = digitalRead(SwitchPin) ;
delay(500);
sensorValue = analogRead(sensorPin);
setTemp = sensorValue / 10.24; //Gives us a set temp range between 0 and 99 degrees
lcd.setCursor(8,0);
printTemperature(insideThermometer);
lcd.setCursor(6,1);
lcd.print(setTemp);
//lcd.setCursor(7,3);
//lcd.print(heat);
Serial.print(“FanPin: “);
http://www.modsbyus.com/diy-arduino-thermostat/ 17/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
Serial.println(FanVal);
Serial.print(“SwitchPin: “);
Serial.println(val);
digitalWrite(SSRFan, HIGH);
Serial.println(“Cooling Mode Fan On, Current Higher than Set”);
digitalWrite(SSRHPin, LOW);
Serial.println(“Cooling Mode Heat Off, Current Higher than Set”);
digitalWrite(SSRCPin, HIGH);
Serial.println(“Cooling Mode Cool On, Current Higher than Set”);
}
http://www.modsbyus.com/diy-arduino-thermostat/ 18/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
/* Otherwise, if the SwitchPin reads 1 and the current temperature is less than the set
temperature (the set temperature has been reached), turn off the A/C and internal fan */
void heating()
{
Serial.print(“Begin Heating Loop”);
digitalWrite(hcLED, LOW);
/* If the SwitchPin reads 0 and the current temperature is less than the set temperature (if its
cold) turn on the HEAT and internal fan */
http://www.modsbyus.com/diy-arduino-thermostat/ 19/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
digitalWrite(SSRCPin, LOW);
Serial.println(“End Heating Mode Fan Off, Temp satis ed”);
}
}
void fan()
{
Serial.println(“Begin Fan Only Loop”);
digitalWrite(SSRFan, HIGH);
digitalWrite(SSRCPin, LOW);
digitalWrite(SSRHPin, LOW);
http://www.modsbyus.com/diy-arduino-thermostat/ 20/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
69
Like
SHARE THIS:
LIKE THIS:
Loading...
Steve Spence
FEBRUARY 4, 2013 AT 7:53 AM
Thanks for mentioning our site and attributing your inspiration to our work!
http://arduinotronics.blogspot.com
Gary
FEBRUARY 7, 2013 AT 10:15 PM
You had the best tutorial out there for using the DS18B20 by far. Thanks for stopping by!
Phil
FEBRUARY 18, 2013 AT 1:12 AM
Nice tutorial! I am looking to make a thermostat with ethernet built in. I am wondering if I could add
an ethernet shield onto this design? Are you currently using this? How is it working for you? What is
the Pot for, adjusting set temp? Might be nice to add two momentary buttons for up and down
instead of a pot…
Gary
FEBRUARY 22, 2013 AT 7:50 AM
I haven’t used an Ethernet shield with this. However, I will look into it for you. I considered using
momentary buttons but went with the pot just because it is my preference to turn a dial. I will see
about revising the code for that option.
http://www.modsbyus.com/diy-arduino-thermostat/ 21/22
1/10/2018 DIY: Arduino Thermostat With the DS18B20 | Modsbyus.com
Gary
FEBRUARY 22, 2013 AT 11:34 AM
You can use an Ethernet shield attached to pins 10, 11, 12, 13. With that said, I haven’t worked
with one so a bit of research will be in order. Also, the relay shield has headers for Xbee
transceivers. For the sake of space and also the convenience of not using an Ethernet cable
attached this may be a better choice.
Violet
FEBRUARY 21, 2013 AT 10:39 PM
Hi April and Gary, so glad to see the Relay Shield in good use:) Thank you so much for sharing,
“Sharing is caring”:))) Looking forward to seeing more of your cool projects!
Gary
FEBRUARY 22, 2013 AT 7:51 AM
You’re welcome! We hope to be releasing another tutorial on a step by step for photo resist PCBs.
http://www.modsbyus.com/diy-arduino-thermostat/ 22/22