1

There are loads of Youtube videos on how to control a relay, but I simply want to be able to power up my Pi and let it activate a relay. Then, when I power it down, it should turn the relay off.

No buttons or interfaces, just on and off.

I've built a arcade machine and have illuminated buttons. But I want them to turn on when I power on the pi and off when I'm finished. I have bought the relay as I have LED strips lighting also and just waiting for it to arrive. Do I need code or can I just simply attach to the GPIO 5v and ground?

I'm not sure if just plugging it in and hope for the best is a good idea. It's only £30 to replace, but it's still £30 quid. Grrr!!

Hopefully someone can help .

Kind regards brent

PS. As you can tell I'm not a programmer. I like the idea, but I just need to get this problem sorted first. Then I can mess with the next Pi I buy.

Starting to get the bug...

  • Welcome to raspberrypi and posting a good question. I would be nice if you would edit your post and mention what OS you've installed. It would also be nice if you would link the mentioned Youtube videos. – MadMike Aug 19 '17 at 14:49
  • Just remember to protect the Raspberry Pi GPIO with a "flyck back" diode. Inductive load can destroy your GPIO ports. And normally the Raspberry Pi can't drive a relay, it needs a transistor circuit. example: i.stack.imgur.com/4qyrw.png – MatsK Aug 19 '17 at 21:01
2

I have made the same to control a relay and switch on/off my 3D Printer from my computer. I assume you are running the latest rasbian. You need to run a python script like that:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.output(12,1)

Make a file (relay.py for example) and copy paste the above script, save and exit. That script will use PIN 12 and will set it HIGH so you can turn on your relay.You run the script with command "python relay.py" You need also to make it autostart on boot.

Your relay need of course to be driven with a NPN transistor. Be sure to connect your leds on the NO (Normaly Open) pin of the relay. That way when you turn off raspi the GPIO will be off and relay in NC (Normaly Closed) contact so leds will turn off. I dont know your experience but look here for some more information https://learn.sparkfun.com/tutorials/raspberry-gpio/python-rpigpio-api

Good luck and always double check connections before you power on!

  • Using Rpi GPIO output mode 3V3 signal to drive an NPN transistor such as 2N2222 which in turn drives a bare RELAY SWITCH such as 5V Songle relay switch is a bit risky. I think a safer solution is to use a cheap 5V RELAY MODULE (recommend High level (NOT Low trigger) trigger type, from Amazon, AliExpress etc). – tlfong01 Sep 8 at 9:41
  • Relay Switch vs Relay Module: imgur.com/gallery/VNGSttK – tlfong01 Sep 8 at 9:50
  • Relay Module Selection Suggestion: raspberrypi.stackexchange.com/questions/99988/… – tlfong01 Sep 8 at 9:54
0

Here's one of many solutions to this problem. It relies on you properly shutting down your Pi by running something like sudo shutdown or clicking a shutdown button on your OS. If you use sudo halt or sudo shutdown -h or simply unplug your Pi, this will not turn the relay off.

Turning the Relay On at Boot

You will need a script that turns the relay on. Most likely this will be a Python script that looks something like:

#!/usr/bin/env python3
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.output(12,1)

Notice the #!/usr/bin/env python3 (known as a shebang). This is important as it tells Linux that this is a Python script, specifically Python 3 (you can replace python3 with python if you are planning on using Python 2.x. Save this "relay on" script (which will not look identitical to what I have posted above) as relayOn with no extension necessary.

Move this file to /etc/init.d/. This is the directory where scripts are ran during boot. You will also need to make this script executable by running chmod +x relayOn.

Turning the Relay Off at Shutdown

Similar to /etc/init.d/ there is a directory in which scripts are executed during a shutdown. This is /etc/rc6.d/. So, to shut down your relay when your Pi shuts down, write a similar Python script (don't forget the shebang) that turns the relay off. Save it as relayOff (again no extension required) and move it to /etc/rc6.d/. Again, make this script executable with chmod +x relayOff. Now this script will execute during a proper shutdown.

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.