I based my code off of Tony D's excellent Arduino GPS Clock project and modified it to work with the specific hardware we're using here, as well as eliminating the GPS receiver code he used to auto-set the clock.
First, make sure you're comfortable using the Arduino IDE to upload code to your Metro Mini. You can start here if you aren't familiar with this, and make sure you can successfully upload the Blink sketch to your Mini.
Next, you'll add three libraries to your Arduino IDE,
Go to Sketch > Include library > Manage libraries... to open the Library Manger. Here, search for and install these three libraries:
- Adafruit GFX Library
- Adafruit BusIO Library
- RTCLib by Adafruit
- Adafruit LED Backpack Library
Now, you're ready to upload the clock code. Copy the code shown here, then paste it into a new Arduino sketch. Save the sketch as metroMiniClock.ino, and then upload it to your board.
// Clock example using a seven segment display & DS3231 real-time clock. // // Must have the Adafruit RTClib library installed too! See: // https://github.com/adafruit/RTClib // // Designed specifically to work with the Adafruit LED 7-Segment backpacks // and DS1307 real-time clock breakout: // ----> http://www.adafruit.com/products/881 // ----> http://www.adafruit.com/products/880 // ----> http://www.adafruit.com/products/879 // ----> http://www.adafruit.com/products/878 // ----> https://www.adafruit.com/products/264 // // Adafruit invests time and resources providing this open source code, // please support Adafruit and open-source hardware by purchasing // products from Adafruit! // // Written by Tony DiCola for Adafruit Industries. // Released under a MIT license: https://opensource.org/licenses/MIT #include <Wire.h> #include <Adafruit_GFX.h> #include <RTClib.h> #include "Adafruit_LEDBackpack.h" // Set to false to display time in 12 hour format, or true to use 24 hour: #define TIME_24_HOUR false // I2C address of the display. Stick with the default address of 0x70 // unless you've changed the address jumpers on the back of the display. #define DISPLAY_ADDRESS 0x70 // Create display and DS1307 objects. These are global variables that // can be accessed from both the setup and loop function below. Adafruit_7segment clockDisplay = Adafruit_7segment(); //RTC_DS1307 rtc = RTC_DS1307(); RTC_DS3231 rtc; // Keep track of the hours, minutes, seconds displayed by the clock. // Start off at 0:00:00 as a signal that the time should be read from // the DS3231 to initialize it. int hours = 0; int minutes = 0; int seconds = 0; // Remember if the colon was drawn on the display so it can be blinked // on and off every second. bool blinkColon = false; void setup() { // Setup function runs once at startup to initialize the display // and clock. // Setup Serial port to print debug output. Serial.begin(115200); Serial.println("Clock starting!"); delay(3000); // wait for console opening // Setup the display. clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(15); // Setup the DS3231 real-time clock. rtc.begin(); // Set the clock if it hasn't been set before. if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (rtc.lostPower()) { Serial.println("RTC lost power, lets set the time!"); // following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); } /* bool setClockTime = !rtc.isrunning(); // Alternatively you can force the clock to be set again by // uncommenting this line: //setClockTime = true; if (setClockTime) { Serial.println("Setting DS3231 time!"); // This line sets the DS1307 time to the exact date and time the // sketch was compiled: rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Alternatively you can set the RTC with an explicit date & time, // for example to set January 21, 2014 at 3am you would uncomment: //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); } */ } void loop() { // Loop function runs over and over again to implement the clock logic. // Check if it's the top of the hour and get a new time reading // from the RTC clock. This helps keep the clock accurate by fixing // any drift. if (minutes == 0) { // Get the time from the DS3231. DateTime now = rtc.now(); // Print out the time for debug purposes: Serial.print("Read date & time from DS3231: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); // Now set the hours and minutes. hours = now.hour(); minutes = now.minute(); } // Show the time on the display by turning it into a numeric // value, like 3:30 turns into 330, by multiplying the hour by // 100 and then adding the minutes. int displayValue = hours*100 + minutes; // Do 24 hour to 12 hour format conversion when required. if (!TIME_24_HOUR) { // Handle when hours are past 12 by subtracting 12 hours (1200 value). if (hours > 12) { displayValue -= 1200; } // Handle hour 0 (midnight) being shown as 12. else if (hours == 0) { displayValue += 1200; } } // Now print the time value to the display. clockDisplay.print(displayValue, DEC); // Add zero padding when in 24 hour mode and it's midnight. // In this case the print function above won't have leading 0's // which can look confusing. Go in and explicitly add these zeros. if (TIME_24_HOUR && hours == 0) { // Pad hour 0. clockDisplay.writeDigitNum(1, 0); // Also pad when the 10's minute is 0 and should be padded. if (minutes < 10) { clockDisplay.writeDigitNum(2, 0); } } // Blink the colon by flipping its value every loop iteration // (which happens every second). blinkColon = !blinkColon; clockDisplay.drawColon(blinkColon); // Now push out to the display the new values that were set above. clockDisplay.writeDisplay(); // Pause for a second for time to elapse. This value is in milliseconds // so 1000 milliseconds = 1 second. delay(1000); // Now increase the seconds by one. seconds += 1; // If the seconds go above 59 then the minutes should increase and // the seconds should wrap back to 0. if (seconds > 59) { seconds = 0; minutes += 1; // Again if the minutes go above 59 then the hour should increase and // the minutes should wrap back to 0. if (minutes > 59) { minutes = 0; hours += 1; // Note that when the minutes are 0 (i.e. it's the top of a new hour) // then the start of the loop will read the actual time from the DS1307 // again. Just to be safe though we'll also increment the hour and wrap // back to 0 if it goes above 23 (i.e. past midnight). if (hours > 23) { hours = 0; } } } // Loop code is finished, it will jump back to the start of the loop // function again! }
Make sure under the Tools -> Board menu the Adafruit Metro is selected, and under the Tools -> Port menu the serial port for the Metro Mini is selected. Then press the upload button or click the Sketch -> Upload item to send the code to the board.
Once uploaded, the Metro Minimalist Clock will start up, and display the time! You can now unplug the USB cable from the computer, and plug it into a wall adapter -- it only needs to get the time from the computer once, from now on it will maintain correct time from the RTC module. Even when unplugged, the RTC clock's battery will keep it ticking.
If you'd like you can change whether the clock displays time in 24-hour or 12-hour time format by slighting changing the sketch code. By default the clock uses 12-hour format and it's set by this line:
// Set to false to display time in 12 hour format, or true to use 24 hour: #define TIME_24_HOUR false
If you'd like to use 24-hour time change the line to set the define as true, like:
// Set to false to display time in 12 hour format, or true to use 24 hour: #define TIME_24_HOUR true
Text editor powered by tinymce.