The Chronobox (Part 3)

After ordering a few more supplies on the weekend my shipment arrived on Wednesday, the main thing I was waiting on was a time keeping chip and I had selected the DS1302 from Maxim IC to do that job. Aside from that I got a crystal for the clock, a few mini servos, some stackable headers (to make my proto shield and motor shield stackable), push buttons, a piezo speaker and some other various headers and connectors.



I was very eager to try out the clock chip and rushes straight home and went to work. I pulled a 3V battery out of an old motherboard and popped it into a 3V battery holder I had bought a while back (note: the 3V battery and holder is represented by a piezo speaker in this diagram as there was no part for a 3V battery). I popped that into one side of the mini breadboard on the Protoshield and installed the DS1302 on the other side. Wiring the circuit was a breeze and I didn’t even have to pull up the data sheet. The chip came attached to a business card sized information sheet that had the pin outs on the back. Thank you HVW Technologies! I’ll be using the pin labels from that card and providing the alternative marker from the Fritzing diagrams in brackets.

The DS1302 has two pins for power input and which ever is capable of providing more power will take over and power the clock and crystal. So I hooked the Vcc1 (V+) to the 3.3v pin and the Vcc2 (Vbat) to the 3v battery. The idea being that if I unplug the device (USB or power adapter) the 3.3v pin will stop providing power to circuit and the battery will seamlessly take over so no time is lost. When the circuit regains power the DS1302 has a built in trickle charge circuit that will charge up the 3v battery for you. The X1 and X2 pins are bridge with a crystal, the ground to the ground rail of course and the remaining three pins are SCLK, I/O and RST (CE) are run to pins 7, 6 and 5, respectively.

The code was very simple and was all build on the RTC Library which I found on the Arduino site. The code is very rough at this point and just prints the time every second over serial, during the range of 9pm to midnight it will print Unlocked and all other hours will print Locked. Eventually the code will be altered to query the locked/unlocked status on button press and if it falls within the time range the servo will unlock the box.

// @author: Brad Fettes <http://www.fettesps.com> // @thanks: Matt Sparks <http://quadpoint.org/projects/arduino-ds1302> // @date: December 9, 2010 // @purpose: Check the ds1302 to see if the chronobox should be locked or unlocked #include <stdio.h> #include <string.h> #include <DS1302.h> // Set the appropriate digital I/O pin connections uint8_t CE_PIN = 5; // RST uint8_t IO_PIN = 6; uint8_t SCLK_PIN = 7; // Create a DS1302 object DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN); // Create buffers char buf[50]; char day[10]; // Thanks to the Arduino library... void check_alarms() { // Get the current time and date from the chip Time t = rtc.time(); // Name the day of the week memset(day, 0, sizeof(day)); // clear day buffer // Check the hour snprintf( buf, sizeof(buf), "%02d:%02d:%02d", t.hr, t.min, t.sec ); Serial.print(buf); Serial.print(" - "); switch (t.hr) { case 21: // After 9 pm Serial.println("UNLOCKED"); break; case 22: // After 10 pm Serial.println("UNLOCKED"); break; case 23: // After 11 pm Serial.println("UNLOCKED"); break; default: Serial.println("LOCKED"); } Serial.println(); } void set_time() { /* Initialize a new chip by turning off write protection and clearing the clock halt flag. These methods needn't always be called. See the DS1302 datasheet for details. */ rtc.write_protect(false); rtc.halt(false); // Make a new time object to set the date and time Time t(2010, 12, 9, 11, 11, 0, 4); // e.g. Thursday, Dec 9, 2010 at 11:11:00 // Set the time and date on the chip rtc.time(t); } void setup() { // Ensure that we have a few seconds to upload code delay(2500); // then start serial communication Serial.begin(9600); // Uncomment to set the time to the one defined above // make sure to upload it again uncommented or it will reset // the date each time gets powered up //set_time(); } /* Loop and print the time every second */ void loop() { check_alarms(); // Lock or Unlock delay(1000); // wait one sec }

The next step here is to combine this sketch with the one from Part 2 where I controlled a locking mechanism with a servo. I will also need a way to provide feedback to the user by a piezo or LCD panel. Stay tuned for Part 4 which will be posted just after the new year. Happy holidays!

Download the Project Files:

Frizing Project

Arduino Code