【MicroPython】 Machine.Pin Functions

From Waveshare Wiki
Jump to: navigation, search
  • machine.Pin(id, mode=None, pull=None, value)
    • Pin object constructor
    • id: GPIO number, the value is 0-29, if GPIO13 is used, fill in 13, here. ;
  • mode: mode, optional None, Pin.IN(0), Pin.OUT(1), Pin.OPEN_DRAIN(2);
  • pull: use internal pull-up and pull-down resistors, only valid in input mode, optional None, Pin.PULL_UP(1), Pin.PULL_DOWN(2);
  • value: port value in output or open-drain mode, 0 is low (off), 1 is high (on);

The first parameter ID represents the GPIO number, and the value should be 0-29. If GPIO13 is used, fill in 13 here.

The second parameter mode represents the GPIO mode and can be set to no initialization, input mode, output mode and open-drain mode

The third parameter pull is to use the internal pull-up and pull-down resistor, which can be set to pull-up, pull-down and floating. Note that this parameter is only valid in input mode.

The fourth parameter is the output value, and the port value is valid in output or open-drain mode.

  • Pin.init(mode=None, pull=None)
    • Reinitialize the GPIO port;
    • mode: mode, optional None, Pin.IN(0), Pin.OUT(1), Pin.OPEN_DRAIN(2);
    • pull: use internal pull-up and pull-down resistors, only valid in input mode, optional None, Pin.PULL_UP(1), Pin.PULL_DOWN(2);

Its function is to re-initialize the GPIO, and the parameters are the same as the PIN constructor, so I won't go into details here.

  • Pin.value([x])
    • Returns the value of the GPIO port without filling in the parameter, and writes the parameter into the GPIO port when filling in the parameter, the parameter can be 0 or 1;

The value function in the PIN class is used to return the value of the GPIO port without filling in the parameter, and write the parameter into the GPIO port when the parameter is filled in, and the parameter can be 0 or 1;

  • Pin.toggle()
    • Flip port settings in output or open-drain mode
    • The toggle function in the PIN class is a flip of the port value in output or open-drain mode
  • Pin.low()
    • Set the port low in output or open-drain mode;
  • Pin.off()
    • Set the port low in output or open-drain mode;
  • Pin.high()
    • Set the port high in output or open-drain mode;
  • Pin.on()
    • Set the port high in output or open-drain mode;
    • The above four functions are used in output or open-drain mode, Low OFF is set to low, high and on are set to high.
  • Pin.irq(handler=None,trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))
    • Used to set external interrupt
  • handler: interrupt trigger callback function;
  • trigger: interrupt trigger condition, set to:
  • Pin.IRQ_FALLING Falling edge interrupt.
  • Pin.IRQ_RISING interrupt on rising edge

The PIN class irq function is an external interrupt function, the first parameter is the interrupt trigger callback function; the second parameter trigger is the interrupt trigger condition, which is set to edge trigger or level trigger.

This article is only for RP2040 MicroPython firmware, and the source code shall prevail. This article is written according to the official source code at the time of writing, which is used to provide convenience for beginners and is for reference only. Those who are capable are recommended to refer to MicroPython.