I am looking for some help with a timer to control a relay. I have a 12V to 120V inverter in my work truck to charge my cordless tool batteries. I have found some code(thank you viralscience) that allows you to push a button to toggle on a relay. What I would like to do is change the code so that when you push the button it toggles the relay on for 2hrs and then shuts it off. This is so when I turn the inverter on it allows for enough time to charge the tool batteries but then automatically shuts off the inverter to save battery life. I'm using an arduino uno and a 4 channel relay board.
Thank you so much for your help. Can you help me understand why the relay is on by default when it 1st starts up? Is there a way to prevent this from happening? It seems like I have to push the button once or twice before it starts working properly. After that is perfect
Can you help me understand why the relay is on by default when it 1st starts up?
Your relay is active LOW. That is you use digitalWrite(relayPin, LOW) to turn on the relay.
At startup, the out put pins default to LOW, so in your case, the relay will be on. You can change this by placing this code in setup() where you digitalWrite the HIGH state you want before setting the pinMode.