这部分是教学里面两个事例例程的整合
第一个是光线强度检测:
光线传感器数据读取,analogio模块,读取模拟信号并进行转换
关键代码
light = anlogio.Analogln(board.LIGHT)
print(light.value)
出来的结果是这样
第二个是温度检测
关键代码
thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE,10000,10000,25,3950)
while True:
temp_c = thermistor.temperature
出来的结果是这样
同时显示了摄氏度和华氏度的读数
把手摁在传感器上面温度会略微升高
眼睛是光线传感器,温度计是温度传感器,丝印标记很清楚
这里有两个官方的示例例程参考。
游乐场光线传感器 |Adafruit Circuit 游乐场快车 |Adafruit 学习系统
操场温度 |Adafruit Circuit 游乐场快车 |Adafruit 学习系统
先下载官方例程,将库拷贝到板子上
然后code部分整合到一起,可以正常运行了
# 在这里写上你的代码 :-)
import time
import board
import neopixel
import analogio
import simpleio
import adafruit_thermistor
# 自定义常量
BOARD_NEO_QTY = 10
BOARD_NEQ_BRI = 0.008
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
OFF = (0, 0, 0)
# 灯带初始化
pixels = neopixel.NeoPixel(board.NEOPIXEL, BOARD_NEO_QTY,brightness=BOARD_NEQ_BRI,auto_write=False)
pixels.fill((0, 0, 0))
pixels.show()
# 传感器
sensor_light = analogio.AnalogIn(board.LIGHT)
sensor_temp = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 25, 3950)
# 定义颜色序列
brightness_colors = [GREEN, YELLOW, RED]
temperature_colors = [BLUE, YELLOW, RED]
while True:
# 获取环境的亮度和温度
peak_light = simpleio.map_range(sensor_light.value, 2000, 62000, 0, 5)
peak_temp = simpleio.map_range(sensor_temp.temperature, 33, 33.5, 0, 5)
print("light.value:", sensor_light.value)
print("--peak_light ", int(peak_light))
print("temp.value:", sensor_temp.temperature)
print("**peak_temp ", int(peak_temp))
# 右侧灯珠表示 亮度
for i in range(0, 5, 1):
if i <= peak_light:
pixels[i] = GREEN
if peak_light > 2 and i >= 2:
pixels[i] = YELLOW
if peak_light > 3 and i >= 3:
pixels[i] = RED
else:
pixels[i] = OFF
# 左侧灯珠表示 温度
for j in range(9, 4, -1):
if j >= 9 - peak_temp:
pixels[j] = BLUE
if peak_temp > 2 and j <= 7:
pixels[j] = YELLOW
if peak_temp > 3 and j <= 6:
pixels[j] = RED
else:
pixels[j] = OFF
pixels.show()
time.sleep(0.1)
蓝色的灯表示温度正常