Love meter on an Arduino Uno
Hold a TMP36 between your fingers and a bar of ten WS2812B LEDs fills from pink to red with your warmth, measured against the room in half-degree steps. The whole build, an Arduino Uno and 3 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.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
What you'll learn
- Reading an analog temperature sensor
- Converting millivolts to degrees
- Calibrating against a baseline at startup
- Mapping a measurement to a number of lit LEDs
- Addressable LEDs with Adafruit_NeoPixel
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A TMP36 temperature sensor outputs 10 mV per degree Celsius, with 500 mV at 0 degrees. At power-up the Uno reads it 20 times over two seconds and saves the average as the room temperature.
After that, every half a degree above the room lights one more LED on a bar of ten. The colors run from pink at the bottom to deep red at the top. Hold the sensor between your fingers and the bar slowly fills as the sensor warms. The serial monitor prints a label for the level, from Just friends to True love!
Parts list
5 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Temperature sensor, TMP36In Starter Projects Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
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 pin | Connects to |
|---|---|
| Resistor, 330 Ω pin 1 | Arduino Uno R3 pin 6 |
| Temperature sensor, TMP36 pin VS | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Temperature sensor, TMP36 pin GND | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Temperature sensor, TMP36 pin VOUT | Arduino Uno R3 pin A0 |
| Addressable RGB LEDs pin DIN | Resistor, 330 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Love meter: hold the temperature sensor between your fingers and a bar of
// ten LEDs fills up with your warmth. The warmer your hands, the more love.
//
// TMP36 VS -> 5 V, VOUT -> A0, GND -> GND
// LED bar 10 WS2812B LEDs, DIN <- 330 ohms <- pin 6, VDD -> 5 V, VSS -> GND
//
// It is the classic love-o-meter, honest about what it measures: the TMP36
// gives 10 mV per degree Celsius, with 500 mV at 0 degrees, so
// degrees = (millivolts - 500) / 10. For the first two seconds the sketch
// reads the room and keeps that as the baseline. After that, every half a
// degree above the room lights one more LED, from pink up to deep red.
//
// A hand warms the sensor slowly, over ten or twenty seconds, because the
// little plastic package has to heat up. That slow climb is the fun part.
//
// Things to try. Drag the sensor's temperature up. Change STEP_TENTHS to make
// the meter harder or easier to fill.
#include <Adafruit_NeoPixel.h>
const int BAR_PIN = 6;
const int LEDS = 10;
const int STEP_TENTHS = 5; // tenths of a degree per LED
Adafruit_NeoPixel bar(LEDS, BAR_PIN, NEO_GRB + NEO_KHZ800);
const char *LABELS[] = {"Just friends", "A little crush", "Sweet on you", "Smitten",
"Head over heels", "True love!"};
long room = 0; // tenths of a degree
int lastLevel = -1;
long tenths() {
long mv = analogRead(A0) * 5000L / 1024;
return mv - 500; // 10 mV a degree is 1 mV a tenth
}
void setup() {
Serial.begin(9600);
bar.begin();
bar.setBrightness(150);
bar.show();
long sum = 0;
for (int i = 0; i < 20; i++) {
sum += tenths();
delay(100);
}
room = sum / 20;
Serial.print("Love meter ready. The room is ");
Serial.print(room / 10);
Serial.print(".");
Serial.print(abs(room % 10));
Serial.println(" C. Hold the sensor!");
}
void loop() {
long warmer = tenths() - room;
int level = constrain(warmer / STEP_TENTHS, 0, LEDS);
for (int i = 0; i < LEDS; i++) {
if (i < level) {
// Pink at the bottom, deep red at the top.
bar.setPixelColor(i, 255, 100 - i * 10, 140 - i * 14);
} else {
bar.setPixelColor(i, 0, 0, 0);
}
}
bar.show();
if (level != lastLevel) {
lastLevel = level;
Serial.print("Level ");
Serial.print(level);
Serial.print(": ");
Serial.println(LABELS[level * 5 / LEDS]);
}
delay(200);
}
Code walkthrough
The key functions of the sketch, and what each one does.
tenths sketch.ino
Reads A0 and converts the count to millivolts, then subtracts 500. Since the sensor gives 10 mV per degree, 1 mV is a tenth of a degree. This keeps all the math in whole numbers.
long tenths() {
long mv = analogRead(A0) * 5000L / 1024;
return mv - 500; // 10 mV a degree is 1 mV a tenth
}setup sketch.ino
Starts serial and the LED bar, then spends two seconds taking 20 readings, 100 ms apart. The average is stored in room, and printed to serial.
void setup() {
Serial.begin(9600);
bar.begin();
bar.setBrightness(150);
bar.show();
long sum = 0;
for (int i = 0; i < 20; i++) {
sum += tenths();
delay(100);
}
room = sum / 20;
Serial.print("Love meter ready. The room is ");
Serial.print(room / 10);
Serial.print(".");
Serial.print(abs(room % 10));
Serial.println(" C. Hold the sensor!");
}loop sketch.ino
Measures how much warmer the sensor is than room, divides by STEP_TENTHS (5) and limits the result to 0 to 10, which is how many LEDs to light. Each lit LED gets a color between pink and red, the rest go dark. It prints a label only when the level changes, then waits 200 ms.
void loop() {
long warmer = tenths() - room;
int level = constrain(warmer / STEP_TENTHS, 0, LEDS);
for (int i = 0; i < LEDS; i++) {
if (i < level) {
// Pink at the bottom, deep red at the top.
bar.setPixelColor(i, 255, 100 - i * 10, 140 - i * 14);
} else {
bar.setPixelColor(i, 0, 0, 0);
}
}
bar.show();
if (level != lastLevel) {
lastLevel = level;
Serial.print("Level ");
Serial.print(level);
Serial.print(": ");
Serial.println(LABELS[level * 5 / LEDS]);
}
delay(200);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The bar starts partly lit or never fills. This project
The room reading is taken in the first two seconds. Keep your fingers off the sensor until the sketch says it is ready. - It fills too quickly or too slowly. This project
ChangeSTEP_TENTHS. A smaller number lights an LED for each smaller rise in temperature. - The part gets very hot, and the reading is nonsense. Temperature sensor
The legs are reversed. With the flat face toward you, the order is +Vs, Vout, GND from left to right. Fix it right away, because a reversed TMP36 heats quickly. - The temperature is off by a lot or negative. Temperature sensor
Output is 0.5 V at 0 degrees C plus 10 mV per degree. So C = (volts minus 0.5) times 100. Compute volts from the ADC using the right reference, 5 V or 3.3 V, not always 5. - 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 first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND. - The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.