Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Using GPIO and confused.

Fri Jul 17, 2015 8:54 am

I am trying to switch on/off a lamp (or whatever) with a raspberry B+. I bought a 2 relay unit for 220V ~. see http://www.ebay.de/itm/321648263942?_tr ... EBIDX%3AIT

This unit does not have optocouplers but I see what looks like transistors to drive the relais.

I connect the relais unit to ground and 5V (of the raspberry) and the green led comes on. I connect a GPIO pin to an input connection (2 available on the relais print ofcourse).

When I setup the GPIO pin for output the GPIO pin is (apparently) grounded and the relais is attracted. When I set the pin (after that) high or low nothing happens. When I setup the pin for input the relais comes back in its rest position.

The good thing is that I can drive the relais as I want.
The confusing thing is that setting the pin high or low does not give the expected result (the relais does nothing).

I am sure this is not the way to drive a relais (but it works this way)


I suspect something with the current settings of the GPIO pin.

I program everything with Python. Other pins give the same result.

Can anybody give me a hint what is going on?

gordon77
Posts: 4689
Joined: Sun Aug 05, 2012 3:12 pm

Re: Using GPIO and confused.

Fri Jul 17, 2015 9:05 am

So you connecting Vcc to +5v, ,a gnd and a GPIO o/p to in1 or 2 ?

The device is made for an Arduino so may need 5v on the in1 or 2 pin.

You would imagine there must be a market for a purpose made 4 relay card for the pi by now.

User avatar
joan
Posts: 14763
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using GPIO and confused.

Fri Jul 17, 2015 9:24 am

It may be damaging your Pi to use it as you are. It may be sinking 20mA through the gpio when the relay operates. If so that is more than the gpios are designed for but may be safe.

It does say it works with a MSP430 which is a 3V3 device like the Pi.

Perhaps a safer way would be to drive a ULN2003A or similar from a Pi gpio and use the ULN2003A to switch the relay.

Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Re: Using GPIO and confused.

Fri Jul 17, 2015 9:41 am

Yes I am using the ground and +5V pin of the raspberry to drive the relais unit. When I connect the input of the unit the the raspberry ground the relais attracts (and the red led goes on). So when I set a raspberry pin to output it actially goes low (and the relais attracts).

I am also worried about the current that goes through the GPIO pin. I therefore mentioned that the relais is problably driven by a transistor. (I should measure the current; I will do that soon).

I suppose an optocoupler draws less current than this setup. Is that (generally) true?

Another relais unit is in the/my pipeline (from China to Germany). It has optocouplers and I will also try that unit.

Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Re: Using GPIO and confused.

Sat Jul 18, 2015 12:58 pm

I measured the current through the GPIO pin and it was only 4 mA. Well below the limit of 20 mA.

I have read things about setting the current limit of an GPIO port. What call do you need for that?
Is there a way to see the current current limit?
What would be the default setting for the current through an GPIO pin?

Or is there an other explanation?

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Sat Jul 18, 2015 1:54 pm

I think this relays module use inverse signal with a transistor.

GND on IN pin = relay activated
5V on IN pin = relay OFF

There is three methods to connect the relay on the PI.

1- Use three diodes in series. (1n4148 or 1n400X) cathode to the GPIO , Anode to the IN relay. This way the 3.3V won't conduct current.

2- Use a low signal NPN transistor, like a 2n3904 or 2n2222, to activate the IN signal. A 10K ohm resistor from the GPIO and the transistor base. Emitter to gnd and collector to the relay IN pin.


3- Use an 3.3V to 5V sparkfun, or adafruit, adapter level converter. https://www.sparkfun.com/products/12009


Option 1 & 3 are inverted signal. A GPIO at low level activate the relay. Option 2 has is own inverter , the transistor, GPIO high is relay activated!

B.T.W. This problem was post on the french side.
viewtopic.php?p=763678#p763678
Last edited by danjperron on Sat Jul 18, 2015 2:05 pm, edited 2 times in total.

User avatar
joan
Posts: 14763
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using GPIO and confused.

Sat Jul 18, 2015 2:02 pm

Rocus wrote:I measured the current through the GPIO pin and it was only 4 mA. Well below the limit of 20 mA.

I have read things about setting the current limit of an GPIO port. What call do you need for that?
Is there a way to see the current current limit?
What would be the default setting for the current through an GPIO pin?

Or is there an other explanation?
That should be quite safe at 4mA.

