Use mode() to configure a pin

Note Using the Pin() constructor to change the mode() of a pin will automatically update the corresponding AT command value to match and vice-versa. For example, setting pin D11 to disabled sets the P1 AT command to 0.

Pin.DISABLED

If you are not using a pin, configure it as Pin.DISABLED.

Pin.IN

Pin acts as an input that you can read with the value() method, which returns 1 for high and 0 for low. See the pull() method for configuring an internal pull up/down resistor on input pins.

>>> button = Pin.board.D0
>>> button.mode(Pin.IN)
>>> button.pull(Pin.PULL_UP)
>>> # or: button = Pin('D0', mode=Pin.IN, pull=Pin.PULL_UP)
>>> button
Pin(Pin.board.D0, mode=Pin.IN, pull=Pin.PULL_UP)
>>> Pin.board.D0
Pin(Pin.board.D0, mode=Pin.IN, pull=Pin.PULL_UP)
>>> button.value()
1
>>> # hold button and then read value again
>>> button.value()
0

Pin.OUT

Pin acts as an output that you can set by passing a parameter to the value() method. Any value that evaluates to True sets the pin high (Vcc) and all other values set it low (ground). Pin objects also support the on() and off() methods as shortcuts for value(1) and value(0) respectively, and toggle() to toggle the current value. For example, you can override the association indicator normally configured for D5 and control it manually:

>>> d5 = Pin.board.D5
>>> d5.mode(Pin.OUT)
>>> # turn LED off
>>> d5.value(0)
>>> # turn LED on
>>> d5.value(1)
>>> # turn LED off
>>> d5.off()
>>> # turn LED on
>>> d5.on()
>>> # flash the LED at 2Hz (on .25 seconds, off .25 seconds)
>>> import time
>>> while True:
...     d5.toggle()
...     time.sleep(.25)
...
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyboardInterrupt:
>>>

Note Using the on() and off() names in your code could be confusing when using outputs wired as "active low."

Pin.ALT

Selects an alternate function for the pin. Use the af_list() method on a Pin object for a list of alternate functions available on a pin. You can select a pin's default alternate function by calling mode(Pin.AF), but you need to use the Pin() constructor to select a specific alternate function if a pin supports more than one. Use the af() method to see what a Pin's current alternate function is. Note that af() returns an integer that you should compare to the Pin.AFx_XXX constants in your code, and not reference directly as they may change between firmware releases.

>>> Pin.board.D5.af_list()
[Pin.AF5_ASSOC_IND]
>>> d5 = Pin('D5', mode=Pin.ALT, alt=Pin.AF5_ASSOC_IND)
>>> "is assoc" if d5.af() == Pin.AF5_ASSOC_IND else "not assoc"
'is assoc'
>>> d5.mode(Pin.IN)
>>> "is assoc" if d5.af() == Pin.AF5_ASSOC_IND else "not assoc"
'not assoc'

Pin.ANALOG

Use the machine.ADC() class instead of configuring a pin mode as Pin.ANALOG. A Pin object in use by the ADC() class reports its mode as Pin.ANALOG.

>>> import machine
>>> a1 = machine.ADC('D1')
>>> # read analog input as value from 0-4095
>>> a1.read()
4095
>>> Pin.board.D1
Pin(Pin.board.D1, mode=Pin.ANALOG)

Pin.OPEN_DRAIN and Pin.ALT_OPEN_DRAIN

These modes from other MicroPython platforms are not supported on XBee products.