Introduction: Arduino Timer With On/Off Set Point

Picture of Arduino Timer With On/Off Set Point

Edited 09/05/2017.New RTC library master adafruit, supports 1307 (tested)and 3231(not tested). You can enter the time manually."Set time rtc" is for rtc 1307 and works as described on the video and steps(automatic)

Hi, with this project you will be able to control the on and off of your devices between the time you want. They may be the lights at the night, water the garden, turn on a machine, etc. We will use the arduino, the LCD and the RTC 1307 to show and control the time. You can set the "ON" hour and the "OFF" hour, by 4 push buttons that let you increase or decrease the "SET POINT". Also, you will learn to make a clock with the arduino. I included fritzing schematics and a video, so you can make this project.

First, watch the video to know what is all about. Use it like a guide

Step 1: Materials

Picture of Materials

-Arduino Uno

-RTC 1307 module

-LCD 16X2

-5V relay module

-10K trimpot

-1K resistor

-10K resistors x 4

-Push buttons x 4

-Breadboard, jumpers.

Step 2: Mounting the Clock

Picture of Mounting the Clock

Follow the Fritzing schematic.

Connect 5v and gnd, from the arduino to their respective rails (Red 5V and Blue GND)

LCD pins to Arduino pins

1 VSS to GND

2 VDD to 5V

3 VO to pot center

4 RS to pin 2

5 RW to GND

6 EN or E to pin 3

7 D0 NC

8 D1 NC

9 D2 NC

10 D3 NC

11 D4 to pin 4

12 D5 to pin 5

13 D6 to pin 6

14 D7 to pin 7

15 A to 5V

16 K to GND by 1K resistor

The extremes of the trimpot to 5V and GND

RTC to Arduino

SDA to pin 4

SCL to pin 5

GND and 5V

Step 3: Setting the Time

Now we have to set the clock. Run the code "Set time RTC". This sketch take the Date and Time according the computer you're using (right when you compile the code) and uses that to program the RTC. If your computer time is not set right you should fix that first. Then you must press the Upload button to compile and then immediately upload.

Warning !: If you compile and then upload later, the clock will be off by that amount of time.

Then open up the Serial monitor window to show that the time has been set

Step 4: The Clock

Picture of The Clock

With the time set, open and upload the sketch "Clock with RTC LCD" . The 10K trimpot is for the lcd contrast.Turn it to adjust the contrast and see the numbers clearly.

If everything works fine, you will have the clock running. See the video.

Step 5: Mounting the Timer

Picture of Mounting the Timer

Now we add the push buttons and the relay. Follow the fritzing schematic.

So, you have pins 8, 9, 10 and 11 connected to gnd by 10K resistor(LOW). When you push, it will be connected to 5V (HIGH).

The relay is connected to pin 12. With the relay you can control your devices. Be careful with the maximun load of the relay!

Step 6: The Timer

Picture of The Timer

Open and upload the code "Timer with on off set point" . You will see the current time, the "ON" set point and the "OFF" set point. The default "ON" and "OFF" time is 12.

The timer runs from 0 to 23 hours, and so on. Push the buttons to change the set point up and down. The timer will start immediately if it is between the setting values.If not, will wait to the "ON" hour.

This code have interesting functions that you can use in other projects. I tried to separate every function to make it clear.

- Add buttons to change settings

-Debounce buttons

-Limit of the set point or any values

- Add a clock to your project

All codes are made by me, exept:

Set the time of RTC , Adafruit data logger shield guide

I hope this tutorial is useful for you!

Nicolás Jarpa


ManiK71 made it! (author)2017-12-10

its perfectly work and user editeable thanks for code

MattiV (author)2017-12-04

were i found same relay controller whit Tiny RTC and minutes too,

set on time hour+min,

set off time hour+min,

set weekday if can too.

MattiV (author)MattiV2017-12-04

and ofcourse today all used i2c LCD not old waste lot pins lcd.

AllanM89 (author)2017-11-27

What if i want to set the delay into countdown timer instead and when there is no countdown set , i can turn it on or off without delay with an additional button.

LeonceZ (author)2017-11-14

Hi, Do you know how to write the code for oled instead of lcd ? i really need a tiny screen 128x64.



NicolasJ7 (author)LeonceZ2017-11-14

sorry my friend, but i've never used an oled before

TonatiuhS (author)2017-09-12

Hi , worked like a charm! Thank you!

NicolasJ7 (author)TonatiuhS2017-09-13

Nice! Thaks for the comment!

AhmedL31 (author)2017-08-15

how can i make it with minutes

NicolasJ7 (author)AhmedL312017-08-15

Hi! add two more constants for minutes, for example:

int set_on_min=XX;

int set_off_min=XX;

Modify the if statements to compare the hours and minutes.

