I have SRD-12VDC-SL-C relay module which I want to control using raspberry pi but unfortunately, none of the gpio off commands are working. However GPIO.cleanup and turning pin to input is working to turn off the relay, however, I am not sure whether using raspberry pi like this is safe or not. Also, I am providing relay module externally 12v and using gpio pins just to control the relay (for providing supply to optocoupler). Please respond as soon as possible.


The circuit is like this, instead of arduino there is raspberry and the relay is of 12v.

  • Hello, Welcome to RPi.SE. Can you upload the schematic how relay is connected to RPi GPIO ? It looks like more of a circuit problem rather than RPi problem. – dhruvvyas90 Jan 25 '17 at 21:50
  • uploaded the image. @dhruvvyas90 – Shreyansh Agarwal Jan 25 '17 at 22:01
  • There should be a common ground involved, most likely. – goldilocks Jan 25 '17 at 22:08
  • @ShreyanshAgarwal: This particular link would be helpful. raspberrypi.org/forums/viewtopic.php?t=19222 Since RPi GPIOs are when high produce 3v3 not 5v, it is most likely the problem in this scenario. You can use a simple transistor as shown in thread and it should be good. Let me if it works out for you. – dhruvvyas90 Jan 26 '17 at 17:07

First, based on you diagram, you need to add a ground wire from the RPi to the ground of the relay. With out a common ground, you will be strange results.

Second, from my experience, optocoupler driven relays are generally triggered with a ground or LOW input. You can verify this by testing the relay "IN" pin(s). Most likely, when you touch ground to the IN pin, the relay should turn on and then off when you remove the ground, which is exactly what happens when you perform a GPIO.cleanup command. GPIO.cleanup returns the GPIO pins to an input floating state (effectively disconnecting them).

For yours to work correctly, you'd want to use a pull up resistor on the output to keep the relay off until you trigger the RPi's GPIO output (to relay IN) with a LOW signal, which will turn the relay on.

To turn the relay off, the output should be set to HIGH. It is possible that the 3.3V HIGH state may not reach the "turn off" threshold needed for that relay since it is expecting 5V. You can just change the pin to "INPUT" to turn it off and then back to OUTPUT LOW to turn it on. If you go the INPUT route, do not use the pull up resistor.


After too much research on this topic and then practically implemented it, I can say that the problem arose because of supplying the optocoupler with 5v from the header and it was likely that the GPIO (which only goes up to 3v3) was not going high enough to switch the optocoupler. And I'll advise that one should not use 5v as it can pull the GPIO higher than 3v3 frying up the pi. Also, If you are having thoughts that using 3v3 can draw more ampere, than its wrong as the optocoupler only takes about 4mA which I have found from https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=172546.

Your Answer

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

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