Embedded 101: Pico GPIO Read/Write – LED, Keys & Debouncing Deep Dive
This experiment implements key-controlled LED toggle on Fengya No.1 board with software debounce, and notes MicroPython Pin only supports positional arguments.
【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.
Fengya No. 1 Board - Universal Compatible Expansion Board.Pin class and the time class, set the GPIO 22 as the key input pin in the initialization configuration, set the GPIO 25 as the LED output pin, and enable the pull-down resistor.time. sleep_ms (50)) to perform key debouncing.LED is off, switch to light, end point output "The LED is ON"LED is on, switch to off, end point output "The LED is OFF"elegance-devkit v1\Demo\01 GPIO_ReadWrite:# Python env : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time : 2024/6/12 下午1:34
# @Author : 李清水
# @File : main.py
# @Description : GPIO读写实验,完成驱动板载LED灯和按键读取的任务
# ======================================== 导入相关模块 ========================================
# 硬件相关的模块
from machine import Pin
# 时间相关的模块
import time
# ======================================== 全局变量 ============================================
# 初始化LED的状态为关闭(False)
led_state = False
# ======================================== 功能函数 ============================================
# ======================================== 自定义类 ============================================
# ======================================== 初始化配置 ==========================================
# 上电延时3s
time.sleep(3)
# 打印调试消息
print("FreakStudio: Using GPIO to control LED and read KEY")
# 需要注意的是 MicroPython 中Pin类的构造函数不支持关键字参数,仅支持位置参数
# 因此下列定义KEY,将会产生一个报错
# TypeError: function missing 1 required positional arguments
# key = Pin(id = 2, mode = Pin.IN, pull = Pin.PULL_DOWN)
# 设置GPIO 22 为按键输入引脚
KEY = Pin(22, Pin.IN)
# 设置GPIO 25为LED输出引脚,下拉电阻使能
LED = Pin(25, Pin.OUT, Pin.PULL_DOWN)
# ======================================== 主程序 ============================================
# 循环检测按键是否按下
while True:
# 检测到KEY引脚按下
if KEY.value() == 0:
# 按键消抖:延时50ms跳过抖动的区域
time.sleep_ms(50)
# 确保按键仍然处于按下状态
if KEY.value() == 0:
# 切换LED的状态
led_state = not led_state
# 根据状态变量设置LED亮或灭
if led_state:
LED.on()
print("The LED is ON")
else:
LED.off()
print("The LED is OFF")
# 等待按键松开(避免连续检测到按键按下)
while KEY.value() == 0:
passMicroPython does not support keyword arguments for the constructor of the Pin class when passing parameters, and only supports positional arguments. Therefore, the following definition of KEY will trigger an error: TypeError: function missing 1 required positional arguments:KEY = Pin(id = 2, mode = Pin.IN, pull = Pin.PULL_DOWN)led_state to True, the LED lights up and the terminal outputs the following:led_state to False, the LED turns off and the terminal outputs the following: