I am a complete newbie here. I bought RPi, flashed the OS. I also bought 5v relay. I have connected the 2nd pin to VCC of the relay, 6th pin to GND of the relay, 40th pin to the 'IN' point of the relay. At present I have not connected anything to the other side of the relay (but the problem also exists if I connect a load of led). Also no other changes have been made to the RPi except the SSH connection enablement and software update using sudo apt get commands i.e RPi is almost at factory settings. My Code:

import RPi.GPIO as GPIO
import time

in1 = 21 //i.e 40th pin 

GPIO.setup(in1, GPIO.OUT)

    GPIO.output(in1, GPIO.HIGH) //1
    GPIO.output(in1, GPIO.LOW) //2
    print("inside try after low") //3
except KeyboardInterrupt:
    GPIO.cleanup() //4

My problem: Once the relay turns high at comment //1 it does not turns off at point //2. I kill the program using 'control + c' then it turns off which is not what I desire. I just want it to turn on and then turn off. The point //3 does get executed. //4 executes when ctrl+c pressed. If I remove cleanup code and do not press ctrl+c then the relay's green light remains turned on continuously. P.S This is raspberry pi 64bit OS

enter image description here

  • 1
    Relays are normally ON when given 0V (LOW) and OFF when given 3.3V (HIGH). Check the specification of the relay board.
    – CoderMike
    Commented May 13 at 6:59
  • 1
    Does this answer your question? Can you use a 5V Relay Module with the Pi?
    – Milliways
    Commented May 13 at 7:14
  • @CoderMike relays are on when powered. Crummy, poorly designed relay modules are another matter.
    – Milliways
    Commented May 13 at 7:17
  • 1
    A picture of a relay module is meaningless. You need to post a circuit. This looks like a different module to most but attempting to control a 5V relay with 3.3V is unpredictable.
    – Milliways
    Commented May 13 at 7:32
  • 1
    Without details of WHAT IS CONNECTED anything else is irrelevant. From the blurry out of focus photo you posted this appears to have a transistor and if this is a PNP with a common 5V rail (like the device in the duplicate) it won't work without additional circuitry. I don't understand why so many of these cheap Chinese devices do this. If they used a NPN transistor (which uses the same number of components) it would work with both Pi & Arduino.
    – Milliways
    Commented May 13 at 10:14

2 Answers 2


Connecting the vcc of the relay to 3.3v of the RPi caused the relay to turn off correctly. I am not sure if this is the solution for this.

  • Couple of cmts I'll make here as the space under your question have gotten a bit crowded: 1. It always best to refer to pins by their GPIO designation - rather than their physical position on the header. See Raspberry Pi Pinout as a guide. 2. From the photo you've added, it's clear that your relay is marked for operation at 5V, and therefore the problem is most likely in your software. If I were in your shoes, I'd use GPIO 17 (or 22, 23, 24, 27) for my output. Finally, "in1 = 29 //i.e 40th pin" in your code looks incorrect; pin #40 is not GPIO29
    – Seamus
    Commented May 13 at 19:20
  • It was a typo. Fixed it. In the code I had used 21. Still the problem persisted.
    – LearneriOS
    Commented May 13 at 19:26
  • @Seamus any reason for suggesting other pins but not GPIO21 ?
    – LearneriOS
    Commented May 13 at 19:29
  • 1
    PLEASE NOTE: I do not use RPi.GPIO; I cannot comment on your code. But to your Q: GPIO21 is a "specialty pin" (my terminology). When I use a GPIO for turning things OFF & ON, I favor the "straightforward" (my terminology) pins; i.e. pins that don't have a special purpose.
    – Seamus
    Commented May 13 at 19:56
  • Thanks for answering @Seamus
    – LearneriOS
    Commented May 14 at 3:46

I think Milliways has a valid point that might not have sunk in on you yet: You are using a relay module. The issue with relay modules is that they are mostly manufactured at fly-by-night factories in China, and typically have no (or uselessly incorrect) documentation. Compounding this issue is that the morons who sell these modules don't know the difference either!

Your challenge is therefore two-fold:

  1. Sort out the hardware
  2. Get your code correct

Unfortunately, we cannot help you sort out the hardware as we have no documentation!! All we can do is point out the difference. This is what I've attempted to do in the schematics below. The top schematic shows a relay (i.e. a coil (solenoid) that actuates contacts); the bottom schematic is a relay module. The relay module MAY (or may not) contain:

  • level shifters (to change 3.3V to 5V)
  • transistors (or other solid-state switches) to drive the relay coil
  • a suppression diode to reduce L*di/dt kickback voltage


simulate this circuit – Schematic created using CircuitLab


Because you are faced with two unknowns (software and hardware), I would recommend you approach this as follows:

  1. Disconnect your RPi GPIO pin(s); set it to one side temporarily

  2. Using the voltages available on your RPi (Ground, 3.3V, 5V), make temporary wiring interconnections to the relay module to learn what makes it work.

  • Connect +5V & Ground to the relay module's supply terminals
  • Try connecting 3.3V at the input & observe the status of the contactors
  • If 3.3V does not actuate contactor, try 5V

It would be preferable (if possible?) to use "lab power supplies" for this trial-and-error, and to have a decent multimeter to measure voltage & resistance. Failing that, you may have to use voltages from your RPi.

Hope that helps - let us know if you have Questions.

  • While l support your testing suggestion it may not work. With 3.3V i.e. presenting 1.7V to the relay circuitry there may be insufficient drive to activate the relay BUT once activated may be sufficient to prevent release.
    – Milliways
    Commented May 13 at 22:52
  • Thanks for answering @seamus
    – LearneriOS
    Commented May 14 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.