The GPIO can't control my relay switch

Hi,

I’m trying to add a water pump in my GreenHouse-project for the coming summer, and I’ve just connected a relay switch to my breadboard according to the tutorial.

My problem is that the signal from GPIO-pin #17, in output mode, is always HIGH regardless of the status it shows on the GPIO-dashboard. The only way to get the relay switch in OFF mode is to disconnect the wire.

Would appreciate some clues in the way to trouble shoot my project. I am using an RPi model B.

Pleas feel free to ask some questions!

Best regards,
Stefan

Hey Stefan!

Did you connect according to Cayenne tutorial? This is likely just a wiring thing, where power is always being router to the relay so it can’t switch between on / off.

We have a list of tutorials here: https://cayenne.mydevices.com/cayenne/tutorial/index

Think you could post a pic of the setup? This would be most helpful I think.

-B

Hi,

Yes I followed the Relay switch tutorial as you probably can see on the pictures.

Thnx for your efforts!

/Stefan

stefanspanberg I have the exact same problem. no matter what i do or what GPIO i choose the input is always high.

@bestes Hi Benny! I think I’ve solved the problem, or at least have an expalnation that I might belive in.

Yesterday I connected my relay to an Arduino, to test the function of the realy, and I used the simple Blink setup. I worked as i should. I then tried to connect the relay to my Cayenne setup again, and it did’nt work as before. To activate the relay the input pin has to be pulled down. I then did some measurements on the GPIO pins, and those measurements confirmed that tha RPI worked as it should and the output levels followed the changes that I inserted via the Cayenne dashboard; when the GPIO was HIGH it measured approimately 3.3V and when it was LOW approximately 0V.

And that was exactly correct and the levels were as they should be.

When I measured the input pins on the relay the levels were approximately 4,7V, and to be able to activate the relay they needed to be pulled down.

My conclusion; of this little experiment is that both of the output levels on my Rpi, 3.3V and 0V, is below 4,7V, and as soon I connect the input pin on my relay to the output pi on my RPis GPIO, it will pull down the realy once, and stay in that state.

I then tried to activate the realy by connecting the input pin of the relay to one of the 5V output pins on the RPi GPIO (#4) and it worked!

Is there a way to get by this, or is there something I’ve missed out? How did you do when you set up the Relay Switch tutorial?

Best regards,
Stefan

sounds like you just need a “level shifter” or “level conversion module”
since most of the relays are 5V active and the pi is only 3.3v and not enough to hold the relay position.
I have to use these on the 16channel sainsmart relays.

You can make them though just google it.

also the arduino is 5V which is why it worked just fine. You can also buy some rf24 modules and make the pi control the arduino wirelessly but its a total PIA to setup. If you want to torture yourself and figure it out and have a long project then look into it.

mase.hacker I dont think this is the case. the same relay works just fine with some simple python code that i wrote.(i have been using it for some time to remotely control my bedroom light).

here is the code:

import socket
import RPi.GPIO as GPIO
import time

GPIO_ON_OFF_PORT = 4
HOST = ‘’
PORT = 6934
BUFFER_SIZE = 512
LISTEN_INTERFACE = 1

def handle_received_data(socket_input):
GPIO.setmode(GPIO.BCM)

if socket_input == "ON":
    GPIO.setwarnings(False)
    GPIO.setup(GPIO_ON_OFF_PORT, GPIO.OUT)
    GPIO.output(GPIO_ON_OFF_PORT, GPIO.HIGH)
elif socket_input == "OFF":
    GPIO.setwarnings(False)
    GPIO.setup(GPIO_ON_OFF_PORT, GPIO.OUT)
    GPIO.output(GPIO_ON_OFF_PORT, GPIO.LOW)
    GPIO.cleanup()
else:
    print("Invalid input")
print("Turning the light %s" % socket_input)

def main():
print(“Server is starting on port:”, PORT)

server_socket = socket.socket()
server_socket.bind((HOST, PORT))
while True:
    server_socket.listen(LISTEN_INTERFACE)
    print("Listening for clients...")
    client_connection, client_address = server_socket.accept()
    print("Client accepted: ", client_address)
    # Read loop.
    while True:
        print("Waiting for data...")
        data = client_connection.recv(BUFFER_SIZE)
        if not data:
            break
        # We do have data from the client
        handle_received_data(data)
    print("Connection closed.")
    client_connection.close()
server_socket.close()

if name == ‘main’:
main()

someone?

Sorry, we were out showcasing Cayenne at IoT World. Playing catch up now.

Have you tried using a port other than channel 4? Channel 4 is reserved for our 1-wire automatic detection.

Is your issue that whenever you power your Pi on, the GPIO is always high? Or that you cannot switch your relay between high and low?

-B

Hi!

Maybe I was a bit unclear; GPIO #4 i.e the 5V-pin and physical pin #4.

I have bought a logical lever converter from Sparkfun that I think will solve the issue. l’ll get back to you if it still will be a problem.

Thnx!

/Stefan

when the pi boots all relays go high and stay that way no matter what state the gpio is in (high/low). I have tested it with 3 relays at all the gpio connections.

Check out this thread…

I think adding a resistor fixed the issue. It has to do with the Relay that was bought, where it always output a certain amount of voltage or something. I’ve not been able to reproduce the issue with the relays that I have, unfortunately.

-B

1 Like

This works for my SRD-05VDC-SL-C relay:

link 3.3V to VCC, ground to GND, (your desired GPIO) to IN.

If I linked 5V to VCC, the relay is always ON regardless of the Cayenne button status. Interesting enough, everything works fine before installing Cayenne using the 5V settings. Anyway, I could use the Cayenne to control my relay happily.

1 Like

Hi,

I finally made it! But I needed some resistors, an diode and a transistor to get it working. I used a 1K resistor on the GPIO-port and connected it to the Base-connector on the transistor, and used the GPIO-output to pull down the IN1 on the relay to Ground. To be sure of the level I connected a 10K resistor from the GPIO-output to Ground.

/Stefan

patrickshingkf it works! thank you

1 Like

@patrickshingkf @stefanspanberg @omerdagan8 Love the community interaction here! Thanks for helping each other out and letting the rest of the community know your progress. It makes a difference!

-B

It’s great to know that it is possible to get support even since I live far away from you if you look at the planet from space.

Thnx for all your efforts!

/Stefan

Hi stefanspanberg,

Is it possible for you to upload a picture of how you setup this? Trying to get my relay to work. Having same problem of it stay at high state always.

Thanks!

Hi phoque6!

Sure! Hope you can see all the connections and the colours of the connection wires to understand what I’ve done.

The yellow wires are the control wire to the relay, and it needs to be LOW (connected to ground) to activate the relay. I used a transistor to get this to work.

The purple wires are the signal wire from my RPi and output pin #24. I used a resistor to decrease the current to the transistor.

Feel free to ask more questions if I’m unclear.

/Stefan

Hi Stefan,

Thanks so much!