Tagged ‘arduino thermostat

What is an arduino? It’s a popular, small, flexible micro-controller with a good software library that will allow me to “easily” write a program to interact with sensors and relays to ultimately build my own multi-zone, 7 day programmable thermostat with a small LCD screen that can be mounted on the wall like a conventional thermostat.

Requirements:

1. Control 2 heat sources.
2. Allow manual and programmed temperature control with override ability for the fireplace.
3. Nice, easy to read LCD containing the following:

  1. Current tempurature
  2. Set tempurature
  3. Whether the temp is rising or falling
  4. Which heating elements are selected
  5. MAYBE: Number of kWh for the current month (limited by available memory storage)
  6. MAYBE: Show rate of change and graph of temperature over the short term

I’ve been receiving the parts one or two at a time for a while (mostly from hong-kong) and started playing around with it last Friday (July15).

Components needed:

  1. An arduino ATMEGA1280 micro-controller
  2. 5 channel relay (5 individually controlled relays for AC or DC)
  3. Temperature sensor
  4. Photo sensor (for dimming the LCD when the room is dark (eg movie mode).
  5. IR receiver (for control with my harmony universal remote)
  6. RTC (real-time clock and RAM) for keeping the time during a power failure and to store the heating schedule
  7. LCD display

I’ll also need some way to power it and a method to enclose and mount it nicely on the wall… which I haven’t quite decided on yet.

So far I’ve hooked up the arduino with the LCD and IR sensor on the breadboard to begin writing the software. Basically right now I’m just writing a framework to wrap my head around how all my user interactions and temp. calculations will function. I can probably get 90% of the code done within the next couple days and then I’ll be all set for when the remaining components arrive (temperature sensor and RTC).

I’m happy to report there hasn’t been any really difficult hurtles yet, and the UI is starting to look pretty good. Cheryl helped me tonight too with a few aesthetic ideas for the main screen which made it look MUCH better (but it’s not shown in the pics below, more on that later).

View all photos at Picasa Web Albums

IMG_0020.JPGIMG_0017.JPGIMG_0013.JPG

For this project I chose a graphic LCD so that it would look reasonably presentable when mounted. The display is 128×64 and there’s already a powerful library written for the arduino that allows me to use bitmap images images, a variety of fonts, and to draw lines, rectangles, etc. Controls for this thermostat will be 100% IR (infra-red) with no hard buttons. I plan to program the specific functions into my logitech harmony remote for control from my sofa 🙂

Development on the user interface has gone quite well and is mostly complete. The main screen, calendar & clock programming, EEPROM storage, IR input, and interrupt timers are functioning well. I chose to implement three modes of operation: Manual, Automatic, and Ad-hoc. Manual mode will control the temperature to the set value indefinitely, Automatic will follow a 7 day programmable schedule, and Ad-hoc will follow a custom schedule outside of the programmed week.

Even though this is an embedded application and the code won’t see any other hardware or heating systems, I’ve tried to write the software to be scalable. The settings screens for schedule and clock setup both use a common functions for drawing user interact-able quadrants, input, and cursor movement. Originally I wrote the main screen to be drawn once and refreshed entirely when updating a single value… unfortunately this didn’t perform to my satisfaction. The refresh rate it pretty good but not quite good enough to redraw the entire screen without it looking obvious, so I broke apart the main screen into separate functions for each value/variable.

The system will allow me to choose either gas heat (from the fireplace), electric heat, or both. The electric option includes a “economy” mode that will only use the electric heat when the temperature differential is greater than 4 degrees (to assist in bringing the room temperature up to the set value faster). The fireplace can also be locked on, for visual purposes.

I haven’t completely wrapped my head around how I’m going to cycle the heaters, I have a basic idea of how the room will heat up but I’m not sure how complicated I should make this. Right now I’m leaning toward a slope calculation for temperature rise only, as to correctly time the first off cycle to not overshoot to set temperature. Next to that I know I don’t want to constantly be cycling the sytem because it it driven by two relays, one at the arduino and one in the baseboard heater itself. Probably 5 minutes will be my minimum “on” time.

I just received the RTC and temperature sensors in the mail today so I’ll get started with those soon. Here’s a vid of the user interface as it is now.

MVI_0044.AVI.wmv

Well I have everything assembled on the breadboard and it’s working properly. The software is done (except for tweaks in the temperature control logic that I can only do when in use) and it clocked in at just under 1000 lines. The bulk of the code is in the UI, as most of the hardware functions are done through the libraries available for these components. I’m using glcd to control the KS0108 128×64 LCD, IRremote to read input from my unknown IR receiver, EEPROM to read/write to the memory on the Arduino ATMega1280, OneWire & DallasTemperature to read temperature from the DS18B20 sensor, and RealTimeClockDS1307 & Wire to keep the time with the DS1307 RTC.

The main loop takes care of three timers: temperature polling, slope calculation, and a pre-emptive heat off timer that is used when the temperature is rising quickly, to ensure the desired temp is not over-shot. It also listens for IR input and adjusts the LCD backlight using an LDR analog input and a PWM output connected to the backlight anode (with 200ohm resistor). I was a bit worried that the current draw would be too much for the arduino digital pin but ohms law says 9.2 milliamps which I confirmed with a multimeter. Not sure why the community using these LCDs seem to think the current draw is too high… hopefully I’m not missing anything.

The slope calculation timer is set at 5 minutes. This is the where I’ve placed the control heat function call, as to limit the on/off cycle to a minimum of 5 minutes. I don’t want the relays to drive me insane with unthrottled clicking! It also calls the graphing function that draw the current temperature on the screen. I’ve put in 24 hour history logging of temperature values and an ability to scroll through the graph, cause I’m thinking it might be cool to see the temp lows of the previous night or how quickly the temperature rises in summer or something. Actual temperature polling and numerical display is every 4 seconds (split into two 2 second parts, to allow time to read the sensor value (The DS18B20 has a ~700ms response time). Only if the temperature changes will the LCD redraw, to eliminate flickering.

Physically, the heat will be controlled using 2 of the 4 relays on a module I picked up off ebay. This module is responsible for the highest current draw of the project, clocking in at about 120 milliamps. Actually it was enough to bring down the USB voltage from 5.12v to about 4.6v. Because of this I’ll probably be using a 9v or 12v input to the barrel connector to make use of the voltage regulator on the arduino board. Without a constant 5v signal the LCD contrast will change when the relays are activated which just looks tacky.

Time is kept with the Dallas DS1307 RTC, which initially I received (as a package) with a defective crystal. After pulling my hair out for a few hours I decided to give up and buy another crystal from a local shop and voilà, the clock started ticking. It’s not the most accurate clock, especially when running on battery. I noted a 6 second lag the first day and about 2-3 second the next few days (while leaving it powered on). I think this might go down a bit more once I move it from the breadboard to a soldered prototype PCB. If it doesn’t, I can set the time every couple of months. My schedule times are only down to the hour anyway so it’s not a big deal.

I don’t have much to say about the IR receiver, except that it works well with the library (IRremote) Ken Shirriff wrote, and hasn’t given me any trouble. Briefly I was only getting garbage reads but quickly found a bad jumper on the breadboard knocking down the voltage too low. It seems to decode almost everything so I won’t have a problem mapping the functions to key codes from the harmony remote. I’ll just pick a random device and use that as a base code set.

Next up is soldering this down to a prototype shield with some headers to connect to the sensors (which will be mounted outside of the enclosure). I still haven’t decided on an enclosure but I’m leaning toward a single gang recessed AC receptacle and a wall mount box to put overtop. That way I can hide the AC/DC transformer and separate the PCB from the powering.

Here’s the loaded breadboard. From left to right we have the DS1307 RTC, The DS18B20 Temp Sensor, the LDR, two LEDs I was using for testing, and the IR sensor. The big metal thing is the potentiometer for the LCD backlight. The Relay module is in the top left and obviously the LCD is above and the arduino is on the right.

IMG_0001.JPG
View all photos at Picasa Web Albums