Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DS3231_Serial_Hard
- // Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
- // web: http://www.RinkyDinkElectronics.com/
- //
- // A quick demo of how to use my DS3231-library to
- // retrieve time- and date-data for you to manipulate.
- //
- // To use the hardware I2C (TWI) interface of the Arduino you must connect
- // the pins as follows:
- //
- // Arduino Uno/2009:
- // ----------------------
- // DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin
- // SCL pin -> Arduino Analog 5 or the dedicated SCL pin
- //
- // Arduino Leonardo:
- // ----------------------
- // DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin
- // SCL pin -> Arduino Digital 3 or the dedicated SCL pin
- //
- // Arduino Mega:
- // ----------------------
- // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin
- // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin
- //
- // Arduino Due:
- // ----------------------
- // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
- // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
- //
- // The internal pull-up resistors will be activated when using the
- // hardware I2C interfaces.
- //
- // You can connect the DS3231 to any available pin but if you use any
- // other than what is described above the library will fall back to
- // a software-based, TWI-like protocol which will require exclusive access
- // to the pins used, and you will also have to use appropriate, external
- // pull-up resistors on the data and clock signals.
- //
- #include <DS3231.h>
- // Init the DS3231 using the hardware interface
- DS3231 rtc(SDA, SCL);
- // Init a Time-data structure
- Time t;
- void setup()
- {
- // Setup Serial connection
- Serial.begin(115200);
- // Uncomment the next line if you are using an Arduino Leonardo
- //while (!Serial) {}
- // Initialize the rtc object
- rtc.begin();
- // The following lines can be uncommented to set the date and time
- rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
- rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
- rtc.setDate(1, 1, 2016); // Set the date to DD/MM/YYYY
- }
- void loop()
- {
- t = rtc.getTime(); // Get data from the DS3231
- // Send date over serial connection
- Serial.print("Date: ");
- Serial.print(t.date, DEC);
- Serial.print("/");
- Serial.print(t.mon, DEC);
- Serial.print("/");
- Serial.print(t.year, DEC);
- Serial.println();
- // Send Day-of-Week and time
- Serial.print("Day of Week: ");
- Serial.print(t.dow, DEC);
- Serial.println();
- Serial.print("Time: ");
- Serial.print(t.hour, DEC);
- Serial.print(":");
- Serial.print(t.min, DEC);
- Serial.print(":");
- Serial.print(t.sec, DEC);
- Serial.println();
- Serial.println("--------------------------------");
- delay(1000); //Delay is for displaying the time in 1 second interval.
- if (t.hour == 14 && t.min == 32 && t.sec == 53) //Setting alarm/timer at every 2:32:53pm, in other words you can insert t.dow for every Thursday?, t.date for specific date?
- { digitalWrite(9,HIGH); //Lets say that your component is wired to pin 9
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement