Using the Pyboard's Digital Pins with 'pyb.Pin'

Contents

Introduction

The STM32F405RGT6 microcontroller on the Pyboard V1.1 board has six I/O ports labelled Ports A, B, C, D, E and H. Port H is only a 2-bit port while the other five ports are 16-bit wide. With the limitations of the 64-pin package there are 51 of these port bits exposed as useable external pins.

In order to keep the Pyboard to a manageable size the designers chose to provide 34 digital pins on the board. However not all of these pins are readily available for I/O:

  • Anytime use: X1..X12, X19..X22, Y1..Y12
  • Only if really needed: X17, X18
  • Avoid: P2, P3, P4, P5

Pin X17 is pulled to GND via a 4.7kΩ resistor if the onboard user switch USR is pressed. Use pyb.Switch class to manage this switch.

Pin X18 has serious current limitations. It shouldn't be used to drive a load such as an LED.

Pins P2, P3, P4 and P5 are connected to the onboard 4 x LEDs. Don't use these pins for general I/O. Use the pyb.LED class to control these onboard LEDs.

Here is a link to the Pyboard V1.1 pinout. We print these pinouts in colour, laminate them and keep them on the workbench for easy reference.

The 'pyb.Pin' Class

The GPIO pins on the Pyboard are mostly controlled by the pyb.Pin class which is a class specifically provided for the Pyboard. This article will cover the most used options but for a complete discussion the official MicroPython documentation should be read.

An alternative is the machine.Pin class. This class is generic and supports most microcontrollers with a MicroPython port. For further details see this article, Using the Pyboard's Digital Pins with 'machine.Pin'.

Creating a Pin object

A pin object is created and configured for the type of use required:


Syntax:
<pin_variable> = pyb.Pin(pin_name,
                         mode,
                         pull=Pin.PULL_NONE
                         *, value=None)

Example
my_pin = pyb.Pin('X1', pyb.Pin.IN)

This example defines pin X1 for digital input.
          
Pin name

There are several different ways to name a pin including using the Pyboard pin name and the microcontroller chip pin name. However by far the easiest way is to use the Pyboard pin name as a string e.g. 'X1', 'Y1'.

mode

The mode   options are:

  • pyb.Pin.IN : Configure the pin for input.
  • pyb.Pin.OUT_PP : configure the pin for output, with push-pull control[1].
  • pyb.Pin.OUT_OD : configure the pin for output, with open-drain control[2].
  • pyb.Pin.ANALOG : configure the pin for analog. The pin must be capable of analog operation (see pinout). This is discussed in Using the Pyboard's Analog Pins for ADC and Using the Pyboard's Analog Pins for DAC.
pull

The pull options are:

  • pyb.Pin.PULL_NONE : no pull-up or pull-down resistors.
  • pyb.Pin.PULL_UP : enable the pull-up resistor[3].
  • pyb.Pin.PULL_DOWN : enable the pull-down resistor[3].
value

The value option if not None will set the pin output value (0 or 1) before the pin is enabled.


Examples:
from pyb import Pin

# Pin Y11 configured for output
# with push-pull control.
# Initial value of High i.e. 1
pin1 = Pin('Y11', Pin.OUT_PP, value=1)

# Pin Y1 configured for input with
# the pull-up resistor enabled.
pin2 = Pin('Y1', Pin.IN, pull=Pin.PULL_UP)

# Pin X1 configured for analog.
pin3 = Pin('X1', Pin.ANALOG)

# Get the pin configurations
print(pin1)
print(pin2)
print(pin3)

Output:
Pin(Pin.cpu.B0, mode=Pin.OUT)
Pin(Pin.cpu.C6, mode=Pin.IN, pull=Pin.PULL_UP)
Pin(Pin.cpu.A0, mode=Pin.ANALOG)
          

Reading & Writing a Pin Value

Digital pins can have one of two logic values: 1 (High) or 0 (Low). Reading or writing a value to a digital pin is done with the Pin.value() method.


Syntax:
Pin.value([value])

Example:
from pyb import Pin

# Set pin X1 to logic 1 (High)
pin1 = pyb.Pin('X1', pyb.Pin.OUT_PP)
pin1.value(1)

# Read logic level of pin Y1
pin2 = Pin('Y1', Pin.IN)
print(pin2.value())
          

If no argument is passed to the method then a 0 or 1 depending on the logic level of the pin is returned.

If an argument value is given then that is used to set the logic level of the pin. It must be 0 or 1.

Notes:



MicroPython Example

This example hooks up an SW-520D switch to the Pyboard. The SW-520D is a tilt switch with two rolling balls that make contact with two pins when the switch is in the vertical position. When tilted the balls break their contact with the pins.

How the two-ball SW-520D switch detects tilt
Fig 1 - Operation of the SW-520D tilt switch

In this example we will be using the popular SW-520D module which simplifies connection to the Pyboard's digital input pin. The module's DO pin is High (1) when in a tilted position and Low (0) when not tilted i.e. vertically placed.

Popular SW-520D module
Fig 2 : SW-520D tilt switch module

The Pyboard will detect whether the module is tilted from the vertical or not. If it isn't tilted then a digital pin configured for output will power a yellow LED. If it is tilted then another digital pin will power a blue LED. The LEDs are current limited with 300Ω resistors.

The code demonstrates configuration of digital pins on the Pyboard (1 x input and 2 x output) and how to set logic levels on the output pins and read the logic level of the input pin.


# Reads the status of the SW-520D tilt switch.

# If the switch is tilted:
#          (1) The module's DO pin = 1
#          (2) The blue LED is lit.

# If the switch isn't tilted:
#          (1) The module's DO pin = 0
#          (2) The yellow LED is lit.

from pyb import Pin

# Define the pins
tilt_pin = Pin('X12', Pin.IN)
led_blue = Pin('Y12', Pin.OUT_PP)
led_yellow = Pin('Y11', Pin.OUT_PP)

# Endless loop
while True:
    # Read tilt switch.
    # A value of 1 means switch is tilted.
    tilted = (tilt_pin.value() == 1)
    if tilted: # Turn on blue LED
        led_blue.value(1)
        led_yellow.value(0)
    else: # Not tilted, turn on yellow LED
        led_yellow.value(1)
        led_blue.value(0)
    pyb.delay(150) # reduce switch 'bounce'            
          
The SW-520D switch isn't tilted so the yellow LED is lit
Fig 3 : SW-520D switch not tilted thus yellow LED is lit