Pictured below is a simple timer relay circuit we recently made which we will detail here together with the source code for the microcontroller since we have had many requests for example code for timers of this type.
We received a request for a timer with two buttons. Pressing the first button was to cause a relay to close for 10 minutes, and pressing the second button was to cause the relay to close for 30 minutes. The relay was to be used to switch a mains powered appliance.
In our article Make a PICAXE Repeating Timer, we show how to make a repeating on/off timer using a PICAXE microcontroller. The timer pictured above differs in that it has button inputs to deal with and also a one-shot instead of repeating timer.
The red LED is used to show which timer is running – off, but flickering on briefly once per second is the 10 minute timer; on, but flickering off briefly once per second is the 30 minute timer. The green LED is connected across the coil of the relay (with a current limiting resistor) to show when the relay is closed.
The PICAXE code below could be greatly reduced in length but to keep it simple to read through, understand, and adapt, we have left it with separate functions for the 10 minute and the 30 minute timers (instead of making one general function which could run for any duration in response to any button press).
symbol button1 = pinC.1 symbol button2 = pinC.2 symbol led = C.0 symbol relay = C.4 ' Start with the relay open and the red LED turned off. low relay low led main: if button1 = 1 then goto run10minutes if button2 = 1 then goto run30minutes pause 100 goto main run10minutes: 'make sure button is held a little before closing the relay, high led for b0 = 1 to 5 delay 50 if button1 = 0 then low led goto main endif next b0 'Close the the relay high relay 'wait for the button to be released. do pause 50 loop while button1 = 1 low led for b0 = 1 to 10 'minutes for b1 = 1 to 60 'seconds high led pause 100 low led pause 900 next b1 next b0 'Open the relay. low relay goto main run30minutes: 'make sure button is held a little before closing the relay, high led for b0 = 1 to 5 delay 50 if button2 = 0 then low led goto main endif next b0 'Close the the relay high relay 'wait for the button to be released. do pause 50 loop while button2 = 1 low led for b0 = 1 to 30 'minutes for b1 = 1 to 60 'seconds high led pause 900 low led pause 100 next b1 next b0 'Open the relay. low relay goto main