A pin is the basic object to control I/O pins (also known as GPIO – general-purpose input/output). 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.
from machine import Pin # initialize `P9` in gpio mode and make it an output p_out = Pin('P9', mode=Pin.OUT) p_out.value(1) p_out.value(0) p_out.toggle() p_out(True) # make `P10` an input with the pull-up enabled p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP) p_in() # get value, 0 or 1
Create a new Pin object associated with the string id. If additional arguments are given, they are used to initialise the pin. See pin.init()
id
from machine import Pin p = Pin('P10', mode=Pin.OUT, pull=None, alt=-1)
Initialize the pin:
mode
Pin.IN
Pin.OUT
Pin.OPEN_DRAIN
pull
None
Pin.PULL_UP
Pin.PULL_DOWN
*
0
1
alt
Returns: None.
Get the pin id.
Get or set the digital logic level of the pin. This only works in Pin.OUT mode. Values can be:
True
False
Pin objects are callable. The call method provides a (fast) shortcut to set and get the value of the pin.
Example:
from machine import Pin pin = Pin('P12', mode=Pin.IN, pull=Pin.PULL_UP) pin() # fast method to get the value
See pin.value() for more details.
pin.value()
Toggle the value of the pin.
Get or set the pin mode. Modes can be:
Get or set the pin pull. Pull can be:
Get or set the pin hold. This functionality can be used to hold a pin’s state after deep sleep, machine.reset() or a watchdog timer reset. Passing Truewill hold the current value of the pin, False will release the hold state. When a pin is in hold state, its value cannot be changed by using Pin.value()or Pin.toggle(), until the hold is released. Only pins in the RTC power domain can retain their value through deep sleep or reset. These are: P2, P3, P4, P6, P8, P9, P10, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, P23
machine.reset()
Pin.value()
Pin.toggle()
P2, P3, P4, P6, P8, P9, P10, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, P23
You can use the following example:
from machine import Pin import machine p3 = Pin('P3', mode=Pin.OUT) p3.value(1) #can also be p3.value(0) p3.hold(True) #hold the pin high machine.reset() # instead, you can use: # machine.deepsleep(10000) # P3 will still be high here
A few things to keep in mind when using the pin hold functionality:
pin.hold()
Set a callback to be triggered when the input level at the pin changes.
trigger
Pin.IRQ_FALLING
Pin.IRQ_RISING
Pin.IRQ_LOW_LEVEL
Pin.IRQ_HIGH_LEVEL
The values can be OR-ed together, for instance trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING
trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING
handler
arg
from machine import Pin def pin_handler(arg): print("got an interrupt in pin %s" % (arg.id())) p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP) p_in.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING, pin_handler)
For more information on how SG’s products handle interrupts, see here.
Contains all Pin objects supported by the expansion board. Examples:
Pin.exp_board.G16 led = Pin(Pin.exp_board.G16, mode=Pin.OUT) Pin.exp_board.G16.id()
Contains all Pin objects supported by the module. Examples:
Pin
Pin.module.P9 led = Pin(Pin.module.P9, mode=Pin.OUT) Pin.module.P9.id()
The following constants are used to configure the pin objects. Note that not all constants are available on all ports.