Arduino + ST to control multiple Relays

Dear All,

I am trying to control 15 relay units with Arduino Mega and ST Shield.
Let us say we have 15 buttons and 15 Relays.

Need to note that I am not super experienced in coding, but worked from the code by @ogiewon ( Announcing the "ST_Anything" Arduino/ThingShield Project ) but seems like I cant create a second or third relay (switch) control.

For example, I can send specific code such as ON1, OFF1, ON2, OFF2 for first and second relay via the IDE, but it doesn’t work in the mobile app.

My Arduino and ST code is basic strip down from ST_Anything.

I would highly appreciate your guidance and support.

Thanks again for your time!

PS: Does anyone know if I can have a custom action for a switch state?


1 Like

Check out this guy’s stuff:

1 Like

Thanks @keithcroshaw for your support!

Just doesn’t work…

I understand this should go trough a smart app but dont see how to do it.

I use for example just one part of the RelayOff1 from the linked code, but it does not send anything to my arduino. If I do the normal on/off shield example i can get a “on” or “off” message to the arduino. But noting when i try to do the RelayOff1 or RelayOff2.

I feel like there is gotta be a simple solution to this, something I am missing greatly.

Thanks again.

Were you monitoring your arduino over serial to see if you were getting the commands? I know that’s a vague question but that’s what I would do to see if the commands came through.

Yes, I monitor thought the serial to see commands.

On the ST example one i get a “on” and “off” but on the relay one I don’t get anything.

Hmm I haven’t used the actual library I linked you too, only his virtual switch propogation. I’m in the same boat where I can’t make anymore ST_Anything switches for my RC_Switch project, so I might soon rewrite the whole thing since I can’t understand @ogiewon’s ninja code.

I would start from the on/off example and get it so on said relay1 on and off relay1 off then go from there.

1 Like


I have found this:

Dont know if you have seen it or not, but it is even more complex :smile:

I will keep on trying to get something done. If you get any progress on the RC_Switch please do let me know.

Thanks again!


I am using this setup for my home watering system. the smartApp has a lot of logic for timing and scheduling. Maybe by commenting out some lines will simplify the ST to a simply 8+ relay switch. you may still have to make virtual switches to control individual relays from other smart apps.

I just started playing with The Smart Room Controller (formerly SmartKitchen). @dome has developed a great starting point for both a light and relay project I am looking to do. you may be able to see what he coded and use the switch part of his setup.

1 Like


I’d be happy to help you use my ST_Anything library. I just need to understand exactly what you are trying to do.

Looks like you want to control 15 relays attached to Arduino digital output pins, correct?

Where are the 15 buttons? Attached to the Arduino’s digital inputs? Or tiles in the ST App on your phone?

Do you need to be able control these relays from SmartApps?

If you can explain it, I can whip up some code pretty quickly.


1 Like


What problem are you having? Adding more RCSwitches should pretty straightforward. Let me know how I can help.

No problem at all, I think it’s working great other than two nights it just froze up but my C++ skills are not good at all so I can’t really troubleshoot it and I kind of want to know every in and out so I’m going to try to rework it myself, if I fail miserably I’ll let you know. Thanks! :smile:


Like @Javid_Mamedov I am soon going to be tackling a project where I will have multiple relays being controlled by ST App tiles and smartApps. This is still on my roadmap but I am interested to be included in the your discussion with Javid as I know this will help me soon.

Thank you for the time you invested in ST_Anything.





I’ll take a stab at cranking out a multiple relay solution this weekend.


1 Like



Thank you very much for your support and time! Highly appreciate that!

I will try to explain in details what is the aim:

X number of relays and buttons (For ease let us assume that we have 15 buttons and 15 relays).

The buttons are normal push button switch you would get for an Arduino. ( ).

A relay would be a normal 5 V triggered relay. ( )

Push buttons would send signal to digital input which will trigger an event in the smartthings to send a signal back to power the relay.

Alternatively, if you would push the virtual button in the SmartThings App you could trigger the relay remotely.

I think the feedback to see if the relay is open or closed is fed from the Output that powers the relay.

I hope i did not cause further confusion and sorry if too many details.

Thanks again for your time and help with this relay. I feel like many people could do a lot more things if you connect X number of relays to the Smartthings.

Again thank you very much!




OK, your explanation helps a lot. I do have one question though. Why does pressing the physical pushbutton connected to the Arduino send a signal back to the ST cloud, instead of simply turning on the relay locally? The act of turning on the relay locally within the Arduino, causes an update to be sent back to SmartThings, thereby updating the status of the switch-type tiles in the ST Phone Application UI. These changes can also be acted upon by other SmartApps if desired.

Would that design be sufficient?

I already have a version of a ST_Anything_Relays.ino Arduino sketch running that handles 16 relays on an Arduino MEGA. I also have a working ST_Anything_Relays.device.groovy custom Device Type running within SmartThings (which is installed once per Arduino) which shows all 16 relays as ST switch-style tiles within the one device. That was the easy part (relatively speaking.)

I am currently working on a Virtual_Switch custom Device Type (which will be installed up to 16 times) as well as a custom Smart App (one of my multiplexer apps) that will link the Arduino’s 16 digital outputs (i.e. the physical relays) to the 16 Virtual Switch devices. The 16 Virtual Switch devices allow you to activate each relay via “normal ST SmartApps”.

Basic bi-directional flow is as follows:

