Saturday, October 3, 2015

Let's make something useful

I've been covering "block" i.e. "building blocks" for a while.

Now let's take a few Arduino building blocks and make a useful thing out of them. I want an alarm clock with all the features I like, and I can't buy it. All the clocks I've ever bought have shortcomings. The most irritating one is that they take a 9v battery for power failure backup, which should be able to run an RTC chip until the next ice age, but somehow they last about 2 months then die.

I would like it to be able to run during power failures (with the display off, or perhaps on demand), wake to alarm during those times. I also want an outdoor temperature sensor. Wireless would be super cool, if I can work out the power.

I also like the phone apps that start out the alarm quiet and it gets slowly louder. I'd like to incorporate that.

Recently I got some Arduino Uno clones for < $4 each. I also picked up some waterproof digital temperature sensors, and some little 4 digit displays that use the same driver chip as the 8 digit + switches display that we played with earlier.

The chip reads TM1637.  The first Google result from that is on Arduino Playground and gives a library for driving it. Here's the Github link referenced in that article:

From that page, click "Download ZIP" and save the file.  Start up Arduino and hit Sketch/Include Library/Add ZIP library. Now you can hit File/Examples/TM1637-master/TM1637Test.

If you look at the code, you'll see that it says that CLK should be wired to I/O pin 2 and DIO to pin 3.  Gnd and VCC should be wired to gnd and +5V.  Wire it up, upload the sketch and you should see the demo working:

Now, I have a little "real time clock" module. What that means is that I have a module that's essentially a watch with no display that can interface to the Arduino.

It says "Tiny RTC".  A little googling reveals that it appears to use a DS1307 real time clock chip. 

Turns out that there's a lib for the DS1307 already loaded in Arduino. Go to File/Examples and find and load the DS1307 sample code.

A quick Google indicates that SDA should go to pin 4 and SCL to pin 5.

To set the time, you would uncomment this line in Setup and fix the values to the current date/time:

    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

Compile and upload the code, and start the serial monitor at 57600 baud as indicated in the code.

You'll get output like this:
2015/10/2 22:39:11 since midnight 1/1/1970 = 1443825551s = 16710d now + 7d + 30s: 2015/10/9 22:39:41
Now we have a basic start on a clock.

By combining lines from the two example sketches, we can get a clock display that works.

 // John Ridley Oct 2 2015  
 // nifty bedside alarm clock  
 // totally not a bomb.  
 // stuff for RTC DS1307 chip  
 #include <Wire.h>  
 #include "RTClib.h"  
 RTC_DS1307 rtc;  
 // stuff for display:  
 #include <Arduino.h>  
 #include <TM1637Display.h>  
 // Module connection pins (Digital Pins)  
 #define CLK 2  
 #define DIO 3  
 TM1637Display display(CLK, DIO);  
 void setup ()   
  // rtc initialization steps  
 void loop ()   
  DateTime now =;  
  // show hour  
  int hrDisplay = now.hour();  
  // Let's do a 12 hour display  
  if (hrDisplay > 12) hrDisplay -= 12;  
  if (hrDisplay == 0) hrDisplay = 12;  
  display.showNumberDec(hrDisplay, false, 2, 0);  
  // show minute  
  display.showNumberDec(now.minute(), true, 2,2);  
  // delay, no point in beating on things more than once a second  

This displays the time, but the colon in the display is not lit up. I'd like it to have it lit. Doing some searching indicates that the module had some pending commits that have not been rolled into the code yet, to control the colon.  We'll need to pick those up.

Go to  Click on "Pull requests" The top open requests says "Colon setter and brightness range fixed" (If it does not, maybe by the time you are reading this the changes have been included).  Click on that. Click on "Commits" tag and then click on the <> (browse files at this point) next to the top thing.  Now you'll need to download the .CPP file and the .H file, and save them over the top of the same files in the library. You'll find them in your "Documents" folder, under Arduino/Libraries/TM1637-master

Now we have control over the colon.  We can add this code around "loop":

 bool colon = true;  
 void loop ()   
  colon = !colon;  

Now the colon blinks once per second

This is probably enough for now. There's still a lot to be done. A second display for temperature, buttons to set the time and other features, an audio system for alarm, and other things.

1 comment:

Barika Davis said...

Hi, John; I'm enjoying your code headered as

// John Ridley Oct 2 2015
// nifty bedside alarm clock.

However, when I follow your instructions to replace the .CPP and .H files in the library
to enable the colon I get the following error msg:

C:\Users\Barry Davis\Documents\Arduino\libraries\TM1637\TM1637Display.cpp: In member function 'void TM1637Display::setBrightness(uint8_t)':
C:\Users\Barry Davis\Documents\Arduino\libraries\TM1637\TM1637Display.cpp:79:1: error: a function-definition is not allowed here before '{' token
C:\Users\Barry Davis\Documents\Arduino\libraries\TM1637\TM1637Display.cpp:84:1: error: a function-definition is not allowed here before '{' token
C:\Users\Barry Davis\Documents\Arduino\libraries\TM1637\TM1637Display.cpp:209:1: error: expected '}' at end of input
Error compiling. end quote.

Is there something amiss in the files or have I pooched something? Thanks for your time and for sharing your great project! ... Barry.