Introduction: Radio-Controlled (R/C) Relay Driver
It is very cheap and easy to connect relays up to a radio controlled receiver !
This project will connect 4 (or 8 or more) relays up to one channel of your radio-controlled receiver so you can remote-control switches and lights and things.
For this project, you will need:
1. an R/C receiver (you already have this!)
2. an Arduino Nano (these cost about $1.67. search eBay: Arduino Nano)
3. a relay module (you can get 8 relays (30v 10amps) for $4.47, or 4 relays (125v 20-amps) for $8.69) search eBay: 8-Channel Relay Shield
4. a servo lead to connect your R/C receiver to the Arduino-Nano; cut one off an old servo or extension lead, or, buy some off eBay. You can even use standard breadboard hookup wiring: it turns out that these female sockets are the exact right size to plug in to R/C receivers! search eBay: female breadboard jumper
5. some wire to connect the Relay-Shield to the Arduino-Nano. I chopped up an old floppy-disk-drive ribbon cable for this.
The position of the R/C stick determines which one of the relays activates. You can de-solder the stick potentiometer and connect a keypad with resistors if you want to trigger the relays using buttons instead (this step not shown).
Step 1: Program the Arduino Nano
The Arduino-Nano (or just Nano for short) is a computer with a bunch of pins that can read and write signals.
First - we will program this to run the software which tells it how to read the R/C signals, and write the Relay outputs.
Unpack your Nano, and plug it into your PC using a USB cable. Some lights should flash a few times, and the power LED should come on.
Download and install the programming software needed to write the program to the Nano: it is called the "ARDUINO IDE" and can be any version - the one as at time of writing is 1.6.13 - get it here: https://www.arduino.cc/en/Main/Software
Step 2: Tell the Arduino-IDE That You Are Using an Arduino-Nano Board
Select the "Tools" menu option, then the "Board" sub-menu, and select the "Arduino Nano" option.
Step 3: Choose the Port Your Arduino-Nano Is Connected To
Your Nano shows up on your PC as a serial port. On mine, it's COM14:
Set the correct COM port using the Tools menu option, and the Port sub-menu.
If you don't know what port to use - you can open your control panel, then open the "System" utility in that, then open the "Device Manager" panel, and expand the "Ports" option. you will see some COM ports in there. Take a close look at the ones you can see, then plug or unplug your Nano - whichever new one goes away or appears, is the one to use.
Step 4: Send the Program to the Nano
Save the attached file ServoRelayPro.ino on your computer somewhere (update: use the ServoRelayProV3_1.ino file if you want more features - see step 9 for details, or, use the ServoRelayPro3_8relay_i10.ino file if you plan to program your Turnigy-i10 transmitter to use 8 relays - see step 10), and open it in the Arduino-IDE.
Click the upload button to compile and send it to the Nano (this is the "right-arrow" in the circle.)... alternatively choose "Sketch" from the menu, and the "Upload" sub option.
If it all worked, the Nano LED should flash 3 times at the end, which is our Relay program letting you know it's running OK.
Step 5: Prepare the Wires
You will be soldering the R/C servo wire and the relay board wires onto the Nano.
Two of these wires need to go through the same hole (labelled VIN) on the Nano, so, use wires that, when twisted together, are still small enough to go through the hole!
Twist the RED servo-wire end together with one of the relay board wire ends (I suggest using another RED one - this is the power lead).
Step 6: Solder Power - the R/C Servo Red Wire and Relay Red Wire, Onto the Nano
Unplug your Nano from your PC.
One of the Nano holes will be labelled VIN - it is usually the one that is the furthest end away from the USB connector.
Push the pair of red wires that are twisted together through this hole, and solder it in place.
Step 7: Solder the R/C Servo Wires
The Black (negative) wire going to the servo plug is negative.
Solder this to the GND pin that is usually located next to the VIN pin on the Nano.
The remaining servo wire is the signal wire, and is usually white - solder this to the Nano digital pin hole that is labelled D12 (this is usually the end pin right next to the USB socket - it's diagonally opposite the VIN pin.)
Step 8: Solder the Relay Wires to the Nano
The VCC is the red wire and should already be soldered to the Nano VIN hole.
Solder the GND pin to the Nano GND hole (usually the 4th hole down from opposite the VIN)
Solder the remaining servo wires in a line, starting at D11 (for 4 relays, this is D11, D10, D9, and D8). For 8 Relays, continue using D7, D6, D5, and D4.
Step 9: Test and Enjoy
Double-check all your wiring!
It should match what you can see in my picture.
------------------------------------------------
To operate:
1. plug in to your R/C and turn your R/C on.
2. your Nano LED light should blink 3 times.
3. Move your R/C stick slowly - as you pass through each of the relay zones, you should see the relay LEDs illuminate and hear the relays "click" - the Nano LED will also light up each time a relay is energized.
------------------------------------------------
How it works!
R/C receivers send pulses through a signal wire (the white one) to tell servos what position they should be in. A pulse of about 1ms in length means "all the way counterclockwise", and a pulse of about 2ms means "all the way clockwise" with a pulse of about 1.5ms means "in the middle".
We program our Arduino Nano to measure the length of these pulses.
We divide the total stick travel into 9 zones (for 4 relays) or 17 zones (for 8 relays). The first zone is "off" (no relays turned on), the second zone means "relay 1 on", the third zone means "off" again (no relays turned on), the fourth zone mean "relay 2 on", and so on - the final end (last) zone is also off again.
So - we have one zone for every relay, and inbetween every relay zone, is a "blank" (zero) zone.
To prevent interference and accidents, this setup will not activate any relay unless it has been selected for at least 200ms (1/5th of a second). Same for de-activation - so - each relay will always turn on (once activated) for at least 200ms minimum.
The Arduino Nano has a built-in power regulator (VIN), which makes it safe to connect your R/C output (usually 6 volts) to the Nano (which is happiest consuming 5volts, which is what the built-in regulator feeds it).
The Relay modules ("shield" is another name for "module") accept the 6volt power from your R/C, and have inbuilt LEDs and opto-isolators of their own, making it safe to connect everything up to one-another without needing any resistors/diodes/etc at all!
One day in future I'll come back and add more instructions here - showing you how to add a keypad to your transmitter to individually activate relays using buttons.
------------------------------------------------
Warning:
Do not plug your computer into the Nano when it is already being powered by your R/C receiver. I don't know what might happen if 2 different things try to power this at the same time!
hint: if you disconnect the red (middle) power wire from your R/C, then it is safe to power the Nano via USB then (this won't work the relays though - we soldered their power to the VIN which is not used when USB is supplying the power)
------------------------------------------------
Extra: attached here is version 3 of the software (ServoRelayProV3_1.ino) - this is functionally the same, however, the Arduino-Nano's LED is used to communicate to you what is going on inside it's brain. If you load this version onto your Nano, then the blinking LED has the following meanings:
a. right at the start - blinks 4 times (if it's programmed for 4 relays) or 8 times (if it's programmed for 8 relays)
After startup:-
b. not blinking - no signal detected from the R/C receiver.
c. blinking really fast continuously - R/C signal detected (it blinks once for every 2 signals that come in - signals come very fast)
d. one short flash, then off for a long time (about half a second): Relay #1 is energized
e. two short flashes, then off for a long time (about half a second): Relay #2 is energized
f. three short flashes, then off for a long time (about half a second): Relay #3 is energized
g. four short flashes, then off for a moment (about half a second): Relay #4 is energized
h. five short flashes, then off for a long time (almost 1.5 seconds): Relay #5 is energized (if you have 8 relays, the delay is 1.5 seconds. If you have 4 relays, the off-delay is only half a second.
i. and so on...
Attachments
Step 10: Setup Your Transmitter to Trigger the Relays
There are lots of ways to use your transmitter to trigger relays.
The easy way is imply to hook up a stick, like a throttle level, and as you move this stick, it will trigger whichever relay is activated by the zone the stick stops in.
Another way is to set up the buttons on your transmitter so they trigger the relay you select directly.
This video shows you how to configure a Turnigy-i10 transmitter with two of it's 3-position-switches so that using all combinations of up+down+middle on each of those two switches, you 9 possible outputs (one for every relay, plus one extra for "no relays".
Basically - this is how we do that:
We "mix" the relay output channel (which normally outputs -100% through 0 to +100%) with itself, so that when it wants to output -100, we mix in an additional 25 (so it really only ends up outputting -75),, we mix 0 with 0 so 0 still outputs 0, and we mix -25 with it's other output of +100 so it really outputs +75. In other words: we limit the range of the relay channel from -75 to +75 with 0 in the middle.
Then, we mix a second channel in with the above, programming this second channel to mix +25 or 0 or -25... giving us the desired 9 possible outputs on one channel.
-75 mixed with -25 = -100
-75 mixed with 0 = -75
-75 mixed with +25 = -50
0 mixed with -25 = -25
0 mixed with 0 = 0
0 mixed with +25 = +25
+75 mixed with -25 = +50
+75 mixed with 0 = +75
+75 mixed with +25 = +100
The attached Arduino Nano sketch goes with these video instructions and the above settings to make all this work nicely.
14 Comments
1 year ago
Great write up Thank you so much for this, I ran into 2 issues with my no-name 8 relay board.
The first you addressed with the note about changing the code from 0 to 1 and 1 to 0 in this section so that the relays weren't always active
if(whichrelay==1)pinval=0; else pinval=1; digitalWrite(RelayPin1, pinval);
Thank you so much I am not skilled enough yet to have found this on my own.
The second that I am not knowledgeable enough yet to resolve is that upon initialization all the relays are energized until the RC input changes to cycle any of them on or off.
My use case for one of these is to use this process to trigger sound FX based on the pin that is selected but they will all be triggered until I cycle the selector.
How can the uno default to all off upon initialization? Should I be using a different Nano brand?
Thanks For doing all the heavy lifting on these sketches.
One thing I ran into but was able to resolve was that only the first 4 relays would work. The nano LED was flashing for 5-8 as they were selected but the relay wouldn't fire. The first 4 were on the first half of the stick movement so I knew it was mostly working. In the sketch that I downloaded there was only up to pinMode(RelayPin4, OUTPUT); adding 4 more lines worked great. Sorry If I missed a step in the instructions. Just wanted to post what I found incase someone else ran into that.
pinMode(RelayPin1, OUTPUT); // Connect these to the relay block
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(RelayPin5, OUTPUT);
pinMode(RelayPin6, OUTPUT);
pinMode(RelayPin7, OUTPUT);
pinMode(RelayPin8, OUTPUT);
Question 2 years ago
After building this I found that all the relay coils are energised when the unit is connected to the receiver. As you move the Tx stick each relay is switched off as the switching position is reached, i.e., it works in reverse. I am using a different relay board, similar to this one (http://wiki.sunfounder.cc/index.php?title=4_Channel_5V_Relay_Module) I think the coils only pull 70mA a piece but I'd rather not have extra current drain on the receiver battery. Do you have any ideas?
Answer 2 years ago
Switch all the pinval numbers - change all the =0 to =1 and all the =1 to =0 :
... so it looks like this after:-
if(whichrelay==1)pinval=0; else pinval=1; digitalWrite(RelayPin1, pinval);
if(whichrelay==2)pinval=0; else pinval=1; digitalWrite(RelayPin2, pinval);
if(whichrelay==3)pinval=0; else pinval=1; digitalWrite(RelayPin3, pinval);
if(whichrelay==4)pinval=0; else pinval=1; digitalWrite(RelayPin4, pinval);
Reply 1 year ago
Thank you so much for coming back to look over and replay to comments. I had the same behavior with my board that the relays were all engaged. Your updated note here fixed that right up. Great write-up and follow-ups.
1 year ago
The project works just like it should, I was wondering if its possible to expand the window for each relay. I'm wanting low stick off then trigger relay #1 with relay #1 still on through the rest of the of the other relay trigger points
Question 3 years ago
Hi can you tell me what resistors to use if I want to replace the pot for 4 relays and 8 relays?
I am trying to switch 12 relays with 2 arduinos and 2ch receiver.
One channel will switch 8 relays from 8 different switches and another one will switch 4 relays from another 4 different switch.
I will use one channel of throttle pot and the other channel of steering pot.
Thanks very much for your feedback
Answer 2 years ago
Disconnect your throttle and you'll see the potentiometer has 3 leads - one is connected to the bit that moves, and the other two are the ends. Measure the resistance between the ends (the measurement should not change when you move the stick). When you know this number, I suggest you buy a small bag of trim pots of that same value. You can then hook them all up to your buttons, and use a small screwdriver to turn them so each one activates whichever relay you want.
3 years ago
Hello cndg,
Is it possible to calculate in milliseconds, the time it takes for the nano arduino to cycle the code and to activate the relay?
Thank you!
Reply 2 years ago
Yes, it's about 62 nanoseconds (0.0000625 milliseconds) - but I don't think that's what you mean.
The variable "stabledelay" is how long the code will wait before it triggers the relay, and that is set to 100ms (you can change it)
3 years ago
Use a regular R/C (radio-control) transmitter and receiver, and then the output signals of the reciever will work for this project.
4 years ago
Great Tutorial, i just like to use one switch on the transmitter to control one relay on/off i try to figure out based on your code how to do that but so far no luck, can you help with that
i am using nano and FS-I6 receiver and transmitter it is for my RCLawnmover project
the Relay just to kill the engine
Reply 4 years ago
How about just using a servo to kill the engine?
You could use just one relay with a nano, but the code for that would be totally different to what Iv'e done here (mine does a stack of "rocket science" to break out all those relays!)
The code you need to write will have to measure the incoming PWM signal from your radio for the switch you want to use, and when the arduino detects the pulse being longer (or shorter) than the "middle" range for your channel, it then needs to output the signal to energise the relay.
5 years ago
I really like how professional you explain the steps and when i went through the code show clear perfection and clear explination for every thing
6 years ago
I have not tried the following, however, this video shows you how to set up your TGY-i10 transmitter to use Switch-F and Switch-H to individually output specific commands to one channel:
Using any two 3-position switches (up/middle/down) gives you 9 possible combinations: so with an 8-relay board connected, you can control every relay individually and use the 9th position for "off".