ESP8266 Temperature Analog Sensor Module Project

This project demonstrates how to interface an analog temperature sensor with an ESP8266 microcontroller to measure and display temperature readings. Analog temperature sensors, such as the LM35 or TMP36, are widely used in various applications to monitor temperature changes.

How It Works

An analog temperature module converts temperature into a voltage the ESP8266 (NodeMCU) can sample directly. Two parts dominate hobby kits: the LM35 precision sensor and NTC thermistor modules such as the KY-013.

The LM35 is the easier of the two because its output is linear by design: exactly 10 mV per °C, referenced to 0 V, so 25 °C produces 250 mV. No calibration curve is required — divide the measured millivolts by 10 and you have degrees Celsius.

A thermistor is a resistor whose value falls as it warms. The KY-013 places a 10 kΩ NTC in a divider with a fixed 10 kΩ resistor, so the module outputs a voltage that changes with temperature but not linearly. Converting it properly needs the Steinhart–Hart relationship, or its simplified B-parameter form, using the thermistor's nominal resistance at 25 °C and its B coefficient (around 3950 for common parts).

The ESP8266 (NodeMCU) samples with a 10-bit ADC, so analogRead() returns 0–1023 across 3.3 V at the NodeMCU A0 header (the bare chip reads 0–1 V).

Components Needed

  • ESP8266 (NodeMCU)
  • Analog Temperature Sensor
  • Jumper Wires
  • ESP8266 (NodeMCU)

Wiring to the ESP8266 (NodeMCU)

Connect the module's signal pin to A0, VCC to the 3.3 V rail and GND to ground. Note that powering the sensor from 3.3 V also sets its maximum output to 3.3 V, which conveniently matches the ADC range.

The ESP8266 has only one analog channel, so a temperature sensor occupies it exclusively. Adding a second analog sensor requires an external ADC such as the ADS1115 or an analog multiplexer.

Keep the sensor away from the board's voltage regulator and any motor driver. Both dissipate heat and will bias readings upward by several degrees if the sensor sits next to them.

Module pinESP8266 (NodeMCU) pinFunction
S / OUTA0Analog voltage proportional to temperature
VCC / +3V3Supply
GND / −GNDCommon ground

Example Code

Reading an LM35 on the ESP8266 (NodeMCU) with averaging to suppress ADC noise. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.

Reading an LM35 on the ESP8266 (NodeMCU) with averaging to suppress ADC noise
const int SENSOR_PIN = A0;
const float VREF = 3.3;      // ADC reference voltage
const int   ADC_MAX = 1023;   // 10-bit converter
const int   SAMPLES = 16;

void setup() {
  Serial.begin(115200);
}

float readCelsius() {
  long total = 0;
  for (int i = 0; i < SAMPLES; i++) {
    total += analogRead(SENSOR_PIN);
    delay(5);
  }
  float counts  = (float)total / SAMPLES;
  float volts   = counts * VREF / ADC_MAX;
  return volts * 100.0;            // LM35: 10 mV per degree C
}

void loop() {
  float c = readCelsius();
  Serial.print("Temperature: ");
  Serial.print(c, 1);
  Serial.print(" C  /  ");
  Serial.print(c * 9.0 / 5.0 + 32.0, 1);
  Serial.println(" F");
  delay(1000);
}

Applications

A analog temperature sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:

  • Room and greenhouse climate logging
  • Over-temperature cut-outs for motors, power supplies and battery packs
  • Thermostat control for heaters, fans and incubators
  • Cold-chain monitoring where a readable margin of ±1 °C is acceptable
  • Compensation inputs for other sensors whose output drifts with temperature

Working with the ESP8266 (NodeMCU)

The ESP8266 (NodeMCU) is built around the ESP8266 and runs on 3.3 V logic with roughly 80 KB usable of SRAM and 4 MB on most NodeMCU boards of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.

The ESP8266 is a 3.3 V part and is not 5 V tolerant — feeding a 5 V sensor output straight into a GPIO can damage the chip. Use a divider or level shifter.

There is only one ADC channel, so reading several analog sensors needs an external multiplexer such as a CD4051 or an ADS1115.

D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are strapping pins sampled at boot; holding them at the wrong level stops the board starting.

Built-in WiFi means the same sketch can publish readings to a dashboard without extra hardware.

ESP8266 (NodeMCU) characteristicValueWhy it matters here
Logic voltage3.3 VSensor outputs above this level need a divider or level shifter
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsa single analog channel, A0Determines how many analog sensors can share the board
PWM outputsany GPIO via software PWMNeeded for brightness, speed and tone control
I²C pinsD2 (SDA, GPIO4) and D1 (SCL, GPIO5) by defaultFixed by hardware — wiring copied from another board may not match
Interrupt pinsany GPIO except D0 (GPIO16)Required for counting fast or asynchronous events
Serialone hardware UART plus a transmit-only second portMonitor runs at 115200 baud by default

Troubleshooting

Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:

  • Readings sit around 0 or jump wildly — the signal pin is on a digital pin rather than an analog one.
  • Temperature reads several degrees high — the sensor is picking up heat from the regulator or a nearby driver; move it away from the board.
  • Values drift slowly over minutes — self-heating. Sample periodically rather than continuously.
  • A thermistor module reads nonsense when treated as linear — the KY-013 needs the B-parameter conversion, not the LM35 formula.
  • The sketch compiles but the board resets or behaves erratically — a 5 V module output is being driven into a 3.3 V pin. Measure the signal before connecting it.
  • Readings differ from an Arduino tutorial for the same part — the 10-bit ADC returns 0–1023, not 0–1023, so any constant copied from an Uno example needs rescaling.

Taking It Further on the ESP8266 (NodeMCU)

Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the ESP8266 (NodeMCU) specifically:

The obvious extension on an ESP8266 is to stop printing to Serial and publish instead. A few lines using ESP8266WiFi and an HTTP client will push each reading to a dashboard such as ThingSpeak, or to an MQTT broker for home automation.

Running an onboard web server turns the board into its own display: serve a small HTML page that fetches the latest reading over AJAX, and any phone on the network becomes the instrument panel.

For battery operation, deep sleep is essential — the ESP8266 draws around 70 mA with WiFi active but under 20 µA asleep. Wire D0 (GPIO16) to RST so the board can wake itself, take a reading, publish and sleep again.

Notes and Practical Limits

Averaging sixteen samples, as the sketch does, trades response speed for stability and removes most of the least-significant-bit jitter. Each ADC step is roughly 4.9 mV, which corresponds to about 0.5 °C with an LM35 — the practical resolution limit without an external amplifier.

For better than ±1 °C, or for readings sent over any distance, prefer a digital sensor such as the DS18B20 or DHT22. Digital parts transmit numbers rather than voltages, so cable resistance and connector corrosion stop mattering.