sarahpi
Posts: 3
Joined: Sat May 02, 2015 6:43 pm

logic is working inverted

Sat May 02, 2015 6:56 pm

Image


Hello, recently I bought the raspberry2, and created my first progrma in python.
My doubt is that the relay actuation occurs in LOW or 0, ie, it seems that logic is working inverted, or am I crazy?

The relay module I'm using is similar, the mine has 4 relays.
I have also found that the order and function of the pins is equal in photo model and raspberry2. It turns out that when you run the program, when you run online LOW, the relay is triggered and turns the LED corresponds to the relay module.

follows the code:

Code: Select all

import RPi.GPIO as GPIO
import time
 
GPIO.setmode(GPIO.BCM)

#board
#rl01=12
#rl02=16
#rl03=18
#rl04=22

#bcm
rl01=18
rl02=23
rl03=24
rl04=25

on=1
off=0

GPIO.setup(rl01, GPIO.OUT)
GPIO.setup(rl02, GPIO.OUT)
GPIO.setup(rl03, GPIO.OUT)
GPIO.setup(rl04, GPIO.OUT)
 
GPIO.output(rl04,GPIO.LOW)
time.sleep(5)
GPIO.output(rl04,GPIO.HIGH)
time.sleep(5)
GPIO.output(rl04,GPIO.LOW)
time.sleep(10)

GPIO.output(rl04,off)
time.sleep(5)
GPIO.output(rl04,on)
time.sleep(5)
GPIO.output(rl04,off)
time.sleep(10)

GPIO.cleanup()
Last edited by sarahpi on Sat May 02, 2015 7:00 pm, edited 1 time in total.

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

Re: logic is working inverted

Sat May 02, 2015 6:59 pm

Most relays modules work with an optocoupler input that you must pull low to activate the relay, nothing really special about that, and I would even hesitate to call that "inverted logic".

Make sure the power for the optocoupler LED comes from the 3V3 supply NOT from the 5V supply, as doing that may mean that 5V may leak through to the GPIO and that will sooner or later blow up the transistors in the GPIO, and therefore your PI!

you are now powering it from pin 2, the 5V pin, that WILL damage your PI!
you MUST remove the jumper connecting VCC and VDD, and power only the actual relays with 5V!

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

Re: logic is working inverted

Sat May 02, 2015 7:02 pm

It's normal with those relay boards due to the way the opto-isolaters are wired on that relay board.

You've two choices
1. Write your code with active low.
2. Wire a transistor between the GPIO pin and the relay to invert the signal.

#1 is easier.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

sarahpi
Posts: 3
Joined: Sat May 02, 2015 6:43 pm

Re: logic is working inverted

Sat May 02, 2015 7:12 pm

thank you for the quick help.
Last doubt. The relay module works with 5V input and IN1 pins, IN2, IN3, IN4 according to manufacturer's specification is also 5V.
The raspberry sends 3.3V right? what can I do to avoid future problems?

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

Re: logic is working inverted

Sat May 02, 2015 7:42 pm

sarahpi wrote:thank you for the quick help.
Last doubt. The relay module works with 5V input and IN1 pins, IN2, IN3, IN4 according to manufacturer's specification is also 5V.
The raspberry sends 3.3V right? what can I do to avoid future problems?
You don't need to worry because there's an opto-isolator between the GPIO and the relay switching transistor.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

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

Re: logic is working inverted

Sat May 02, 2015 9:28 pm

sarahpi wrote:thank you for the quick help.
Last doubt. The relay module works with 5V input and IN1 pins, IN2, IN3, IN4 according to manufacturer's specification is also 5V.
The raspberry sends 3.3V right? what can I do to avoid future problems?
If the inputs of the module are 5V, then you cannot use this module, not without an extra transistor to convert the input signal from 3V3 to 5V.

But it probably isn't like that at all, its like this:
Image

as you can see the relay will turn on when you tie IN0 low, so that current can flow from VCC through R1 through the LED in U1, and through the LED IN1.

the original way you would do that is to connect 5V to VCC, and close the jumper, so that the relay will also be powered by the same 5V (VCC).

That doesn't work with a PI, as its outputs switch between 3,3V and GND, not 5V and GND.

It also means that when the GPIO is programed as input, 5V can leak through R1, U1 and IN1 and put 5V on the GPIO PIN.
THAT IS DEADLY FOR A PI!

so the only way to use a relay like this is to disconnect the jumper, and put 3V3, not 5V on VCC.
in theory this should not works, as two LED's together in series need about 4V before current will flow, in practice however it seems to work. Note JD-VCC (AKA VDD) must be connected to 5 or 6V for the relay to get power.

Hans Naert
Posts: 1
Joined: Wed Dec 09, 2015 10:11 am

Re: logic is working inverted

Wed Dec 09, 2015 10:14 am

I measured the GPIO voltage when connected JD-VCC and VCC together with 5V.
The measured voltage on the GPIO pin when the Pi was not configured was 2.4V (GPIO23/GPIO24), what is ok for the PI.

andre_x
Posts: 2
Joined: Wed Apr 05, 2017 3:17 pm

Re: logic is working inverted

Wed Apr 05, 2017 3:58 pm

I know this is an old post, but this answer may comes handy for the ones who arrive here by forum search.

Why not just invert it echoing 1 into /sys/class/gpio/gpioxx/active_low?
For example for GPIO 26:

Code: Select all

sudo sh -c 'echo 1 >/sys/class/gpio/gpio26/active_low'

Return to “Beginners”