Level 2Unit B · Session 12
12

Unit B · Things that move, things that show

A machine that watches the room

What we are making: a device that measures the temperature, decides whether it is too warm, says so on its screen, and sounds a buzzer if it is. Three parts, one machine, and a decision you make yourself.

No new parts today

Everything on this page you have already used. The new thing is that they are working together — sense, decide, act — which is the shape of every machine in this book and most of the ones outside it.

1
DHT11 sensor — still on pin 2
1
LCD screen — still on A4 and A5
1
Active buzzer — new, on pin 8
0
Anything else

The number is yours

int tooWarm = 28; — that 28 is not a fact, it is somebody's opinion, and today it becomes yours. Before you upload: read the room's temperature, write it down, pick your trigger number, and write one sentence saying why. You will be asked to defend it.

Sense, decide, act
1#include <LiquidCrystal_I2C.h>
2#include <DHT.h>
3 
4LiquidCrystal_I2C screen(0x27, 16, 2);
5DHT sensor(2, DHT11);
6int buzzerPin = 8;
7 
8int tooWarm = 28; // <-- YOUR NUMBER GOES HERE
9 
10void setup() {
11 screen.init();
12 screen.backlight();
13 sensor.begin();
14 pinMode(buzzerPin, OUTPUT);
15}
16 
17void loop() {
18 float temp = sensor.readTemperature();
19 
20 screen.setCursor(0, 0);
21 screen.print("Temp: ");
22 screen.print(temp);
23 screen.print(" C ");
24 
25 screen.setCursor(0, 1);
26 if (temp > tooWarm) {
27 screen.print("TOO WARM ");
28 digitalWrite(buzzerPin, HIGH);
29 } else {
30 screen.print("OK ");
31 digitalWrite(buzzerPin, LOW);
32 }
33 
34 delay(2000);
35}

Do it

  1. Wire the buzzer to pin 8 and GND. The longer leg is the one that goes to pin 8.
  2. Put your own number into the tooWarm line.
  3. Upload, then cup your hands around the sensor and breathe on it until it triggers.
  4. Write down how long that took.

The problem you are about to find

Set tooWarm to exactly the room's temperature and upload. The alarm goes on, off, on, off, because the reading wobbles a tenth of a degree either side. That is chatter. The fix is a gap: on above 28, but not off again until it drops below 27.

What you seeWhyWhat to do
The buzzer never stopsYour threshold is below room temperature.Read the screen, then set tooWarm above it.
The buzzer is barely audibleIt is the passive buzzer.Swap for the active one — the one with a sticker on top.
The alarm flickers on and offThe reading is sitting on your threshold.That is chatter, not a fault. Leave a gap between on and off.
TOO WARM stays up after it coolsOK is shorter.Print trailing spaces so the longer word is wiped.

Hand in

  • Your machine, working.
  • Your number, and one sentence saying why you chose it.
  • How long it took to trigger when you breathed on it.

What you learned

  • Sense, decide, act. Every machine in this book has these three parts.
  • Somebody chooses the threshold. Today that somebody was you.
  • A number that sits right on the boundary makes a machine indecisive.
Challenge optional — only if you finish early
  • Add a second threshold so the screen says COLD, OK or TOO WARM, not just two states.
11. The click that switches something bigger