Skip to content

Search the documentation

Kit
Firmware

Room thermometer — Smart Home

The board reads the DHT11 sensor and writes the temperature and the humidity of the room into the serial monitor, over and over.

This is the example that proves the DHT11 works. There is nothing to look at on the board itself — the whole test is the two lines that keep appearing.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Thermometer starting"
  • repeat forever
    • Print to serial (create text with "Temperature (C): " Read DHT11 Temperature)
    • wait 2 seconds
    • Print to serial (create text with "Humidity (%): " Read DHT11 Humidity)
    • wait 2 seconds
  • A Smart Home board with the DHT11 temperature-and-humidity sensor.
  • A USB cable, and the serial monitor open in MoBot.
  1. Open 02-thermometer.mobot in MoBot.
  2. Plug the board in, pick its port, press Send to Device.
  3. Open the serial monitor.
[MOBOT] Thermometer starting
[MOBOT] Temperature (C): 24
[MOBOT] Humidity (%): 41
[MOBOT] Temperature (C): 24
[MOBOT] Humidity (%): 41
...

A new pair every four seconds.

The real test: breathe gently on the sensor for a few seconds. The humidity number should climb — often past 70 — and then fall back on its own over the next minute. The temperature may rise a degree. A sensor that answers but never changes is not really working.

Sensible readings indoors: temperature 15–30, humidity 20–70. A DHT11 only reports whole numbers, so a room that “feels the same” really will print the same number for minutes at a time.

A DHT11 is slow. It needs about a second between measurements, and asking it twice in a row can make it complain or hand back the last answer again. So the program asks for the temperature, waits, asks for the humidity, waits.

If you shorten the waits to 0.1 the sensor starts failing — the program stops with an OSError in the serial monitor. That is worth doing once, on purpose, so a child sees what an impatient program looks like.

block what it does
iot_dht Reads the DHT11. The dropdown picks Temperature or Humidity.
text_join Glues the words and the number into one line.
iot_print Sends that line to the serial monitor.
wait Pauses, in seconds.
repeat_forever Keeps it going.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

02-thermometer.firmware-3.main.py
# Room thermometer — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 02-thermometer.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 02-thermometer.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Thermometer starting')
while True:
print("[MOBOT]", ('Temperature (C): ' + str(home.temperature_sensor.temperature())))
time.sleep(2)
print("[MOBOT]", ('Humidity (%): ' + str(home.temperature_sensor.humidity())))
time.sleep(2)
  • Take the board somewhere cold and watch the number fall.
  • Change the words to Il fait (C): — the block will print whatever you type.