Part 3: Getting started with Python & PiFace
Load IDLE
1. First import the piface.pfio module, to save typing out piface.pfio each time add the abbreviation to the import: import piface.pfio as pfio 2. To use the PiFace the board must be initialised using the code: pfio.init() 3. There are three main functions that can be coded to control the PiFace, these include the number of the pin that is being either turned on or off. The second number denotes the state of the pin, this is represented by a 1 for On or 0 for Off. The three simple codes are: 1. Reading the state of a pin: pfio.digital _read(pin number) Reads the INPUT state of the pin number and returns the state as either a 1 On or 0 Off, 2. Write / Changing the state of a pin: pfio.digital _write(pin number, state) Sets the state of the OUTPUT, as identified by the pin number and to either 1 On or 0 Off. On, 1 will turn the the corresponding LED on and enables the current to flow. 3. Enabling a pull up: pfio.digital _write_pullup(pin number, state) This sets a 10k pullup on the input that has been identified by the pin_number. There are again two states, 1 means that the pull is enabled, 0 means it is not. This is useful for checking if a button has been pressed. |
Part 4: Simple Python Code to Test the PiFace1. Reading the state of a pin:
import piface.pfio as pfio pfio.init() pfio.digital _read(2) This code reads the state of the pin number 2, if a button is pressed it returns a value of 1 and if not it returns a 0. The state is only checked once and requires a while statement to continually check the state. 2. Write / Changing the state of a pin: import piface.pfio as pfio pfio.init() pfio.digital _write(3, 1) This code sends the state value of 1 to pin number 3, (it basically turns on pin number 3). This can be combined with the following line of code, pfio.digital _write(3, 0) to turn it off again. It is advisable to use the time sleep code to add a small delay between the changes of state. import time import sleep import piface.pfio as pfio pfio.init() pfio.digital _write(3, 1) #turns on pin 3 sleep(1) pfio.digital _write(3, 0) # turns off pin 3 (This will flash LED 3 once) Remember: Although there are 8 pins (inputs and outputs) when programming, the first pin number is pin 0 and the last pin is pin number 7. There are 8 pins, numbers 0,1,2,3,4,5,6,7 |
- Home
- Python
- Ras Pi
-
Pi Hardware
- Pi-Hacks
-
Pi-Hacks 2
- The Joker
- Hologram Machine
- Google Vision: Camera Tell
- Yoda Tweets
- Pi Phone
- Darth Beats
- Twitter Keyword Finder
- Crimbo Lights Hack
- Xmas Elf
- Halloween 2016
- Halloween Hack 2015
- Socrative Zombie
- Voice Translation
- The Blue-Who Finder
- GPIO, Twitter
- Pi Chat Bot >
- PiGlow & Email
- Pibrella Alarm System
- SMS with Python >
- Pi-Hacks 3
- Minecraft
- Computing
- Contact Me
- Random Hacks