RPi.GPIO for LED, Push Button, Relay Module


RPi.GPIO for LED, Push Button, Relay Module

(Tested on RPi 3)



Install RPi.GPIO

$ sudo su
# apt-get update
# apt-get install python-pip python-dev gcc
# pip install rpi.gpio

(may showed some warnings but will successfully installed)


RPi.GPIO Test

connect ‘LED’:
[pin 39] — [jumper] — [-LED+] — [jumper] — [R100ohm] — [jumper] — [pin 40]

$ sudo su
# python
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BOARD)
>>> GPIO.setup(40, GPIO.OUT)
>>> GPIO.output(40, 1)
>>> GPIO.output(40, 0)
>>> GPIO.cleanup()
>>> exit()

GPIO.output(40, 1) = LED on
GPIO.output(40, 0) = LED off



GPIO - LED on/off

connect ‘LED’:
[pin 40] — [jumper] — [+LED-] — [jumper] — [R100ohm] — [jumper] — [pin 39]

create python on script:

$ sudo su
# nano /home/osmc/lon.py

add to the script:

#!/usr/bin/python
import RPi.GPIO as GPIO
		
# connect RPi numbered pin
led = 40
		
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led, GPIO.OUT, initial=1)

create python off script:

# nano /home/osmc/loff.py

add to the script:

#!/usr/bin/python
import RPi.GPIO as GPIO
		
# connect pins
led = 40
		
# pins setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led, GPIO.OUT)
		
GPIO.output(led, 0)
GPIO.cleanup()

change files permision:

# chmod +x /home/osmc/*.py

LED on:

# /home/osmc/lon.py

LED off:

# /home/osmc/loff.py


GPIO - Push Button + LED

connect ‘push button’:
[pin 33] — [jumper] — [push button] — [jumper] — [pin 34]

connect ‘LED’:
[pin 40] — [jumper] — [+LED-] — [jumper] — [R100ohm] — [jumper] — [pin 39]

create python button script:

$ sudo su
# nano /home/osmc/lflash.py

add to the script:

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

# connect RPi numbered pins and flashing duration
led = 40
button = 33
sec = 0.5

# hide warning
GPIO.setwarnings(0)
# pins setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led, GPIO.OUT)

# wait for button press
print('Wait for button press...')
GPIO.wait_for_edge(button, GPIO.FALLING)

# led flash 3 times then off
GPIO.output(led, 1)
time.sleep(sec)
GPIO.output(led, 0)
time.sleep(sec)
GPIO.output(led, 1)
time.sleep(sec)
GPIO.output(led, 0)
time.sleep(sec)
GPIO.output(led, 1)
time.sleep(sec)
GPIO.output(led, 0)

# clear settings
GPIO.cleanup()

change file permission:

# chmod +x /home/osmc/*.py

Activate:

# /home/osmc/lflash.py

LED flash:
press button



GPIO - Push Button + Command (e.g. shutdown)
(Note: A remote control with keymaps should be easier than a push button.)

connect ‘push button’:
[pin 33] — [push button] — [jumper] — [pin 34]

create python button script:

$ sudo su
# nano /home/osmc/bshutdown.py

add to the script:

#!/usr/bin/python
import RPi.GPIO as GPIO
import os

# hide warning
GPIO.setwarnings(0)

# connect RPi numbered pin
button = 33

# hide warning
GPIO.setwarnings(0)

# pins setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# wait for button press
print('Wait for button press...')
GPIO.wait_for_edge(button, GPIO.FALLING)
		
# activate system command
os.system('sudo shutdown -h now')

change file permission:

# chmod +x /home/osmc/*.py

Shutdown

# /home/osmc/bshutdown.py

press button



GPIO - Relay Module

connect ‘relay module’ (some pins may glow module LEDs by default)

.[pin 6] — [jumper] — [module GND]
.[pin 35] — [jumper] — [module IN1]
.[pin 36] — [jumper] — [module IN2]
.[pin 37] — [jumper] — [module IN3]
.[pin 38] — [jumper] — [module IN4]
.[pin 4] — [jumper] — [module VCC]

create python relay-on script:

$ sudo su
# nano /home/osmc/ron.py

add to the script:

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

# custom variables
# connected RPi numbered pins
r1 = 35
r2 = 36
r3 = 37
r4 = 38
# delay in seconds
r1delay = 3
r2delay = 1
r3delay = 2
r4delay = 3
		
on = 0
off = 1

