micro:bit V1能否实现两路独立LED?消除其他LED联动点亮现象
micro:bit V1实现两路独立LED的解决方案
可以实现两路独立工作的LED,但要注意micro:bit V1的板载LED矩阵是行共阳、列共阴的多路复用结构,直接操作引脚会因复用逻辑串扰导致其他LED误亮,需针对性处理:
方案1:使用板载LED矩阵中的两个独立LED
利用官方API控制特定像素点,避免直接操作引脚破坏矩阵扫描逻辑,示例代码(MicroPython):
from microbit import * import utime import _thread # 心跳灯:(0,0)位置LED,每秒闪烁一次 def heartbeat_loop(): while True: display.set_pixel(0, 0, 9) utime.sleep(0.5) display.set_pixel(0, 0, 0) utime.sleep(0.5) # 启动心跳灯线程 _thread.start_new_thread(heartbeat_loop, ()) # 状态灯:(4,4)位置LED,按A键切换运行/停止状态 state_running = True while True: if button_a.was_pressed(): state_running = not state_running display.set_pixel(4, 4, 9 if state_running else 0) utime.sleep(0.1)
官方display.set_pixel()会自动处理矩阵的多路扫描,不会出现串扰问题。
方案2:使用外部独立LED(无串扰风险)
选择micro:bit V1上未与LED矩阵复用的引脚(如P5、P6、P7、P9、P10),直接外接LED(需串联220Ω限流电阻),示例代码:
from microbit import * import utime import _thread # 初始化外部引脚 heartbeat_pin = pin5 status_pin = pin6 heartbeat_pin.write_digital(0) status_pin.write_digital(0) # 心跳灯线程 def heartbeat_loop(): while True: heartbeat_pin.write_digital(1) utime.sleep(0.5) heartbeat_pin.write_digital(0) utime.sleep(0.5) _thread.start_new_thread(heartbeat_loop, ()) # 状态灯控制 state_running = True while True: if button_a.was_pressed(): state_running = not state_running status_pin.write_digital(1 if state_running else 0) utime.sleep(0.1)
问题原因说明
你之前遇到的其他LED误亮,是因为直接操作了LED矩阵复用的引脚(如P0-P4列引脚、P13-P17行引脚),破坏了矩阵的多路扫描逻辑,导致未被正确控制的行/列引脚产生串扰,触发其他LED点亮。
内容的提问来源于stack exchange,提问作者Simon Wright
相关产品推荐
相关产品推荐

