Is there a way to control 3 relays with 1 button?

My idea is: There are 3 modes; mode 1 for relay 1, mode 2 for relay 2 and mode 3 for relay 3.

With a long press you switch between modes (After 3 comes 1) and with a short press you activate the relay which is in the activated mode.

I need this for my wheelchair so please help me.

Thank you, Michael

Further question:
Does the relays really need a external power supply?
Because I have a small amount of power to control with the relays.

  • you asked only one question ... the answer is "yes" .... is that what you really wanted to ask? – jsotola Aug 19 '19 at 23:23
  • What do you need to control? If the thing you need to control takes DC, and it's voltage and current needs are reasonable, you might be able to control them using MOSFET transistors rather than relays. You could power those just fine from the Arduino's voltage regulator – Duncan C Mar 8 at 23:54

Sure you can. This site isn't a great place for open-ended questions like this though. Since you have a serious real-world need I'll give you some guidance anyway.

You'd need to break it into pieces and solve each one in turn.

First create code that recognizes long and short presses (not using delay().) You'll probably want to respond on releasing the button, since at that point it's easy to tell if it was a long or a short press.

The pseudo-code might look something like this:

void loop()
   if button_is_pressed
      if !press_in_progress
         press_in_progress = true
         start = millis()
      if press_in_progress //Button has been released
         elapsed = millis() - start
         press_in_progress = false
         if elapsed > long_press_threshold

For controlling the relays, I suggest getting "digital" relays that are made to be controlled with 5V logic signals. Relays like this work well. (Link). Since you need to control 4 relays, a 4 channel model like this one would be a better choice. Note that you'll need a fairly high current power supply. A 2A 5V regulated supply should be plenty for both your Arduino and your relays, although it's more of a sure thing if you use separate power supplies for the arduino and the relays (that way the voltage to the arduino doesn't bounce around as the relays energize and de-energize.

You'll need a global variable for the currently active relay. The long press would increment that value modulo 4: (going from 0 to 3 and then back to 0)

relay_index = (relay_index + 1) % 4;

You'd need an array of 4 bools for the states of each relay:

bool relay_states[4];

The short press would toggle the state of the current relay

relay_states[relay_index] = !relay_states[relay_index];

Note that if you're more interested in solving your wheelchair problem cost-effectively than tinkering, this is the sort of thing that a maker space would be ideal for. There are lots of tinkerers there who love a challenge, and would likely pitch in and help you.

| improve this answer | |
  • You posted an answer to your question asking if you really need an external power supply. Don't do that. Post a comment on my answer. But the answer is "very likely". Each relay draws 50-60 mA. At 60 mA, that's 240 mA just for the relays. You might be able to get away with powering that from the 5V supply on the Arduino, but as the relays energize and de-energize they will cause dips and spikes in the voltage from the supply, as the supply tries to adjust to the varying load. You can try it, and if you have problems like the Arduino restarting, switch to a higher current external supply. – Duncan C Aug 20 '19 at 0:55
  • Note that you should NOT use bare relays. You want relays that are made to be driven from logic signals and have built in transistors and flyback diodes. (Bare relays will almost certainly fry the Arduino used to control them.) – Duncan C Aug 20 '19 at 0:57
  • @MichaelMesser Consider buying a relay driver board such as this one: Arduino Compatible 4 Channel 12V Relay Module – sa_leinad Aug 20 '19 at 1:18
  • @sa_leinad I included a link to a 5V 5 channel relay module in my answer. – Duncan C Aug 20 '19 at 12:11
  • Can I use a powerbank as a powersupply? – Michael Messer Aug 20 '19 at 13:48

Yes, it is possible to do that and there are libraries for that purpose. Check for example ObjecButton library, which supports click, press and double click action. It is well documented and comes with many examples...

And does the relays really need a external power supply? It depends, what kind of relays you want to use, what kind of power supply is available on wheelchair, what is the load switched by these relays and so on...

| improve this answer | |
  • The load switched by the relays is really low. Are there alternatives for a relay which have the same function, but a lower power consumption for switching on and off? – Michael Messer Aug 20 '19 at 21:06
  • Yes, there are. For DC load use transistors, for AC load use e.g. triacs or SSR relays. There is plenty Arduino modules on the market with these kind of switching "elements". – JSC Aug 20 '19 at 22:05

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.