The Sensing Principle: Bandgaps and Silicon
At the heart of almost every modern electronic temp sensor is the silicon PN junction bandgap. As the temperature of a silicon chip rises, the forward voltage drop across a specialized transistor junction decreases at a highly predictable rate—typically around -2mV per degree Celsius. This physical phenomenon provides a reliable, linear baseline for measuring thermal energy without the mechanical fragility of liquid-in-glass thermometers or the non-linear resistance curves of basic thermistors.
Manufacturers split this sensing principle into two distinct architectural camps. Analog sensors (like the TMP36) amplify this millivolt-level bandgap shift and output it directly as a continuous voltage. Digital sensors (like the DS18B20) integrate a precision analog-to-digital converter (ADC) and a logic controller directly onto the same silicon die, converting the bandgap shift into a discrete digital data packet before it ever leaves the chip.
Output Signals: Analog Voltage vs. Digital 1-Wire
Understanding what the output actually is dictates how you wire and code your microcontroller. You cannot treat these two categories interchangeably.
Analog Voltage Output (e.g., TMP36, LM35)
The Output: A continuous DC voltage, typically scaled to 10mV/°C. The LM35 outputs 0V at 0°C, while the TMP36 includes a 500mV offset, outputting 0.5V at 0°C to allow for negative temperature readings on single-supply systems.
Calibration: These are factory-trimmed and require no user calibration for the sensor itself. However, the microcontroller's ADC requires scaling. The ESP32's ADC is notoriously non-linear below 0.1V and above 3.1V, which will introduce severe errors if you are measuring extreme temperatures without software offset tuning.
Interference: Analog outputs are highly susceptible to Electromagnetic Interference (EMI) and voltage drop. Running a TMP36 on a 10-foot unshielded cable near AC mains will turn the wire into an antenna, injecting 50/60Hz noise into your ADC readings.
Digital 1-Wire Output (e.g., DS18B20)
The Output: A digital data packet transmitted over a single shared data line using Maxim's 1-Wire protocol. The output is a 16-bit two's complement hexadecimal value representing the temperature.
Calibration: Factory calibrated with internal digital compensation. The microcontroller simply reads the final calculated value; no ADC scaling math is required.
Interference: Immune to analog EMI and voltage drop over long runs. However, 1-Wire is highly sensitive to parasitic capacitance. If your wire is too long or you omit the mandatory 4.7kΩ pull-up resistor, the signal edges become too slow, causing the microcontroller's strict timing windows to miss the data bits entirely.
Wiring and Pinout Reference
Below is the hardware specification table for the most common embedded temperature sensors. Always verify the logic level of your microcontroller before applying power.
| Sensor Model | Interface | Supply Range (VDD) | Logic Level | Pinout (Flat face towards you) |
|---|---|---|---|---|
| TMP36GZ | Analog Voltage | 2.7V to 5.5V | Ratiometric to VDD | 1: VDD, 2: VOUT, 3: GND |
| LM35DZ | Analog Voltage | 4.0V to 30V | Ratiometric to VDD | 1: VDD, 2: VOUT, 3: GND |
| DS18B20+ | Digital 1-Wire | 3.0V to 5.5V | 3.3V or 5V tolerant | 1: GND, 2: DATA (DQ), 3: VDD |
| BME280 | I2C / SPI | 1.71V to 3.6V | 3.3V strict (Do not use 5V) | Varies by breakout board |
Raw-to-Unit Math: Converting Readings to Celsius
A sensor is useless if you cannot convert the raw microcontroller reading into a physical unit. Here is the exact C++ math required for both architectures.
Analog Math (TMP36 on ESP32)
The ESP32 features a 12-bit ADC (0-4095) and operates at a 3.3V logic level. The TMP36 outputs 10mV per degree Celsius with a 500mV (0.5V) offset at 0°C.
// ESP32 12-bit ADC reading on GPIO 34
int adc_raw = analogRead(34);
// Step 1: Convert raw ADC to Voltage (assuming 3.3V reference)
float voltage = (adc_raw * 3.3) / 4095.0;
// Step 2: Apply TMP36 offset and scale to Celsius
float tempC = (voltage - 0.5) * 100.0;
// Step 3: Convert to Fahrenheit (optional)
float tempF = (tempC * 9.0 / 5.0) + 32.0;
Digital Math (DS18B20 1-Wire)
The DS18B20 handles the ADC conversion internally. It returns a 16-bit signed integer. The resolution is configurable, but at the default 12-bit resolution, each bit represents 0.0625°C.
// Assuming 'raw_16bit' is the signed integer read from the 1-Wire scratchpad
int16_t raw_16bit = 0x0550; // Example: 85°C power-on reset value
// Step 1: Divide by 16.0 to get floating point Celsius
float tempC = raw_16bit / 16.0;
// Note: If the sensor is in 9-bit mode, divide by 2.0.
// In 10-bit mode, divide by 4.0. In 11-bit mode, divide by 8.0.
Decision Tree: Which Electronic Temp Sensor to Buy
Stop guessing in the parts aisle. Use this decision matrix to select the exact component for your build constraints.
| Project Constraint | If True... | Recommended Sensor |
|---|---|---|
| Wire run is longer than 3 feet (1 meter) | Analog voltage drop and EMI will ruin your data. | DS18B20 (Digital) |
| You need to measure humidity and pressure too | Temperature alone isn't enough for environmental logging. | BME280 (I2C) |
| Sensor must be submerged in liquid or buried in soil | Exposed TO-92 silicon will short and corrode. | DS18B20 Waterproof Probe |
| You have zero digital pins left, only an analog pin | You are forced to use an ADC reading. | TMP36 (Analog) |
| Measuring extreme heat (>125°C) near a boiler | Silicon bandgap sensors will fail or drift heavily. | Type-K Thermocouple + MAX6675 |
Step-by-Step Interfacing for the Winning Pick (DS18B20)
Here is how to properly wire and code the DS18B20 on an ESP32, avoiding the most common pitfall: the missing pull-up resistor.
Hardware Wiring Steps
- De-energize the circuit. Disconnect the ESP32 from USB or battery power before wiring.
- Connect Power: Wire the DS18B20 Red wire (VDD) to the ESP32 3V3 pin.
- Connect Ground: Wire the DS18B20 Black wire (GND) to the ESP32 GND pin.
- Connect Data: Wire the DS18B20 Yellow/White wire (DATA) to ESP32 GPIO 4.
- Install the Pull-Up: Insert a 4.7kΩ resistor between the 3V3 pin and the DATA pin (GPIO 4). Do not skip this. The 1-Wire protocol uses open-drain logic; without the pull-up, the line will float and return -127°C errors.
Firmware Implementation (Arduino IDE)
Install the OneWire and DallasTemperature libraries via the Arduino Library Manager. Flash the following code:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into GPIO 4 on the ESP32
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
// Force 12-bit resolution for maximum precision (0.0625°C steps)
sensors.setResolution(12);
}
void loop() {
sensors.requestTemperatures(); // Send the command to get temperatures
// Read the first sensor found on the bus
float tempC = sensors.getTempCByIndex(0);
// Check for disconnected sensor error code
if(tempC == DEVICE_DISCONNECTED_C) {
Serial.println('Error: Sensor disconnected or missing pull-up resistor');
} else {
Serial.print('Temperature: ');
Serial.print(tempC);
Serial.println(' °C');
}
delay(1000); // 1-second polling rate (750ms required for 12-bit conversion)
}
By standardizing on the digital 1-Wire architecture and applying the correct pull-up hardware, you bypass the ESP32's analog hardware flaws entirely, resulting in rock-solid thermal data for your embedded projects.






