Lab: a temperature alarm
The TK12 thermistor on A3 is read in degrees Celsius with the B equation and the constants printed in its part number. At 28 °C the TK36 buzzer on D11 starts beeping once a second, and it stops only below 27 °C, so a temperature sitting on the line cannot make it stutter.
The TK12 thermistor is a divider whose top half changes with temperature, read as a voltage divider; the TK36 buzzer sounds while its pin is HIGH. The beeps are timed with millis(), so the sketch keeps reading while it beeps.
Where they plug in
| Module | Socket | Pin |
|---|---|---|
| TK12 NTC thermistor | A2/A3 | A3 |
| TK36 active buzzer | D10/D11 | D11 |
From counts to degrees
The arithmetic is the TK12 book's own, from counts to degrees, with its constants: a 10 kΩ fixed resistor from VCC to SIGNAL, and a thermistor that is 10 kΩ at 25 °C with a B value of 3950 K, the "103" and "3950" in its part number. First the thermistor's resistance from the average of 32 readings:
return R_FIXED * n / (1023.0 - n);Then the B equation turns ohms into kelvin, and 273.15 less is Celsius:
float invT = 1.0 / T0 + log(ohms / R25) / B;
return 1.0 / invT - 273.15;At 25 °C both halves of the divider are 10 kΩ and the reading is half of 1023. Because the UNO's ADC measures against the same 5 V that feeds the divider, the supply cancels out: a USB port at 4.8 V gives the same degrees as one at 5.1 V.
An alarm that does not stutter
Pick Off at 28 °C and run it. A fingertip warms the thermistor to just over the line and holds it there, and the reading wobbles by about half a degree. Every dip under 28 stops the alarm, and every climb back starts it again with a fresh beep: it stutters a dozen times. Pick Off below 27 °C and run it again. The alarm starts once, beeps once a second, and stops after the finger comes away. The trace is drawn to show the behaviour, not recorded.
The sketch holds the two lines in two comparisons, each of which can only move the alarm one way:
if (!alarmOn && lastC >= ALARM_C) { alarmOn = true; alarmStart = now; }
if (alarmOn && lastC < ALARM_C - HYST_C) alarmOn = false;Beeps that do not stop the sketch
The TK36 makes its own tone, so the sketch only switches it. While the alarm is on, the buzzer is HIGH for the first 150 ms of every second, counted from the moment the alarm started:
bool beep = alarmOn && (now - alarmStart) % PERIOD_MS < BEEP_MS;Nothing in loop() waits. A new reading is taken when 250 ms have passed
since the last one, and between readings the loop runs round and round,
checking the time. A delay(150) for each beep would stop the reading for
that long; here nothing does.
The TK36 has no pull-down on its input, so the sketch makes D11 an output and
drives it LOW before anything else in setup().
Make it yours
- Beep faster the hotter it gets: make
PERIOD_MSa variable and shorten it by 100 ms for every degree over the line. - Add a silence button: the TK04 in its D6/D7 socket, on D7, sets a flag that keeps the buzzer quiet until the alarm has gone off and come on again.
- Turn it into a frost alarm: swap the comparisons so it sounds below a line and stops only a degree above it.
The code
Every 250 ms, average 32 readings, turn them into ohms and then degrees, and decide whether the alarm is on. Between readings, millis() times the beeps: 150 ms of sound at the top of each second while the alarm is on. The Serial Monitor prints every reading.
/*
A temperature alarm TinkerBlock UNO kit, /manuals/tkbu
The TK12 NTC thermistor is read in degrees Celsius. At 28 C or
above the TK36 active buzzer beeps once a second; it stops only when
the reading falls below 27 C. The one-degree gap is what stops it
stuttering when the temperature sits on the line.
Wiring, on the TinkerBlock UNO shield:
TK12 NTC thermistor in the A2/A3 socket, so it is on A3
TK36 active buzzer in the D10/D11 socket, so it is on D11
A module is on the second pin printed beside its socket.
Arduino IDE
Tools > Board: Arduino Uno
Tools > Port: the CH340 port
No library needed. Serial Monitor at 115200.
*/
#include <math.h>
const int SENSOR_PIN = A3; // TK12 SIGNAL
const int BUZZER_PIN = 11; // TK36 SIGNAL: HIGH sounds it
const float ALARM_C = 28.0; // on at or above this
const float HYST_C = 1.0; // off only below ALARM_C - HYST_C
const unsigned long BEEP_MS = 150; // sound this long
const unsigned long PERIOD_MS = 1000; // once in this long
const unsigned long READ_MS = 250; // a new reading this often
// The TK12's own constants: its book, "Counts to degrees".
const int SAMPLES = 32;
const float R_FIXED = 10000.0; // the 10 kOhm from VCC to SIGNAL
const float R25 = 10000.0; // the thermistor at 25 C: "103"
const float B = 3950.0; // its B value: "3950"
const float T0 = 298.15; // 25 C in kelvin
bool alarmOn = false;
float lastC = NAN;
unsigned long lastRead = 0;
unsigned long alarmStart = 0;
// The thermistor's resistance from an average of 32 readings, or NAN
// if SIGNAL sits at either end (a cable out, or the wrong socket).
float thermistorOhms() {
float sum = 0;
for (int i = 0; i < SAMPLES; i++) sum += analogRead(SENSOR_PIN);
float n = sum / SAMPLES;
if (n <= 0 || n >= 1023) return NAN;
return R_FIXED * n / (1023.0 - n);
}
float celsius(float ohms) {
float invT = 1.0 / T0 + log(ohms / R25) / B;
return 1.0 / invT - 273.15;
}
void setup() {
// No pull-down on the TK36: drive it LOW before anything else.
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(115200);
}
void loop() {
unsigned long now = millis();
if (now - lastRead >= READ_MS) {
lastRead = now;
float r = thermistorOhms();
if (isnan(r)) {
Serial.println("No reading: is the TK12 in the A2/A3 socket?");
alarmOn = false;
} else {
lastC = celsius(r);
if (!alarmOn && lastC >= ALARM_C) {
alarmOn = true;
alarmStart = now; // the first beep starts now
}
if (alarmOn && lastC < ALARM_C - HYST_C) alarmOn = false;
Serial.print(lastC, 1);
Serial.println(alarmOn ? " C ALARM" : " C");
}
}
// While the alarm is on: BEEP_MS of sound at the top of each second.
bool beep = alarmOn && (now - alarmStart) % PERIOD_MS < BEEP_MS;
digitalWrite(BUZZER_PIN, beep ? HIGH : LOW);
}Hold a fingertip on the thermistor to test it: skin is warmer than most rooms and the reading climbs past 28 °C in several seconds. If the room is already over 28 °C, raise ALARM_C. Set HYST_C to 0 to hear the stutter the figure shows.
View on GitHub · kits/tkbu/temperature-alarm/Temperature_Alarm/Temperature_Alarm.ino @ v1.14When it does not work
SIGNAL is at 0 or at 1023, which the thermistor never gives on its own: the cable is out, or the TK12 is in the wrong socket. It goes in the A2/A3 socket, on A3. In the A0/A1 socket it would be on A1 and the sketch would read nothing on A3.
Give it a minute after plugging in: a module warm from a hand reads warm. Then compare with a thermometer beside it. The parts on the TK12 are 1 % parts, worth about a quarter of a degree near room temperature; an error of several degrees means the wrong constants or the wrong socket.
The TK36 has no pull-down, so until setup() drives D11 LOW it does whatever the pin does. The sketch drives it LOW in its first two lines. If yours chirps, check those lines come before Serial.begin() and anything else.
HYST_C is 0, or the alarm and the off line are the same number. With a one-degree gap the reading has to fall a whole degree before the alarm stops, which the wobble of an averaged reading never does.
A library from the Library Manager, five colours, and what the bar draws.
Lab: the first colours →Edit this page — content/books/tkbu/temperature-alarm.mdx
Questions about this product
See what other owners have asked, and read their solutions.
TinkerBlock UNO R3 Starter Kit, 12 Modules
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.