Learning101
Posts: 21
Joined: Sat Feb 10, 2018 10:54 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Tue Feb 13, 2018 10:16 pm

mahjongg wrote:
Mon Feb 12, 2018 12:03 pm
Don't know the BC529B, but yes a BC546 would work too, in fact almost all small NPN transistors will work.
So I picked up some 2n2222 and it worked a treat thanks.

I have the 5v from the PI connected to the VCC on the relay, is this the correct way?

User avatar
QBall1977
Posts: 76
Joined: Thu Aug 09, 2012 10:06 am
Location: Hull, East Yorkshire
Contact: Website

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Wed Feb 21, 2018 11:07 am

I know this is an old thread, but I'm looking for some clarity as I keep reading conflicting things. :shock:

I'm trying to power a Sainsmart Optoisolated Relay (there's 2 of them on the board). I'm using the normally closed function. I just want to be able to apply a signal to one of the pins to turn the relay off. (or rather the device by pulling to the 'Open' function of relay)

The relay is 'SainSmart 2-Channel 5 V Relay Module Board for Arduino PIC AVR DSP Electronic Relay Module MCU' from Amazon - (https://www.amazon.co.uk/gp/product/B00 ... UTF8&psc=1)

I am trying to power the relay from the RPi3, using the 5v and 3.3v. 5v as the JD-VCC, and 3.3v as the VCC (as per this guide) - https://github.com/foosel/OctoPrint/wik ... m-your-RPi

Now, this is where I'm confused as what I have seen written here that says leave the jumper between (JD-VCC and VCC)
.
I am using a transistor the BC548B NPN transistor that I picked up at Maplins. I've tried applying a 1k pulldown resistor, however upon reading this is for 'Active High' and this relay is 'Active Low'

Should I be using a 2.2k resistor and a 10k resistor (as shown in one of the earlier posts in this thread).

So my questions are:
- Do I leave or remove the jumper from the relay board?
- Are the resistor values still the same for RPi3 and the BC548B transistor?

Any clarity (quick yup or otherwise) would be greatly appreciated before I got and source additional parts.
____________________________________________
Looking for where to start - try Kernel Panic - http://goo.gl/EEQ5J

pcmanbob
Posts: 6689
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sun Feb 25, 2018 7:44 pm

So most of these relay boards are designed to work off 5V and trying to drive them off 3.3v does not always work without moding the relay board.

Seeing you intend to use a transistor to interface the pi to the relay board my advice would be remove the 3.3v connection to the relay board.
replace the jumper between vcc and jd-vcc and connect 5V the relay VCC pin

Then connect your transistor circuit like this
Image

if you find the relay does not switch on reliably change the 2.2K resistor on the base for 1K resistor.

your relay board will now be active high.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

snowbord
Posts: 12
Joined: Thu Dec 20, 2018 12:37 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Fri Mar 22, 2019 11:56 am

Great post. I am going to be implementing this. One observation and more of a question really... we know that lines that are not pulled up/down can be susceptible to EMI. The 'From GPIO pin' has been pulled down, which is great, but what about the line to the Relay board?

As the relay triggers when pulled down, in order to avoid EMI, would pulling this line up the rest of the time be a good idea? I.e. pull up the collector line when the collector isn't open. What about adding a bypass cap to the collector (0.1 uF)? That way if the relay board is a long way away, long leads won't end up randomly triggering the relay board. Or will the Opto-isolators circumvent this?

At the end of the day when we connect the relay board direct to the Pi, we manually set the GPIO pin to HIGH and here we are in effect doing no such thing. We are setting the GPIO pin to LOW before the transistor and doubling this over with a pull-down resistor. The line from the transistor to the relay opto-isolator remains susceptible (in my limited experience this is how I understand it).
Last edited by snowbord on Fri Mar 22, 2019 12:08 pm, edited 1 time in total.

pcmanbob
Posts: 6689
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Fri Mar 22, 2019 12:06 pm

The IN pin on the relay board is actually the ground side of the control circuit on the relay board , for the circuit to switch on the relay you need to provide a good ground for current to flow from the VCC to the ground via the IN pin, no EMI is going to provide this good ground so no current can flow and no relay activation.

explanation of typical relay board circuit here : viewtopic.php?f=91&t=83372&p=1225448#p1225448
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

snowbord
Posts: 12
Joined: Thu Dec 20, 2018 12:37 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Fri Mar 22, 2019 12:13 pm

Wow thanks for the quick response and for clarifying! Another observation and please do correct me if I am wrong - this circuit seems a perfect match to drive a LED, especially one with a built-in resistor, for example those 'ring LED' push buttons: https://www.adafruit.com/product/481

The only change that as far as I understand it needs to happen is for the 2.2k resistor to be adjusted - 33k if a standard LED is to be used (together with a resistor). If the 'ring LED' button with built-in resistor is to be used, perhaps 2.2k will be good. You tell me!

Thanks!

pcmanbob
Posts: 6689
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Fri Mar 22, 2019 2:13 pm

Yes you can drive an LED like that, a standard red LED with a 220Ω resistor would flow 16mA so nice and bright.

like this using a different transistor
Image

the button you posted the link for sates it has a built in resistor for voltages from 3 to 6V so no additional resister would be required and the 2.2K resistor in the circuit between the gpio and transistor base may be fine as well.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

wdezell
Posts: 1
Joined: Fri May 17, 2019 5:27 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Fri May 17, 2019 5:44 pm

An excellent collection of useful information but in case anyone is interested it is possible to safely drive the Sainsmart 4-Relay board directly from the RPi without an intermediary circuit. The same probably holds true for the 5-relay board as well but I don't have one to test.

The Sainsmart relay board is a well-made but poorly documented piece of hardware. However, if you dig around on the Internet you can find its schematic (symbols only, no values for components). However, the inputs are opto-isolated and can be configured for direct 3.3V drive (RPi, Beaglebone Black, etc..). As-shipped it's in a 5V-drive configuration (Arduino, etc...).

To connect to the RPi do the following:
  • Remove the JD-VCC jumper. Identify which of the two uncovered pins is connected to the VCC pin that's with the inputs. Store the jumper by sliding it back onto this pin.
  • Connect the other uncovered pin to the RPi's 5V rail. This powers the relays.
  • Connect the VCC pin adjacent to the inputs to the RPi's 3.3V rail. This powers the opto-isolators.
  • Connect RPi GPIO lines directly to the desired Input pins.
The relay board is negative logic. A GPIO low activates the relay. The RPi 5V rail is fed directly from the USB power feed and should be sufficient to energize all simultaneously. I have no insight on inductive kickback.

pcmanbob
Posts: 6689
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sat May 18, 2019 10:35 am

wdezell wrote:
Fri May 17, 2019 5:44 pm
An excellent collection of useful information but in case anyone is interested it is possible to safely drive the Sainsmart 4-Relay board directly from the RPi without an intermediary circuit. The same probably holds true for the 5-relay board as well but I don't have one to test.

The Sainsmart relay board is a well-made but poorly documented piece of hardware. However, if you dig around on the Internet you can find its schematic (symbols only, no values for components). However, the inputs are opto-isolated and can be configured for direct 3.3V drive (RPi, Beaglebone Black, etc..). As-shipped it's in a 5V-drive configuration (Arduino, etc...).

To connect to the RPi do the following:
  • Remove the JD-VCC jumper. Identify which of the two uncovered pins is connected to the VCC pin that's with the inputs. Store the jumper by sliding it back onto this pin.
  • Connect the other uncovered pin to the RPi's 5V rail. This powers the relays.
  • Connect the VCC pin adjacent to the inputs to the RPi's 3.3V rail. This powers the opto-isolators.
  • Connect RPi GPIO lines directly to the desired Input pins.
The relay board is negative logic. A GPIO low activates the relay. The RPi 5V rail is fed directly from the USB power feed and should be sufficient to energize all simultaneously. I have no insight on inductive kickback.
Hi.

Yes the Sainsmart ones often work when configure like that , but many of the cheap copies do not , some don't even have the option to split the the 2 supplies by removing a jumper which is why people run in to problems.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Interfacing (DSI, CSI, I2C, etc.)”