When you need to log ambient room temperature, monitor a 3D printer hotend, or track a solar battery bank's thermal state, temperature sensors are your first line of defense. But grabbing a random sensor from a parts bin and wiring it to a microcontroller often leads to noisy data, skewed readings, or fried pins. To interface these components correctly, you need to understand the underlying physics, the exact nature of the output signal, and the mathematical scaling required to turn raw binary into usable Celsius or Fahrenheit values.
The Physics: How a Temp Sensor Actually Works
At the component level, temperature sensing relies on two primary physical phenomena. The first is the thermistor (thermal resistor), typically made of sintered metal oxides. In Negative Temperature Coefficient (NTC) thermistors, heat excites electrons in the semiconductor material, allowing them to jump the bandgap and conduct electricity more freely. As temperature rises, electrical resistance drops in a highly predictable, though non-linear, exponential curve. These are incredibly fast and cheap, but require complex Steinhart-Hart math to linearize.
The second principle is the silicon bandgap sensor, used in integrated circuits like the TMP36 or LM35. These chips exploit the temperature-dependent voltage drop across a forward-biased PN junction. By running a constant current through two matched transistors operating at different current densities, the chip generates a delta-V that is strictly proportional to absolute temperature (PTAT). Internal op-amps then amplify and offset this voltage, yielding a beautifully linear analog output (e.g., exactly 10mV per degree Celsius) that requires virtually no complex math to decode.
Analog vs. Digital: What the Output Actually Is
A common beginner mistake is conflating analog and digital temperature outputs, or misunderstanding what 'digital' actually means in cheap sensor modules. The output signal defines how your microcontroller must be configured to read it.
- Analog Output (Voltage): Sensors like the TMP36 output a continuous, variable DC voltage. The microcontroller must use an Analog-to-Digital Converter (ADC) pin to sample this voltage and convert it into a discrete binary number. The output is strictly a voltage level (e.g., 0.75V at 25°C).
- Digital Output (Serial Data): Sensors like the DS18B20 or BME280 contain an internal ADC and a digital communication controller. They output serialized binary data over protocols like 1-Wire, I2C, or SPI. The microcontroller reads discrete data packets containing the pre-calculated temperature.
- The 'Digital Module' Trap: Many $2 'digital temperature sensor modules' sold online are actually analog NTC thermistors paired with an LM393 voltage comparator. They do not output temperature data; they output a simple HIGH or LOW logic signal when the temperature crosses a threshold set by a physical potentiometer on the board. If you need actual degree readings, avoid these.
Wiring and Pinout Reference
Below is the standard wiring reference for the two most common hobbyist sensors. Always verify the supply voltage range; pushing 5V into a 3.3V-rated digital sensor will destroy the internal logic gates.
| Sensor Model | Type | Supply Range (VDD) | Pin 1 | Pin 2 | Pin 3 | Required Passives |
|---|---|---|---|---|---|---|
| TMP36 | Analog (Voltage) | 2.7V to 5.5V | VDD (Power) | VOUT (Signal) | GND | 0.1µF decoupling cap across VDD/GND |
| DS18B20 | Digital (1-Wire) | 3.0V to 5.5V | GND | DQ (Data) | VDD (Power) | 4.7kΩ pull-up resistor from DQ to VDD |
The Math: Converting Raw Readings to Celsius
Reading the sensor is only half the battle. You must scale the raw microcontroller reading into a physical unit. Here is the exact math for both paradigms.
Analog Scaling (TMP36 on Arduino Uno)
The TMP36 outputs 0.5V at 0°C, and scales up by 10mV (0.01V) per degree. The Arduino Uno uses a 5V reference and a 10-bit ADC (0-1023).
int rawAdc = analogRead(A0);
float voltage = (rawAdc * 5.0) / 1024.0;
float tempC = (voltage - 0.5) * 100.0;
The ESP32 ADC Non-Linearity Problem
If you wire a TMP36 to an ESP32 and use the standard 12-bit formula (rawAdc * 3.3) / 4095, your readings will be wildly inaccurate—often drifting by 2°C to 4°C. The ESP32's internal ADC is notoriously non-linear at the extreme high and low ends of its voltage range. The fix: Use the modern Arduino-ESP32 core function analogReadMilliVolts(pin), which uses the chip's internal eFuse calibration data to return a highly accurate millivolt reading, bypassing the raw ADC curve entirely.
int millivolts = analogReadMilliVolts(34);
float voltage = millivolts / 1000.0;
float tempC = (voltage - 0.5) * 100.0;
Digital Scaling (DS18B20)
With digital sensors, the math is handled by the silicon and the library. The DS18B20 defaults to 12-bit resolution, returning data in 0.0625°C increments. Using the OneWire and DallasTemperature libraries, the conversion is abstracted:
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
Interference, Calibration, and Failure Modes
Sensors rarely fail in isolation; the environment and the wiring usually introduce the errors. Here are the most common interference sources and how to mitigate them.
- 50/60Hz Mains Hum (Analog): High-impedance analog traces act as antennas for AC mains noise. If your TMP36 readings are jittering by ±1°C, you are picking up EMI. Fix: Keep analog wires under 30cm, use shielded twisted-pair cable, and place a 0.1µF ceramic capacitor directly across the sensor's VDD and GND pins at the sensor head, not the microcontroller end.
- 1-Wire Bus Capacitance (Digital): The DS18B20 relies on sharp voltage edges to read serial data. Long cables introduce parasitic capacitance, rounding off the square waves and causing CRC (Cyclic Redundancy Check) errors. Fix: For cable runs over 10 meters, drop the pull-up resistor from 4.7kΩ to 2.2kΩ to charge the line faster, or use an active 1-Wire master like the DS2480B.
- Self-Heating: Passing current through a sensor generates heat. A standard NTC bead thermistor can heat itself by 1°C to 2°C in still air if the excitation current is too high. Fix: For precision analog sensing, keep excitation current below 50µA, or power the sensor from a GPIO pin and only turn it on for the 10 milliseconds required to take a reading.
Decision Tree: Which Sensor Should You Buy?
Stop guessing based on whatever is in your parts bin. Use this decision matrix to select the right tool for your specific application constraints.
| Application Requirement | Recommended Sensor Type | Specific Part Number |
|---|---|---|
| Need ultra-fast thermal response (<1 second) for a soldering iron tip or 3D printer hotend. | Bare NTC Thermistor (Glass bead) | EPCOS B57560G104F (100kΩ) |
| Need to measure ambient room temperature on a simple breadboard with an Arduino Uno. | Analog Silicon Bandgap | Analog Devices TMP36GT9Z |
| Need to run a sensor cable over 5 meters to monitor a solar battery bank or outdoor weather. | Digital 1-Wire | Maxim DS18B20 (Waterproof probe) |
| Need temperature, humidity, and pressure in a single package for an ESP32 IoT dashboard. | Digital I2C Environmental | Bosch BME280 |
The Default Pick
If you do not have a strict requirement for sub-second response times or ultra-low power consumption, buy the DS18B20 Waterproof Probe (typically $4 to $6 on Adafruit or Amazon). It eliminates ADC non-linearity headaches, ignores EMI noise over long cable runs, and comes pre-sealed in a stainless steel housing that survives outdoor weather and liquid immersion. For 90% of maker and home-automation projects, it is the undisputed correct choice.






