Uncategorized

JDVcc Relay

Asked 
Active 13 days ago
Viewed 85 times
1

Just starting to explore python, Raspberry and a relay. I want to control my 2-channel relay with a button. This is my test code:

import RPi.GPIO as GPIO
import time
import requests
from datetime import datetime
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)   # button
GPIO.setup(22, GPIO.OUT)                    # IN1 on relay
GPIO.setup(23, GPIO.OUT)                # IN2 on relay

try:
    while True:
        input_state = GPIO.input(18)
        if input_state == False:
            print('Button push')
            GPIO.output(22, 1)
            GPIO.output(23, 1)
            sleep(0.5)
            GPIO.output(22, 0)
            GPIO.output(23, 0)
except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt  
    GPIO.cleanup()

Now when I run the code, the relay is switched on and only powered off when I hit ctrl-c. The push of the button has no effect. Just the GPIO.setup seems to trigger the relay. I power the relay with 5V from the Raspberry (2B).

Probably some newbie coding failure. Any help is appreciated!

Regards

edit: This is the relay I’m trying to use. I bought some years ago, I think from eBay (not Amazon).

This is the circuit: Raspberry 2b and a relay

This is a picture of the relay: Relay

share  edit  follow  close  flag

2 Answers

1

Question

The OP’s has a relay module, but his python program is not working. How to fix it?


jdvcc relay


relay toggle


Answer

1. First thing first. Let us confirm that the following is the OP’s relay module.


relay 1

relay 2


2. Let us look at the relay’s schematic and how to do the wiring.

relay 3

relay 4


3. 3V3 Logical Level Signal Manual Testing (Without using Rpi)

a. Put Rpi aside. Do not use the Rpi’s 5V and 3V3 rail.

b. Remove JD-Vcc jumper.

c. Connect JD-Vcc terminal to 5V power (External power, not Rpi’s 5V rail)

d. Connect Vcc terminal to 3V3 power (External power, not Rpi’s 3V3 rail, can use 3V battery)

e. Use a jumper wire and connect one end to IN1 terminal.

f. By hand, connect/touch the other jumper end to ground to see if the LED and relay switch turn on/click.

g. Touch jumper wire to 3V/3V3 and see if LED and relay turn off,

h. Use a multimeter to measure the driver current, between IN1 and ground. The current should be less than 5mA.


4. 5V0 Logical Level Signal Manual Testing (Without using Rpi)

If you are using a logical level converter (for example NPN BJT 2N2222) to shift up Rpi GPIO pin’s 3V3 logical signal to 5V, then you don’t need to remove the JDVcc jumper, and no need to power the input circuit with 3V3 power, as shown below. The testing procedure is same as above, except you are now using 5V power and also 5V logic level signal to test the module.

relay test 5V


5. Python program to toggle relay

# relay toggle test 2019jan10 tlfong01 2019jan109hkt1625 ***
# Rpi3B+ stretch (Linux 4.14.34-v7+ arm) IDLE python 3.5.3 

# Program Description
# 1. Setup Rpi.GPIO BCM mode
# 2. Set GPIO11 as relayPin to control 5V relay KY019
# 3. Toggle 100 times relayPin at 1Hz

import RPi.GPIO as GPIO
from   time import sleep

# *** GPIO Functions ***

def setGpioMode():
    GPIO.setwarnings(False) 
    GPIO.setmode(GPIO.BCM)
    return

# *** Set Gpio pin as output ***

def setGpioPinOutput(gpioPin):
    GPIO.setup(gpioPin, GPIO.OUT)
    return

# *** Set Gpoio pin High/Low ***

def setGpioPinHigh(gpioPin):  
    GPIO.output(gpioPin, GPIO.HIGH)
    return

def setGpioPinLow(gpioPin): 
    GPIO.output(gpioPin, GPIO.LOW)
    return

# *** Toggle Gpio pin ***

def toggleGpioPin(gpioPin, highSecond, lowSecond, count): 
    for i in range(count):
        setGpioPinHigh(gpioPin)
        sleep(highSecond)
        setGpioPinLow(gpioPin)
        sleep(lowSecond)
    return

# *** Main ***

print('Begin togglePin(), ...')

relayPin   = 11
highSecond = 1
lowSecond  = 1
count      = 100

setGpioMode()
setGpioPinOutput(relayPin)

toggleGpioPin(relayPin, highSecond, lowSecond, count)

print('End   togglePin().')

# *** End ***

/ to continue, …


Relay Newbie FAQ

Q1. Why relay is always turned on?

Q2. Why relay is turned off only when there is the Ctrl-C interrupt?

Q3. Is there another way to turn off the relay beside the Ctrl-C interrupt?

Q3. What does the JDVcc jumper do?

Q4. Is the JDVcc jumper only for Rpi?

Q5. Is there any work around if no JDVcc jumper is available?

Q6. Is it safe to power Vcc and JDVcc with 5V, with the JDVcc jumper on?

Q7. Is the optical isolator really necessary?

Q8. What is the meaning of Total optical isolation?

Q8. Is it safe to let the relay module to pass 10A?

Q9. Is it safe to let the relay module to switch 250VAC?

Q10. How many types of relay modules are there?

Q11. Is there any relay selection guide for the relay newbies?

Q12. Is it OK to get a fake relay module from eBay?

/ to continue, …


References

(1) Amazon 5V Two 2 Channel Relay Module with optocoupler Compatible with Arduino PIC AVR DSP ARM by Atomic Market

/ to continue, …


Appendices

Appendix A – Songle 5V Relay Switch Characteristics


songle relay 1


relay hysteresis

Appendix B – Simple High Level Triggered Relay Module without JDVcc jumper and optical isolation


high trig relay


Appendix C – Relay with High / Low Level Triggered Select Jumper


High Low Trig Relay


high low trig relay

The above eBay relay is an example of High/Low level trigger select relay.
The relay can be used for Rpi as High or Low trigger as summarized below:

(1) High trigger

No modification is necessary. Just jumper H and there you go.

(2) Low trigger

There is 50% chance that Low trigger won’t work, because Rpi’s High signal (about 3V) is not High enough (need 4.2V) to untrigger the relay. The workaround is simple: just add a series 4k7 to the IN terminal (See Appendix D’s discussion section). Rpi’s GPIO signal now goes to the other end of 4k7 not connected to IN.

Warning: eBay can give you a big surprise: No guarantee no nothing won’t melt down or blow up. You burn Me no burn. Good luck.


Appendix D – How to modify an Arduino High Trigger relay module to make it Rpi compatible

modify arduino

Discussion – In the above High trigger application, Rpi High signal is not high enough to pass big enough current to drive the LED, and the workaround is to decrease the series resistance by adding another resistance in parallel.

In an complimentary case where a relay or buzzer is low triggered, the Rpi’s High is not high enough to deactivate/untrigger the LED or switching transister. Then the workaround is to add a series resistor to the input terminal, so that the even Rpi High is not high enough, input current is smaller now and so can deactivate/untrigger the LED/switching transistor. See the following Q&A for an example of active buzzer.

Active Piezo Buzzer Makes Sound On Both Rpi GPIO Low and High Level Signal

relay_workaround


/ to continue, …


Figures

/ to continue, …


share  edit  undelete  flag
1

You are another victim of the shoddy products for sale on the web.

This is not a coding problem – the relay is UNSUITABLE for the Pi.

They can be only be used with additional circuitry, or modifying the module.

See https://raspberrypi.stackexchange.com/a/100014/8697

The particular model you linked is even worse than most! It would not meet the standards for mains isolation.

share  edit  follow  flag
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: