Manage GPIO by Python software (Debian)
Posted: Tue Feb 09, 2016 3:43 pm
Hi ! Please help me to write code to control GPIO by Python language. I try'ed (by root privileges)
======
Creating of file GPIO:
echo 11 > /sys/class/gpio/export
Customize output (out):
echo out > /sys/class/gpio/gpio11/direction
Write value (0 или 1):
echo 1 > /sys/class/gpio/gpio11/value
LED wil be enables.
echo 0 > /sys/class/gpio/gpio11/value
LED wil .be disables
Destroy connection to GPIO:
echo 11 > /sys/class/gpio/unexport
Example of code in Python language:
wget https://sites.google.com/site/semillero ... T_blink.py
python ADT_blink.py
Script ask's , which GPIO connected by LED and how many time will by blink / Par example open ADT_blink.py with nano editor :
nano ADT_blink.py
Let's try manipulation with button. button connect by GPIO17.
Create file access to GPIO:
echo 17 > /sys/class/gpio/export
Customize as Input 1 (in):
echo in > /sys/class/gpio/gpio17/direction
Read value:
cat /sys/class/gpio/gpio17/value
At first time commend return "1" (voltage are +3.3В). Let's press a button and try command:
cat /sys/class/gpio/gpio17/value
The command will must return 0 value
Example a Python programm (file test_button.py):
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)
while True:
if (GPIO.input(17)==0):
print("Button Pressed")
time.sleep(1);
Run the script:
python test_button.py
Script must write “Button Pressed”, when the button are pressed.
We use enable GPIO-input up to logical zero with external resistor. But we may do it programmable with pull_up_down script :
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
p.s. How use with GPIO in Python
1) Importl library GPIO
import RPi.GPIO as GPIO
2) Set numeration mode GPIO
GPIO.setmode(GPIO.BCM)
# GPIO.BCM – use a GPIO numeration
# GPIO.BOARD – use numeration as P1-26
3) Configure input/output
Par example, GPIO 11, as output, а GPIO 17 – as input
GPIO.setup(11, GPIO.OUT) #
GPIO.setup(17, GPIO.IN) #
With additional parameter pull_up_down of function setup you will "connect" logical Zero :
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # conect to power 3,3 v
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # connect to ground level
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # connection disabled
4) Set output signal, get input signal
Write “1” і “0” to GPIO 11 and read from GPIO 17:
GPIO.output(11, True) # write to GPIO 11 “1” (3.3 V)
GPIO.output(11, False) # write to GPIO 11 “0” (0 V)
signal = GPIO.input(17) # read GPIO 17 to value signal
5) End of work
GPIO.cleanup() # restore GPIO to initial state.
==
Best regards, Serge
======
Creating of file GPIO:
echo 11 > /sys/class/gpio/export
Customize output (out):
echo out > /sys/class/gpio/gpio11/direction
Write value (0 или 1):
echo 1 > /sys/class/gpio/gpio11/value
LED wil be enables.
echo 0 > /sys/class/gpio/gpio11/value
LED wil .be disables
Destroy connection to GPIO:
echo 11 > /sys/class/gpio/unexport
Example of code in Python language:
wget https://sites.google.com/site/semillero ... T_blink.py
python ADT_blink.py
Script ask's , which GPIO connected by LED and how many time will by blink / Par example open ADT_blink.py with nano editor :
nano ADT_blink.py
Let's try manipulation with button. button connect by GPIO17.
Create file access to GPIO:
echo 17 > /sys/class/gpio/export
Customize as Input 1 (in):
echo in > /sys/class/gpio/gpio17/direction
Read value:
cat /sys/class/gpio/gpio17/value
At first time commend return "1" (voltage are +3.3В). Let's press a button and try command:
cat /sys/class/gpio/gpio17/value
The command will must return 0 value
Example a Python programm (file test_button.py):
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)
while True:
if (GPIO.input(17)==0):
print("Button Pressed")
time.sleep(1);
Run the script:
python test_button.py
Script must write “Button Pressed”, when the button are pressed.
We use enable GPIO-input up to logical zero with external resistor. But we may do it programmable with pull_up_down script :
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
p.s. How use with GPIO in Python
1) Importl library GPIO
import RPi.GPIO as GPIO
2) Set numeration mode GPIO
GPIO.setmode(GPIO.BCM)
# GPIO.BCM – use a GPIO numeration
# GPIO.BOARD – use numeration as P1-26
3) Configure input/output
Par example, GPIO 11, as output, а GPIO 17 – as input
GPIO.setup(11, GPIO.OUT) #
GPIO.setup(17, GPIO.IN) #
With additional parameter pull_up_down of function setup you will "connect" logical Zero :
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # conect to power 3,3 v
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # connect to ground level
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # connection disabled
4) Set output signal, get input signal
Write “1” і “0” to GPIO 11 and read from GPIO 17:
GPIO.output(11, True) # write to GPIO 11 “1” (3.3 V)
GPIO.output(11, False) # write to GPIO 11 “0” (0 V)
signal = GPIO.input(17) # read GPIO 17 to value signal
5) End of work
GPIO.cleanup() # restore GPIO to initial state.
==
Best regards, Serge