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…
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!
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)
#!/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
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
/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.