RPi.GPIO for LED, Push Button, Relay Module
(Tested on RPi 3)
---------------------------------------------------------------------------------------------------------------------
updated 20170108 - Python, GCC and RPi.GPIO already installed by default. Installation part removed.
---------------------------------------------------------------------------------------------------------------------
Symlink Python
ln -s /usr/bin/python2.7 /usr/bin/python
---------------------------------------------------------------------------------------------------------------------
RPi.GPIO Test
connect 'LED':
- [pin 39] --- [jumper] --- [-LED+] --- [jumper] --- [R100ohm] --- [jumper] --- [pin 40]
# 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:
# nano /root/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 /root/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 /root/*.py
LED on:
/root/lon.py
LED off:
# /root/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:
# nano /root/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 /root/*.py
Activate:
# /root/lflash.py
LED flash:
---------------------------------------------------------------------------------------------------------------------
GPIO - Push Button + Command (e.g. shutdown)
(Note: A remote control with XBindKeys should be easier than a push button.)
connect 'push button':
- [pin 33] --- [push button] --- [jumper] --- [pin 34]
create python button script:
# nano /root/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('/var/www/command/rune_shutdown; poweroff')
change file permission:
# chmod +x /root/*.py
Shutdown
# /root/bshutdown.py
---------------------------------------------------------------------------------------------------------------------
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:
# nano /root/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 /root/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 /root/*.py
relays on:
# /root/ron.py
relays off:
# /root/roff.py
---------------------------------------------------------------------------------------------------------------------
GPIO - Run Script Startup
create systemd service file:
# nano /lib/systemd/system/bshutdown.service
add to the file:
[Unit]
Description = GPIO shutdown button
[Service]
Type = idle
ExecStart = /root/bshutdown.py
[Install]
WantedBy = multi-user.target
enable the service:
# systemctl enable bshutdown
start the service:
# systemctl start bshutdown