Pin - Control I/O Pins

This class provides methods of reading and writing GPIO. A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output). The pin class has methods to set the mode of the pin (IN, OUT, etc) and methods to get and set the digital logic level.

Example:

from machine import Pin

# Creates a GPIO object
gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 1)

# Gets the pin level
gpio1.read()

# Sets the pin level
gpio1.write(0)
gpio1.write(1)

# Sets input and output modes
gpio1.set_dir(Pin.IN)
gpio1.set_dir(Pin.OUT)

# Gets input and output modes
gpio1.get_dir()

Constructor #

machine.Pin #

class machine.Pin(GPIOn, direction, pullMode, level)

Parameter:

  • GPIOn - Integer type. GPIO number. Click here to view the mapping relationship between GPIO pin numbers and physical pins.
  • direction - Integer type. I/O mode. IN - Input mode. OUT - Output mode.
  • pullMode - Integer type. Pull selection mode. Descriptions are as follows:
    PULL_DISABLE - Floating mode
    PULL_PU - Pull-up mode
    PULL_PD - Pull-down mode
  • level - Integer type. Pin level. 0 - Set pin to low level. 1 - Set pin to high level.

Example:

>>> # Creates a GPIO object
>>> from machine import Pin
>>> gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0)

Mapping Relationship Between GPIO Pin Numbers and Physical Pins:

Description of GPIO corresponding pin numbers: GPIO pin numbers provided in the document correspond to external pin numbers of the module. For example, for EC600M-CN module, GPIO1 corresponds to pin10, which is an external pin number of the module. See the provided hardware documents for external pin numbers of the module.

适用平台GPIO numberPin number
BC25PAGPIO1Pin3
BC25PAGPIO2Pin4
BC25PAGPIO3Pin5
BC25PAGPIO4Pin6
BC25PAGPIO5Pin16
BC25PAGPIO6Pin20
BC25PAGPIO7Pin21
BC25PAGPIO8Pin22
BC25PAGPIO9Pin23
BC25PAGPIO10Pin25
BC25PAGPIO11Pin28
BC25PAGPIO12Pin29
BC25PAGPIO13Pin30
BC25PAGPIO14Pin31
BC25PAGPIO15Pin32
BC25PAGPIO16Pin33
BC25PAGPIO17Pin2
BC25PAGPIO18Pin8

Methods #

Pin.read #

Pin.read()

This method reads the pin level.

Return Value:

Pin level. 0 - low level. 1 - high level.

Pin.write #

Pin.write(value)

This method sets the pin level.

Note: You need to ensure that the pin is in the output mode before you set the pin level.

Parameter:

  • value - Integer type. Pin level. 0 - low level. 1 - high level.

Return Value:

0 - Successful execution

-1 - Failed execution

Example:

>>> from machine import Pin
>>> gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0)
>>> gpio1.write(1)
0
>>> gpio1.read()
1

Pin.set_dir #

Pin.set_dir(value)

This method sets I/O mode of the pin.

Parameter:

  • value - Integer type. I/O mode of the pin. 0 - Input mode. 1 - Output mode.

Return Value:

0 - Successful execution

-1 - Failed execution

Pin.get_dir #

Pin.get_dir()

This method gets I/O mode of the pin.

Return Value:

I/O mode of pins.

0 - Input mode.

1 - Output mode.

Example:

>>> from machine import Pin
>>> gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0)
>>> gpio1.get_dir()
1
>>> gpio1.set_dir(Pin.IN)
0
>>> gpio1.get_dir()
0

Constants #

适用平台ConstantDescription
BC25PAPin.GPIO1GPIO1
BC25PAPin.GPIO2GPIO2
BC25PAPin.GPIO3GPIO3
BC25PAPin.GPIO4GPIO4
BC25PAPin.GPIO5GPIO5
BC25PAPin.GPIO6GPIO6
BC25PAPin.GPIO7GPIO7
BC25PAPin.GPIO8GPIO8
BC25PAPin.GPIO9GPIO9
BC25PAPin.GPIO10GPIO10
BC25PAPin.GPIO11GPIO11
BC25PAPin.GPIO12GPIO12
BC25PAPin.GPIO13GPIO13
BC25PAPin.GPIO14GPIO14
BC25PAPin.GPIO15GPIO15
BC25PAPin.GPIO16GPIO16
BC25PAPin.GPIO17GPIO17
BC25PAPin.GPIO18GPIO18
BC25PAPin.INInput mode
BC25PAPin.OUTOutput mode
BC25PAPin.PULL_DISABLEFloating mode
BC25PAPin.PULL_PUPull-up mode
BC25PAPin.PULL_PDPull-down mode
型号选择: