• 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: Raspberry Pi Autofocus Camera SpotPear Raspberry Pi 4 Camera Install Espressif IDF Plugin Tutorial User Guide ESP32-C3FN4 Milk-V Duo S User Guide Total Raspberry-Pi-1.54inch-LCD-Display-Touchscreen-configuration-Bookworm-Lite For Pi5 Raspberry Pi WatchDog Raspberry Pi 5 PCIe to USB3.2 Gen1 Hub 5Gbps For Raspberry Pi OS Drive free Retail electronic Labe ESP32 S3 Development Board 2.41 inch AMOLED Display 600x450 TouchScreen For Arduino LVGL With SD-Port/ QMI8658C 6-Axis Sensor Raspberry Pi AI Camera Official Original 12MP IMX500 Intelligent visual Sensor On-Board RP2040 7.5 inch Passive NFC e-Paper BW Display 7.5inch E-ink Screen No Need Battery Wireless Powered Data Transfer ESP32 C3 Jetson Orin Nano UGV Beast PT ROS2 AI OpenCV Robot Car MediaPipe Raspberry Pi 3D Display FLASH File System Operation UGV Rover ROS2 PT AI OpenCV Robot Car MediaPipe For Jetson Orin Nano RS232/485/422 To CAN Raspberry Pi Pico 1024×600