TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • 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 >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

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 PiFace


1. 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 
Copyright 2020 TeCoEd @dan_aldred