If you need to measure temperature with an Arduino, ESP32, or Raspberry Pi Pico, the DS18B20 is the default choice for 90% of DIY and prototyping projects due to its digital noise immunity and waterproof probe options. However, if you need ultra-fast ambient air sensing on a bare PCB, the TMP36 analog sensor is the right pick, while the BME280 wins for combined environmental monitoring. Below is the exact decision framework, wiring pinouts, raw-to-Celsius math, and interference mitigation for modern microcontroller temperature sensors.
The Sensing Principle: How Solid-State and Digital Temperature Sensors Work
Analog solid-state temperature sensors (like the TMP36) rely on the predictable temperature coefficient of a base-emitter voltage (VBE) in a matched transistor pair. As the silicon die temperature rises, the VBE drops at a highly linear rate (typically -2mV/°C). Internal operational amplifiers scale and offset this thermal voltage into a usable, continuous analog voltage output that a microcontroller's ADC can read.
Digital temperature sensors (like the DS18B20 or BME280) use a similar silicon bandgap reference but pair it with an onboard sigma-delta ADC and digital logic. They convert the thermal voltage directly into a digital bitstream, transmitting it via 1-Wire, I2C, or SPI protocols. This onboard conversion eliminates analog signal degradation, meaning the digital output remains perfectly accurate regardless of wire length or minor voltage drops in the supply line.
Decision Tree: Which Temperature Sensor Should You Actually Buy?
Stop guessing based on whatever breakout board is in your parts bin. Use this decision matrix to select the exact part number for your application constraints.
| Application Constraint | Recommended Sensor | Interface | Approx. Cost (2026) |
|---|---|---|---|
| Waterproof, long cable runs, liquids | DS18B20 (Stainless Probe) | 1-Wire | $3.50 - $5.00 |
| Ambient room temp + humidity/pressure | BME280 | I2C / SPI | $4.00 - $7.00 |
| Ultra-fast response on a bare PCB | TMP36 | Analog Voltage | $1.50 - $2.50 |
| High-precision industrial (-200 to 850°C) | PT100 RTD + MAX31865 | SPI | $15.00 - $25.00 |
Wiring, Pinouts, and Power Supply Ranges
Supply voltage and pull-up requirements are where most beginners brick their sensors or get floating readings. Here is the spec-sheet data for the big three.
| Sensor | VCC Range | Pinout (Sensor to MCU) | Pull-up Resistor Required? |
|---|---|---|---|
| DS18B20 | 3.0V to 5.5V | VCC, GND, Data (DQ) | Yes (4.7kΩ on Data line to VCC) |
| TMP36 | 2.7V to 5.5V | VCC, GND, Vout | No |
| BME280 | 1.71V to 3.6V | VCC, GND, SCL, SDA | Yes (Usually included on breakout board) |
Numbered Wiring Steps: DS18B20 to ESP32 DevKit V1
- Power: Connect the DS18B20 Red wire (VCC) to the ESP32
3V3pin. Connect the Black wire (GND) to the ESP32GND. - Data: Connect the Yellow/White wire (Data) to ESP32 GPIO
4(or any available digital pin). - The Critical Pull-Up: Insert a 4.7kΩ resistor between the
3V3pin and the Data wire. Without this, the 1-Wire bus will float, and your code will return-127°Cor85°C(the power-on reset default). - Verify: Use a multimeter to measure between VCC and GND at the sensor terminals. You should read a stable 3.2V to 3.3V. If it reads lower, your wire gauge is too thin or the run is too long for parasitic power.
Output Signal Math: Converting Raw Readings to Celsius
A common failure point in embedded projects is conflating analog voltage outputs with digital register words. Here is exactly what the output is and the raw-to-unit math for both.
Analog Output: TMP36 (Voltage)
The TMP36 outputs a continuous analog voltage. The scale factor is 10mV/°C with a 500mV (0.5V) offset at 0°C. This means at 25°C, the output is 0.75V.
Raw-to-Celsius Math (10-bit Arduino Uno, 5V reference):
Temp_C = ((Raw_ADC * (5.0 / 1023.0)) - 0.5) / 0.01
Raw-to-Celsius Math (12-bit ESP32, 3.3V reference):
Temp_C = ((Raw_ADC * (3.3 / 4095.0)) - 0.5) / 0.01
Digital Output: DS18B20 (16-bit Two's Complement Word)
The DS18B20 does not output a voltage; it outputs a 16-bit digital register via the 1-Wire protocol. The resolution defaults to 12-bit, where each least significant bit (LSB) represents 0.0625°C.
Raw-to-Celsius Math:
If the sensor returns the raw hex register 0x0190 (which is 400 in decimal):
Temp_C = 400 * 0.0625 = 25.0°C
Note: In practice, you will use the OneWire and DallasTemperature Arduino libraries, which handle the bit-shifting and two's complement conversion for negative numbers automatically via the sensors.getTempCByIndex(0) function.
Calibration, Scaling, and Real-World Interference
Even with perfect math, environmental factors can skew your readings. Here is how to handle calibration and the specific interference sources that plague temperature sensors.
Calibration and Scaling Needs
- TMP36 (Analog): Requires offset calibration. The datasheet guarantees ±2°C accuracy at 25°C. To calibrate, place the sensor in a sealed bag submerged in an ice-water bath (0°C). Record the raw ADC value, calculate the voltage, and add the difference as a static offset variable in your code.
- DS18B20 (Digital): Factory-calibrated to ±0.5°C from -10°C to +85°C. No physical calibration is needed. If your specific probe reads 0.3°C high compared to a reference thermometer, simply apply a software scaling offset:
final_temp = raw_temp - 0.3. - BME280: Contains internal calibration registers burned into the silicon at the factory. The Bosch API/library reads these registers automatically to compensate the raw ADC data. Never attempt manual offset calibration on a BME280 without a certified reference chamber.
Common Interference Sources and Mitigation
Temperature sensors are highly susceptible to specific types of electrical and thermal interference. Identify your failure mode below:
| Interference Source | Affected Sensor | Symptom | Hardware / Code Fix |
|---|---|---|---|
| Switching Power Supply EMI | TMP36 (Analog) | Jittery readings, fluctuating ±2°C rapidly | Add a 0.1µF ceramic capacitor between VCC and GND as close to the sensor pins as possible. |
| Voltage Drop over Long Wires | TMP36 (Analog) | Readings are consistently higher than actual | Analog sensors draw ~50µA, but thin wires drop voltage. Switch to DS18B20 for runs > 1 meter. |
| 1-Wire Bus Capacitance | DS18B20 (Digital) | CRC errors, sensor drops off bus, returns 85°C | Lower the 1-Wire pull-up resistor to 2.2kΩ or 1kΩ for cable runs exceeding 10 meters. |
| Self-Heating / PCB Thermal Mass | BME280 / TMP36 | Ambient reads 1-3°C higher than room temp | Mount the sensor away from voltage regulators and MCUs. Use thermal relief slots in the PCB copper pour. |
By matching the sensor's output type to your microcontroller's ADC capabilities and physical environment, you eliminate the most common embedded systems debugging headaches. For further technical specifications, refer to the Analog Devices DS18B20 datasheet and the Bosch Sensortec BME280 documentation.






