FitPC, GPIO and Python

Post Reply
DaveLangstaff
Posts: 5
Joined: Thu Oct 29, 2015 10:21 am

FitPC, GPIO and Python

Post by DaveLangstaff »

Having spent a little while getting Python to talk to the GPIO on a Fitlet, I thought I'd share.

The first thing is to make sure the kernel is up to date. I downloaded and installed fitlet_kernel_3.19_2015-12-16 following the instructions on this site.

A user needs superuser privileges in order to talk to the hardware, so you need to launch the development environment as a super user, so I started with the desktop, brought up a terminal prompt and type "sudo spyder" to get editing.

The following code turns on and off an LED connected between pin gpio89 and ground via a 300 ohm resistor. All interaction with the gpio driver is done by writing (and reading) files as shown in the example. Unfortunately the forum seems to strip indentation before display, so the for loop in the example will need fixing before you try and run the code.

I'd be interested to know how other people get on and if anyone has success in getting interrupts etc working from within Python.

dave

# -*- coding: utf-8 -*-
"""
@author: Dave Langstaff dpl@aber.ac.uk
Turn LED on gpio89 on and off
"""
import time # needed for time.sleep to time loop
import os # needed for permissions fudge on files

# first set up gpio pin 89
f=open('/sys/class/gpio/export','w')
f.write('89')
f.close()

# fudge to get write permission
# This fudge could/should be fixed in a future driver release
os.system('chmod a+rw /sys/class/gpio/gpio89/*')

# define pin 89 as output
f=open('/sys/class/gpio/gpio89/direction','w')
f.write('out')
f.close()

# open file to write values to pin
f=open('/sys/class/gpio/gpio89/value','w')

# turn LED on
f.write('1') # note it is the character 1 we are writing
f.flush() # need to use flush() or close() after each write

# now loop around to flash off and on
for i in range(10):
time.sleep(0.1)
f.write('0'); f.flush() # LED off.
time.sleep(0.1)
f.write('1'); f.flush() # LED on
# end of loop

# turn LED off at end
f.write('0');f.flush()

# close file
f.close()

# release gpio line
f=open('/sys/class/gpio/unexport','w')
f.write('89')
f.close()

# Enjoy!!

mbirger

Re: FitPC, GPIO and Python

Post by mbirger »

Thanks for sharing. Your example is shared on fitlet FAQ page:
http://fit-pc.com/wiki/index.php/FAQ:fi ... der_Python

Post Reply

Return to “General fitlet questions”