I think the wiringPi and the bcm2835 libraries provide methods to change the "pad" amperage.

I should repeat I do not understand electronics so be guided by people who do like danjperron above.

Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Re: Using GPIO and confused.

Sun Jul 19, 2015 10:15 am

Thanks for the replies.

I am not sure the french posts describe my situation. (maybe my fault because my french is a bit rusty).

I understand my problem comes from the 3.3V versus 5 V discrepancy. (but I am not sure)

What I observe is the following:

The board is connected to ground and 5V. The green led is on. (everything OK)
I connect the input of the relais board to ground (of the Pi), the relais attracts (everything OK)

I setup a GPIO pin as output and the relais attracts.

I set the port HIGH or LOW: nothing happens; the relais stays attracted

I set the GPIO port as input and the relay comes in its rest position. The internal resistance in the port gets high and
the relais returns to its unattracted position.


So The High is not enough for this relais board. (3.3 opposed to 5)

I can switch the relais on/off with setting the GPIO port as output and later as input.
My question is: is this in any way harmfull to the raspberry?
It obviously is not the preferred way , but it works.

I understand the 3 remedies given in the other posts but they all require extra components. Is there a relais board that works whitout extra components and switches with the HIGH / LOW commands?

User avatar
DougieLawson
Posts: 37732
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Using GPIO and confused.

Sun Jul 19, 2015 10:58 am

Rocus wrote: I am not sure the french posts describe my situation. (maybe my fault because my french is a bit rusty).
In that case why do you keep writing relays with the French spelling "relais"?

Take a look at the top of your relays what voltage can they switch, what voltage do they need to activate the coil? Can you trigger them by simply wiring one side of the coil to GND and the other to 5V0 (no Raspberry Pi needed)?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Re: Using GPIO and confused.

Sun Jul 19, 2015 11:29 am

Sorry for my spelling. I am switching between English, French, Dutch and German.

There are no details on the board except somewhere 5V. It can switch 220V. The input to the ground attracts the relay.

I want the raspberry to switch on/off things. In fact it must switch off a modem and router because after an outage of my internet connection they don't reconnect sometimes. I live sometimes elsewhere so a hard reset is not possible. This setup is an automated reboot of things (otherwise I can not use this network because it is not connected to the internet). A software reboot of my modem/router is difficult because they can only be reached by webinterface (and sometimes the webinterface in not reachable). So do things the microsoft way: control-alt-del or reboot).

Next step would be to ask the nabours to reboot my modem/router but if things can be automated, they must be automated!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12670
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Using GPIO and confused.

Sun Jul 19, 2015 12:25 pm

if your relay module is working with a PNP transistor, and driving the IN pin low turns the relay on, (thus it is using the schematic of the french forum) then it is NOT directly compatible with a PI !
In fact because connecting the IN of the relay module to a GPIO of a PI may actually damage the PI, because 5V may be put on the GPIO Pin!
That it doesn't work is logical, because the PI can either output a 0V or a 3V3 signal, and both turn on the PNP transistor!

To make it work add an NPN transistor, (2N2222 or BC547) with a 4K7 base resistor. Emitter to GND, collector to IN of relay module, gate via 4K7 to a GPIO pin.

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Sun Jul 19, 2015 2:30 pm

The board is connected to ground and 5V. The green led is on. (everything OK)
I connect the input of the relais board to ground (of the Pi), the relais attracts (everything OK)
Just the last sentence confirm that you have the same kind of relay!

At least did you try one of the methods I described to you?

Others options are possible but they are problematic.

- Use a UNL2803 or ULN2003 to drive the relay. There is a possible leakage current, you will have to add a load resistance.

- It is possible to add a resistor value to the relais module between the emitter and the base of the PNP transistor to change the trigger point . Something around 330Ω but this method is problematic because there will be current at the gpio to the power 3.3V. This could damage the GPIO or increase the 3.3V power enough to hang the PI.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12670
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Using GPIO and confused.

Sun Jul 19, 2015 11:16 pm

something like an ULN2003 (darlington array) is completely unnecessary as it will only need to drive the base of the PNP transistor, that is actually driving the relay. Also, the relay itself draws just 30 or so mA @ 5V.

Just a simple NPN transistor (or N-FET) is needed to solve the problem of driving the PNP transistors base low with an 0V/3V3 signal.

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Mon Jul 20, 2015 12:59 am

@mahjongg
something like an ULN2003 (darlington array) is completely unnecessary
I know that since I put the best solutions on the sixth post. The NPN transistor was if of them.

This was other possible solutions and I has said not the best.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12670
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Using GPIO and confused.

Mon Jul 20, 2015 5:39 pm

danjperron wrote:@mahjongg
something like an ULN2003 (darlington array) is completely unnecessary
I know that since I put the best solutions on the sixth post. The NPN transistor was if of them.

This was other possible solutions and I has said not the best.
Yeah, you can alternatively drive a similar relay without needing a relay board, with an ULN2003.

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Mon Jul 20, 2015 6:18 pm

Yeah, you can alternatively drive a similar relay without needing a relay board, with an ULN2003.
But not 220VAC like the owner of the post try to do ;-)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12670
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Using GPIO and confused.

Wed Jul 22, 2015 3:53 pm

But not 220VAC like the owner of the post try to do ;-)
I read that as the 220V AC is on the switch side of the relay, (not on the coil side) and the PI is perfectly able to drive a mains switching relay. So yes, driving a mains switching relay with a ULN2008 is possible.

Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Re: Using GPIO and confused.

Mon Jul 27, 2015 10:49 am

Again, I understand the answers given by the vareous people. They all have the disadvantage that you need extra components. I prefer a simple connection between the relais unit and the raspberry, ie 3 wires.

As I understand it a simple transistor would be enough in my case. It just would convert the 3.3 V to 5 Volt level. Because I received another relais unit with opto couplers a few days later, I tried that and it works as expected: GPIO pin as output and High/Low swithes the relais. Because this solution is also inexpensive and requires 3 wires, I do with this unit.

Indeed the 220V side is at the switch side. The relais must switch a modem off and on again.

I still don't understand one thing:

To switch the relais on/off with the GPIO pin as output/input pin works. I agree that is not the preferred method but I don't see why this would be harmfull to the GPIO input pin. When I set the pin as output and thereby low the relais switches on (the current only 4mA) can not be harmfull. Setting the pin as input exposes (maybe) the pin to 5V (of the relais unit) but the current will be low because the internal resistance of the input pin of the relais unit will be high. (because it is designed as input)

Can anybody explain to me what is wrong with this reasoning.


Anyway, the conclusion is for me to use relais units with optocouplers...

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Using GPIO and confused.

Mon Jul 27, 2015 11:16 am

Hi there,

I think the problem is that the transistor is latching 'on'.

When you drive the GPIO output low it causes the transistor to conduct, which energises the relay.

To turn the transistor off, you must bring the base to 5V, however, when you drive the GPIO high it only outputs 3.3V. This is insufficient to turn off the transistor, so it continues to conduct.

My guess is that there is a pull-up resistor on the PCB which will pull the transistor base up to 5V. When you switch the GPIO to input, it becomes a Hi-Z load and "disappears", so the pull-up will have an effect and turn off the transistor. So, you are connecting a 5V line to the GPIO pin through the pull-up. Most people will tell you that's bad. And it probably is.

Can you draw a schematic of the relay board? I couldn't find one for that model online.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12670
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Using GPIO and confused.

Mon Jul 27, 2015 11:38 am

When I set the pin as output and thereby low the relais switches on (the current only 4mA) can not be harmfull. Setting the pin as input exposes (maybe) the pin to 5V (of the relais unit) but the current will be low because the internal resistance of the input pin of the relais unit will be high. (because it is designed as input)
NO!
that doesn't matter, if 5V reaches the GPIO pin, even with little or no current flow (actually no current will flow, as there are no protection diodes it can flow into) the GPIO will be damaged!
In the past I did not believe that either, because I thought the current would flow through the "protection diodes" into the 3V3 supply, and with low enough currents the GPIO voltage would not lift above 3.3V plus the forward voltage of the diode.
However this is NOT the case, (the protection diodes do not exist!) and 5V will end up over the GPIO transistors, which cannot handle it !.

I understand that you dislike using other components, but I don't see a safe way in which you could!
The IN pin must be lifted to almost 5V, or the PNP transistor in this driver won't block.
Face it, the relay module is designed for an Arduino, (or other 5V logic) and without an extra component is simply incompatible with a PI!

don't believe me, and Control the IN pin by either driving it low as an output, or let it float to 5V bij switching the GPIO off (switch it to input) . This may work for a short while but WILL eventually blow up the GPIO!
You bought the wrong relay module!
You MUST add either an NPN transistor (plus base resistor) or better an opto-isolator.

p.s. my comments are based on the fact that your relay module uses a PNP transistor, like the below schematic diagram:
PNP_RELAY.png
PNP_RELAY.png (21.66 KiB) Viewed 2489 times
as you can see it already sports a pullup to 5V , so the transistor is blocked when the I pin isn't driven low.
Adding another pullup therefore won't do any good.

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Mon Jul 27, 2015 1:37 pm

that doesn't matter, if 5V reaches the GPIO pin, even with little or no current flow (actually no current will flow, as there are no protection diodes it can flow into) the GPIO will be damaged!
Not really it is always a question of current. All CMOS have diode to protect against static. RCA finds out with the CD4000 first series. Just touching the I.C. and you damage it.

This is a representation of the BCM2835.
Image

ref: http://www.mosaic-industries.com/embedd ... ifications


It can't be 5V since there is a resistor at the base of the transistor. But what could appends is that you could pass too much current and first the 3.3V will increase and hang the Pi, And if the current is too much you will burn the diode or one of the parts hook the 3.3V and burn it.

So a little current won't do a thing unless you increase the 3.3V power. So a few of mA on 1 pin won't do a thing but if all of them are like that then it is a big problem.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6195
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Using GPIO and confused.

Mon Jul 27, 2015 1:58 pm

danjperron wrote:
that doesn't matter, if 5V reaches the GPIO pin, even with little or no current flow (actually no current will flow, as there are no protection diodes it can flow into) the GPIO will be damaged!
Not really it is always a question of current. All CMOS have diode to protect against static. RCA finds out whith the CD4000 first series. Just touching the I.C. and you damage the I.C.

This is a representation of the BCM2835.
Image

ref:http://www.mosaic-industries.com/embedd ... fications


It can't be 5V since there is a resistor at the base of the transistor. But what could appends is that you could pass too much current and first the 3.3V will increase and hang the Pi, And if the current is too much you will burn the diode or one of the parts hook the 3.3V and burn it.

So a little current won't do a thing unless you increase the 3.3V power. So a few of mA on 1 pin won't do a thing but if all of them are like that then it is a big problem.
Wrong. Please re-read mahjongg's post. I can confirm his conclusion with 100% certainty. The 'equivalent circuit you've linked' seems to be entirely made up rather than anything official. I don't know how accurate it is, but it's certainly not accurate for the purpose of this discussion.

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Mon Jul 27, 2015 2:02 pm

Well I will increase the current on one if my gpio pin and see if the 3.3v start to increase. If it is there is protection diode or the mosfet act like it will increase. It don't beleive that passing 1 or 2 ma on a GPIO will burn it!

Rocus
Posts: 8
Joined: Fri Nov 29, 2013 1:24 pm

Re: Using GPIO and confused.

Mon Jul 27, 2015 3:45 pm

Quite a discussion!

I really can not deduct from the board the actual schematics. I see only one resistor, one transistor and, what I think, might be a diode. Apart from this 3 leds and 3 resistors. I measure between the input and ground 5kohm (in one direction) around the same between input and Vcc. So the base resistor might be 5 kohm, and the diode will protect the transistor (it might be parallel to the coil).

I really believe you Mahjongg, but I believe only things when a reasonable explanation is given (as you did). That's why I don't believe ;)

As I said, I have another relais unit (with optocouplers) that works well and uses no extra components. The origional unit, about which alll this discussion happens, will end in the big bin side by side to the old TTL logic I recently inherited.

danjperron
Posts: 3454
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Using GPIO and confused.

Tue Jul 28, 2015 12:04 am

Ok I test a Pi B+ and I must apologize.

Looks like that there is no ESD protection diode. At least not the way I'm use to have on any microprocessor I work with.

In that experiment I use 1K resistor to limit the current to 1.7ma on each GPIO.

On Output mode the GPIO clamp to ground or to 3.3V. With 12 GPIOS at 5V with an individual 1K resistor the GPIO got 3.44V instead of 3.28V.

Output is not an issue until you drive too much current.

The real problem is the INPUT mode. When the PI boot all GPIO are on input mode!
Since I can't find any literature about on the BCM2835 on GPIO electric caracteristic,it will be wise not to have anything more than 3.3V.

I put 12V via a 100K resistor and got 5.8V. Change the resistor to 10K and got 6.7V on the GPIO pin. So I will change all my configuration design to be sure that I never put more than 3.3V.

B.T.W. I didn't damage any of my GPIOs, but none of them had more than 1.7ma.

Return to “General discussion”