GAMERIFTRobotics & Coding Shop kits
HomeKitsFor schoolsAboutGAMERIFT Store ↗
Beginner Kit · Lesson 09 of 12

Weather Desk

Real degrees. Real humidity. Your desk becomes a tiny weather station reporting on your actual room.

Unlock your lessons

Enter the access code from the card inside your Beginner Kit (or from your GAMERIFT order email). Your name appears on every page and printout as the licensee.

Don't have a kit yet? Get the Beginner Kit, the code ships inside. Lost your code? Contact us with your order number.

LICENSED TO , PERSONAL USE · © GAMERIFT

BRIEF A sensor that speaks in sentences

Your photoresistor mumbled a raw number and left you to figure out what it meant. Today's sensor is a different creature. The DHT11 measures temperature and humidity and reports them in real units: degrees Celsius, percent humidity. The same family of sensor lives inside air conditioners, incubators and greenhouses. By the end of this lesson your room will have its own weather report, updating live on your screen.

Time: 45–60 minutes · Difficulty: 3/5 · You need: Lesson 06 done (Serial Monitor habits).

KIT What you need

Concept · Libraries, or standing on shoulders

The DHT11 speaks a fussy timing language, signals measured in microseconds. You could learn it... or you could use the code someone already wrote and tested for the whole world. A library is exactly that: expert code you download once and command with easy words like readTemperature(). Professional engineers use libraries every single day. Knowing when not to reinvent is a skill.

Concept · When the sensor says nothing

Sensors sometimes fail to answer, a loose wire, a too-hasty question. When that happens the library hands you a special not-a-number value called NaN. Good programs check for it and say something useful instead of printing garbage. Today you write your first error handling, which is the moment your code starts becoming trustworthy.

SETUP Install the library, once

  1. In the Arduino IDE open Tools → Manage Libraries.
  2. Search "DHT sensor library" and install the one by Adafruit.
  3. If it offers to install the Adafruit Unified Sensor helper too, say yes, the DHT library needs it.

BUILD Wire it

Unplug the USB. Push the module's three pins into the breadboard and wire from its rows. Check the tiny letters printed next to the pins, most kits mark them S (signal), middle (power), and (ground), but trust the letters on YOURS over any diagram.

FromToWhy
Module S pinR3 pin 2The data line, where the sentences arrive
Module middle pinR3 5VPower for the sensor's tiny brain
Module − pinR3 GNDLoop complete

CODE Program it

// GAMERIFT Beginner Kit, Lesson 09: Weather Desk
// A live weather report for one small room.

#include <DHT.h>                // invite the library in

const int dhtPin = 2;
DHT dht(dhtPin, DHT11);          // our sensor, by name and type

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("Weather Desk starting...");
}

void loop() {
  float temp = dht.readTemperature();   // degrees C, with decimals
  float hum  = dht.readHumidity();      // percent

  if (isnan(temp) || isnan(hum)) {      // did the sensor not answer?
    Serial.println("Sensor not answering. Check the three wires.");
  } else {
    Serial.print(temp);
    Serial.print(" C   |   ");
    Serial.print(hum);
    Serial.println(" % humidity");
  }

  delay(2000);   // the DHT11 needs 2 seconds between questions
}
  1. Upload and open the Serial Monitor (9600 baud).
  2. There it is: your room, in degrees and percent, refreshing every two seconds.
  3. Compare it with a weather app. Your room is probably warmer than outside, now you can prove it.

BREAK IT Now break it on purpose

  1. Breathe on the sensor slowly, like fogging a window. Watch humidity leap 20 points and temperature rise, your breath is warm, wet weather. This is also how you know it is really measuring.
  2. Change delay(2000) to delay(200). The DHT11 starts failing or repeating stale readings, it simply cannot think that fast. Some sensors are marathon runners, not sprinters. Respect the datasheet. Put it back.
  3. Pull out the S wire. Instead of nonsense numbers, your program calmly says the sensor is not answering, because YOU wrote error handling. Feel slightly heroic. Reconnect.

REMIX Make it yours

LOG IT Show and tell

Screenshot your weather report (bonus: the breath spike) for your lesson log. Stuck? Contact us with a photo of the three wires.

Next · Lesson 10: The Clapper

Your machines can feel light and air. Next they get ears: a sound sensor and a lamp you switch on by clapping, the classic sci-fi gadget, real, on your desk.

All lessons