The DHT11 sends its readings as a digital message. The TMP36 is simpler: it outputs a voltage that rises by exactly 10 millivolts for every degree. In this lesson the ESP32 measures that voltage with its analog-to-digital converter (ADC), calculates the temperature, and switches on an LED when the room gets warm.
What you'll learn
- How an analog temperature sensor works
- How to measure millivolts with
analogReadMilliVolts() - How averaging makes a noisy reading steady
- Which ESP32 pins can measure while Wi-Fi is on
What you need
- ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
- TMP36 temperature sensor (ESP32 IoT Starter Kit)
- 1 LED and 1 resistor, 220 Ω (ESP32 IoT Starter Kit)
- Breadboard and jumper wires (ESP32 IoT Starter Kit)
Wiring
Hold the TMP36 with the flat side facing you and the legs pointing down. The left leg is +, the middle leg is the output and the right leg is GND. Connected the wrong way round, the sensor gets very hot within seconds. If it feels hot, unplug the USB cable at once.

| From | To |
|---|---|
| TMP36 left leg (+) | 3V3 |
| TMP36 middle leg (out) | ESP32 GPIO 34 |
| TMP36 right leg (GND) | GND |
| ESP32 GPIO 26 | 220 Ω resistor, then the LED's long leg |
| LED short leg | GND |
The sketch
const int SENSOR_PIN = 34;
const int LED_PIN = 26;
const float WARM = 28.0; // the LED lights at this temperature (°C)
float readTemperature() {
long total = 0;
for (int i = 0; i < 32; i++) { // average 32 readings
total += analogReadMilliVolts(SENSOR_PIN);
delay(2);
}
float millivolts = total / 32.0;
return (millivolts - 500.0) / 10.0; // 500 mV = 0 °C, +10 mV per degree
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
float celsius = readTemperature();
Serial.print("Temperature: ");
Serial.print(celsius, 1);
Serial.println(" °C");
digitalWrite(LED_PIN, celsius >= WARM ? HIGH : LOW);
delay(1000);
}
Upload the sketch and open the Serial Monitor at 115200 baud. Hold the sensor between two fingers and watch the temperature rise.
How it works
The TMP36 is a tiny circuit whose output voltage follows the temperature: 500 mV at 0 °C, plus 10 mV for every degree. At 25 °C it outputs 750 mV. analogReadMilliVolts() uses the ESP32's built-in calibration to return the voltage in millivolts, so the sketch only needs one line of maths to get degrees.
The ESP32's ADC is a little noisy, so one reading can be off by a degree or so. Taking 32 readings and using their average smooths that out. Use GPIO 32 to 39 for analog sensors in Wi-Fi projects; the other analog pins stop working while Wi-Fi is on.
The ESP32's ADC is less accurate than a dedicated sensor, so your reading may differ from a room thermometer by a degree or two. Compare it with the DHT11, and if needed add a small correction to the formula.
Try this next
- Show the temperature in Fahrenheit as well: °F = °C × 9 / 5 + 32.
- Record the lowest and highest temperature since the ESP32 started.
- Show the reading on a web page, like in the relay lesson.
Troubleshooting
- The sensor is hot: it's connected the wrong way round. Unplug at once and check the legs against the wiring table.
- The reading is around −50 °C: the middle leg isn't reaching GPIO 34.
- The value jumps by several degrees: check that GND is solid, and keep the sensor wires short.
- Compile error about analogReadMilliVolts: update the ESP32 board package in the Boards Manager.