You will need to work on the interface to show and change the parameters

Please visit my instructable to have an idea of how to make the interface, unless you want to use 8 buttons.


Tobiasmx (author)2017-07-14

Usted tiene razón, esto hará que sea mucho más fácil y muy barato.

Tobiasmx (author)2017-07-10

In case of a power failure do I need to reprogram, the clock? If so can you recommend a battery backup for your design, please.

NicolasJ7 (author)Tobiasmx2017-07-11

Hi! You don't need to reprogram the clock (the battery of the rtc module is the backup), but you will lose the settings (ON & OFF)

Tobiasmx (author)NicolasJ72017-07-13

Thanks for your reply. here in MX I have a lot of power loss. My plan is to us this setup to run my Pool pump. If you know of a back up circuit you like let me know, i will look around the internet to see what I can come up. Thanks again I will send you pics of my finish version when I am done.

NicolasJ7 (author)Tobiasmx2017-07-13

Mi consejo es que si no necesitas cambiar regularmente el seteo ,no incluyas el lcd y botones, programa los horarios "ON/OFF" (12/12 por defecto) directo en el programa. El funcionamiento del timer es "entre" los horarios que programes. Así si se corta la energía, cuando vuelva, el timer volverá encender el relé. Para eso usa un arduino nano que es más barato.La idea es hacer el proyecto viable.Saludos!

iTECHKING (author)2017-05-22

Hi there,

Thanks for such a nice Instructable. I finally made it but I'm an issue.

Everything is working fine but after each 30 seconds (approx) my arduino getting reset and the On and Off time is set to default (ON 12 OFF 12).

I tried so much to fix this issue but no success. I even tried to modify the code.

Kindly enlight me what to do.

NicolasJ7 (author)iTECHKING2017-05-22

Hi! Strange behavoir.....never happened to me. The default "on", "off" time is declared at the begining, so can not be called again on void loop. Did you check the push buttons wiring? 4 pin push buttons are configurated as "pull down". Remember pin 1 and 2 are connected, the same for 3 and 4. So, pin arduino to push button pin 1 or 2 and push button pin 1 or 2 to 10k resistor to gnd, and pin 3 or 4 to 5V. Look for "pull down" examples and you can see what i am telling you.

iTECHKING (author)NicolasJ72017-05-24

I checked it again and everything seems fine and still the problem is same.

I thought the problem may be with the board so I used Recently bought new Arduino board but still same issue. It seems the board is being reset ,don't know why?

NicolasJ7 (author)iTECHKING2017-05-24

Try to disconnect components and test by stages. Disconnect the rtc and test the buttons, the same with the relay. Try another version of arduinio IDE (sometines works for me) I have used 1.6.9 for this project.

iTECHKING (author)NicolasJ72017-05-25

Issue resolved.
I did as you instructed and found the board, without any connection, is resetting again and again. View there was only one thing to check, power supply.
I used external power supply and then it worked fine. I think my computer USB is not providing enough current.
thanks for your assistance...
There's one more thing I want to know that what changes have I to make in the code to convert the single ON OFF timer to multiple ON OFF timer, say I want to on the relay 3 times in 24 hours.


NicolasJ7 (author)iTECHKING2017-05-25

You can look on my instructables. There is a BT programmable timer and you can select every hour of the day to be on or off (multiple timer). All interface is on your phone , just need to connect a BT module, rtc and relay to the arduino. I included the app.

I did`t want to add more functions to this project , because the interface, that`s why i chose the phone, and you can begin to create apps with appinventor, by the way. Regards.

jwhitsonbell (author)2017-04-20

Can you use an RTC ds3231 instead? and is there a way to make settings for 18 hour and 24 on/off ?

iTECHKING (author)jwhitsonbell2017-05-25

I'm using DS3231 with DS1307 library, I mean I didn't change anything in the code provided here.
And it's working fine...

NicolasJ7 (author)iTECHKING2017-05-25

