jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Problem with GPIO and relay

Wed Mar 18, 2020 5:05 pm

Hello everyone. I'll explain my problem as I haven't found a solution in the Internet.
I've installed a fan for my Raspberry Pi 4 model B that needs 5V input. I wanted to enable or disable it when I want so I bought a relay and wired everything. My problem is that if I plug the wire that should be pluged in a GPIO pin to any GPIO pin, it doesn't work (I hear a very small noise, the "click" of the relay), but when I plug it to a 3v3 pin it works perfectly (also the "click" from the relay sounds louder).

Any ideas why is that happening?
Thank you (and sorry if there's any mistake, I'm not from the UK :P)

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

Re: Problem with GPIO and relay

Wed Mar 18, 2020 5:23 pm

Read this sticky about problems with relay (modules).
viewtopic.php?f=91&t=83372&p=1225448#p1225448

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

Re: Problem with GPIO and relay

Wed Mar 18, 2020 6:04 pm

Hi.

Can you post a link to the relay board you have, do you know if it's active high or active low ?

What you need is a driver circuit between the gpio and the relay board.

If you can answer the question above I will post a diagram of the circuit you need to use.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Wed Mar 18, 2020 9:36 pm

I have this one (in the 5 V version; I haven't been able to find it's datasheet):
http://www.farnell.com/datasheets/26266 ... 1584567076

I can confirm it works perfectly if it's connected to 3v3 pin, but doesn't when pluged into a GPIO pin

User avatar
Burngate
Posts: 6372
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Problem with GPIO and relay

Thu Mar 19, 2020 10:08 am

http://www.farnell.com/datasheets/26266 ... 1584567076
On page 5, the table gives 40mA for the 5v version. That's far too much for a GPIO to supply - 16mA is the most you should take from a GPIO.

As pcmanbob says, you need a driver circuit between the GPIO and the relay - it may be as simple as a FET

As an aside, that relay is called "5v" because it's going to work at 5v; how much less you can get away with is just luck - the next one out of the box may work at 3v3 like this one, or it may not

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

Re: Problem with GPIO and relay

Thu Mar 19, 2020 11:16 am

When you said you had a relay I assumed you had a relay on a PCB with a driver circuit , as you just have the relay you are going to have to built your own driver circuit.

As you have a 5v relay you will need to drive it like this

Image

The diode is to protect the circuit and you pi from the reverse voltage the coil of the relay generates when you switch it off, and must be fitted.

You can use any gpio output pin to drive the base of the transistor to switch the relay on.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Thu Mar 19, 2020 4:32 pm

Ok. I think I understand it. thanks a lot guys!

LTolledo
Posts: 4122
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Problem with GPIO and relay

Thu Mar 19, 2020 9:56 pm

I do the same even with 3v bare relays.....

why? to protect my RPi investment ;)
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

JohnsUPS
Posts: 188
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Problem with GPIO and relay

Thu Mar 19, 2020 10:43 pm

Yeah, as a rule I use the GPIO pins as a source of logic switching only, and ALWAYS buffer the pin in some way or another. In the case of the diagram that pcmanbob posted, it is the combination of the resistor and transistor.
I am wonder how many times pcmanbob has posted this schematic.....

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

Re: Problem with GPIO and relay

Thu Mar 19, 2020 10:58 pm

JohnsUPS wrote:
Thu Mar 19, 2020 10:43 pm

I am wonder how many times pcmanbob has posted this schematic.....
That particular diagram is only 3 months old so not that often :lol: , but I have posted many many diagrams for interfacing relays and relay boards over the years.

Some even turn up in google searches now. :o
Last edited by pcmanbob on Fri Mar 20, 2020 12:11 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

LTolledo
Posts: 4122
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Problem with GPIO and relay

Thu Mar 19, 2020 11:35 pm

pcmanbob is the master for diligently making these schematics..... :D

which makes me wonder what app he's using in making those schematics
I've only used EasyEDA if I wanted to do some...
(sorry to be off-topic)
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

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

Re: Problem with GPIO and relay

Fri Mar 20, 2020 9:05 am

In answer to your question I use TinyCAD, https://www.tinycad.net/

This is only available for windows so wont run on a Pi.

Better get back on topic
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Mon Jun 22, 2020 3:56 pm

Hi again. I finally bought the components needed for the circuit. I've wired it as the diagram pcmambob shared, but it doesn't seem to work. I've checked it and everything makes contact , so it's not a wire problem, I've also checked the relay alone with the fan and works fine, but when I add the diode and the transistor it doesn't work. I've also checked that the raspberry pi pins are on.

I've checked that if I made contact between the transistor's base and collector, it works.

I'll add an image of the actual wiring. It's not high quality, but it's not very hard to see where each wire goes to.
Image

Any idea about what is wrong?
Attachments
wires.jpg
wires.jpg (222.55 KiB) Viewed 762 times

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

Re: Problem with GPIO and relay

Mon Jun 22, 2020 7:06 pm

