Built-in project · Arduino Uno R3

Temperature alarm: thermistor on A0, RGB on 10, 11 and 12, on an Arduino Uno

A thermistor in a divider on A0 reads the temperature; an RGB LED (red 10, green 11, blue 12) shows blue when it is cold, green when it is comfortable and red when it is too hot. The whole build, an Arduino Uno and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Thermistors and voltage dividers
  • The beta equation for temperature
  • Thresholds with if and else
  • Driving an RGB LED with digital pins
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

A 10k NTC thermistor and a 10k resistor make a voltage divider on A0. The thermistor sits at the bottom, and its resistance falls as it warms, so the reading on A0 drops as the temperature rises.

The sketch turns the reading back into degrees Celsius with the beta equation. Then it sets an RGB LED, on pins 10 (red), 11 (green) and 12 (blue). Below 18 C it is blue, above 28 C it is red, and in between it is green.

The temperature and a word (cold, comfortable or too hot) go to the serial monitor twice a second.

Parts list

8 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 12
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 10
Resistor, 10k Ω pin 2Arduino Uno R3 pin 5V
RGB LED pin CArduino Uno R3 pin GND
Thermistor, 10k NTC pin 1Arduino Uno R3 pin GND
Thermistor, 10k NTC pin 2Arduino Uno R3 pin A0
Resistor, 10k Ω pin 1Arduino Uno R3 pin A0
Resistor, 220 Ω (3) pin 2RGB LED pin R
Resistor, 220 Ω (2) pin 2RGB LED pin G
Resistor, 220 Ω (1) pin 2RGB LED pin B

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A temperature alarm: a thermistor on A0 and an RGB LED that changes color.
//
//   10k from 5V -> A0; the thermistor from A0 -> GND
//   RGB red -> 220 ohm -> pin 10   green -> 220 ohm -> pin 11
//   RGB blue -> 220 ohm -> pin 12   the long common leg -> GND
//
// A thermistor's resistance falls as it warms, so the bottom of the divider
// shrinks and A0 reads lower. The beta equation turns the reading back into
// degrees. Below 18 C the light is blue, above 28 C it is red, and between
// the two it is green.
//
// Drag the temperature on the thermistor.

const int SENSOR = A0;
const int RED = 10;
const int GREEN = 11;
const int BLUE = 12;
const float SERIES_OHMS = 10000.0;

// The thermistor's temperature from the divider's reading, by the beta
// equation: 1/T = 1/T0 + ln(R/R0)/B, with T in kelvin.
float celsius(int reading) {
  float r = SERIES_OHMS * reading / (1023.0 - reading);
  float kelvin = 1.0 / (1.0 / (25.0 + 273.15) + log(r / 10000.0) / 3950.0);
  return kelvin - 273.15;
}

void color(int r, int g, int b) {
  digitalWrite(RED, r);
  digitalWrite(GREEN, g);
  digitalWrite(BLUE, b);
}

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: temperature alarm, thermistor on A0");
}

void loop() {
  float c = celsius(analogRead(SENSOR));
  Serial.print(c);
  if (c < 18) {
    color(LOW, LOW, HIGH);
    Serial.println(" C: cold");
  } else if (c > 28) {
    color(HIGH, LOW, LOW);
    Serial.println(" C: too hot!");
  } else {
    color(LOW, HIGH, LOW);
    Serial.println(" C: comfortable");
  }
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

celsius sketch.ino

Turns an ADC reading into degrees. First it works out the thermistor's resistance from the divider. Then it applies the beta equation with 25 C, 10k and a beta of 3950, giving kelvin. Subtracting 273.15 gives Celsius.

// The thermistor's temperature from the divider's reading, by the beta
// equation: 1/T = 1/T0 + ln(R/R0)/B, with T in kelvin.
float celsius(int reading) {
  float r = SERIES_OHMS * reading / (1023.0 - reading);
  float kelvin = 1.0 / (1.0 / (25.0 + 273.15) + log(r / 10000.0) / 3950.0);
  return kelvin - 273.15;
}

color sketch.ino

A small helper that writes HIGH or LOW to the red, green and blue pins in one call, so the main loop reads like a list of colors.

void color(int r, int g, int b) {
  digitalWrite(RED, r);
  digitalWrite(GREEN, g);
  digitalWrite(BLUE, b);
}

loop sketch.ino

Reads A0, converts it with celsius and prints it. If it is under 18 the LED turns blue, over 28 red, otherwise green. Each case prints its label too. Then it waits 500 ms.

void loop() {
  float c = celsius(analogRead(SENSOR));
  Serial.print(c);
  if (c < 18) {
    color(LOW, LOW, HIGH);
    Serial.println(" C: cold");
  } else if (c > 28) {
    color(HIGH, LOW, LOW);
    Serial.println(" C: too hot!");
  } else {
    color(LOW, HIGH, LOW);
    Serial.println(" C: comfortable");
  }
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The colors are wrong for the temperature This project
    Red is pin 10, green is 11 and blue is 12 in this sketch. Check which LED leg goes to which resistor and pin.
  • The alarm trips at the wrong temperature This project
    The 18 C and 28 C limits are in loop. Change them there to suit your room.
  • The colors are wrong or one color never lights. RGB LED
    Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on.
  • One color is much brighter than the others, or the LED runs too hot. RGB LED
    Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The temperature reads far off or jumps around. Thermistor
    Use a fixed resistor equal to the thermistor's 10k at 25 C in the divider. Convert with the Steinhart-Hart or Beta equation (Beta is about 3950 for common parts), not a straight line.
  • The reading goes the wrong way, or self-heating shifts it. Thermistor
    An NTC lowers its resistance as it warms, so the divider voltage moves with the wiring side. Check which side the thermistor is on. Keep current small, with 3.3 V or 5 V through 10k, so it does not self-heat.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.