P1输入口常为“1”无法改变
-
为什么我定义的P1输入口,它的value一直是“1”,不管是否用P1接触VIN:
sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(20) p1_in = Pin('P1', Pin.IN, Pin.PULL_NONE) value1 = p1_in.value() while(True): img = sensor.snapshot() if value1 == 1: led = pyb.LED(2) led.off() led = pyb.LED(1) led.on() time.sleep(1) led.off() print(value1) else: led = pyb.LED(2) led.on() print(value1)
-
有2个问题都要解决
1,要么硬件上加下拉电阻;要么把Pin.PULL_NONE改为Pin.PULL_DOWN,开启芯片内部下拉电阻。
2,p1_in.value()要在死循环里面调用。你的代码只在最开始调用一次,value1永远都不会更新。import sensor from pyb import Pin import pyb import time sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) p1_in = Pin('P1', Pin.IN, Pin.PULL_DOWN) while(True): value1 = p1_in.value() img = sensor.snapshot() if value1 == 1: led = pyb.LED(2) led.off() led = pyb.LED(1) led.on() time.sleep(1) led.off() print(value1) else: led = pyb.LED(2) led.on() print(value1)
-
另外,为了安全,应该用3.3V引脚连接P1,而不是VIN引脚连接P1。
-
@kidswong999 谢谢大咖