Random Nerd Tutorials
Shares

How To Do Daily Tasks with Arduino

Shares

In this tutorial we’re going to show you how to perform daily tasks with the Arduino. We’re going to turn an LED on and off at a specific time of the day, everyday. You can then, easily modify the example provided to perform any other task.

Project overview

Parts required

For this example you need the following parts (click the links below to find the best price at Maker Advisor):

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic

Wire the DS1307 RTC module to the Arduino and the LED, as shown in the schematic below.

You can also refer to the following table to wire the DS1307 RTC module to the Arduino.

DS1307 RTC Module Pin Wiring to Arduino Uno
SCL A5
SDA A4
VCC 5V
GND GND

Installing libraries

For this example you need to install the following libraries: Time, TimeAlarms, and DS1307RTC created by Michael Margolis and maintained by Paul Stoffregen:

To install these libraries, in the Arduino IDE go to Sketch > Include Library > Manage Libraries. Then, enter the libraries’ name to install them.

Set DS1307 RTC module time

To set the DS1307 RTC module time, you need to upload the next sketch to your Arduino board and run it once:

This is what you will see in your Arduino IDE serial monitor:

Once the time and date is properly set, you can continue this project and upload the final sketch.

Code

The code provided turns off the LED every morning at 9:00 AM, and turns it on every evening at 7:00 PM. Copy the following code to the Arduino IDE and upload it to your Arduino board.

This code is simple to understand and can be easily modified. Continue reading this page to learn how the code works.

Importing libraries

First, you import the needed libraries to create time alarms and to interact with the RTC module:

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>

Getting the time

In the setup(), you get the time for the RTC with the following line:

setSyncProvider(RTC.get);

You also display a message on the serial monitor, so that you know whether the time has successfully synced:

if (timeStatus() != timeSet) 
  Serial.println("Unable to sync with the RTC");
else
 Serial.println("RTC has set the system time");

You can set the time manually – simply uncomment the next line. This is specially useful to test your sketches, because you can easily modify the time and test if the alarms are being triggered.

//setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

Setting Alarms

The following line triggers the MorningAlarm function at 9:00 AM everyday. The MorningAlarm is defined after the loop().

Alarm.alarmRepeat(9,0,0, MorningAlarm); // 9:00am every day

To change the time, you just have to change the numbers 9,0,0, with your desired time. The first number is the hour, the second number is for the minutes, and the third for seconds.

The EveningAlarm function is triggered at 7:00 PM everyday.

 Alarm.alarmRepeat(19,0,0, EveningAlarm); // 19:00 -> 7:00pm every day

loop()

In the loop() you display the time on the serial monitor every second by calling the digitalClockDisplay() function, defined later in the code.

void loop() {
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

Defining the tasks

The MorningAlarm() and EveningAlarm() functions will be called at the time you’ve defined at the setup().

To edit your tasks, you just have to write your tasks inside the MorningAlarm() and EveningAlarm() functions.

void MorningAlarm() {
  // write here the task to perform every morning
  Serial.println("Alarm: - turn lights off");
  digitalWrite(led, LOW);
}
void EveningAlarm() {
  // write here the task to perform every evening
  Serial.println("Alarm: - turn lights on");
  digitalWrite(led, HIGH);
}

Here we’re just  printing a message on the serial monitor and turning an LED on and off.

The idea is that you use this example to make your Arduino perform a useful task everyday at the same time.

Wrapping up

In this tutorial we’ve shown you how you can perform daily tasks with the Arduino. The example provided is simple so that you can easily adapt it to your projects. Here’s some ideas of tasks you can automate using this technique:

  • Turn on/off lights at a specific time
  • Close your window blinds
  • Feed your pets

Do you intend to use the Arduino daily task in one of your projects? Let us know by posting a comment down below.

If you like Arduino, you may like the following blog posts:

We hope you’ve found this post useful. Thanks for reading.

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Early Black Friday 2017 Special ⏰
Download all our courses up to 35% off