A temperature sensor works by converting thermal energy into a measurable electrical change—either a varying voltage, resistance, or a digital data stream. In analog sensors like the TMP36 or LM35, a built-in solid-state PN junction exhibits a predictable voltage drop change (typically 10mV/°C) as temperature shifts. Digital sensors like the DS18B20 use a similar internal diode but pair it with an onboard ADC and 1-Wire controller to output calibrated binary data directly.

The output is never "temperature" natively; it is always an electrical proxy. For analog sensors, the output is a continuous DC voltage (or resistance in thermistors) that your microcontroller's ADC must sample and scale. For digital sensors, the output is a serialized bitstream (voltage pulses) representing the pre-calculated Celsius value. Understanding this distinction is critical because it dictates whether your microcontroller does the heavy mathematical lifting or if the sensor handles it internally.

Spec Sheet Showdown: TMP36, LM35, and DS18B20

Before wiring anything to your GPIO pins, you need to know what the output actually is and what the silicon can tolerate. Pushing 5V into an ESP32's 3.3V ADC pin from a 5V LM35 is a classic way to fry a microcontroller. The table below breaks down the real-world specifications for the three most common hobbyist and prototyping temperature sensors.

Table 1: Core Sensor Specifications and Output Types
Parameter TMP36 (Analog) LM35 (Analog) DS18B20 (Digital) 10k NTC Thermistor
Supply Voltage Range 2.7V to 5.5V 4.0V to 30V 3.0V to 5.5V N/A (Passive)
Output Signal Type DC Voltage DC Voltage Digital (1-Wire) Variable Resistance
Sensitivity / Scale 10 mV/°C 10 mV/°C 0.0625°C / bit Non-linear (Beta eq.)
Accuracy at 25°C ±1°C ±0.5°C ±0.5°C ±1°C to ±3%
Quiescent Current 50 µA 60 µA 1.5 mA (active) Depends on bias
Calibration Needed? Offset subtraction Direct scaling None (Factory trimmed) Steinhart-Hart math
Bench Tip: Notice the LM35 requires a minimum of 4.0V. If you are running an ESP32 or Raspberry Pi Pico on a 3.3V rail, the LM35 will not function correctly and will output garbage data. Use the TMP36 or DS18B20 for 3.3V native systems.

Wiring and Pinout Reference

Both the TMP36 and LM35 come in TO-92 packages that look identical to a standard 2N2222 transistor. Holding the flat face toward you with the pins pointing down, the pinout is strictly VCC, VOUT, GND. The DS18B20 shares this exact physical pinout, but its middle pin is a digital data line requiring a pull-up resistor.

Table 2: Standard TO-92 Pinout and Microcontroller Wiring
Pin (Flat face forward) TMP36 / LM35 Function DS18B20 Function Required External Components
Pin 1 (Left) VCC (Supply) VDD (Supply) 100nF decoupling cap to GND
Pin 2 (Middle) VOUT (Analog Out) DQ (Digital Data) 4.7kΩ pull-up to VCC (DS18B20 only)
Pin 3 (Right) GND GND Direct to MCU GND

Raw Reading to Physical Units: The Math You Actually Need

The most common point of failure in sensor projects is conflating the raw ADC integer with the actual physical voltage. Your microcontroller does not read volts; it reads a ratio of the input voltage against its internal reference voltage. Here is the exact math to get from a raw ADC integer to degrees Celsius.

Analog Math: TMP36 on an ESP32 (12-bit ADC, 3.3V Logic)

The TMP36 outputs 0.5V at 0°C, and increases by 10mV (0.01V) for every degree Celsius. The ESP32 features a 12-bit ADC, meaning raw readings range from 0 to 4095.

  1. Convert Raw to Voltage: Voltage = (Raw_ADC / 4095.0) * 3.3
  2. Apply TMP36 Offset & Scale: Temp_C = (Voltage - 0.5) * 100.0

Example: If the ESP32 reads a raw value of 1860, the voltage is (1860 / 4095) * 3.3 = 1.498V. The temperature is (1.498 - 0.5) * 100 = 99.8°C.

Analog Math: LM35 on an Arduino Uno (10-bit ADC, 5V Logic)

The LM35 outputs 0V at 0°C (no offset) and scales at 10mV/°C. The Arduino Uno uses a 10-bit ADC (0 to 1023) referenced to 5V.

  1. Convert Raw to Voltage: Voltage = (Raw_ADC / 1023.0) * 5.0
  2. Apply LM35 Scale: Temp_C = Voltage * 100.0

