Power & Source of Big Ideas

Manage GPIO by Python software (Debian)

Moderators: chensy, FATechsupport

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

while True:
if (GPIO.input(17)==0):
print("Button Pressed")

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.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
Hi we have a code sample here:
http://wiki.friendlyarm.com/wiki/index. ... ompact_Kit

You can search for "python".

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 5185 on Wed Jan 22, 2020 1:44 pm

Users browsing this forum: No registered users and 1 guest