Archive for May, 2013

Programming the ChronoDot DS3231 Real Time Clock

In my last post, I introduced my new hobby tinkering with the Arduino microcontroller. I also mentioned a timer that I’m building to power some lamps in my living room.

Since the Arduino doesn’t have a clock built into it, there is an interesting problem wherein there is no way to ensure that the Arduino will turn the relays on and off when I want it to. Enter the Real Time Clock (RTC). It’s a small clock chip with a battery. You program it once to set the time, and from then on it will keep time rather well. The one I have will keep time within a minute or two per year, which is plenty accurate enough for turning on lamps.

The biggest issue I’ve run into is that it has been hard to figure out how to program it with the right time. There are example sketches (program code) out there that will do it, but not exactly as I need it.

The code I got from Jack Christensen over at Adventures in Arduinoland assumes that the clock is set to UTC time, not local time, and the examples that I have found all set the clock to local time. Setting to UTC is important because it automatically adjusts for daylight savings time. It’s also easier to program the code for use anywhere in the world (share it with others) when it is based on UTC time.

So because I had such a hard time, I modified the example that is found here to set the clock to UTC, and figured out how I needed to modify it here. All you need to do is paste this into the Arduino programmer, modify the variable for the UTC offset to fit your timezone, and upload it. (I’m in UTC – 5, so mine is set to float UTCOffset = -5.0; It is set as a float for those locations that may be offset by an amount that is not a whole hour (for example, Nepal is UTC +5.75). If you’re powering the RTC from the pins, you’ll have to uncomment those and modify them to fit your configuration.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"


float UTCOffset = -5.0;    // Your timezone relative to UTC

void setup () {
    // Uncomment the following lines to use pins 4 & 5 as power and ground for the RTC
    // pinMode(4, OUTPUT);
    // digitalWrite(4, HIGH);  // HIGH for power
    // pinMode(5, OUTPUT);
    // digitalWrite(5, LOW);   // LOW for ground
    RTC.adjust(DateTime(__DATE__, __TIME__));  // sets the clock to the time when this sketch is compiled
    DateTime now =;
    DateTime UTCTime(now.unixtime() - 3600 * UTCOffset);   // Adjust the time from local to UTC

void loop () {
    DateTime now =;
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(, DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print(future.month(), DEC);
    Serial.print(, DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(future.minute(), DEC);
    Serial.print(future.second(), DEC);

Learning Programming with the Arduino Microcontroller

A few weeks ago, I got my hands on an Arduino starter kit. The Arduino is a microcontroller that is capable of doing a lot of really cool things. The starter kit included the Arduino board, a collection of electronic parts (LEDs, a breadboard, wiring, resistors, potentiometers, an LCD screen, and some other things), and a book to start learning the programming. The Arduino is programmed using a modified version of C++, so learning to program it means that your programming skills can be easily transfered elsewhere.

Learning to program the Arduino is not hard, and it’s a lot of fun. The first thing you’ll learn is how to make the LED on the board blink. It’s very simple, but seeing it do what you tell it to do is exciting.

I’ve gone about half way through the book, putting together example projects. Once I got half way through, I started having my own ideas that I wanted to try. The projects I’ve done include: A “Spaceship interface”, “Love-o-meter”, Color mixing lamp, Mood Cue, Light Theremin, and a Crystal Ball. The Spaceship interface is the first project, to give you the basics of hooking up buttons, LEDs, resistors, and working with inputs & outputs in the code. It’s very simple, just a couple lights that change when you push the button. When you’re done, it looks like something you’d see in an old sci-fi movie.

The Love-o-meter is simply a temperature sensor connected to some lights. The warmer it gets, the more lights turn on.

The color mixing lamp is really cool. It’s an LED with red, green, and blue elements in it. You can make any color you want by mixing the light from the elements.

The mood cue uses a stepper motor to point an indicator. The purpose is to learn the use of the stepper motor.

The light theremin is interesting. It uses a light sensor, and outputs a varied pitch tone based on the amount of light it sees.

I modified the code for the Crystal Ball. It’s supposed to be like a “magic 8 ball”. You program it with 8 possible answers and have a switch that’s activated by turning the board over. I never got the switch to attach to the breadboard because the pins are too short, so I just wired in a pushbutton switch instead. Instead of 8 random answers, I put in 8 random trivia facts. It was a fun project to learn how to get an LCD screen working.

Beyond that, I’ve been working on a few other projects that came to mind. One of them is a timer to control two lamps in my living room. Right now those lamps are on timers and come on in the evening, and turn off around 10:30. However, I have to adjust the timers every few weeks to follow the changing sunset time. My new timer will have a clock built in, and be able to calculate when sunset is, so I won’t have to adjust for it any more.

While contemplating that project, I found this. It’s a goofy little project, but it does almost exactly what I want my timer to do. The only difference is that I will drive a relay instead of an LED.

I emailed the guy who made that and asked if he could point me in the right direction for tracking sunrise/sunset times, since that is still a bit beyond my current abilities. He gladly shared his code with me, and has been very helpful in getting it modified to suit my needs. He also pointed me to the Arduino forums, where there is a wealth of knowledge to help figure this interesting little board out.

The other project I’m working on is a simple one to understand and build. I bought an old stoplight at a surplus store, and am fixing it up for my rec room. It will be driving by relays that are controlled by an Arduino when I’m done with it. The programming is super simple, not much more complicated than the “blink” program that is the first thing you learn with the Arduino, it will just have three lights instead of one, and the “blinks” will be very slow, and never on at the same time. I may get fancy and figure out how to connect a motion sensor to it so it only runs while we are in the room (no point in powering lights when there’s no on there to see them).

I have been having a great time, and look forward to a lot of fun projects in the future. Not only that, but I’m getting things figured out now, so when my kids get a little older, I can help them learn to program and make neat little projects too!