Digital Math: DS18B20 (No ADC Required)

Because the DS18B20 handles the analog-to-digital conversion internally, you bypass ADC math entirely. The sensor returns a 16-bit signed integer via the 1-Wire protocol. To get the physical unit, you simply divide the raw payload by 16.0.

  • Scale: Temp_C = Raw_16bit_Integer / 16.0

For deeper integration, reference the official Analog Devices DS18B20 datasheet for the exact 1-Wire ROM search algorithms and CRC8 validation checks.

Wiring, Interference, and Real-World Gotchas

If you wire a TMP36 directly to an ESP32 with 6-inch jumper wires on a breadboard, your serial monitor will likely show the temperature jumping erratically by 3°C to 5°C. This is not a bad sensor; it is electrical noise. Analog temperature sensors output incredibly small voltage changes (10mV per degree). A 50mV spike of noise from a nearby switching power supply or WiFi antenna translates directly into a fake 5°C temperature swing.

Common Interference Sources

  • ESP32 ADC Non-Linearity: The ESP32's internal ADC is notoriously non-linear near the 0V and 3.3V rails. Readings below 0.1V and above 3.1V will be highly inaccurate. Keep your analog sensor output in the 0.5V to 2.5V sweet spot.
  • Wire Resistance & Voltage Drop: Running an analog sensor over 3 meters of thin untwisted wire introduces resistance that drops the voltage, resulting in a permanent "cold" reading error. Digital sensors (DS18B20) do not suffer from this, as long as the 1-Wire bus capacitance doesn't exceed 400pF.
  • RF and Mains Hum: Long analog wires act as antennas, picking up 50/60Hz AC mains hum and 2.4GHz RF interference from the ESP32's own WiFi transmissions.

Step-by-Step: Building a Low-Pass RC Filter for Analog Sensors

To kill high-frequency noise before it hits your microcontroller's ADC, build a hardware low-pass filter. This is mandatory for precision analog temperature readings.

  1. Solder a 10kΩ resistor in series with the VOUT pin (Pin 2) of the TMP36.
  2. Solder a 1µF ceramic capacitor between the microcontroller side of that resistor and GND.
  3. Wire the junction of the resistor and capacitor to your ESP32/Arduino ADC pin (e.g., GPIO 34).
  4. Add a 100nF (0.1µF) decoupling capacitor directly across the VCC and GND pins of the sensor to stabilize the power rail.
ESP32 Software Fix: If you are using the modern ESP32 Arduino Core (v2.x or v3.x), stop using analogRead(). Instead, use analogReadMilliVolts(). This function utilizes the ESP32's internal eFuse calibration data to correct the ADC non-linearity in hardware, returning a highly accurate millivolt reading directly and bypassing the manual 4095/3.3V math entirely. See the Espressif ADC Oneshot Documentation for implementation details.

Choosing the Right Sensor for Your Next Build

Selecting between analog and digital isn't just about math preference; it's about the physical environment of your project. Here is a practical decision framework based on real-world deployment constraints.

Table 3: Deployment Decision Matrix
Scenario Recommended Sensor Why It Wins Here
Outdoor/Weather Station DS18B20 (Waterproof Probe) Digital signal ignores long cable voltage drops; epoxy sealed.
Multi-Point Array (10+ nodes) DS18B20 1-Wire protocol allows dozens of sensors on a single GPIO pin via unique 64-bit ROM addresses.
High-Speed Thermal Logging TMP36 / LM35 Analog output is continuous; digital sensors require 750ms conversion delays at 12-bit resolution.
Simple 5V Arduino Classroom Kit LM35 Zero offset math (0V = 0°C) makes it easier for beginners to grasp basic ADC scaling.

For a comprehensive look at the analog front-end design and thermal mass considerations, review the Texas Instruments LM35/LM35A datasheet, which details how the physical packaging (TO-92 vs. TO-46) drastically alters the sensor's thermal time constant in moving air versus still air.

Ultimately, how a temp sensor works on your bench is defined by how well you manage the electrical proxy it outputs. Treat analog sensors as fragile voltage sources that need filtering and precise ADC references, and treat digital sensors as network nodes that need proper bus termination. Master the raw-to-unit math and the noise mitigation, and your thermal data will be rock solid.