[Digi-Key Follow me Issue 3] Task 5: Using external sensors: temperature and humidity sensors
[Copy link]
This issue is about the expansion board of Seeed Studio XIAO ESP32C3, which has multiple external device interfaces:
There are two I2C interfaces, which can easily connect I2C devices.
This article uses the ATH20 temperature and humidity sensor based on the I2C interface:
1. Hardware Preparation
Seeed Studio XIAO ESP32C3 baseboard provides a standard interface, which can be directly connected using the connection cable that comes with Seeed Studio ATH20.
2. Driver Installation
To use the ath20 module in micropython, you need to install a corresponding driver library.
Driver address:
Download ahtx0.py and upload it to the lib/directory using the development tools, as follows:
3. Read temperature and humidity data
To read data from ath20, using the ahtx0 library above is very convenient:
from machine import Pin, I2C
import ahtx0
i2c = I2C(0, scl=Pin(7), sda=Pin(6), freq=400000)
sensor = ahtx0.AHT10(i2c)
while True:
print("\nTemperature: %0.2f C" % sensor.temperature)
print("Humidity: %0.2f %%" % sensor.relative_humidity)
utime.sleep(1)
In the above code, the sensor is instantiated through sensor = ahtx0.AHT10(i2c), and then the temperature value and relative humidity information can be obtained through sensor.temperature and sensor.relative_humidity.
Running the above code will read and display:
4. Display temperature and humidity information on the screen
Combined with the previous [Digi-Key Electronics Follow me Issue 3] Task 2: Driving the OLED screen on the expansion board (characters, Chinese, instruments, poems) , it is very convenient to display temperature and humidity information on the screen.
The specific code is as follows:
import utime
from machine import Pin, SoftI2C
import ahtx0
from color_setup import ssd
from color_setup import i2c
from gui.core.nanogui import refresh
from gui.core.writer import CWriter
from gui.widgets.textbox import Textbox
from gui.widgets.label import Label
from gui.core.colors import *
# 字体调用
import gui.fonts.arial10 as arial10
# 清屏
refresh(ssd, True)
# 显示输出
CWriter.set_textpos(ssd, 0, 0)
wri = CWriter(ssd, arial10, verbose=False)
wri.set_clip(True, True, False)
# 显示日期
txt = Label(wri, 0, 0, "Environmental information")
pargs = (13, 2, 124, 4) # Row, Col, Width, nlines
tb = Textbox(wri, *pargs, clip=False)
#tb.append(content, ntrim = 100)
#i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
# Create the sensor object using I2C
sensor = ahtx0.AHT10(i2c)
while True:
print("\nTemperature: %0.2f C" % sensor.temperature)
print("Humidity: %0.2f %%" % sensor.relative_humidity)
tb.clear()
tb.append("\n", ntrim = 100)
tb.append(" Temperature: %0.2f C" % sensor.temperature, ntrim = 100)
tb.append(" Humidity: %0.2f %%" % sensor.relative_humidity, ntrim = 100)
refresh(ssd)
utime.sleep(5)
The above code displays the temperature information based on the original OLED display and updates it every 5 seconds.
V. Conclusion
The good design of Seeed Studio makes it very convenient to connect Seeed Studio XIAO ESP32C3, baseboard and sensors.
The powerful support of MicroPython makes the operation of specific sensors and displays extremely simple.
This development board is very good!!!
|