loading

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 http://wiringpi.com/pins/ and http://www.hobbytronics.co.uk/raspberry-pi-gpio-pi...
    • 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 http://wiringpi.com/download-and-install/

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.

Enjoy!

<p>I tested wiringpi and changed the pins to output mode successfully, but after editing the config file, the Octoprint server will not start after a reboot. I deleted the changes and it started working again. Attached is a picture of the code changes, which looks identical to yours.</p>
<p>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. </p><p>Is the only way for me to use a mosfet to make a &quot;<strong>Bi-Directional MOSFET Voltage Level Converter 3.3V to 5V</strong>&quot;? Or is there any other way without buying any other components?<br>It would be great if you helped me!</p>
<p>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.</p><p>I don't know what module you have, but some relays &quot;stick&quot; 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).</p>
<p>Hi there!</p><p>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?</p><p>Thanks in advance</p>
<p>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 http://amzn.to/2kIuwQF </p>
Thanks. Can this h-bridge used in AC environment as well? my motors parameters: 230v, 121 w.
<p>Absolutely not &quot;5V-35V Drive current 2A(MAX single bridge). Max power 25W Storage&quot;. 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.</p>
<p>What would it take to use something like this to control a reset switch on a desktop PC?</p>
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.
<p>Hi Julienri, </p><p>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</p><p>Don G</p>
<p>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 http://amzn.to/21SItZu 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.</p>
<p>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 https://www.raspberrypi.org/forums/viewtopic.php?t=26041&amp;p=237424</p>
<p>Consider providing the references of the board...</p>
<p>consider it done. I will be adding links to the parts I used where possible in my instructables as I review them.</p>
<p>links I put last night were broken, sorry. Re-did it today.</p>
<p>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.</p>
<p>Note that the single relay modules may lack the optocoupler.</p><p>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.</p>
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.
<p>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.</p>
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.
Oh, I missed that the module has extra circuitry to drive the relay.<br>So the 3.3V signal off the Pi is recognized by the 5V relay module as &quot;high&quot;?<br>Nice. I can now shop for 5V relay modules instead of 3.3V. Thanks!
<p>I'm a regular user of the 4 and 8 relay versions connected to b+ raspberries running raspbian wheezy. It works like a charm. I used them as my home automation system. I control heaters, lights, gate with them. I've tested a lot of home automation systems, it's the most reliable system i've had. I've completed with some am2302 temperature/humidity sensor and some E18-D80NK position sensors. It's a very cheap and reliable solution. One of my 4 relay board is outside next to my gate, it has resisted to the hot summer and cold winter inside a waterproof electric box. I order these babies on the sainsmart.com website, il's a long delivery but it's cheap!</p>
<p>ps. If you like this, please vote for it in the contest! :) (top right corner of the page) Thanks!</p>
<p>WAH THIS IS AWESOME</p>
<p>haha, thanks!</p>

About This Instructable

83,908views

575favorites

License:

Bio: https://www.youtube.com/making www.thingiverse.com/MakinThings/designs
More by MakinThings:3D Printer PCB Etching Turn your 3D printer into a plotter 100 AMP multimeter upgrade 
Add instructable to: