• 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: Industrial USB TO 8CH TTL Converter USB to UART Multi Protection Circuits Multi Systems X1301 Raspberry Pi 5 HDMI to CSI-2 Shield 1080P@60fps & Audio & Video Also For 4B/3B FT4232HL USB TTL RS232 Industrial UART 4-Ch UART Laser 450nm 5W high precision module Adjustable focal length For Laser Carving / Laser 3D Printing ESP32 ST7789 configuration Raspberry Pi 5 Argon-NEO-M.2-NVME-PCIE-Expansion-Board Only For Argon NEO 5 Case (Not include) ESP32-S3 1.14inch LCD Bus servo driver board Industrial Isolated USB TO 4CH RS485 (B) Converter UART CH344L For Wall/Rail-Mount Core3566002032 Jetson Orin Nano UGV Beast PT ROS2 AI OpenCV Robot Car MediaPipe Raspberry Pi 5 Pi5 Milk-V RJ45 Raspberry Pi Mini Jetson-nano-to-boot-the-system User Guide Gripper Serial Robotic Arm Gripper Mechanical claw Serial Bus Servo ST3215/CF35-12 4 inch e Paper HAT+ E Ink Spectra E6 Full-Color 600x400 Screen Display For Arduino /Raspberry Pi /Jetson Nano /STM32 Camera Font-Modulation-Tutorial User Guide