How to control 16 relays when running out of pins?

Hello :slight_smile:

I plan to buy a 16 opto-isolated relays board from Sainsmart.

But, I run out of pins on the Arduino mega2560 (from Sainsmart too) because I have sensors, touchscreen, and sd card. In fact, I still haven't received my Arduino, I'm just anticipating!

Those relays will control lights, pump, fans, whatever... following a program depending of the sensors's values.

So I need a solution to control all of the 16 relays individually, not just one at a time, they could be all turned ON if necessary, or just few of them, or most of them...

I have read about multiplexers and shift registers, but I'm so much confused, I don't really understand both and I'm not even sure if one of them is a solution for me. In this tutorial they use a shift register and call it multiplexer, so I'm even more confused.

Help a newb' please :slight_smile:

Have you used up all 16 of the analog pins also? I ask in case you weren't aware that they can be used as digital input or output pins also.

Lefty

I'm not sure yet, how many pins will be remaining once I plugged all sensors, touchscreen, real time clock, etc :D, in fact I would like to not run out of pins (when relays are connected) so I can extend my project by adding other sensors or whatever, at a later time. The damn touch screen is annoying, it uses 46 pins (including the sd card slot)...But I need it.

Example circuit and code here for controlling multiple LEDs using a shift register.

Doing relays instead is just a different device.

Well depending on the current requirement of the relay you might have to follow the shift register output with a transistor to drive the relay.

Indeed. And maybe a diode to protect the Arduino/transistor from the relay coils.

But, I think I have read somewhere that the LEDS aren't all turned ON at same time but rather only one at a time, very fast switching to the next LED (persistance of vision). If it's true then it will not work well with mechanical relays such as the ones I linked to :confused: . True, or false?

Grumpy_Mike:
Well depending on the current requirement of the relay you might have to follow the shift register output with a transistor to drive the relay.

I'm not sure what you mean, the relays are opto isolated so all the shift register have to do is turning a small LED ON or OFF, to activate or deactivate a relay, correct? Relays themself are powered by the 12VDC from the relay board and require 20mA each.

It's OK or do I really need a transistor? Sorry, I'm very beginner..

Please see my reply to Grumpy Mike and tell me if diode is still necessary :slight_smile:

You can see relay board here, and full diagram is available in this .rar!

guix:
But, I think I have read somewhere that the LEDS aren't all turned ON at same time but rather only one at a time, very fast switching to the next LED (persistance of vision). If it's true then it will not work well with mechanical relays such as the ones I linked to :confused: . True, or false?

False in this case. You are thinking of multiplexing displays which indeed work like that, but shift registers like the one I linked to have independent outputs which you load up by shifting out the bits you require (using SPI for example) and then "latch" the new configuration which copies the bits from an internal buffer to the output drivers.

guix:
I'm not sure what you mean, the relays are opto isolated so all the shift register have to do is turning a small LED ON or OFF, to activate or deactivate a relay, correct? Relays themself are powered by the 12VDC from the relay board and require 20mA each.

In that case you should be in spec for the 595 shift register. You also would not need the diodes then because the opto isolators isolate the relay coils from the 595 chip.

Thank you Nick Gammon, I have favorited your interesting website.

And Karma++ for everyone :slight_smile: