Level 2Unit B · Session 9
9

Unit B · Things that move, things that show

Is it warm in here?

What we are making: a thermometer that shows temperature and humidity on your screen. Two modules working at once, for the first time.

The idea

The DHT11 is a sensor that already knows the answer. You do not have to convert a voltage or work out a formula — you ask it for a temperature and it hands you a number in degrees Celsius. Sensors like this are called digital sensors, and they are the easy kind.

The interesting part of today is not the sensor. It is that two modules are sharing the same board: the screen on A4 and A5, the sensor on pin 2, both drawing power from the same 5V and GND. That is how every machine you build from now on will be arranged.

New words
DHT11A sensor that reports temperature in °C and humidity as a percentage.
floatA number with a decimal point, like 23.40. An int cannot hold that.
Digital sensorOne that does the working-out itself and hands you the answer.
Arduino UnoUSB to the laptop5VGNDA4A52LCD1602 I2CVCCGNDSDASCLDHT11temperature and humidity+out−dataBoth modules share the same 5V and GND. That is normal and expected.
Two modules at once. The screen talks on A4 and A5; the sensor talks on pin 2.
Temperature and humidity, on the screen
1#include <LiquidCrystal_I2C.h>
2#include <DHT.h>
3 
4LiquidCrystal_I2C screen(0x27, 16, 2);
5DHT sensor(2, DHT11); // the sensor's data wire goes to pin 2
6 
7void setup() {
8 screen.init();
9 screen.backlight();
10 sensor.begin();
11}
12 
13void loop() {
14 float temp = sensor.readTemperature();
15 float hum = sensor.readHumidity();
16 
17 screen.setCursor(0, 0);
18 screen.print("Temp: ");
19 screen.print(temp);
20 screen.print(" C ");
21 
22 screen.setCursor(0, 1);
23 screen.print("Humidity: ");
24 screen.print(hum);
25 screen.print("% ");
26 
27 delay(2000); // the DHT11 cannot be read faster than this
28}

Do it

  1. Keep the screen wired exactly as it was last session. Add the sensor to pin 2.
  2. Install the DHT sensor library.
  3. Upload, and wait. The first reading can take two or three seconds.
  4. Breathe gently on the sensor and watch the humidity climb.

Change it

  1. Swap the two lines round so humidity is on top.
  2. Shorten "Humidity: " to "Hum: " and see how much room that frees.
  3. Change the delay to 5000. Does the reading feel less jumpy?
💡 Tip
Why delay(2000) and not less? The DHT11 physically cannot produce a new reading faster than about once a second. Ask it more often and it hands you the old number, or nan — which means ‘not a number’ and is the sensor's way of saying you rushed it.
What you seeWhyWhat to do
nan on the screenRead too fast, or the data wire is loose.Check pin 2 is firmly in, and that the delay is at least 2000.
Temperature never changesThe DHT11 only reports whole degrees.Not a fault. It is a coarse sensor; breathe on it to see it move.
The screen went blank when I added the sensorA power wire came out.Reaching in to add the sensor usually pulls the 5V wire. Reseat it.
Old digits left behind new onesShorter text does not erase longer text.Print two or three trailing spaces after each value.

What you learned

  • Two modules can share one board, one 5V and one GND. This is normal.
  • A digital sensor gives you the answer, not a voltage to interpret.
  • Every sensor has a speed limit, and asking faster does not help.
Challenge optional — only if you finish early
  • Write down the temperature every five minutes for the whole lesson. Highest? Lowest?
8. A screen of your own