/* I2C Digital Clock using Arduino Wire David Johnson-Davies - www.technoblogy.com - 17th September 2019 CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include // Digital clock ********************************************** const int RTCAddress = 0x68; const int DisplayAddress = 0x70; int Colon = 2; char Segment[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; void SetClock (int hr, int min) { Wire.beginTransmission(RTCAddress); Wire.write(0); Wire.write(0); Wire.write(min); Wire.write(hr); Wire.endTransmission(true); } void InitDisplay () { Wire.beginTransmission(DisplayAddress); Wire.write(0x21); Wire.endTransmission(false); Wire.beginTransmission(DisplayAddress); Wire.write(0x81); Wire.endTransmission(false); Wire.beginTransmission(DisplayAddress); Wire.write(0xe1); Wire.endTransmission(true); } void WriteWord (uint8_t b) { Wire.write(b); Wire.write(0); } void WriteTime (uint8_t hrs, uint8_t mins) { Wire.beginTransmission(DisplayAddress); Wire.write(0); WriteWord(Segment[hrs / 16]); WriteWord(Segment[hrs % 16]); WriteWord(Colon); WriteWord(Segment[mins / 16]); WriteWord(Segment[mins % 16]); Wire.endTransmission(true); } // Setup ********************************************** void setup() { Wire.begin(); InitDisplay(); SetClock(0x12, 0x34); // Set the time to 12:34 } void loop () { // Read the time from the RTC Wire.beginTransmission(RTCAddress); Wire.write(1); Wire.endTransmission(false); Wire.requestFrom(RTCAddress, 2, true); int mins = Wire.read(); int hrs = Wire.read(); // Write the time to the display WriteTime(hrs, mins); // Flash the colon Colon = 2 - Colon; delay(1000); }