jcgd wrote:
Mon Jun 22, 2020 3:56 pm
Hi again. I finally bought the components needed for the circuit. I've wired it as the diagram pcmambob shared, but it doesn't seem to work. I've checked it and everything makes contact , so it's not a wire problem, I've also checked the relay alone with the fan and works fine, but when I add the diode and the transistor it doesn't work. I've also checked that the raspberry pi pins are on.

I've checked that if I made contact between the transistor's base and collector, it works.

I'll add an image of the actual wiring. It's not high quality, but it's not very hard to see where each wire goes to.
Image

Any idea about what is wrong?
Well looking at your image the transistor appears to be wired the right way round , but I cant clearly see the diode so cant say if that is the right way round, the white ring should be wire to the positive side of the circuit as shown in the diagram.

what you are trying to do with the fan side of the project I have no idea, why have you got 3 wires connected to the relay contacts !!

Depending on the relay contact lay out you are either connecting the fan positive to 2 different gpio pin ( which can't power a fan by the way )
or you are connecting on gpio pin to the fan and then in the other state you are connecting 2 gpio pins together ( which might kill your pi under the right conditions )

so I suggest you do not power this pi or this circuit until you correct your errors.

1. please post more detailed images, just up load them to imgur.com and post links in your post.
2. please post the contact layout diagram for the relay your are using or a link to were you got it.
3. you cant power fans from gpio pins thy can only supply 16mA max, if you are using 5V fans,
then you need to power them from a 5v header pin

4. I would suggest you would do better to get yourself a breadboard and some dupont cables to set up this circuit for testing before you lay it out on a prototyping pcb for final soldered connections, twisted wires do not make good connections.

you may have damaged your transistor but hope fully you have not damaged your pi yet.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Mon Jun 22, 2020 8:50 pm

That's the datasheet from the relay.
http://www.farnell.com/datasheets/26266 ... 1584567076
I've tried it connecting the brown wire into ground (and the pink one into a 5v pin) and it worked fine, so I supposed it was right

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

Re: Problem with GPIO and relay

Tue Jun 23, 2020 9:14 am

So this is how you need to connect the fan to the relay.

Image

once you have the relay operating correctly
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Tue Jun 23, 2020 2:44 pm

I've setted that circuit exactly the same as the photo, but still not working. the only way it works is if I add one extra wire joining the base and the collector of the transistor, as in the image, if I do that it works fine (although it doesn't make sense as it would be connecting a gpio which is on with a 5v pin), otherwise doesn't work.
https://imgur.com/a/hnCD2pf
I guess I'll let it go for a few weeks until I'm ready to try it again. Thanks anyway for all the schemas, they were very useful.

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

Re: Problem with GPIO and relay

Tue Jun 23, 2020 3:21 pm

jcgd wrote:
Tue Jun 23, 2020 2:44 pm
I've setted that circuit exactly the same as the photo, but still not working. the only way it works is if I add one extra wire joining the base and the collector of the transistor, as in the image, if I do that it works fine, otherwise doesn't work.
https://imgur.com/a/hnCD2pf

Do not do that you are connecting a 5V circuit to the gpio pin , you could end up killing your pi doing that.

post more pictures showing your wiring as I suggested before making sure we can clearly see all the connections
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Tue Jun 23, 2020 3:32 pm

That's how the circuit should be in the raspberry (I did the same in a breadboard this morning but now I haven't got it as it weren't mine). I haven't started the raspberry for obvious reasons (and also because the join of some wires is a little bit dirty). It's the same as the schema but with the black wire from the fan to another ground different than the wire from the transistor.
https://imgur.com/a/RRSTNi6

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

Re: Problem with GPIO and relay

Wed Jun 24, 2020 7:41 am

As you seem to be having so many problems with this simple circuit I decided to build an example just to prove it works.

https://www.youtube.com/watch?v=FPxb2UA ... e=youtu.be

I replaced the fan with an LED so you could see it working , sorry for the bad focus but I wanted the camera this close to pick up the relay operating

and this is a clearer image of the breadboard

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jcgd
Posts: 8
Joined: Wed Mar 18, 2020 4:57 pm

Re: Problem with GPIO and relay

Wed Jun 24, 2020 4:20 pm

Ok. I have checked everything, now it's the same as the image and the video, but doesn't work, so I guess some component has died, I'll have to buy new ones. Thanks for all the help, it was amazing :D

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

Re: Problem with GPIO and relay

Wed Jun 24, 2020 4:29 pm

I suspect the transistor is what you have damaged.

If you had damage the resistor or diode you would have had to pass sufficient current to make them get very hot, same applies to the relay.

I would suggest you get yourself a breadboard , a red or green LED , 470ohm resistor and some Dupont cables.

That way you can build the circuit but replace the relay and diode with the LED and resistor in series and then you can test the circuit and see it working before trying with the relay.

Regarding the relay I know you posted a link to it but is there any information actually printed on the relay ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

MarkDH102
Posts: 414
Joined: Fri Feb 13, 2015 3:18 pm

Re: Problem with GPIO and relay

Wed Jun 24, 2020 4:31 pm

Try using a different GPIO pin.
You may have destroyed the one you originally used by drawing too much current from it...

Return to “General discussion”