• sales

    +86-0755-88291180

【MicroPython】 Machine.Pin Functions

  • 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.

TAG: TTL To CAN Sipeed MaixCAM SG2002 RISC-V AI Camera Kit with Screen Audio WIFI6 Linux Board Raspberry Pi 5 PCIe to USB3.2 Gen1 Hub 5Gbps For Raspberry Pi OS Drive free Raspberry Pi Monitor Official Original DisplayScreen 15.6 inch LCD Full HD 1920x1080 15.6inch Raspberry Pi Pico 2 P2350-Plus Smart Car Kit PicoGo2 Autonomous Driving Learning Car Sensor STM32 1.54inch OLED ESP32-S3-Zero Electronic EYE 0.71 inch Round Double LCD Display Dual Screen For Arduino Raspberry Pi ESP32 Pico ST USB to SPI Raspberry Pi 5 POE PCIe To M.2 E KEY NGFF WIFI7 WIFI6 For BE200/AX210/AX200/RTL8822CE BPI-M4 Berry ESP32 S3 Development Board 1.46 inch Round LCD 1.46inch Display 412x412 QMI8658 Sensor / MIC / SD /MP3 Audio /Battery Port LVGL/HMI For Arduino Industrial RJ45 Ethernet To 16-ch Relay Module Modbus RTU TCP With Protection and guide rails Pi5 Raspberry Pi 64MP Camera Arduino Thermal imaging Spotpear Programmable Keyboard Raspberry Pi 5 PCIe to M.2 NVMe SSD Adapter Board C HAT Pi5-PCIe-2280-2242-2230