I'm working on my first python/hardware project and I want to create a simple (or at least I thought it was simple) cabinet lock, which will lock/unlock when certain RFID tags are approached to the RFID reader. I have the RFID side of code working, however I'm struggling with the lock part. I have a solenoid lock (this) powered by a 12V/1A power supply and I want to control the solenoid lock with RPI GPIO pins via a single channel relay. I have the following code:

#!/usr/bin/env python3
#GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
import time
import RPi.GPIO as GPIO
pin = 13

print "Unlocking"
GPIO.setup(pin, GPIO.OUT)
print "Locking"

Now, when I run this code, the LED on the relay blinks, however, the solenoid lock does not move at all. It's not a problem of the power supply - when I power the lock directly from the PSU, it "unlocks". Please see the picture of the wiring, maybe it is wrong, but I was following one of the guides on the Internet and it should be fine. My guess is that the code is wrong, because when I manually wire GND wire (from RPi) to the IN1 pin on the relay, the solenoid activates and lock gets unlocked. Any help would be appreciated, I'm a newbie and I've never worked with electronics before...

Many thanks!


  • 3
    We need a link to the specs of the "single channel relay" and a clear photo of the wiring.
    – joan
    Feb 27, 2020 at 17:39
  • 1
    At a first glance the relay module seems to have an opto-isolator, like the dozens of others who have asked similar questions these are unsuitable for use with the Pi. See raspberrypi.stackexchange.com/a/100014/8697
    – Milliways
    Feb 27, 2020 at 22:33
  • Hello @jnko, Welcome and nice to meet you. Ah, let me. Your relay seems to be an Arduino compatible "High level trigger" relay. For this kind of relay, a Low level signal is used to switch off, a High signal (for Arduino, High means higher than roughly 4.2V) to switch on. Now, if you are using Rpi to switch off this Arduino relay, you have a problem, because Rpi's GPIO High signal is at most 3.3V, therefore CANNOT switch off the relay. In other words, Rpi GPIO High is not high enough to switch of this relay. There are a couple of workarounds, including the following: / to continue, ...
    – tlfong01
    Feb 28, 2020 at 2:07
  • Rpi 5V relay problem and solution references: (1) raspberrypi.stackexchange.com/questions/99988/… (2) raspberrypi.stackexchange.com/questions/83494/…. If you do not have a spec of the relay, you might like to do trial and errors. Good luck. Best Regards, Yours sincerely, Ta and cheers, :)
    – tlfong01
    Feb 28, 2020 at 2:11
  • 1
    The code may or may not be wrong. We need 2 things to help you: A simple schematic, and specifications on your relay - even a link to the vendor's page or a model number may suffice.
    – Seamus
    Feb 28, 2020 at 3:11


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.