kxvsm
Posts: 3
Joined: Sun Jul 15, 2018 10:18 pm

Turn on lights when raspberry pi is powered on

Sun Jul 15, 2018 10:29 pm

Hi guys, my name is Kevin. Long time reader of the forums, first time poster so bare with me please if I’ve asked any questions that have already been answered.

I’ve been making a few DIY arcade fight sticks for friends and family but now I’m looking to do something a little more and tidy up my current work.

My question is, how can I power on a light bulb when the raspberry pi is powered on? I’m trying to incorporate a light into my marquee, as well as a few LED strips into the sides of the cabinet. The lights would be externally powered. I’ve seen people control gpio pins over web servers but that’s not really what I’m looking for. Would I need some sort of relay connected to power and then the pi and run that with a startup script?

Thank you in advance for any advice.

mutrised
Posts: 44
Joined: Thu Nov 08, 2012 12:41 am
Location: France

Re: Turn on lights when raspberry pi is powered on

Mon Jul 16, 2018 7:34 am

Hi,

If you don't want to mess up with high voltage circuit, I recommend you buy some modules with integrated relay. You have some cheap one on the internet (try googling relai module).
So you can safely drive 12V LED strips or even 220/240V light sources (with care of course). I bough several of these module yet, they seems to me well made but I didn't try to use them in projects (no enough time yet, or maybe lack of idea).

Once your module is plugged you just need to add a python script to your /etc/rc.local.

Code: Select all

# GPIO lib import
import RPi.GPIO as GPIO

# Use BOARD pin numbering
GPIO.setmode(GPIO.BOARD)

# Configuration
LED=18
GPIO.setup(LED, GPIO.OUT, initial=GPIO.HIGH)
Save it under let's say /etc/coucou.py, you then add to /etc/rc.local (before "exit 0" line)

Code: Select all

python /etc/coucou.py
And there you have it =)

Hope that's what you needed ;)
RPI2B 7/24 - web, NAS/media, Owncloud and more
RPI0 7/24 - VPN, DHCP, DNS (including filtering), wakeonlan proxy
RPI0W - VPN gateway providing secure WIFI AP and network router, some kind of internet BOX when on the move ;)

kxvsm
Posts: 3
Joined: Sun Jul 15, 2018 10:18 pm

Re: Turn on lights when raspberry pi is powered on

Mon Jul 16, 2018 5:55 pm

Thank you for your advice. figured it was something along those lines but wasn’t sure what code I should use to enable the gpio pins I’ll be ordering a relay today and give it a shot when it comes in.

Will this script also turn off the lights or GPIO pins when I execute a shutdown as well Like into a halt state? I have a button rigged up to gpio 5 and 6 for easy on and off and wanted that same button to also trigger the lights to turn on when the pi boots up. Thank you again, just trying to make sure I’m following correctly.

mutrised
Posts: 44
Joined: Thu Nov 08, 2012 12:41 am
Location: France

Re: Turn on lights when raspberry pi is powered on

Mon Jul 16, 2018 8:42 pm

I'm not sure of how GPIO are handled during halt state. I supposed they are IO state are cleared so light should turn off I think.

If it's not the case:

If you already added a script to handled the on.off button, add a line to set the light GPIO to low.

If you didn't, you could create a script the listen for event on this same button and shut the light off when event is triggered,
or a more "system" way to do it would be to create a script that run at logout, using rc script for example to run similar python code to set the GPIO low.

Get back to me if you need further help, I try to check the forum sometime at work ;)
RPI2B 7/24 - web, NAS/media, Owncloud and more
RPI0 7/24 - VPN, DHCP, DNS (including filtering), wakeonlan proxy
RPI0W - VPN gateway providing secure WIFI AP and network router, some kind of internet BOX when on the move ;)

kxvsm
Posts: 3
Joined: Sun Jul 15, 2018 10:18 pm

Re: Turn on lights when raspberry pi is powered on

Mon Jul 30, 2018 9:45 pm

Okay so ive taken a stab at this now after ordering a few parts. ive got it working, only one problem now.
the lights startup and shutdown correctly as id like them too, but they don't turn on at the time Id like which would be immediately upon boot. instead it waits for a ES splash screen.
ive posted a video showing whats happening along with the code im using ( don't mind my messy setup )

https://www.youtube.com/embed/B7rZnrQ61iY

Is their a better way to use this code? I tried the one you supplied with no luck.
im using Recalbox and from what I saw theirs no rc.local directory? I have this script located in /recalbox/share/system/
I also have it located in /etc/init.d but that doesn't seem to do anything for me.

Code: Select all

#!/usr/bin/python
# GPIO lib import
import RPi.GPIO as GPIO
import time

channel = 18

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
Any advice would be greatly appreciated.

PhatFil
Posts: 1248
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Turn on lights when raspberry pi is powered on

Tue Jul 31, 2018 2:21 am

As its always best to keep mains power well away from diy electronics may i suggest a £4($5) Sonnof wifi relay control. flash the onboard esp8266 with alternative firmware you can write your own but there are a number of established projects such as tasmota..
https://github.com/arendst/Sonoff-Tasmota
If you use tasmota you can host a mqtt broker on your pi and control your mains device by publishing the relevant mqtt topic/payload ;)

Return to “Automation, sensing and robotics”