Use python to control gpio

Source: Internet
Author: User
Blinky, button & gpio examples using Python
Contents

[Hide]

  • 1 Examples using Python
    • 1.1 Blinky example
    • 1.2 Button example
    • 1.3 Gpio example


Examples using Python

The following application note covers the use of parameter um100's peripherals using python. sysfs method is used to access the gpio's. to create the interfaces for gpio's under/sys/class/gpio/*, the following configuration has to be enabled in the board configuration file

 
Config_gpio_sysfs = y

Blinky example

User LED is connected to gpio pa30. an interface for the user LED is created in the kernel by adding the following code in board-electrum-100.c

 
/** LEDs */static struct gpio_led ek_leds [] ={{/* led1, yellow */. name = "ds1 ",. gpio = at91_pin_pa25 ,. default_trigger = "Heartbeat",}, {/* led2, green */. name = "DS2 ",. gpio = at91_pin_pa30 ,//. active_low = 1 ,. default_trigger = "NONE ",}};

And add the following lines in ek_board_init ()

 
/* LEDs */at91_gpio_leds (ek_leds, array_size (ek_leds ));

Python script for blinky example:

 
Import sysimport timeprint "blinking user led" print "Enter Ctrl + C to exit" def ledon (): value = open ("/sys/class/LEDs/DS2/brightness ", "W") value. write (STR (1) value. close () def ledoff (): value = open ("/sys/class/LEDs/DS2/brightness", "W") value. write (STR (0) value. close () While true: ledon () time. sleep (. 5) ledoff () time. sleep (. 5)

Button example

In parameter um100, gpio pa31 is used for the user button.

Import sysimport timeprint "button example" print "press Ctrl + C to exit" def pins_export (): Try: pin1export = open ("/sys/class/gpio/export ", "W") pin1export. write (STR (63) pin1export. close () Counter t ioerror: Print "info: gpio 63 already exists, skipping export gpio" def read_button (): FP1 = open ("/sys/class/gpio/gpio63/value", "R") value = fp1.read () Return Value fp1.close () def write_led (value ): fp2 = open ("/sys/class/LEDs/DS2/brightness", "W") fp2.write (STR (value) fp2.close () pins_export () While true: button_value = read_button () write_led (button_value)

Gpio example

the following example allows the user to set a selected gpio pin as an input or output and read the values from the pins configured. as some of the gpio's have alternate functions, it is required that, pins have to be initialized as gpio's before executing the following test example. set-gpio is a C-binary that initializes a single pin at a time. (default as input pin)

Import sysimport timeimport redef setpins (pin_no, pin_direction): gpioopnum = "gpio % s" % (STR (pin_no ),) pin1dir = open ("/sys/class/gpio/" + gpioopnum + "/Direction", "W") if pin_direction = 1: pin1dir. write ("high") else: pin1dir. write ("in") pin1dir. close () def readpins (pin_no): gpioopnum = "gpio % s" % (STR (pin_no ),) pin1val = open ("/sys/class/gpio/" + gpioopnum + "/value", "R") Output = pin1val. read () print "The value on the pin % s is: % s" % (STR (pin_no), STR (output) pin1val. close () def unexport_pins (PINs): Try: fp4 = open ("/sys/class/gpio/unexport", "W") fp4.write (STR (PINs) fp4.close () failed t ioerror: Print "gpio % s is not found, so skipping unexport gpio" % (STR (PINs),) def export_pins (PINs): try: FP1 = open ("/sys/class/gpio/export", "W") fp1.write (STR (PINs) fp1.close () handle T ioerror: Print "GPI O % s already exists, so skipping export gpio "% (STR (PINs),) print" Warning: Make sure that C-binary to initialize gpio's is executed prior to this. the script may not work as intended if the ports are not initialized properly "+" \ n "for I in range (1, Len (sys. argv): export_pins (sys. argv [I]) Direction = raw_input ("Configure pin % s as output (1)/input (2 )?: "% (STR (sys. argv [I]),) setpins (sys. argv [I], INT (Direction) print "\ n" + "reading pin values ..... "For I in range (1, Len (sys. argv): readpins (sys. argv [I]) for I in range (1, Len (sys. argv): unexport_pins (sys. argv [I])

Procedure:

 
Electrum100:/home/Python # Set-gpio 32 pioa 0 set as input1_um100:/home/Python # Set-gpio 55 pioa 23 set as input

Before connecting the short cable between pa0 and pa23

 
Export um100:/home/Python # Python gpio1.py 32 55 warning: Make sure that C-binary to initialize gpio's is executed prior to this. the script may not work as intended if the ports are not initialized properly configure pin 32 as output (1)/input (2 )? : 1 configure pin 55 as output (1)/input (2 )? : 2 reading pin values... the value on the pin 32 is: 1The value on the pin 55 is: 0

After connecting the short cable between pa0 and pa23

Export um100:/home/Python # Python gpio1.py 32 55 warning: Make sure that C-binary to initialize gpio's is executed prior to this. the script may not work as intended if the ports are not initialized properly configure pin 32 as output (1)/input (2 )? : 1 configure pin 55 as output (1)/input (2 )? : 2 reading pin values... the value on the pin 32 is: 1The value on the pin 55 is: 1

The Convention used to represent the PIN numbers using sysfs procedure is explained with examples.

 
Pa base-32 Pb base-64 PC base-96 ex: to initialize pa1, use pa-base + 1 = 32 + 1 = 33 pa31, use 32 + 31 = 63 pb0, use Pb-base + 0 = 64 + 0 = 64 pc10, use 96 + 10 = 106

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.