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
A seasoned Senior Solutions Architect with 20 years of experience in technology design and implementation. Renowned for innovative solutions and strategic insights, he excels in driving complex projects to success. Outside work, he is a passionate fisherman and fish keeper, specializing in planted tanks.
Still looking forward to the LCD implementation… Can’t wait for you to update the post 🙂
Hello, just would like to knw, how many Mhz are you using for the crystal?
Thank you so much for making this project open source ! it’s just ace! But I’m still interested how it works with the lcd panel, is part 4 coming soon? I’d be the happiest person if you could post that 🙂
Thank you
Hi! This post looks to be over 4 years old now, so I guess Part 4 looks unlikely..
I’m sure I’m not alone in hoping for it though; this has helped so much – I’m trying to build the same thing.
Also, I can’t seem to find a way to contact you on your site? =(
Just seen my comment approved.. Signs of activity!
I will very happily donate $$$ if you can share your completed chronobox!!! 🙂
By using this code I am getting error that DS1302 does not a name type and the following errors:
DS1302_3:16: error: ‘DS1302’ does not name a type
DS1302_3.ino: In function ‘void check_alarms()’:
DS1302_3:25: error: ‘Time’ was not declared in this scope
DS1302_3:25: error: expected `;’ before ‘t’
DS1302_3:33: error: ‘t’ was not declared in this scope
DS1302_3.ino: In function ‘void set_time()’:
DS1302_3:62: error: ‘rtc’ was not declared in this scope
DS1302_3:67: error: ‘Time’ was not declared in this scope
DS1302_3:67: error: expected `;’ before ‘t’
DS1302_3:70: error: ‘t’ was not declared in this scope
Please give solution for above problem. I am not able to find solution for that. I want to display time and date from RTC DS1302 by Using arduino on serial monitor. I am using arduino1.0.3 software and my arduino board is arduino Duemilanove Atmega 328. Pls reply the solution to display time