本站改版新增arduino频道
通过光敏传感器对外界环境光照强度测量并显示
#导入相关模块
from machine import Pin,SoftI2C,ADC,Timer
#初始化ADC,Pin=4,11DB衰减,测量电压约为0-3.3V
Light = ADC(Pin(4))
Light.atten(ADC.ATTN_11DB)
#中断回调函数
def fun(tim):
print('Light test:') # 次行显示实验名称
value=Light.read() #获取ADC数值
#显示数值
print(str(value)+' (4095)')
#计算电压值,获得的数据0-4095相当于0-3.3V,('%.2f'%)表示保留2位小数
print(str('%.2f'%(value/4095*3.3))+' V')
#判断光照强度,分3档显示。
if 0 < value <=1365:
print('Bright')
if 1365 < value <= 2730:
print('Normal')
if 2730 < value <= 4095:
print('Weak ')
#开启定时器0,周期1s
tim = Timer(0)
tim.init(period=1000, mode=Timer.PERIODIC, callback=fun)
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.010558128356934 seconds