【Follow me Season 2 Episode 1】Task 2: Monitor ambient temperature and light, and display comfort level through onboard LEDs
[Copy link]
Hello everyone, I am Zheng Gong, a little engineer who is lost in this world.
There were some things going on at home these two days, which delayed us for some time. We moved on to the next task, monitoring the temperature and illumination.
First, the temperature. Let's look at the schematic diagram.
It can be seen that the principle is relatively simple. It is a thermistor, a 10k resistor is pulled down for voltage division, and the voltage of this port can be read using the ADC interface.
However, after searching for information, I found that CircuitPython has a library called adafruit_thermistor that can directly adapt to this situation. The code is as follows:
import adafruit_thermistor
# 模拟输入的管脚
pin = board.TEMPERATURE
# 热敏电阻的常温阻值
resistor = 10000
# 普通电阻的阻值
resistance = 10000
# 热敏电阻的阻值温度
nominal_temp = 25
# 热敏电阻的B系数
b_coefficient = 3950
# 还有最后一个参数,参数名为high_side,表示热敏电阻是否连接在上端,默认为True,可以不填
thermistor = adafruit_thermistor.Thermistor(
pin, resistor, resistance, nominal_temp, b_coefficient)
You only need to read thermistor.temperature to get the Celsius temperature value.
Then to the illumination value, let's look at the schematic diagram first.
The principle is that a phototransistor is connected to a 10k resistor. When light shines on the phototransistor, photocurrent will be generated, the equivalent resistance will decrease, and the voltage will increase. The initialization code is as follows:
import analogio
# Adc的值为0-65535(16bit)
light = analogio.AnalogIn(board.LIGHT)
You only need to query light.value to get the value of adc, the range is 0-65535
The subsequent code displays the comfort level according to the obtained value, using neopixel lamp beads for display. The initialization code is as follows:
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.05, auto_write=False)
pixels.fill((0, 0, 0))
pixels.show()
The functional logic behind this is that high temperature is red, low temperature is blue, and the intermediate temperature switches between these two colors
As for illumination, the lower the illumination, the more lamp beads there are, which means it is more comfortable and more suitable for sleeping.
This is also a bit of a messy logic, I haven't studied it carefully. If you have any ideas, please tell me and I will modify it. The following is the specific code
index = 0
while True:
# light value remapped to pixel position
peak = simpleio.map_range(light.value, 500, 10000, 0, 10)
# def map_range(
# x: float,
# in_min: float,
# in_max: float,
# out_min: float,
# out_max: float) -> float:
light_num = 10 - peak
print('\n\n')
print(f'time: {index}')
index = index + 1
print(f'light val:{light.value}')
print(f'light num:{int(light_num)}')
color = simpleio.map_range(thermistor.temperature, 10, 35, 0, 255)
temp_color = (int(color) , 0, 255 - int(color))
print(f'temperature:{int(thermistor.temperature)}')
print(f'temp num:{color}')
for i in range(10):
if i <= light_num:
pixels[i] = temp_color
else:
pixels[i] = (0, 0, 0)
pixels.show()
time.sleep(1)
The map_range function of simpleio is used, which will make a level division based on the input data.
|