Cool!! i have a lot of 1307 , that`s the reason why i don´t use 3231. I am waiting for a parcel with those.

NicolasJ7 (author)jwhitsonbell2017-04-20

Of couse, use rtc 3231 library, i don´t get the second question....

jwhitsonbell (author)NicolasJ72017-04-21

Is there a way to have 18 hours on and 6 off preset as one repeating cycle?

NewK2 (author)2017-05-11

Good project ... can sketh be made for the last point setting stored in the arduino board

Thank You...

NicolasJ7 (author)NewK22017-05-11

Yes, you need to use the arduino´s eeprom , but you have limited space and limited write cycles . I really don´t like to mess with that ,because when i need to store data i use sd card, a lot of space and write cycles. Anyway, if you want to use it, you need to make a special function to store data when you want ,or when you change the setpoints, never use it on void loop because you will be out of memory in few cycles.

jmandradiar (author)2017-05-09

when i upload the set time code and open serial monitor, i get: ÌêàoãYã©Iqð#¾ÖCBùRç‹“bÒCèA YèÆ2QC�ÊíCMð�


what do i do and nothing is displayed on the lcd

NicolasJ7 (author)jmandradiar2017-05-09

Hi! Did you check the baud rate? (57600) You can see that on the video .When you are in this step, nothing is displayed on the lcd, it is just for the RTC.

jmandradiar (author)NicolasJ72017-05-10

yeah it works now, thanks a lot... But when i upload the simple clock code to the board, nothing gets displayed..... Waiting for your reply....

The code name: clock with rtc lcd

NicolasJ7 (author)jmandradiar2017-05-10

Did you adjust the potentiometer? check your connectors, re-uploaded to my timer and is working.

jmandradiar (author)NicolasJ72017-05-11

I checked all those, I tried the hello world display from arduino site , it's it not working still. tried changing the lcd but still didn't work.... suspect my pc, but I don't know what to do....

NicolasJ7 (author)jmandradiar2017-05-11

Can be anything....dead wires, broken rail on the breadboard, damaged potentiometer (it happened to me) even the arduino, did you use the code on another arduino? mount the circuit again, sometimes something is missing and we can not see.

jmandradiar (author)NicolasJ72017-05-11

I checked everything

KaMiZoL (author)2017-05-02

Hello, thank you for this tutorial, it works almost correctly, but it seems to me that my relay works in reverse.

It is active outside the time range and is inactive in the time slot.

Is this normal?

I have not changed your code yet, and the wiring is the same.

thank you for your reply.

NicolasJ7 (author)KaMiZoL2017-05-02

Hi! I think you have an "inverted" relay module. It happened to me with a 4 relay module that i bought (i didn´t read the description well).We assume 5v (high) is ON and 0v (low) is OFF, but some relay has an inverted logic state.So, you need to use other relay or change the relay states in the code ,high for low /low for high

deezone112 (author)2017-04-01

thanks for the information.. i want ask again.. how i add some servo for that ?

NicolasJ7 (author)deezone1122017-04-02

Do you mean a servo position at specific time?

deezone112 (author)2017-03-25

i have a problem.. i buy lcd with ic2.. and the lcd must sda on a4 and scl on a5.. i use arduino uno.. so the rtc1307 sda and scl dont have slot.. can you help me ?

NicolasJ7 (author)deezone1122017-03-25

A4 and A5 are I2C bus ,that means that you can connect more than 100 devices at the same time.You need to declare the direction of you lcd or/and RTC (like 0x22 or 0x27). If you don´t know, there is a "I2C scanner".Look for more info about I2C bus, there is a lot of examples and tutorials.Regards

GeoffS44 (author)2017-03-13

Can I include the date in the timer settings?

NicolasJ7 (author)GeoffS442017-03-14

Yes by modifying the code, the rtc library let you "call" date and time , but you will need to buid and interface (buttons ,pads) to enter the parameters.

Railes (author)2016-12-15

do you have a link the RTC lib you are using? my version seems to have some type of conflict.

NicolasJ7 (author)Railes2016-12-29

herneise (author)2016-12-10


i need to make daily program my servo motor on arduino. for example;

23:00 at night ,it should turn right 90';
8:00 at morning, it should turn left(back) 90'
can i use your scheme for this? how?
when i need; i should turn this servo, left-right manually via internet.
how can i make this? any idea?

I appreciate any help that you can provide.

MaciejG9 (author)2016-08-23

Hello, great project ;] why when i use arduino leonardo your sketch doesnt work(Timer_with_on_off_set_point). On arduino Uno is ok, where is difference?? And what i must to do if i want run this project without communication with PC (after verify and compile sketch on arduino)?

NicolasJ7 (author)MaciejG92016-08-23

Thanks! There is some differences between UNO and Leonardo,On this project we are using SDA (A4) and SCL (A5), for the clock. Leonardo has different pins: SDA pin 2 and SCL pin 3. You can find more information on the web.On the video you can see that i am not connected to the pc.Use a suitable power supply or baterry pack and connect the plug to the power jack on the Arduino.

Henmarsh made it! (author)2016-08-21

Great stuff and a beautifully clear instructable. I built this today without problems (well, except for the couple of 'dead' jumper wires that had me confused for a while). I'm hoping to learn enough from your code (reading and de-bouncing switches) to

improve some of my existing projects. Many thanks and keep 'em coming!

NicolasJ7 (author)Henmarsh2016-08-21

Thanks my friend! Iam glad to help you.

About This Instructable




More by NicolasJ7:Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.Arduino& Labview With 2 DS18B20 Arduino/Android Timer (with App!).Control Your Lights and Other Stuff
Add instructable to: