komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

python and relay module

Mon Feb 12, 2018 2:09 pm

hello i have a simple and inexpensive relay module connected to my raspberry pi zero w
the module has 3 pin and i connect them like this:
GND -> raspi gnd
VCC -> raspi 5v
IN -> raspi gpio 15
i am running a simple python code like this:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(15, GPIO.OUT)

GPIO.output(15, 0)
time.sleep(2)
GPIO.output(15, 1)
time.sleep(2)
GPIO.output(15, 0)

GPIO.cleanup()

unfortunately the relay doesnt close the contact (except on the SETUP statement and CLEANUP at the end)
but if i try to connect directly the "IN" pin of the relay module to the raspi GND the realy works actually.

what's the problem?
why the relay doesnt work when i set the gpio 15 to LOW?

thanks

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

Re: python and relay module

Mon Feb 12, 2018 2:54 pm

First thing to do is post a link to the board you have .

second what happens is you disconnect the wire from gpio 15 but leaving it connected to relay IN

then connect it to 5v pin on pi result = ?

then connect it to Gnd pin on pi result = ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Paul Hutch
Posts: 411
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: python and relay module

Mon Feb 12, 2018 3:09 pm

Since you're using BCM mode make sure you are connecting to the correct physical pin, GPIO 15 is on pin #10 (pin #15 is GPIO 22)

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python and relay module

Mon Feb 12, 2018 3:14 pm

pcmanbob wrote:
Mon Feb 12, 2018 2:54 pm
First thing to do is post a link to the board you have .

second what happens is you disconnect the wire from gpio 15 but leaving it connected to relay IN

then connect it to 5v pin on pi result = ?

then connect it to Gnd pin on pi result = ?
if i disconnect the wire from gpio15 the relay turn off... if i connect it to GND the relay turn ON
when conected to gpio15 the realy always stay ON (despite the state of the gpio 15 LOW or HIGH)

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python and relay module

Mon Feb 12, 2018 3:15 pm

Paul Hutch wrote:
Mon Feb 12, 2018 3:09 pm
Since you're using BCM mode make sure you are connecting to the correct physical pin, GPIO 15 is on pin #10 (pin #15 is GPIO 22)
yes i m sure :)

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python and relay module

Mon Feb 12, 2018 3:29 pm

you know what....
i connected the gpio15 at the Gate of a IRFZ44N
then the Source of the IRFZ44N at GNF of raspi... and Drain of IRFZ44N at the "IN" of the relay module
and now it's working perfectly
can you please explain me why i have to use a mosfet to make it work... and it's not working directly with the gpio?
thanks

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

Re: python and relay module

Mon Feb 12, 2018 3:32 pm

komarek wrote:
Mon Feb 12, 2018 3:14 pm

if i disconnect the wire from gpio15 the relay turn off... if i connect it to GND the relay turn ON
when conected to gpio15 the realy always stay ON (despite the state of the gpio 15 LOW or HIGH)
No quite the tests I asked for, I wanted to prove 5v turned it off, also asked for link to board you have.

Anyway your relay needs 5v to switch it off and ground to switch it on, but the 3.3v gpio is not capable of switching your relay off
but as you have discovered if your remove the wire from gpio/IN then the relay will switch off.

So you need to use a transistor/resistor interface between your relay board and the pi gpio.

Image

this will also make your relay board active high so setting gpio 15 high will turn on your relay.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python and relay module

Mon Feb 12, 2018 3:37 pm

pcmanbob wrote:
Mon Feb 12, 2018 3:32 pm
komarek wrote:
Mon Feb 12, 2018 3:14 pm

if i disconnect the wire from gpio15 the relay turn off... if i connect it to GND the relay turn ON
when conected to gpio15 the realy always stay ON (despite the state of the gpio 15 LOW or HIGH)
No quite the tests I asked for, I wanted to prove 5v turned it off, also asked for link to board you have.

Anyway your relay needs 5v to switch it off and ground to switch it on, but the 3.3v gpio is not capable of switching your relay off
but as you have discovered if your remove the wire from gpio/IN then the relay will switch off.

So you need to use a transistor/resistor interface between your relay board and the pi gpio.

Image

this will also make your relay board active high so setting gpio 15 high will turn on your relay.
yes exactly
i found it working with a IRFZ44N mosfet
sorry i didnt post the board but i didnt know what you meant... anyway it was a cheap relay module bought on aliexpress


thank you again

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

Re: python and relay module

Mon Feb 12, 2018 3:42 pm

komarek wrote:
Mon Feb 12, 2018 3:29 pm
you know what....
i connected the gpio15 at the Gate of a IRFZ44N
then the Source of the IRFZ44N at GNF of raspi... and Drain of IRFZ44N at the "IN" of the relay module
and now it's working perfectly
can you please explain me why i have to use a mosfet to make it work... and it's not working directly with the gpio?
thanks
Because the relay board needs 5v to turn it off, the 3.3v of the pi gpio is not high enough to do it.
But by disconnecting the IN from the ground using the IRFZ44N the relay will turn off because it no-longer has a ground connection on the IN pin.

you also need a current limiting resistor between the gpio and the gate on the IRFZ44N or you risk damaging your pi.

try 1K if it does not work try 470 ohms
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pascale
Posts: 15
Joined: Mon Feb 05, 2018 11:04 am

Re: python and relay module

Sun Jul 08, 2018 12:26 pm

Hi
I had the same problem, So I changed my python code from

Code: Select all

if read_sensor(sID1) + x > read_sensor(sID2):
            GPIO.output(25,GPIO.HIGH)
          # print("it's Hot!")
To

Code: Select all

if read_sensor(sID1) + x > read_sensor(sID2):
            GPIO.output(25,GPIO.LOW)
          # print("it's Hot!")
And now the relay woks :D

Return to “Python”