What is the best time to close your blinds? Temperature logging with Arduino (Part 1)
As the mercury keeps dropping lower and lower here in Canberra, I keep finding myself reaching for the heater. This got me thinking, is there a way to minimise the use of the heater, to save wasting the electricity it would be using? In an ideal world, I would open the windows during the day, let my room warm up and close everything up in the afternoon, keeping as much of the warmth inside as possible. So this got me thinking, what is the optimal time to close my blinds? This post will be part one of two, where I go over building the temperature sensor, and in a week or two when I get enough data I’ll write up part two, and see what is the optimal time to close the blinds.
The main requirements of the logger are to be able to log the temperature inside and outside, and record this, along with the date and time of day to an SD card, where it can be analysed later. With this in mind, I grabbed an Arduino, a few temperature sensors, a Real Time Clock, and a SD card reader, and got to work building the temperature logger.
Along with some jumper wires, the parts I used are listed below:
After some trial and error, my circuit found its final form, which is shown in the below Fritzing diagram. Whilst the circuit is relatively straightforward, there are a few things to point out. Firstly, as the SD card uses SPI to transfer data, and the RTC uses I2C to talk to the Arduino, they must be connected to the specific SPI and I2C pins on the Arduino, which are noted on the diagram. The DS18B20s and LEDs can be connected to any of the unoccupied digital I/O pins, as long as their location is defined in the code.
The DS18B20 temperature sensors I am using are digital sensors, which when using the Dallas Temperature library, directly reports the temperature to the Arduino. One cool thing with the DS18B20’s is that you can connect them all on one wire, which cleans up the wiring substantially. There are some complexities in using this method, however I will discuss this later when I get onto the code.
I used a DS1307 Real Time Clock for the project, as its drift is less than 5 seconds per month, which is more than enough for this project. It was also around half the price of the more accurate DS3231 model, which sweetened the deal.
I also added two debugging LED’s to the board, which turn on and off depending on if there is an error with the data logging. This enables the state of the unit to be determined quickly, and if there is an error connecting it to a computer will show exactly what is not working as intended.
After all of the hardware was completed, it was time to start coding the project. After two afternoons of mucking around with the code, I got everything working, and it can be found here on GitHub. Whilst I won’t go over what every line does, the high level overview is outlined below:
In setup(), the RTC and SD card are checked and if they are working properly, a CSV file is created on the card, with a header being written to the first row of the CSV. If there is a pre-existing CSV with the correct name, it will write to the next empty row. If there is an error with the initialisation of the RTC or SD card, the red LED will turn on, and persist until the issue is resolved. If power cycling the Arduino does not fix the issue, plugging the Arduino into a computer and opening the serial monitor will report the issue, and from there troubleshooting should be a bit easier.
In loop(), the RTC and Temperature sensors (more on that later) are polled and their values written to the CSV on the SD card. If this is successful, the green LED is flashed and then the program sleeps for 6 minutes, before taking another measurement. If there is an issue writing to the SD card, the red LED is pulled high, and it requires a power cycle to clear that error, ensuring it is hard to miss that there is an issue with the logger.
One thing to note is that as the DS18B20 temperature sensors are using the same data line, there needs to be a way to differentiate between them. Lines 20 and 21 of the code allow you to assign the unique identifier of each sensor to their name, and this code from Terry King allows you to determine what these identifiers are. Once you have identified the values of your sensors, replace the values in my code with them and you should be good to go.
With all of this done, it was just a matter of placing the sensors around the house and waiting. I made two data loggers, one which was placed in the lounge room near the fire, and another one with two probes in my room, with one of the sensors dangling out the window. Whilst the lounge room sensor does not help answer the question of when to shut the blinds, I thought it would be interesting to see what the ‘ideal’ temperature was for the house, as most of the time there is someone in the lounge room.
With all of this said and done, the sensors are currently logging the temperature and when I have enough data to make a conclusion I’ll write up part two. In the meantime, enjoy the below graphs, as in all honesty making cool graphs was the main motivation behind this project.
If you have any comments or questions about any aspect of the post, please leave it below and I’ll get back to you. In particular, if you are having any issues getting the logger to work I’d be more than happy to help.