Posts: 1
Joined: Mon Feb 11, 2019 7:21 pm

Raspberry Relay

Mon Feb 11, 2019 7:28 pm

I'm trying to control a relay via GPIO, but when i connect the relay to the Raspberry it's always on, no matter if the output is high or low.
The relay can change state both with 3.3V and with 5V.
(sorry for my english, but i'm italian :) )

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

Re: Raspberry Relay

Tue Feb 12, 2019 12:18 pm


I suggest you post a link to the relay board you purchased as the first step so we know what you are working with.

Many of the ready made relay boards do work well with the pi because they are designed to work with the 5v arduino and use an active low to turn the relay on and the 3.3v of the pi gpio will not turn the relay off when high.

There are ways round this using a transistor interface between the pi gpio and the relay board, once we know more about your relay board we can help with the interface.
We want information… information… information no help
The use of crystal balls & mind reading are not supported

Posts: 4
Joined: Wed Feb 13, 2019 2:53 am

Re: Raspberry Relay

Wed Feb 13, 2019 3:13 am

I had this issue with my relay board as well and I still do if I unplug the unit and plug it back in 5 of the led slightly are on for some reason no matter what the input, But I did come across this script from a youtuber and I call it my script which will run each relay in order and then cleanup, that always seems to work for me, so ill post the code below mind you need to change the gpio's numbers to the correct ones your using

Code: Select all

import RPi.GPIO as GPIO
import time


# init list with pin numbers

pinList = [2, 3, 4, 17, 27, 22, 10, 9]

# loop through pins and set mode and state to 'low'

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)

# time to sleep between operations in the main loop

SleepTimeL = 2

# main loop

  GPIO.output(2, GPIO.LOW)
  print "ONE"
  GPIO.output(3, GPIO.LOW)
  print "TWO"
  GPIO.output(4, GPIO.LOW)
  print "THREE"
  GPIO.output(17, GPIO.LOW)
  print "FOUR"
  GPIO.output(27, GPIO.LOW)
  print "FIVE"
  GPIO.output(22, GPIO.LOW)
  print "SIX"
  GPIO.output(10, GPIO.LOW)
  print "SEVEN"
  GPIO.output(9, GPIO.LOW)
  print "EIGHT"
  print "Good bye!"

# End program cleanly with keyboard
except KeyboardInterrupt:
  print "  Quit"

  # Reset GPIO settings

# find more information on this script at

User avatar
Posts: 91
Joined: Tue Jan 08, 2019 7:44 pm

Re: Raspberry Relay

Wed Feb 13, 2019 7:02 pm

My guess is that both of you are using a Solid State Relay (SSR) and a lot of them are not designed to switch DC loads. They are for switching AC loads only. So look at the specification of your relay or relay board and see what the output specs say or look near the output terminals/pins of the device and see if it only says AC with no mention of DC.

The reason they do not turn off is that those SSRs are made with thyristors/Diacs to switch the output. The way a diac works is once triggered it will conduct and keep conducting until the current running through it goes to zero and then it automatically turns off. With AC the current goes positive and then negative and then repeat and each time it passes through zero. A SSR will have a "zero crossing" detect so that so long as the input to the SSR is active, then it will keep retriggering that diac each half cycle of the AC wave. When you want to turn off the SSR, when the input is inactive, the SSR no longer retriggers the output so it will automatically shut off when the current next passes through zero.

Soooo, you can use this to turn on a DC load but since DC isn't alternating, it never passes through zero so it basically will never turn off unless you cut the power to your load. This might explain why you guys turn on the SSR and then can't turn it off again.

As an aside, you can use this behavior to your advantage. For example, you could use a pin to turn on a DC load and then the pin could be used for other purposes as the load would simply remain on even if your GPIO pin is no longer commanding the load to be on. Of course if you need to have the load turn off then you either need a way to control cutting off the power to the load or you need the load to have its own way of turning off its own power.

For example say you have a series of water tanks you need to fill when they get low using DC pumps. You could use a few pins as addresses for which tank to fill. Say three GPIO pins could be the selection for 8 pumps. Four pins give you 16 pumps, etc. You select the tank which causes the SSR for that tank to activate the DC pump and it starts filling. You can now go onto other tanks if needed to activate them. When the tank reaches the full point you can have a simple float switch to cut the power to the pump (which will also cause the SSR output to turn off).

Return to “Beginners”