I need a simple code to turn on 12 outputs one after another 2 seconds apart. A pushbutton will initiate this and the outputs will stay on for 3 minutes at which time all outputs will turn off awaiting the pushbutton input

So far:

Void setup 
pinMode (10,output);
PinMode (11,output);
PinMode (12,output);

Void loop
digitalWrite (10,HIGH);
delay (2000);

digitalWrite (11,LOW);
digitalWrite (12,LOW);
digitalWrite (10,LOW);

Push remote reset button to restart.

Only tried 3 outputs for now and haven’t tried “Crossroads” code yet but I will.

  • 2
    you cannot do that without hardware ... and all hardware is not the same ... so i think that you forgot include all of the information – jsotola Jul 28 '19 at 1:08
  • 1
    you avoided a downvote from me because you explained the desired functionality very clearly ... you can use that clear description and turn it into code ... simple loop with an incrementing pointer and a 2 second delay ... 2 minute 58 seconds delay after loop finishes ... then all off and back to begining – jsotola Jul 28 '19 at 1:42
  • turning a relay on is no different than turning on an LED, so start writing the code. functions you gonna need mainly is delay(), pinMode() and digitalWrite(). after finishing the prototype using LEDs replace the LEDs with a NPN transistors like a 2N2222 to turn on relays. don't connect relays directly to arduino... – ElectronSurf Jul 28 '19 at 5:42
  • 1
    What have you tried so far? – chrisl Jul 28 '19 at 6:56
  • 2
    @newbie, NO, not delay(). That is a dead-end. You, and the OP, need to search on "blink without delay explained" and read the resulting article. – Duncan C Aug 4 '19 at 10:58

Don't use Delay. CrossRoads provided a decent starting point for your code, but his code is based on delay(), which is a dead end.

Instead of delay, use a bool running to track whether you're relay sequence is running or not. Also have an unsigned long startTime, and an array of bools to track if each relay is on or off.

If !running, check the button. If the button is pressed, set running to true, and set startTime to millis().

Then, use millis() to figure out where you are in the sequence. Calculate (millis()-startTime)/1000/num_relays. Let's call it relayIndex.

When relayIndex is less than num_relays - 1, check the bool for that relay, and if it's off, set it on and turn on that relay. When relayIndex reaches num_relays-1, you're done turning on relays. Now it's time to wait for 3 minutes to go by. After 3 minutes, turn off all the relays and relay bools, and set running=false. Have the reset button do the same thing.


Also note that controlling relays from an Arduino (or from other solid state devices for that matter) requires special hardware and precautions. You should probably use a "digital" relay (something like this, or better yet this 6 channel relay) that isolates the Arduino control line from the high current needed to drive the relay.

Failing that, you should build a control circuit from a MOSFET transistor and a "flyback diode". Without flyback diodes, the inductive coils on your relays emit "back EMF" which will likely fry your transistors (and possibly the Arduino)

  • I particularly liked the fact that you did not provide a complete working solution, but rather pointers for the OP to write his own code.. +1 – frarugi87 Aug 5 '19 at 9:30
  • 1
    Yup. I don't like to provide code to beginners. I think it's counter-productive. More often than not they copy/paste it into their program, complain if there are minor compile errors, and don't really gain any understanding. By outlining a solution and supporting the OP they actually learn. – Duncan C Aug 5 '19 at 12:35
  • While I agree you shouldn't give a complete working solution; I do think this source (forward.com.au/pfod/ArduinoProgramming/…) will help to atleast get an understanding of the possible implementation. – Paul Aug 7 '19 at 10:49

Simple code:

byte buttonPin = 2;
byte relayPins [] = {3,4,5,6,7,8,9,10,11,12,13,14,};
byte x;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  for (x=0; x<12; x=x+1) {
    pinMode(relayPins[x], OUTPUT);

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    for (x=0; x<12; x=x+1) {
      digitalWrite(relayPins[x], HIGH); // drive base of NPN or N-MOSFET to sink current
    delay(180000); // dumb 3 minute delay
    for (x=0; x<12; x=x+1) {
      digitalWrite(relayPins[x], LOW);

And of course, supporting hardware to drive relay coils.

  • 1
    The Arduino will not sense your button presses because, most of the time, it will be busy delaying when you press the button. – Edgar Bonet Aug 4 '19 at 20:48
  • 1
    Sorry, my comment was wrong: I was mislead by your poor indentation (and missing braces) and didn't notice the delay() was within the if. I fixed your code formatting to make the intent clearer. – Edgar Bonet Aug 5 '19 at 9:22
  • 1
    This code will miss button presses most of the time while running a relay sequence because it will spend the vast majority of its time in one of the 2 delay statements. A reset button wouldn't work very well, and would not stop the 3 minute period while all the relays are active (unless you press the hardware reset button and reboot the Arduino.) – Duncan C Aug 5 '19 at 16:50
  • 1
    Is there any requirement to do anything else for (2 seconds * 12) + 3 minutes? No. So let the delay whining go already. – CrossRoads Aug 5 '19 at 20:24
  • 1
    It's a bad habit to use that approach because it only works in the most trivial of cases, and if you start out with delay() and move beyond the trivial to doing anything of any complexity, you have to throw out your delay-based code and rewrite it. The logic flow of using millis() is different, so it can be a painful rewrite. I speak from experience. – Duncan C Aug 6 '19 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.