Summer is coming… My plants require more often watering. But how watering is connected to the time measuring. It is one of use case in smart installation. One of the ideas is create watering system with proper schedule. We should water plants only in certain hours – in the morning or late afternoon when sun is not so high.
DS3231 module
After reading multiple articles I decided tu use DS3231 module. There is a battery slot which keep tracking time in case of power not connected. There is also hidden feature. Module has integrated temperature sensor that might be used in the future. DS3231module board looks like below.
Pinouts Wiring
In order to use clock you have to properly connects it to your Arduino board. I use Arduino Uno. Wiring should be as below:
GND –> Arduino GND
VCC –> Arduino 5V
SCL –> SCL or A5
SDA –> SDA or A4
Software library
DS3231 module can be programmed by using Henning Karlsen’s library that you might download from:
http://www.rinkydinkelectronics.com/library.php?id=73
You will find also full documentation with other functions available.
Software
Code below I get from the examples included in library. kod poniżej pochodzi z przykładów dołączonych do biblioteki. Listing is very well commented so you should not get any headache when using.
Please remember that in first step you should program correct date and hour. In order to achieve it please uncomment lines 57 to 59 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
// DS3231_Serial_Easy // 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 // quickly send time and date information over a serial link // // 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); 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(WEDNESDAY); // Set Day-of-Week to SUNDAY //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 } void loop() { // Send Day-of-Week Serial.print(rtc.getDOWStr()); Serial.print(" "); // Send date Serial.print(rtc.getDateStr()); Serial.print(" -- "); // Send time Serial.println(rtc.getTimeStr()); // Wait one second before repeating :) delay (1000); } |
After modification we may use time as condition in our code and execute tasks at specific time. Please define Time structure that you re-use in loop().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <DS3231.h> // Init the DS3231 using the hardware interface DS3231 rtc(SDA, SCL); // Structure for time Time t; void setup() { // Setup Serial connection Serial.begin(115200); // Initialize the rtc object rtc.begin(); } void loop() { //Set variable t = rtc.getTime(); // Send time Serial.println(rtc.getTimeStr()); if( t.sec == 01 || t.sec==30 ){ Serial.println(" ^-^-^ "); } // Wait one second before repeating :) delay (1000); } |
As the result we get every 30s the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
20:10:00 20:10:01 ^-^-^ 20:10:02 20:10:03 20:10:04 20:10:05 20:10:06 20:10:07 20:10:08 20:10:09 20:10:10 20:10:11 20:10:12 20:10:13 20:10:14 20:10:15 20:10:16 20:10:17 20:10:18 20:10:19 20:10:20 20:10:21 20:10:22 20:10:23 20:10:24 20:10:25 20:10:26 20:10:27 20:10:28 20:10:29 20:10:30 ^-^-^ 20:10:31 20:10:32 |
For more examples please familiar with documentation mentioned above first.