Project Overview and Parts
This project reads ambient temperature and reacts to it — lighting an LED and printing a warning when it crosses a threshold. You need an Arduino Uno, a temperature sensor, an LED with a 220-ohm resistor, and a breadboard. Two common sensor choices are the analog TMP36, which outputs a voltage proportional to temperature, and the digital DHT22, which sends calibrated temperature and humidity over a single data line. Understanding which sensor you use dictates how you read it: analogRead() for the TMP36 versus a library call for the DHT22.
Cricket analogy: Gathering the parts first is like a team confirming the playing eleven before the toss — sensor, LED, resistor, and board are your named players, and starting without one is like taking the field a batter short.
// Temperature monitor with a TMP36 analog sensor and warning LED
const uint8_t TMP_PIN = A0; // TMP36 output on analog pin A0
const uint8_t LED_PIN = 8;
const float THRESHOLD_C = 28.0; // trigger above 28 degrees C
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
float readTemperatureC() {
int raw = analogRead(TMP_PIN); // 0..1023
float voltage = raw * (5.0 / 1023.0); // convert to volts
// TMP36: 10 mV per degree C, 500 mV offset at 0 C
return (voltage - 0.5) * 100.0;
}
void loop() {
float tempC = readTemperatureC();
Serial.print(F("Temp C: "));
Serial.println(tempC, 1);
digitalWrite(LED_PIN, tempC > THRESHOLD_C ? HIGH : LOW);
delay(1000); // demo pacing; use millis() in production
}Reading and Converting the Sensor
For the analog TMP36, analogRead() returns a value from 0 to 1023 mapping the 0–5V range through the Uno's 10-bit ADC. You convert that number to a voltage, then to temperature using the sensor's datasheet: the TMP36 outputs 10 millivolts per degree Celsius with a 500 mV offset at 0°C, giving tempC = (voltage - 0.5) * 100. Accuracy depends on a stable reference voltage; using the AREF pin or the internal 1.1V reference reduces noise from a fluctuating 5V USB supply. For the DHT22 you instead include a library, call dht.readTemperature(), and check for NaN to detect a failed read.
Cricket analogy: Converting the raw ADC value is like translating a bowler's raw pace reading into a strategy — the speed gun gives a number, but only the datasheet formula tells you what it means for field placement.
The Uno's ADC reference defaults to the 5V rail, which is noisy on USB power and drifts under load, skewing temperature readings by a degree or more. For better accuracy, use analogReference(INTERNAL) for the stable 1.1V reference (and scale accordingly) or feed a clean reference into AREF — but never apply a voltage to AREF while INTERNAL/DEFAULT is selected, as that can damage the chip.
Adding Alerts, Display, and Logging
Once you have a temperature value, decide how to surface it. The simplest output is Serial, viewable in the IDE's Serial Monitor or Plotter for a live graph. Add a 16x2 I2C LCD or an OLED for a standalone readout, or an LED and buzzer for threshold alerts as shown in the code. To log over time, write timestamped readings to an SD card via the SD library, or push them to the cloud with an ESP32. Use hysteresis on the threshold — for example, turn the alert on above 28°C but only off below 27°C — so a value hovering at the boundary does not flicker the output rapidly.
Cricket analogy: Hysteresis on the threshold is like the DRS umpire's-call margin: a borderline decision does not flip back and forth every replay, giving a stable verdict instead of constant reversals.
The Arduino IDE's Serial Plotter (Tools > Serial Plotter) graphs any numeric values you println() at the chosen baud, letting you watch temperature trends live without extra hardware. Print one value per line, or multiple space/comma-separated values to plot several series at once.
- A basic temperature monitor needs a board, a sensor (TMP36 or DHT22), an LED with resistor, and a breadboard.
- The analog TMP36 is read with analogRead(); convert using tempC = (voltage - 0.5) * 100.
- The digital DHT22 uses a library call and should be checked for NaN failed reads.
- ADC accuracy depends on a stable reference; consider analogReference(INTERNAL) or AREF.
- Surface data via Serial Monitor/Plotter, an LCD/OLED, or SD/cloud logging.
- Use hysteresis on thresholds to stop the alert output flickering at the boundary.
- Wrap constant prints in F() and move to millis() timing for a production-quality build.
Practice what you learned
1. How do you convert a TMP36 voltage reading to Celsius?
2. What range does analogRead() return on an Arduino Uno?
3. How should you detect a failed reading from a DHT22?
4. Why apply hysteresis to a temperature alert threshold?
5. Which built-in tool graphs numeric Serial output without extra hardware?
Was this page helpful?
You May Also Like
Choosing the Right Arduino Board
How to pick the correct Arduino board for a project by matching microcontroller, I/O count, memory, connectivity, and voltage to your requirements.
Arduino Best Practices
Practical conventions for writing reliable, readable, and memory-safe Arduino sketches, from non-blocking timing to power and wiring discipline.
Arduino Quick Reference
A condensed reference of the essential Arduino functions, data types, pin modes, and communication calls for fast lookup while coding.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics