The Sensing Principle: How Silicon Measures Heat
Bandgap temperature sensors like the DS18B20 and BME280 rely on the predictable voltage drop across a forward-biased silicon PN junction. As the silicon lattice heats up, the bandgap energy shifts, altering the base-emitter voltage ($V_{BE}$) of an internal transistor at a highly linear rate of roughly -2mV/°C. This physical property allows the silicon die to measure its own junction temperature with high repeatability.
Instead of outputting this raw analog millivolt shift, modern digital sensors integrate an onboard ADC and logic block. The chip digitizes the $V_{BE}$ delta against an internal reference and packages it into a data register. This means the microcontroller never deals with analog noise directly; it simply reads a digital word representing the sensor temperature over a serial bus, shifting the burden of signal integrity from hardware filtering to software parsing.
Wiring and Pinout: DS18B20 vs BME280
Both sensors output digital data, but they use fundamentally different physical layers. The DS18B20 uses Maxim's 1-Wire protocol (a single bidirectional data line), while the BME280 uses I2C or SPI. Below is the hardware specification and wiring matrix for integrating these with a standard 3.3V ESP32 DevKit.
| Parameter | Maxim DS18B20+ (TO-92 / Probe) | Bosch BME280 (I2C Breakout) |
|---|---|---|
| Supply Range (VDD/VIN) | 3.0V to 5.5V | 1.8V to 3.6V (Strict 3.3V logic) |
| Interface Protocol | 1-Wire (Half-duplex) | I2C (up to 3.4MHz) / SPI |
| ESP32 Pin Mapping | DQ → GPIO 4 (Any GPIO) | SCK → GPIO 22, SDI → GPIO 21 |
| Mandatory Passives | 4.7kΩ pull-up on DQ to VDD | 4.7kΩ pull-ups on SDA/SCL (usually on breakout) |
| Active Conversion Current | 1.0 mA (during 12-bit conversion) | ~700 µA (at 1Hz sampling) |
Output Signal Math: Raw Bytes to Degrees Celsius
The output of both sensors is strictly digital—specifically, multi-byte integers representing fractional degrees. Converting these raw registers into physical units requires specific bit-shifting and scaling math.
DS18B20: 16-Bit Signed Integer Math
The DS18B20 outputs temperature as a 16-bit signed, two's complement integer. At the default 12-bit resolution, the least significant bit (LSB) represents 0.0625°C.
- Read the Scratchpad: Read bytes 0 (LSB) and 1 (MSB) from the sensor's 9-byte scratchpad.
- Combine Bytes: Shift the MSB left by 8 bits and OR it with the LSB to form a signed 16-bit integer.
int16_t raw = (msb << 8) | lsb; - Scale to Celsius: Multiply the raw integer by the resolution step.
float temp_c = raw * 0.0625;
Worked Example: If the sensor reads 0x01 0x90 (MSB = 0x01, LSB = 0x90), the combined 16-bit integer is 0x0190 (400 in decimal). Multiplying 400 by 0.0625 yields exactly 25.0°C. For sub-zero temperatures, the two's complement handles the sign automatically. A reading of 0xFF5E equals -162 in decimal; -162 * 0.0625 = -10.125°C.
BME280: 20-Bit ADC and OTP Compensation
The BME280 is more complex. It outputs a 20-bit unsigned integer across three registers (0xFA to 0xFC). However, this raw ADC count is not a temperature. It must be scaled using factory-programmed OTP (One-Time Programmable) calibration words stored in registers 0x88 to 0x9F (dig_T1, dig_T2, dig_T3).
The Bosch compensation algorithm requires 32-bit integer math to prevent floating-point overhead on smaller microcontrollers. The core scaling step looks like this:
int32_t var1 = ((((adc_T >> 3) - ((int32_t)dig_T1 << 1))) * ((int32_t)dig_T2)) >> 11;
int32_t var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) * ((adc_T >> 4) - ((int32_t)dig_T1))) >> 12) * ((int32_t)dig_T3)) >> 14;
int32_t t_fine = var1 + var2;
float temp_c = (t_fine * 5 + 128) >> 8; // Final physical unit in Celsius
If you skip reading the OTP calibration registers at boot, your sensor temperature output will be garbage data. Always use a proven library like Adafruit's BME280 or the ESP-IDF I2C driver to handle this math.
Interference, Noise, and Calibration Realities
Digital sensors eliminate analog voltage sag, but they introduce new failure modes. Here are the three most common interference sources on the bench and how to mitigate them.
- Thermal Coupling and Self-Heating: The ESP32's onboard LDO and WiFi PA (Power Amplifier) generate significant heat. If your sensor is mounted less than 5cm from the ESP32 antenna or voltage regulator, you will measure the PCB's thermal mass, not the room. Fix: Mount the sensor on a 4-wire JST pigtail at least 10cm away from the MCU, or put the ESP32 to sleep between sensor reads to drop the baseline ambient.
- 1-Wire Parasitic Power Sag: The DS18B20 supports a "parasitic power" mode where it draws its 1mA conversion current directly from the data line's pull-up resistor. On cable runs longer than 1 meter, the wire resistance causes the data line voltage to sag below the logic threshold during the 750ms conversion window, resulting in CRC errors or the sensor locking up. Fix: Always wire the VDD pin to a dedicated 3.3V rail for runs over 1 meter; do not use parasitic power.
- The 85°C Power-On Reset Trap: When a DS18B20 powers up, its scratchpad defaults to
0x0550, which translates to exactly 85.0°C. If your code reads the sensor before the first 750ms conversion completes, it will log a false 85°C spike. Fix: Implement a software filter that rejects any reading of exactly 85.0°C, or enforce a blocking delay on boot. - I2C Bus Capacitance: The BME280 relies on sharp I2C square waves. Long breadboard jumper wires add parasitic capacitance, rounding the clock edges and causing the ESP32's I2C peripheral to throw
ESP_ERR_TIMEOUT. Keep I2C traces under 30cm and ensure 4.7kΩ pull-ups are present.
The Decision Tree: Which Sensor Should You Buy?
Stop guessing based on whichever breakout board is cheapest on AliExpress. Use this decision matrix to select the correct part for your specific physical environment.
| Application Scenario | Required Traits | Winner |
|---|---|---|
| Measuring liquid temps (water tanks, brewing, hydroponics) | Waterproof, long cable runs, galvanic isolation from MCU | DS18B20 (Waterproof Probe) |
| Indoor HVAC monitoring, weather stations, server racks | Fast I2C polling, low self-heating, multi-variable (humidity/pressure) | BME280 (I2C Breakout) |
| Multi-point thermal mapping (e.g., battery pack cells) | Addressable on a single bus without I2C address conflicts | DS18B20 (Unique 64-bit ROM) |
| Battery-powered remote node (deep sleep) | Ultra-fast conversion, minimal quiescent current | BME280 (1-wire conversion takes 750ms; BME takes <100ms) |






