Building an Arduino based relay switch (Updated)

19 June 2015

Recently I came across the MySensors project and used that to build a temperature, light and humidity sensor based on an Arduino Pro Mini micro processor. I want to automate the control of some outdoor lights I have around my home. Currently I have to switch these on and off manually. I could add a timer switch to them but the time it gets dark changes everyday and is also influenced by weather conditions. Hence the light sensor to measure light intensity.

Now that I have build that sensor (see my last blog post ) it's time to build the relay that will switch the lights.

I've changed the relay used in this blog post.

The sensor is based on a Arduino Pro Mini. This is a tiny Arduino computer without a USB connector that runs at 8 Mhz on 3.3V. The relay will switch the mains powered lights so I will have mains power available for it. That means I don't have to focus on power saving and I can use the relay as a repeater node to increase the range of the MySensors network. The easiest way to convert 230V to something the Arduino can use is to use a USB power adapter. So this project is based on the Arduino Nano. It's a bit larger than the Pro Mini, comes with build in USB and it can output both 5V and 3.3V. To power it I only need to plugin a USB cable connected to a USB power adapter.

The relay used will switch the 230V line going in to the LED driver. It would be possible to switch the 12V DC line coming from the LED driver. However that would mean that the LED driver would be turned on 24/7 consuming elecrtricity while there would be no load on the driver for most of that time.

I've started with a cheap mechanical relay. That relay however stopped working after two days. It stayed stuck open keeping the lights burning 24/7. I've switched to a solid state relay and have been using that for two weeks now without any problems. I've updated the parts list. The source code does not need an update.

The complete list of parts:

Together with the three parts you will need some wire, solder and a soldering iron to connect them. When prototyping I use dupont cables.

Hardware prototype

For the prototype I connect the radio and relay using dupont cables. The breadboard isn't needed with this, none of the cables are shared. The relay uses 5 volts and the radio 3.3 volts. The Nano has both outputs so there's no need to upsample the voltage. When using a Pro Mini you would need to do that.

The wiring diagram for the radio:

Arduino pin Radio pin
CE 9
SCK 13

The relay connects to one of the digital pins of the Arduino.

Arduino pin Relay pin

On the 230 volt side there are three connectors. A normally open, a normally closed and the input. The 230 volts mains will go into the input connector. Then the 230 volts cable to the LED driver will connect to the normally open output. The normally closed output isn't used but it will be powered when the relay is switched off so beware of that.

The relay has a jumper switch to set how the normally open and closed work. I connected it so that when the Arduino signal pin is set to high the relay will switch the normally open to on. This way when the Arduino is powered off the relay will be off as well.

Arduino MySensors Relay switch

The software

As the relay will be mains powered I have the opportunity to use the relay as a MySensors repeater. I included that option in the source code. The source code is hosted on Github and commented to explain how it works. The software stores the most recent state of the relay. When the Arduino resets for whatever reason the relay will be switched to the state it was in before the reset. It will also report it's state to the controller so that is also up-to-date.

The Nano has a build-in USB connection allowing it to be connected to the computer directly. No need for the FTDI connector.

Adding to Domoticz

On power on the actuator will register itself with Domoticz. Don't forget to allow new devices to be included when you changed the default setting. The relay will show under devices and when added to Domoticz it is shown under the "Switches" tab.

Writing the scene

Now I can manually turn on/off the relay using Domoticz. However I want to use it to automatically switch the lights based on how dark it is outside. For that I will use the lux value reported by the light sensor to determine if the lights need to be turned on.

The lights don't need to be turned on between 22:00 and sunrise. When the light level reaches 200 lux the light needs to be turned on. To program this scene I use the build-in blockly interface of Domoticz. Everytime the sensor reports the light level the blockly will run and switch on the light if needed.

The lights don't need to be on after 22:00 hours. A second blockly checks the time and turns off the lights when it's 22:00 hours and the lights are on.


For the case I use a standard grey electrical box that are available at hardware stores here in The Netherlands.


To give you an idea what the total costs are of this switch here's a list of the parts with the price I paid for them.

Part Price
Arduino Nano $3.37
Solid state relay $3.99
NRF24L01+ wireless radio $0.86
1x 10 uF capacitor $0.04
USB power adapter $2.97

Total cost: $11.23