class Pin – control I/O pins¶
A pin is the basic object to control I/O pins. It has methods to set the mode of the pin (input, output, etc) and methods to get and set the digital logic level. For analog control of a pin, see the ADC class.
Usage Model:
Board pins are identified by their string name:
g = pyb.Pin('GP9', af=0, mode=pyb.Pin.IN, type=pyb.Pin.STD, strength=pyb.Pin.S2MA)
You can also configure the Pin to generate interrupts. For instance:
def pincb(pin):
print(pin.name())
pin_int = pyb.Pin('GP10', af=0, mode=Pin.IN, type=pyb.Pin.STD_PD, strength=pyb.Pin.S2MA)
pin_int.callback (mode=pyb.Pin.INT_RISING, handler=pincb)
# the callback can be triggered manually
pin_int.callback()()
# to disable the callback
pin_int.callback().disable()
Now every time a falling edge is seen on the gpio pin, the callback will be executed. Caution: mechanical push buttons have “bounce” and pushing or releasing a switch will often generate multiple edges. See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed explanation, along with various techniques for debouncing.
All pin objects go through the pin mapper to come up with one of the gpio pins.
Constructors¶
-
class
pyb.
Pin
(name, ...) Create a new Pin object associated with the name. If additional arguments are given, they are used to initialise the pin. See
pin.init()
.
Methods¶
-
pin.
init
(af, mode, type, strength) Initialise the pin:
af
is the number of the alternate function. Please refer to the pinout and alternate functions table. for the specific alternate functions that each pin supports.mode
can be one of:Pin.OUT
- no pull up or down resistors.Pin.IN
- enable the pull-up resistor.
type
can be one of:Pin.STD
- push-pull pin.Pin.STD_PU
- push-pull pin with pull-up resistor.Pin.STD_PD
- push-pull pin with pull-down resistor.Pin.OD
- open drain pin.Pin.OD_PU
- open drain pin with pull-up resistor.Pin.OD_PD
- open drain pin with pull-down resistor.
strength
can be one of:Pin.S2MA
- 2mA drive capability.Pin.S4MA
- 4mA drive capability.Pin.S6MA
- 6mA drive capability.
Returns:
None
.
-
pin.
high
()¶ Set the pin to a high logic level.
-
pin.
low
()¶ Set the pin to a low logic level.
-
pin.
value
([value])¶ Get or set the digital logic level of the pin:
- With no argument, return 0 or 1 depending on the logic level of the pin.
- With
value
given, set the logic level of the pin.value
can be anything that converts to a boolean. If it converts toTrue
, the pin is set high, otherwise it is set low.
-
pin.
name
()¶ Get the pin name.
-
pin.
toggle
()¶ Toggle the value of the pin.
-
pin.
info
()¶ Return a 5-tuple with the configuration of the pin:
(name, alternate-function, mode, type, strength)
Warning
This method cannot be called within a callback (interrupt-context) because it needs to allocate memory to return the tuple and memory allocations are disabled while interrupts are being serviced.
-
pin.
callback
(*, mode, priority=1, handler=None, wakes=pyb.Sleep.ACTIVE)¶ Create a callback to be triggered when the input level at the pin changes.
mode
configures the pin level which can generate an interrupt. Possible values are:Pin.INT_FALLING
interrupt on falling edge.Pin.INT_RISING
interrupt on rising edge.Pin.INT_RISING_FALLING
interrupt on rising and falling edge.Pin.INT_LOW_LEVEL
interrupt on low level.Pin.INT_HIGH_LEVEL
interrupt on high level.
priority
level of the interrupt. Can take values in the range 1-7. Higher values represent higher priorities.handler
is an optional function to be called when new characters arrive.wakes
selects the power mode in which this interrupt can wake up the board. Please note:- If
wakes=pyb.Sleep.ACTIVE
any pin can wake the board. - If
wakes=pyb.Sleep.SUSPENDED
pinsGP2
,GP4
,GP10
,GP11
, GP17`` orGP24
can wake the board. Note that only 1 of this pins can be enabled as a wake source at the same time, so, only the last enabled pin as apyb.Sleep.SUSPENDED
wake source will have effect. - If
wakes=pyb.Sleep.SUSPENDED
pinsGP2
,GP4
,GP10
,GP11
,GP17
andGP24
can wake the board. In this case all of the 6 pins can be enabled as apyb.Sleep.HIBERNATE
wake source at the same time. - Values can be ORed to make a pin generate interrupts in more than one power mode.
- If
Returns a callback object.
Constants¶
-
Pin.
IN
input pin mode
-
Pin.
OUT
¶ output pin mode
-
Pin.
STD
¶ push-pull pin type
-
Pin.
STD_PU
¶ push-pull pin with internall pull-up resistor
-
Pin.
STD_PD
¶ push-pull pin with internall pull-down resistor
-
Pin.
OD
¶ open-drain pin
-
Pin.
OD_PU
¶ open-drain pin with pull-up resistor
-
Pin.
OD_PD
¶ open-drain pin with pull-down resistor
-
Pin.
INT_FALLING
¶ interrupt on falling edge
-
Pin.
INT_RISING
¶ interrupt on rising edge
-
Pin.
INT_RISING_FALLING
¶ interrupt on rising and falling edge
-
Pin.
INT_LOW_LEVEL
¶ interrupt on low level
-
Pin.
INT_HIGH_LEVEL
¶ interrupt on high level
-
Pin.
S2MA
¶ 2mA drive strength
-
Pin.
S4MA
¶ 4mA drive strength
-
Pin.
S6MA
¶ 6mA drive strength