ST_Anything_Relays Arduino Sketch
SmartThings Shield
ST Hub
ST_Anything_Relays SmartThings Device Type (visible on phone app)
ST_Anything_Relays_Multiplexer SmartThings SmartApp (installed via phone app)
Virtual Switch SmartThings Device Type ( 16 of these) (visible on phone app)
"Normal SmartApps" that can turn on/off switch devices (if not needed, virtual devices and multiplexer not required)

Does this make sense? It can be very confusing at first, and even more so when you start coding it up! :smile: It took me a very long time to figure this all out the first time last year when I made my first Arduino/ThingShield project.

I am pretty good at writing the Arduino code and the Device Type code. The SmartApps (heavy duty groovy code) is not my specialty. I am trying to find a more elegant way to create a multiplexer app without having to copy and paste the same code 16 times with minor tweaks…


1 Like

@Javid_Mamedov, @chaospup, @zuperman4ever, @Todd_Whitehead


I have completed the development of the 16 Relay ST_Anything project and all of the necessary code can be found at

If you’ve never used ST_Anything, follow the basic instructions in the ReadMe to get started. You’ll want all of the Arduino code most likely, especially all of the libraries as I have recently broken them out into smaller subsets to simplify things for users who do not need everything at once…

You’ll also find some new Groovy code that you’ll need. Two Device Types (ST_Anything_Relays.devicetype.groovy and VirtualSwitch.devicetype.groovy.) Use the ST_Anything_Relays device type for your Arduino MEGA which should be running ST_Anything_Relays.ino.

After you get the Arduino and its Device Type working smoothly, you can move on to installing (by hand in the IDE) up to 16 copies of the VirtualSwitch device type. Name them something easy like “Relay1”, “Relay2”, … “Relay16”. Then load/publish the STA_Anything_Relays_Multiplexer.smartapp.groovy as a new SmartApp in the IDE. Add this smartapp using your phone. During its initialization, select the 16 virtual switch devices and the Arduino. Now you have glued the virtual devices to the individual relays in the Arduino.

Review the Arduino sketch as it has PIN assignments for both the 16 relays (digouts), plus 16 local pushbuttons (digins). The pushbuttons were requested by Javid. Using them will update ST with the change to the relay state. Pressing the local pushbuttons will toggle the relay. I have used the Internal Pullup feature of the Arduino so wire your buttons to the pin assigned and to ground.

The relays are set up to turn on a digital output (i.e. supply 5V) when triggered to the On state. Also, the default state for all 16 relays is LOW when the Arduino powers on. These two settings can be changed in the sketch when each relay’s EX_Switch object is created. No changes to the library code should be required.

I have also had to update some of the ST_Anything library files to support all of the features and to improve performance and data integrity. Please download the latest and greatest version of the files.

Have fun! Gotta run!




Thank you very very much for your help once again.

To answer your previous question - yes the buttons would trigger local event while ST just tracks the status and updates the app.

For some reason ( I keep trying but… )cant get trough the Arduino to do anything.

Could this be the fact that i need to change the pins for RX from 3 to 10 and add jumper wire between these pins?

Arduino sketch uploads fine without any errors (following the latest and greatest version downloaded from github).

On the serial I don’t get anything but:

Everything: init started
Everything: Free RAM = 5994
Everything: UNKNOWN

i will keep on trying. Thanks again for your time and help! Really really appreciate!



I believe you are using an Arduino MEGA, correct? If so, my ST_Anything library utilizes one of the MEGA’s additional Hardware Serial ports on pins 14 & 15 as mentioned in the main ReadMe for ST_Anything at github. Using Hardware Serial offers better performance and is more reliable than using Software Serial. Also, this is why you must use the version of the SmartThings library from my GitHub repository. It has added support for the Arduino MEGA whereas the version from ST does not. We also made several optimizations to reduce memory usage and improve performance.

From the ReadMe…

•If using an Arduino MEGA, Hardware Serial is used on pins 14/15 (add jumpers from Pin14 to Pin2 and another from Pin15 to Pin3)

You’ll want to keep the small switch on the ThingShield set to pins2/3. Just do not try using pins 2 & 3 for anything else. Pins 0, 1, 2, 3, and 6 are essentially reserved for use by the ThingShield.

So, just wire pin 14 to pin 2, and wire pin 15 to pin 3. With nothing else wired, that should be enough to get the Arduino talking to ST.

If you connect a pushbutton between GND and pin 38, pressing it will turn on Relay 1. Pressing it again will turn relay 1 off.

Note: in the serial debug window of the Arduino, you will see the “relays” called “switch1”, “switch2”, etc… This is because they are supporting the “Switch” capability of the SmartThings world. This is the same ST Capability that is supported by any device that can be turned on/off, like a light switch, wall outlet, lightbulb, etc… The Arduino’s digital outputs are "switch"ing the connected relays on and off.

Hopefully this gets you started… Happy to help if you run into any other issues.




Any update on whether or not you got everything to run as designed? Just curious how it’s going.


1 Like

Dear Javid

I am new comer in Arduino.
I need some help from you.
I tested Your project with Arduino Mega for control multiple relays (15 relys)
It it possible to put another switch (example Switch No.16) to on and off for the entire 15 relays.
Whie all15 relays on, is it possible to switch off all relys except a switch what I pressed.
foe example ,while all relays on by switch 16,when I press the any switch-say switch 5 is it possible remain on for that corresponding relay 5.

Sorry for my bad english.

Thanks in advance for the code for this.