misiek
Posts: 12
Joined: Thu Mar 31, 2016 3:18 pm

[SOLVED] GPIO relay

Fri May 19, 2017 11:08 pm

Hello,

Can you please inspect the code below, as it doesn't switch the relay off according to the FOR loop, it just switches everything off at once at the end of the code when I use GPIO.cleanup()

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

pinList = [18, 27]
SleepTimeValue  = 2

for i in pinList:
    GPIO.setup(i, GPIO.OUT)
    try:
        GPIO.output(i, GPIO.HIGH)
        print('OFF')
        time.sleep(SleepTimeValue)

    except KeyboardInterrupt:
        print("Quit")

GPIO.cleanup()
Any advice please?
Thanks
Last edited by misiek on Mon May 29, 2017 7:07 pm, edited 1 time in total.

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO relay

Sat May 20, 2017 7:38 am

Hello,
your code is doing the following actions:

Code: Select all

setup ( pin = 18 dir= OUT)
output ( pin = 18 level= HIGH)
print OFF
sleep(2.000000)
setup ( pin = 27 dir= OUT)
output ( pin = 27 level= HIGH)
print OFF
sleep(2.000000)
cleanup
Assuming that you want to turn off the relais by setting the GPIO to LOW, then add a command to set the GPIO to low.
for example:

Code: Select all

for i in pinList:
    GPIO.setup(i, GPIO.OUT)
    try:
        print('ON')
        GPIO.output(i, GPIO.HIGH)
        
        time.sleep(SleepTimeValue)
        print('OFF')
        GPIO.output(i, GPIO.LOW)
        
    except KeyboardInterrupt:
        print("Quit")
Hope this helps,
Gerhard

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

Re: GPIO relay

Sat May 20, 2017 9:23 am

It might depend on the particular relay board you're using: to switch off, some require a high level that's higher than the 3v3 the Pi can supply.

misiek
Posts: 12
Joined: Thu Mar 31, 2016 3:18 pm

Re: GPIO relay

Sat May 20, 2017 3:35 pm

Thanks Gerhard but the code didn't switch the relay off at all.

Burngate, how do I verify this? Or is there a way to control my relay board correctly?

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO relay

Sat May 20, 2017 3:55 pm

Hello, which relais board do you use ? Perhaps you have a link to a datasheet, part number or alike ?

klricks
Posts: 6609
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: GPIO relay

Sat May 20, 2017 4:00 pm

Most relay boards are ACTIVE LOW which means that a LOW will turn the relay ON.
Post a link to the relay board you have.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

misiek
Posts: 12
Joined: Thu Mar 31, 2016 3:18 pm

Re: GPIO relay

Sat May 20, 2017 4:27 pm


User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: GPIO relay

Sat May 20, 2017 5:17 pm

With a 3V3 GPIO and those LED indicators on the board you may not have enough volts/amps to drive the opto-isolator that switches the relay.

Try using a small 2N2222 transistor as an extra switch then you can a) get it running active high and b) switch a 5V relay running at 5V with a 3V3 GPIO pin.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: GPIO relay

Sat May 20, 2017 5:24 pm

Here's a thread about similar relay boards: viewtopic.php?f=63&t=183943&p=1164047
DougieLawson's post there is relevant

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: GPIO relay

Sat May 20, 2017 5:26 pm

Burngate wrote:Here's a thread about similar relay boards: viewtopic.php?f=63&t=183943&p=1164047
DougieLawson's post there is relevant
The MCP23008 or MCP23017 is a good working alternative to wiring up your own transistors.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO relay

Sat May 20, 2017 5:32 pm

Hello,
looks as if this relais board is similiar to this one described in http://henrysbench.capnfatz.com/henrys- ... er-manual/
When you are familiar with electronics, then disconnect GPIO and connect
vcc1 to 5V
vcc to 3.3V, disconnect jumper.
and then an input in1..in4 to gnd. The relais should switch, the corresponding led should light. If ok, there is good chance that your Pi can switch this.

If not, check if this works with vcc connected to 5V (do not connect GPIO in this condition directly). The relais should switch. If this works, then follow DougieLawson's advice to use an extra transistor to switch the relais.
There are many discussions about 'sainsmart' relais which has similar schema.
Hope this helps,
Gerhard

misiek
Posts: 12
Joined: Thu Mar 31, 2016 3:18 pm

Re: GPIO relay

Sat May 20, 2017 5:44 pm

DougieLawson wrote:With a 3V3 GPIO and those LED indicators on the board you may not have enough volts/amps to drive the opto-isolator that switches the relay.

Try using a small 2N2222 transistor as an extra switch then you can a) get it running active high and b) switch a 5V relay running at 5V with a 3V3 GPIO pin.
If I fallow this method, I understand I need to buy 4 of those (1 for each pin/relay)?
What other parts do I need to purchase to make it work? And also can you be so kind to explain or sketch which leg (e,b,c) connect to what element?

Is there 4 relay board available that would work straight out of the box?

Many thanks

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO relay

Sat May 20, 2017 7:02 pm

There is a sketch in http://ras-pi.de/wp-content/uploads/201 ... 00x149.png which explains how to connect the transistor.

misiek
Posts: 12
Joined: Thu Mar 31, 2016 3:18 pm

Re: GPIO relay

Sat May 20, 2017 7:04 pm

Excellent!

Many thanks everyone. I will order the parts now and will post a reply once I'm done.

Thank you

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

Re: GPIO relay

Sat May 20, 2017 7:14 pm

I modified a very similar board viewtopic.php?f=37&t=63659&p=470954#p470954

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: GPIO relay

Sun May 21, 2017 1:45 pm

ghp wrote:There is a sketch in http://ras-pi.de/wp-content/uploads/201 ... 00x149.png which explains how to connect the transistor.
Thank you so much, I was looking on Google to try to find a picture like that.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

misiek
Posts: 12
Joined: Thu Mar 31, 2016 3:18 pm

Re: GPIO relay

Mon May 29, 2017 7:05 pm

Thank you ghp and DougieLawson.

Works perfectly! Switches ON with output HIGH and OFF with LOW.

Many thanks :)

Return to “Python”