Use Arduino as a timer - how it works
This practical tip shows you how you can easily use the Arduino as a timer. All you need is the right module and the right code.
Use Arduino as a timer - how it works
You can easily use the Arduino as a timer. All you need for it (besides your Arduino) is a DS3231 real-time clock module.
- Connect "SDA" to "SDA" and "SCL" to "SCL" on the Arduino. The ground and 5V pin also connect to a ground and 5V pin on the Arduino.
- Now import the required library with the "#include" command. Make sure you have installed it beforehand.
- Use the commands "DS3231 clock;" and "RTCDateTime dt;" the required instances.
- In the setup method you have to use the command "clock.begin ();" start the clock and with "clock.setDateTime (__ DATE__, __TIME__);" set the sketch compiling time.
- Now you can in the loop method with the command "dt = clock.getDateTime ();" update the clock and use the command "a = clock.dateFormat (" dmY H: i: s ", dt);" retrieve the current time with date. In this case, the time and date are saved under the "a" variable that you previously declared. If you only need the exact time to the minute, simply omit parts of the code: "a = clock.dateFormat (" H: i ", dt);".
- You can now easily build a timer. If the string of the current time corresponds to another previously defined string, various commands are executed. The best way to do this is with an if statement.
In the next practical tip, we will explain how you can control LEDs with the Arduino.