Connect with us

Rasberry Pi + Relay

Discussion in 'General Electronics Discussion' started by geek_01, Jan 8, 2013.

Scroll to continue with content
  1. geek_01

    geek_01

    6
    0
    Jan 8, 2013
    First off, I apologize but I am quite clueless when it comes to electronics. I am starting to learn, but it seems that I have a long way to go.

    I purchased a Raspberry Pi and was thinking to combine it with a relay as it could prove entertaining. I looked around at relays and the SainSmart ones look nice, I included a link for a 2 Channel Solid State and a 4 channel below.

    I know I have a 5 volt line coming out of the Raspberry Pi, so it seems strait forward to use that, connect the ground and the gpio ports. But, I have seen on forums I need to protect the Rasberry Pi from the relay boards, but I don't know what that means.

    Also, on the 4 channel, it looks like there is a jumper between VCC and JD-VCC. What exactly is that?

    Thanks in advance for any of your time.

    http://www.sainsmart.com/arduino-co...y-module-board-omron-ssr-avr-dsp-arduino.html

    http://www.sainsmart.com/4-channel-5v-relay-module-for-pic-arm-avr-dsp-arduino-msp430-ttl-logic.html
     
  2. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    Driving a relay is a very common and relatively simple thing to do. So I thought a quick Google search would turn up lots of documents that explain it simply, clearly and thoroughly. I was half right - there are lots of documents!

    The best I found after a quick search was http://mbed.org/users/4180_1/notebook/relays1/

    This page has some other information that you might find interesting as well. Have a good careful read of the first part, at least.

    The first diagram shows the general arrangement - a current limiting resistor feeding the base of an NPN transistor, with the emitter grounded, and the relay coil connected between the collector and a positive supply rail, and a diode across the coil to protect the transistor against "back EMF" when the relay is turned OFF.

    The supply voltage for the relay coil must be the same as the relay coil's rated voltage. This is often 5V or 12V. It is better to use 12V if you have 12V available, and it's not a good idea to use a 5V logic supply rail (or 3.3V - that's a very bad idea) for the relay, because of the surges that occur when the relay turns on and off, and if an insulation breakdown occurs, which could disrupt or even damage the semiconductors.

    The relay coil will have a rated current, as well as a rated voltage. This tells you how much current the transistor will have to pass when it turns the relay ON.

    The back EMF diode is needed. You need a diode with a current rating comfortably higher than the relay coil current rating. Generally people use a 1N4001 or similar, which is a general purpose 1A silicon diode.

    The 0V rail of this circuit must be connected to the 0V rail of your Raspberry Pi.

    The transistor must be rated to withstand the relay coil supply voltage; all general purpose transistors have no problem with 12V or 24V. It must also be rated to carry the relay coil current. This is the Ic (collector current) specification. Typical transistors for relay driving are BC547B and BC337 (UK and Europe, 100 and 500 mA maximum collector currents respectively), 2N3904 and 2N4401 (USA, 200 and 600 mA maximum collector current), and 2SC1815 and others (Japan). You don't say where you're located.

    You choose the base resistor to supply enough current to saturate the transistor when driving the specified current through the relay. Let's assume the relay coil is rated at 50 mA and you're using a 2N3904, which has a minimum current gain (Hfe) of 60 at 50 mA collector current. Divide the collector current by the gain, you get 0.83 milliamps. To saturate the transistor you need to feed several times that much current into the base. Let's say five times as much, which is 4.2 milliamps.

    Assuming your Raspberry Pi's GPIO swings to +3.3V when it's high, and the transistor's base-emitter voltage is about 0.7 (a good rule of thumb at low currents), you will have 2.6V across the resistor. From Ohm's Law (R = V / I), resistance is 2.6 / 0.0042 which is 624 ohms. Closest lower preferred values are 620 ohms and 560 ohms.

    You also need to check that the Raspberry Pi's GPIO is rated to deliver at least 4.2 mA from its output.

    You can also use a MOSFET as the switching element. These draw no current from the GPIO pin, which seems like an advantage, but it also means that they could float and activate spuriously if the GPIO pin is tri-stated or in input mode, which might happen during reset, for example. A pulldown resistor can be added to prevent this. Also, MOSFETs that will saturate at 3.3V gate voltage are a bit unusual. Finally, modern MOSFETs are mostly SMT (surface-mount technology) and aren't as easy to work with as big ol' transistors in TO-92 package with three wire leads.

    You can also use a Darlington transistor to drive a relay. This has the advantage of low base current, so low loading on the GPIO pin, but you lose about 1~1.5V in the transistor, so your relay doesn't see all of the supply voltage. This isn't normally an issue. Darlingtons are covered on the page I linked to, I think. I often use the MPSA14 which is widely available and rated at 500 mA collector current, I think.
     
    Last edited: Jan 9, 2013
  3. geek_01

    geek_01

    6
    0
    Jan 8, 2013
    I have to admit, it is rather depressing how much of what you said went over my head.

    I think if I reread it a few (hundred) times and mix liberally with Wikipedia, maybe I can reason it out. Thank you for your time.
     
  4. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    Well, I'm glad that you want to fully understand what you're doing. You seem to be living up to your username :)

    BTW re the jumper between VCC and JD-VCC, I don't know. I found several hits by Googling jumper VCC to JD-VCC but no clarification. It should be covered in the documentation for the I/O board. If you can point me to a schematic of the circuitry that drives the I/O connector, and includes that jumper, I might be able to explain its purpose.

    What do you want to control using your relay? Do you want to use an electromechanical relay or a solid state relay? What voltage rails do you have available?
     
  5. geek_01

    geek_01

    6
    0
    Jan 8, 2013
    So, my original idea was to connect some speakers and make a little server that did Air Play, streaming and a few other interesting idea's like a scripted playlist on certain mornings to help me through my morning routine.

    I though to use a solid state relay to connect to some computer speakers, so I wouldn't have to leave the speakers on all the time. (Save power, avoid the hum of speakers in the room I sleep in.) However the occasional warning to not use mains power unless you are really, really sure you know what you are doing is kind of scaring me off.

    Is this what you asked for?

    http://www.sainsmart.com/4-channel-5v-relay-module-for-pic-arm-avr-dsp-arduino-msp430-ttl-logic.html

    Also this page has information on the Raspberry Pi. It has a 5v line that gives you whatever is left over, so if I used a 5v 1a power supply, I would have 5v 300ma left over.

    http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins

    Thanks again for your time.
     
  6. geek_01

    geek_01

    6
    0
    Jan 8, 2013
    Sorry, but I am going to shamelessly self bump.

    /bump
     
  7. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    Sorry, I must have missed your post #5.

    That page with the I/O connector pinouts is excellent. It doesn't say anything about the JD-VCC jumper though. It looks like there's 5V available on the I/O connector regardless of the jumper position.

    That Sainsmart board needs 15~20 mA drive current on the control signal. That's quite a lot. If you use it, I think you should use a transistor buffer to drive it. Do you plan to build an interface board, or a base board to plug the Raspberry Pi onto, or were you hoping you could just wire the boards together with no glue circuitry?

    That Sainsmart board will also need quite a bit of current from the 5V rail. Those electromechanical relays are pretty heavy duty. They will need lots of coil current. I would go for a solid state relay. How much current do you need to switch? I guess it will be AC mains?

    I'm worried about two things: isolation, and safety.

    The Pi's I/O pins connect straight to the Broadcom chip, so they're vulnerable. You need to keep them as far away from mains voltages as possible, both electrically and physically. The Sainsmart board has optocouplers for this, which is good but is the reason for the high current required from the control lines.

    Safety is always an issue when you're working with mains voltages. A preassembled relay board with connectors for the controlled circuit, like the Sainsmart board, is really good here.

    If it wasn't for those two concerns, I would suggest a very simple circuit using a CPC1945Y solid state relay (see http://www.digikey.com/product-detail/en/CPC1945Y/CLA163-ND/565909), driven directly from the Pi (with a current limiting resistor) and switching the AC mains load directly.

    Do a search for a preassembled board that uses a solid state relay. Something that has the switched circuit brought out on big connectors, and can be controlled by a logic-level signal.
     
  8. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    That looks good.
     
  9. geek_01

    geek_01

    6
    0
    Jan 8, 2013
    I have reread this entire post a number of times, but I am still somewhat at a loss what I would need to do at this point.

    Lets assume I go with the 4x solid state unit I linked. I will be using GPIO from the Rasperry Pi. I am more than happy to put as much gear as is wise between the two pieces of equipment, as I want to do it right. (And hopefully understand why it is right.)

    If I have a slight clue... I need a transistor and a resistor?

    Thank you again for all of your time.
     
  10. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    There's not much detail on the Sainsmart web page. I hope they would include a sheet with the unit, to explain how to connect it up.

    The page says that the board requires a power supply of 5V and draws up to 160 mA. Is this available from the Pi?

    The control inputs on the board are specified in terms of voltage ranges. There are no current figures mentioned. So I assume the board includes some kind of buffering, and it won't draw any significant current from the I/O on the Pi. You can connect them directly together.

    So, assuming there is a 5V rail available from the Pi that can supply 160 mA, I don't think you'll need anything apart from some wires to connect the Sainsmart SSR board to the Pi.

    The board should come with a sheet that tells you how to connect it up.
     
  11. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,449
    2,809
    Jan 21, 2010
    In all likelihood, all you need to do is have a common ground between the RPI and the relay board (that means, in this case, the negative end of their power supplies is tied together) and then to connect the output of your RPI to the input on that board.

    Oh, and the board will need to be powered as well.

    Connecting with a wire should be sufficient :)

    You do need to be careful because the pins you're connecting to on the RPI board go directly to the processor (I believe) and as such a mistake could be a bad thing to make

    edit: Kris' answer above is better.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-