Welcome to World of Module Sensor Kit repository
6.6 Read temperature humidity
1. Learning target
In this course, we will earn how to use Micro:bit and Temperature humidity module to read the current ambient temperature and humidity.
2. Preparation
Connect the module to Micro:bit board by expansion board, as shown below.
3. About code
# -*- coding: utf-8-*-# Encoding cookie added by Mu Editor
from microbit import *
import WOM_Sensor_Kit
while True:
# Parameter definition: WOM_dht11 (pin, read data)
# Return corresponding data, including WOM_temp_C, WOM_temp_F, WOM_humidity
# The interval of each acquisition must be more than 1s
display.show("C ")
display.scroll(WOM_Sensor_Kit.WOM_dht11(pin0, WOM_Sensor_Kit.WOM_temp_C))
sleep(1500)
display.show("F ")
display.scroll(WOM_Sensor_Kit.WOM_dht11(pin0, WOM_Sensor_Kit.WOM_temp_F))
sleep(1500)
display.show("H ")
display.scroll(WOM_Sensor_Kit.WOM_dht11(pin0, WOM_Sensor_Kit.WOM_humidity))
sleep(1500)1) from microbit import * means to import all library files from the microbit library. This statement is required for every program when using the microbit.
2) import WOM_Sensor_Kit means to import the library of the magic block world. This library must be imported when using the related functions of the magic block world.
3) while True: means is that infinite loop.
4) display.show("C ") Micro:bit dot matrix display “C”.
5) display.scroll(WOM_Sensor_Kit.WOM_dht11(pin0, WOM_Sensor_Kit.WOM_temp_C)) Scrolling display of temperature/humidity on Micro:bit dot matrix.
4.Writing and download code
4.1 You should open the Mu software, and enter the code in the edit window, , as shown below.
Note! All English and symbols should be entered in English, use the Tab key (tab key) to indent and the last line must be a space.
4.2 You can click the “Check” button to check if our code has an error.
If a cursor or underline appears on a line, it indicates a syntax error, please check and modify. If there is no error in the program, the bottom left of the interface will prompt that there is no problem in detection.
4.3 Click the ‘REPL’ button to check whether the WOM_Sensor_Kit Python library has been downloaded.
If not, please refer to [Preparation before class] --> [Python Programming Guide] .
4.4 After the program is written, use a micro USB cable to connect the computer and the micro:bit board. Please click the ‘Flash’ button to download the program to the micro:bit motherboard (You need to click the ‘REPL’ button again to close the function of importing library files before you download the program).
4.5 If the download failed, please confirm whether the micro:bit is connected to the computer through the micro USB data cable, and confirm whether the WOM_Sensor_Kit Python library has been imported.
5. Phenomenon
After the program is downloaded successfully. The temperature and humidity of the current environment will be displayed on the Micro:bit dot matrix.