# hide warning
GPIO.setwarnings(0)

# pins setup
GPIO.setmode(GPIO.BOARD)

# set pin to output
GPIO.setup([r1,r2,r3,r4], GPIO.OUT, initial=off)

# activate on sequence
if GPIO.input(r1) == on:
    print('Already ON')
else:
    print ('Activate ON sequence in %d s' % r1delay)
    time.sleep(r1delay)
    GPIO.output(r1, on)
    print ('#1 on - wait %d s' % r2delay)
    time.sleep(r2delay)
    GPIO.output(r2, on)
    print ('#2 on - wait %d s' % r3delay)
    time.sleep(r3delay)
    GPIO.output(r3, on)
    print ('#3 on - wait %d s' % r4delay)
    time.sleep(r4delay)
    GPIO.output(r4, on)
    print ('#4 on')
    print ('OFF: roff.py')

create python relay-off script:

# nano /home/osmc/roff.py

add to the script:

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

# custom variables
# connected RPi numbered pins
r1 = 35
r2 = 36
r3 = 37
r4 = 38
# delay in seconds
r1delay = 3
r2delay = 1
r3delay = 2
r4delay = 3
	
on = 0
off = 1

# hide warning
GPIO.setwarnings(0)

# pins setup
GPIO.setmode(GPIO.BOARD)

# set pin to output
GPIO.setup([r1,r2,r3,r4], GPIO.OUT)

# activate off sequence
if GPIO.input(r1) == off:
    print('Already OFF')
else:
    print ('Activate OFF sequence in %d s' % r1delay)
    time.sleep(r1delay)
    GPIO.output(r1, off)
    print ('#1 off  - wait %d s' % r2delay)
    time.sleep(r2delay)
    GPIO.output(r2, off)
    print ('#2 off - wait %d s' % r3delay)
    time.sleep(r3delay)
    GPIO.output(r3, off)
    print ('#3 off - wait %d s' % r4delay)
    time.sleep(r4delay)
    GPIO.output(r4, off)
    print ('#4 off')

# clear settings
GPIO.cleanup()

change file permission:

# chmod +x /home/osmc/*.py

relays on:

# /home/osmc/ron.py

relays off:

# /home/osmc/roff.py


GPIO - Remote Control + Command

Keymaps:
‘System.Exec’
- has some difficuties running commands and scripts.
‘RunScript’
- a workaround but it will not run python scripts directly
- scripts must be called from another python script

os.system(‘sudo scriptfile’)
- system commands run fine with

os.system(‘sudo command’)

create python system command script for lon.py:

$ sudo su
# nano /home/osmc/loncmd.py

add to the script:

#!/usr/bin/python
import os
os.system('sudo /home/osmc/lon.py')

create python system command script for loff.py:

# nano /home/osmc/loffcmd.py

add to the script:

#!/usr/bin/python
import os
os.system('sudo /home/osmc/loff.py')

change file permission:

# chmod +x /home/osmc/*.py

edit keymaps file (keyboard - use keyboard.xml):

# cp /usr/share/kodi/system/keymaps/remote.xml /home/osmc/.kodi/userdata/keymaps/
# nano /home/osmc/.kodi/userdata/keymaps/remote.xml

add to the script:

<?xml version="1.0" encoding="UTF-8"?>
<keymap>
<global>
    <remote>
        <zero>RunScript(/home/osmc/loffcmd.py)</zero>
        <one>RunScript(/home/osmc/loncmd.py)</one>
    </remote>
</global>
</keymap>

Restart to load the keymaps:
Power > Exit

LED on:
press button

LED off:
press button



GPIO - Run Script Startup

create systemd service file:

$ sudo su
# nano /lib/systemd/system/bshutdown.service

add to the file:

[Unit]
Description = GPIO shutdown button

[Service]
Type = idle
ExecStart = /home/osmc/bshutdown.py

[Install]
WantedBy = multi-user.target

enable the service:

# systemctl enable bshutdown

start the service:

# systemctl start bshutdown

3 Likes

I’m a noob when it comes to osmc. I’m trying to add an indicator light for when I turn my pi on/off. This the LED portion of this looks promising but, it isn’t clear as to whether or not this’ll work for a single led connected to the GPIO of a Pi 3. I have retropie running on another Pi 3,and this looks similar to what was needed for the on/off button and LED indicator I use with that.
Could you clarify a bit and add an image of the GPIO used in this please?

If you need only the extra power indicator, this method would be easier.