Controlling Any Device Using a Raspberry Pi and a Relay Module




Introduction: Controlling Any Device Using a Raspberry Pi and a Relay Module


The goal of this ible is to show how to get started with setting up a relay module on your pi and controlling it. My goal is to add scripts to my octopi to turn on and off LED lights, but anyway you decide to eventually control the GPIO commands, the setup is the same.

Step 1: Material

You will need:

  1. A relay module board for the pi. They come with up to 8 channels, you can use as many as you have free controllable GPIO ports. I am using two, if you need more pins, refer to the diagrams found here and
    • I will be using pins that do not change between versions here, but the difference is important to note.
    • The two following pages will help you identify your board
    • Also note that I am using a 5v board, as is the case with most of the relay boards. However, there are 3v boards out there, if that is what you have, just make sure you change the power accordingly to the 3v pin. It is not a very popular option because the 3.3v on the pi is limited to 50ma, so it can take 1 MAYBE 2 relays of the 3v types, but as many 5v relays as the board can take.
    • Last comment, these boards differ from bare relays (i.e. just buying a 50c relay), in that the board has built in protection circuitry so you do not damage your pi
  2. A raspberry pi(does not matter which one, but check the site above to make sure you are using the correct pin mapping.
  3. Wiring and the proper adapters. Ribbon cables are popular, in my case I had old speaker\fan\USB headder connectors from old computers that I just reused and spliced. Single pin header cables would be best.
  4. soldering iron (I like the cordless ones) if you are not using single pin wires\connectors and need to rearrange a few wires.
  5. An 8th inch flat blade screwdriver for the relay board's screw terminals.

  6. optional: Shrink tubing and lighter so that you don't have exposed wiring or gunky electrical tape.

Step 2: Wire the Relay to the Board

We will be using pins 2 (5v), 6 (ground), 11, and 12. They are not side by side, and so I need to rearrange the cable a bit so it has the correct connectors. Also, note that 11 and 12 are the "header numbers" (physical location), but the wiringpi software that controls them calls them pins 0 and 1.

I am using 2 headers to plug into the pi, but still only the same 4 wires, so I snipped the wires that needed to go further and soldered them to the other connector.

The first step is to change it so that on the relay side, pins 1-4 of the connector need to co to a connector that is at least 3 wide and go in the holes 3-1 of the connector on the pi side (i.e. pins 2 and 6 on the pi since that is where the 5v and ground are). So the connector now has to do the following:

Pi 2 - relay 4

Pi 6 - relay 1

Meanwhile, pins 2 and 3 on the relay side are each sent to pins 11 and 12, so it can use a simple 2 wide connector.

Step 3: Connecting the Device to the Relay

In order to connect whatever you are controlling, you will have to cut a leg of the power so send it through the relay. If it is a device that uses only AC power, be careful, and if you can, use the hot as your on\off. If it goes to an DC adaptor (as the LEDs I will be using do), then cut on the DC side. This is much MUCH safer to operate. Cut the - line, so that even if there were some sort of short, there would not be any power going through it until you turn it on.

These relays do have 3 slots because there are 2 options. One is that it turns off when power is applied from the pi, and the other (more conventional use) is that it turns on when power form the pi is applied. Select the one that works for your use.

Step 4: Installation

First turn off your pi, connect everything in place, and then turn the power back on.

I am using wiringpi. Many builds come with wiringpi installed. To test this, use the commands

gpio -v

gpio readall

If it did not succeed, then try installing wiring pi. More information is provided on the wiringpi website

Step 5: Testing

We can now test the relay by sending the commands to turn them on\off.

First use gpio readall to see everything mapped out (useful to know, and good for diagnostics... more on that lower)

Second, assuming you used the same pins as me (otherwise look at your schematic and figure it out), use the commands gpio read 0 and gpio read 1 (this will read the states of pins 0 and 1). With my module, 0 is on, 1 is off.

Third, we want to change the pin mode. We want an out pin, not an in pin. The commands are gpio mode 0 out and gpio mode 1 out.

Lastly we want to actually use the pin, so we will send gpio write 1 0 (the first number is the pin number, the second number is the state).

If it turns on and off as expected, then you are done. If like me, you have some issues, the next step is to read back the pin you just wrote to. It turns out I have problems on my pin 1, and only my pin 0 worked properly. As it turns out, it is because my pin was still set to IN rather than OUT.

That's it, now you can use your devices via SSH terminals.

Step 6: Extra; Adding the On/off Command to Octopi

The code used to add an option to the octoprint web server is fairly simple.

First edit the file ~/.octoprint/config.yam I use nano so it goes

sudo nano ~/.octoprint/config.yaml

Then go down to actions and add the folowing (obviously naming it as you want)

-action: lights on

command: gpio mode 0 out; gpio write 0 0

name: lights on

-action: lights off

command: gpio mode 0 out; gpio write 0 1

name: lights off

I added the mode change for the off option as well just in case the pi were to reboot with the lights on but the mode to IN.


Raspberry Pi Contest 2016

Runner Up in the
Raspberry Pi Contest 2016



  • Fix It! Contest

    Fix It! Contest
  • Creative Misuse Contest

    Creative Misuse Contest
  • Tiny Home Contest

    Tiny Home Contest

25 Discussions

I needed this!!! I will be using servos/motors to control how treats are dispensed for my doggo treat machine with web camera. This looks recent enough to provide valuable information on my own project thanks!

I'm really stuck with my not so original (aliexpress special) relays. Nevertheless I know for sure they work. I have VCC GND and IN ports on my relay. I apply 5v from RPi to VCC, and connect GND to RPi's GND. And when I connect IN to 0V it clicks (which is strange to be honest). I was wondering if when I connect 3.3v from GPIO it will turn it off but with no luck. Switching it between 3.3v and 0v does nothing it is still turned on.

Is the only way for me to use a mosfet to make a "Bi-Directional MOSFET Voltage Level Converter 3.3V to 5V"? Or is there any other way without buying any other components?
It would be great if you helped me!

1 reply

In should be connected to a pin that you can actuate. When you say 0V it makes me think that you connected it to the ground.

I don't know what module you have, but some relays "stick" by design and have to be actively turned on or off. Is this what you have? It would really help if you posted the part numbers. Also, sometimes the PI defaults to having the GPIO pins active so that you have to send a first command to actually turn them off (I had to reverse the default setting on mine).

Hi there!

Thanks for the good post. I would have one question: Would it be possible to control something (actually a window blind motors in my case) in this way. The difference as I see is that you need to be able to move the motors in both directions (up and down). How would you do this?

Thanks in advance

3 replies

Short answer: yes but you probably don't want to. Long answer: for the sake of a DIY project where you want to learn and understand the circuit, you could use 4 relays so that each side of the motor has a + and a - line interrupted by the relay, so that you can turn on pairs of relays to change the side. However, this opens up the risk of a short, and would require some sort of fail safe. You will be better off with one of these modules

Thanks. Can this h-bridge used in AC environment as well? my motors parameters: 230v, 121 w.

Absolutely not "5V-35V Drive current 2A(MAX single bridge). Max power 25W Storage". I didn't realize this was AC. Are you modifying an existing device that has physical buttons? In that case, you could just use relays to bypass / be remote versions of the buttons. You would just have to make sure you can't short anything once again though.


1 year ago

What would it take to use something like this to control a reset switch on a desktop PC?

1 reply

It can pretty much be used as is since it can take AC or DC on the switching side. You would have to figure out if the reset switch on the PC is one where when you press it it opens or closes the circuit. Once you know that, you simply use the relay in a way which works for you. These kinds of relays have the option of always on to off, or always off to on when you apply power to the other side. After that I guess it's just a matter of flipping it back right after you activated it.

Hi Julienri,

I want to build a battery tester for a battery that turns off the load when the voltage drops to 60 volts. Do you have a recommendation on how to sense voltage with a Rasberry Pi and then control a relay to disconnect the load. I will be testing the battery at about 10 amps. Thanks

Don G

2 replies

I was thinking about it some more... I assume this is for some sort of lithium battery? likely lipo? If so, you could use 2 of these they are programable on a per-cell level so that if any cell goes to the low-battery state, the alarm would turn on. You could rewire that alarm to instead feed a relay that is set to tun off when current runs through it.

what you are looking to build is a BMS (they are readily available online for e-bike stuff). To use the pi, you would need to use some sort of shunt and somehow collect safe readings from that, but it's out of my league. You can look here for ideas

consider it done. I will be adding links to the parts I used where possible in my instructables as I review them.

links I put last night were broken, sorry. Re-did it today.

Hi, from your text and photos, it appears that you're directly connecting the inputs of the (5V) relay to the Pi's GPIO pins (3.3V). No transistors, no voltage level shifting necessary? It that correct? Thanks.

4 replies

Note that the single relay modules may lack the optocoupler.

Personally, even at 5v, I've had problems driving a 4-relay module using a PCF8574. For the RBP I've read that an 74LS244 or 74LS245 buffer IC may help, also with bridging different voltage levels. Just thought I'd mention that.

As to your question of the GPIO voltage: the actual power comes from the second connection to the 5V. The GPIO only controls the on/off state but does not power the relay. If you search Internet for relay module and raspberry you'll find a few drawings that show it well.

haha, looks like learn4ever was faster than me :p thanks for the input. Yes that is correct, the 3.3v GPIO is just for the state and the power comes from the 5v pin. I will clarify in the instructions that it should be a module, not just a bare relay.

Well he's not connecting it to a relay directly. He's connecting it to a relay module that has the necessary protection. Have a close look at the relay module. It has a loop back diode and driver. Don't connect a 'bare' relay to GPIO.