Skip to content
Boris Lovosevic edited this page Jun 26, 2018 · 3 revisions

machine Module

Class Pin


This class includes support for using ESP32 gpios

The ESP32 chip features 40 physical GPIO pads.
Some GPIO pads cannot be used or do not have the corresponding pin on the chip package(refer to technical reference manual).
Each pad can be used as a general purpose I/O or can be connected to an internal peripheral signal.
Note that GPIO6-11 are usually used for SPI flash.
GPIO34-39 can only be set as input mode and do not have software pullup or pulldown functions.



Create the Pin instance object

pin = machine.Pin(gpio [,mode] [,pull] [,value=None] [,handler=None] [,trigger=machine.Pin.DISABLE] [,debounce=0] [,acttime=0])

Argument Description
gpio gpio number, if invalid gpio number is entered, an exception will be raised
mode optional; pin operating mode
use one of the constants: IN, OUT, OUT_OD, INOUT, INOUT_OD
default: IN
Modes with _OD suffix are configure as open drain outputs.
Warning: pins 34-39 are input-only pins and cannot be used as output.
pull optional; activate internal pull resistor if pin is configured as input
use one of the constants: PULL_UP, PULL_DOWN, PULL_UPDOWN, PULL_FLOAT
default: PULL_FLOAT
Warning: pins 34~39 do not have pull-up or pull-down circuitry
value optional; set the initial value for an output pin; default: do not set
handler optional; Pin irq callback function
If set, the pin interrupt will be enabled and the given function will be called on interrupt
default: no irq enabled
trigger optional; the pin interrupt mode
use on of the constants: IRQ_DISABLE, IRQ_RISING, IRQ_FALLING, IRQ_ANYEDGE, IRQ_LOLEVEL, IRQ_HILEVEL
default: IRQ_DISABLE
Warning: if the level interrupt is selected, the interrupt will be disabled before the interrupt function is executed and must be explicitly enabled in interrupt function if needed
debounce optional; debounce time on pin interrupt in micro seconds, 0 for debounce disable
valid range: 100 - 500000 µs
default: 0
acttime optional; the minimal time the pin must be in active state after interrupt for interrupt function to be executed, in micro seconds
valid range: 100 - 500000 µs, must be <= debounce
default: 0

Only gpio argument is required, all the other are optional and will be set to the default values if not given.
Arguments value, handler, trigger, debounce, acttime must be given as kw arguments (arg=value)


Methods


pin.init([,mode] [,pull] [,value=None] [,handler=None] [,trigger=machine.Pin.DISABLE] [,debounce=0] [,acttime=0])

Change the pin configuration and options after the pin instance object was created.

For arguments description see Create the Pin instance object


pin.value([val])

Get or set the pin value.
If no argument is given, returns the current pin value (level), 0 or 1
It the val argument is given, set the pin level. val can be 0, 1, False or True

Note: If the pin is set to output-only mode (OUT or OUT_OD), the returned value will always be 0
If you want to get the value of the output pin, it must be se to INOUT or INOUT_OD mode.


pin.irqvalue()

Returns the pin level at the time when the last interrupt occured.
Can be used inside the interrupt handling function.