How Do You Interface a Digital Temperature Sensor?
Last updated 27 June 2026 · 9 min read
Direct Answer
A digital temperature sensor outputs temperature as a digital value over a communication bus, eliminating the need for an external ADC or resistance-to-temperature conversion. The DS18B20 uses the 1-Wire protocol: a single GPIO pin handles both bus power and bidirectional data. Up to 64 DS18B20 sensors share one wire, each addressed by a unique 64-bit ROM code. Resolution is configurable from 9 to 12 bits (0.5°C to 0.0625°C). Conversion time: 93 ms at 9-bit, 750 ms at 12-bit. The MCP9808 uses I2C (up to 400 kHz), reports 13-bit signed temperature data, includes a configurable alert pin for threshold events, and achieves ±0.25°C accuracy (typical) across −40°C to +125°C.
Detailed Explanation
Digital temperature sensors integrate the measurement circuit, ADC, and calibration data in a single IC, reducing design complexity compared to an NTC thermistor voltage divider. The two most widely used types in embedded design are the DS18B20 (1-Wire bus) and the MCP9808 (I2C bus).
Analog vs Digital Temperature Sensing
Before selecting a digital sensor, understand the trade-offs against an NTC thermistor circuit:
| NTC Thermistor | DS18B20 | MCP9808 | |
|---|---|---|---|
| Interface | Analog (ADC) | 1-Wire | I2C |
| MCU ADC required | Yes | No | No |
| External components | Divider resistor | 4.7 kΩ pullup | 4.7 kΩ pullup |
| Accuracy | ±1–2°C (without cal.) | ±0.5°C (−10 to +85°C) | ±0.25°C (typ) |
| Range | −55°C to +150°C | −55°C to +125°C | −40°C to +125°C |
| Multi-sensor on one bus | No | Yes (64 devices) | Yes (up to 8 via A0–A2) |
| Conversion time | Immediate (ADC) | 93–750 ms | 250 ms typical |
| Cost | < A$0.10 | ~A$1–2 | ~A$2–4 |
Use a digital temperature sensor when: accuracy matters more than cost; you have no free ADC channels; you need multiple sensors on one wire; or you need a configurable alert interrupt.
For industrial applications requiring ±0.1–0.5°C accuracy over a wide range (−200°C to +850°C), platinum RTD sensors such as the PT100 and PT1000 are the standard choice — but they require an analog excitation source, Wheatstone bridge, and instrumentation amplifier rather than a simple digital bus connection.
DS18B20 — 1-Wire Protocol
Hardware Interface
The DS18B20 requires only two connections in powered mode (three if you count VDD):
MCU GPIO (e.g. PA5) ──── 4.7 kΩ ──── VCC (3.3 V or 5 V)
│
DS18B20 DQ pin
DS18B20 VDD pin ──── VCC
DS18B20 GND pin ──── GND
The 4.7 kΩ pullup resistor is mandatory — the 1-Wire bus is open-drain and the pullup defines the idle high state.
Parasite power mode (VDD tied to GND, two wires only): possible but not recommended for new designs — see FAQ above.
1-Wire Protocol Basics
1-Wire is a serial protocol that encodes bits using precise timing on the single data line. The master (MCU) initiates all communication; the slave (DS18B20) responds by pulling the line low.
Key timing sequences:
- Initialization: Master pulls line low for 480–960 µs (reset pulse), releases; sensor pulls low for 60–240 µs (presence pulse)
- Write 1: Master pulls low < 15 µs, releases; line recovers to high for remainder of 60 µs slot
- Write 0: Master pulls low for 60–120 µs
- Read: Master pulls low < 15 µs; sensor holds low (bit 0) or releases (bit 1) — master samples at 15 µs
These timing requirements mean you must either use a hardware UART (which can implement 1-Wire timing in oversampled UART mode) or bit-bang with precise microsecond delays. Most MCU platforms have a OneWire library that handles this automatically.
Reading Temperature from DS18B20
Typical firmware sequence (simplified):
// 1. Issue Reset, then Skip ROM (0xCC) to address all sensors
onewire_reset();
onewire_write(0xCC); // Skip ROM
// 2. Start temperature conversion
onewire_write(0x44); // Convert T command
// 3. Wait for conversion (750 ms for 12-bit)
delay_ms(750);
// 4. Issue Reset, Skip ROM again, then Read Scratchpad (0xBE)
onewire_reset();
onewire_write(0xCC);
onewire_write(0xBE);
// 5. Read 9 bytes of scratchpad data
for (int i = 0; i < 9; i++) {
scratchpad[i] = onewire_read();
}
// 6. Extract temperature from bytes 0 and 1 (little-endian 16-bit signed)
int16_t raw = (scratchpad[1] << 8) | scratchpad[0];
float temp_C = raw / 16.0f; // For 12-bit resolution (0.0625°C per LSB)
The 9-byte scratchpad includes: temperature LSB, temperature MSB, TH and TL alarm registers, configuration register, and CRC. Always verify the CRC (last byte) to confirm data integrity — especially important for long cable runs.
DS18B20 Resolution Configuration
The configuration register's R1:R0 bits set resolution:
| R1:R0 | Resolution | Conversion time |
|---|---|---|
| 00 | 9-bit (0.5°C) | 93.75 ms |
| 01 | 10-bit (0.25°C) | 187.5 ms |
| 10 | 11-bit (0.125°C) | 375 ms |
| 11 | 12-bit (0.0625°C) | 750 ms |
For temperature monitoring with 1°C reporting resolution, 9-bit is sufficient and 8× faster than 12-bit.
MCP9808 — I2C Interface
The MCP9808 from Microchip is a ±0.25°C accuracy digital temperature sensor using a standard I2C bus. It is well-suited to applications requiring tighter accuracy than the DS18B20's ±0.5°C.
Hardware Interface
MCU SCL ──── SCL (MCP9808)
MCU SDA ──── SDA (MCP9808)
VDD ──── 2.7 V to 5.5 V (typically 3.3 V)
GND ──── GND
A0, A1, A2 ──── GND or VDD (set I2C address)
ALERT (open-drain) ──── 10 kΩ to VDD ──── MCU GPIO input
I2C pull-up resistors (4.7 kΩ to 3.3 V) are required on SCL and SDA — typically already present if other I2C devices are on the bus. See what is I2C? for pull-up sizing details.
I2C Address
The MCP9808 has a 7-bit base address of 0x18, with address bits A2:A1:A0 set by pins. This allows up to 8 MCP9808 devices on a single I2C bus, which is sufficient for multi-zone thermal monitoring in a product enclosure.
| A2 | A1 | A0 | 7-bit address |
|---|---|---|---|
| 0 | 0 | 0 | 0x18 |
| 0 | 0 | 1 | 0x19 |
| ... | ... | ||
| 1 | 1 | 1 | 0x1F |
Reading Temperature
The ambient temperature register is at register address 0x05. It contains a 13-bit signed value in units of 0.0625°C per LSB, with flag bits for threshold violations.
// Read 2 bytes from register 0x05
i2c_write(MCP9808_ADDR, 0x05); // Set register pointer
i2c_read(MCP9808_ADDR, buf, 2); // Read MSB, LSB
// Extract temperature
uint8_t msb = buf[0] & 0x1F; // Mask flag bits [7:5]
uint8_t lsb = buf[1];
int16_t raw;
if (msb & 0x10) { // Sign bit set (negative temperature)
raw = 0xF000 | (msb << 8) | lsb;
} else {
raw = (msb << 8) | lsb;
}
float temp_C = raw * 0.0625f;
Alert Pin for Interrupt-Driven Temperature Monitoring
Connect the ALERT pin to a GPIO interrupt input on the MCU. Program threshold registers:
// Set TUPPER = 40.0°C, TLOWER = 20.0°C
i2c_write_reg(MCP9808_ADDR, 0x02, 0x02, 0x80); // TUPPER = 40°C
i2c_write_reg(MCP9808_ADDR, 0x03, 0x01, 0x40); // TLOWER = 20°C
// Enable alert output in CONFIG register (reg 0x01), enable alert output
i2c_write_reg(MCP9808_ADDR, 0x01, 0x00, 0x08); // ALERT output enabled
Configure the MCU GPIO as an interrupt input on the falling edge (ALERT is active-low). The ISR reads the temperature register to clear the alert latch and records the event. This pattern keeps the MCU in a low-power sleep state and wakes it only when the temperature leaves the acceptable range — ideal for battery-powered products.
Other Digital Temperature Sensors
TMP117 (TI, I2C): ±0.1°C accuracy (−20°C to +50°C), 16-bit resolution, one-shot mode for low power. Used in medical and HVAC applications where MCP9808 accuracy is insufficient.
HDC1080 / SHT40 (I2C): Combined temperature and humidity sensors. The SHT40 from Sensirion achieves ±0.2°C / ±1.8% RH accuracy. Used in environmental monitoring, weather stations, HVAC.
MAX31855 (SPI): Dedicated thermocouple-to-digital converter. Connects directly to a K-type thermocouple, performs cold-junction compensation internally, outputs temperature over SPI (14-bit, 0.25°C resolution). Used in high-temperature applications (300–1350°C range) such as reflow oven control, combustion monitoring.
MAX31865 (SPI): Dedicated RTD (PT100/PT1000)-to-digital converter. Handles excitation, ratiometric ADC, and fault detection internally; returns the RTD resistance code over SPI. The equivalent of the MAX31855 for platinum RTDs rather than thermocouples. See using the MAX31865 with a PT100 for wiring, register configuration, and temperature conversion.
For the signal conditioning and ADC interface considerations that apply when analog sensors feed into MCU ADC inputs, the digital sensor approach eliminates those concerns entirely — another reason to prefer digital ICs when accuracy and PCB simplicity matter more than component cost.
For thermal management design in a product, including sensor selection, placement, and alert threshold configuration, Zeus Design's electronics engineering team provides full sensor integration and firmware development support.
Design Considerations
- Place the sensor away from heat sources: PCB traces carrying significant current, voltage regulators, and power transistors all radiate heat that will bias the temperature reading. Mount the DS18B20 or MCP9808 on a separate section of PCB, or on a short extension cable, to measure the ambient environment rather than the board's self-heating.
- Add a 100 nF decoupling capacitor close to VDD: Digital temperature sensor ICs draw brief current spikes during I2C or 1-Wire communication. A 100 nF ceramic capacitor directly at the VDD pin prevents voltage dips on the power supply from causing spurious readings or communication errors.
- Validate 1-Wire CRC in firmware: Long cables, inductive loads nearby, and EMI can corrupt 1-Wire data. The DS18B20 CRC byte (byte 8 of the scratchpad) detects single-bit errors. Always check the CRC and retry the read if it fails rather than accepting corrupted temperature data.
Common Mistakes
- Wrong 1-Wire pullup resistor: The DS18B20 datasheet specifies 4.7 kΩ as the nominal pullup. A 10 kΩ resistor is too weak for buses longer than ~20 cm, causing intermittent communication failures that are difficult to diagnose. A 1 kΩ resistor creates excessive loading on the line. Use 4.7 kΩ as the default.
- Using 12-bit resolution when 9-bit suffices: Every polling cycle takes 750 ms at 12-bit resolution. For a thermostat controller reporting temperature once per second, this is fine. But if the firmware loop must run at 100 Hz for other reasons, 750 ms blocking wait causes missed cycles. Use 9-bit (93 ms) for coarse monitoring, or start the conversion asynchronously and read the result in the next loop iteration.
- Forgetting I2C address conflicts: The MCP9808's default address 0x18 can conflict with other I2C devices — check the full I2C device map for your design before deciding whether to use the address pins. If 0x18 is occupied, set A0=1 to move to 0x19.
Frequently Asked Questions
- What is parasite power mode in DS18B20 and should I use it?
- In parasite power mode, the DS18B20 draws its operating current from the data line itself — the VDD pin is tied to GND. This saves a wire (only two connections needed: GND and data), but has limitations. During temperature conversion, the DS18B20 needs up to 1.5 mA peak current. In parasite mode, the bus master must hold the data line strongly high (strong pullup, not just a weak resistor) during the conversion period. Some microcontroller 1-Wire libraries do not implement strong pullup correctly for parasite power. Multiple sensors in parasite mode on one bus can cause conversion errors if the combined current exceeds the pullup transistor's drive strength. For most designs with only one or two sensors, using a dedicated 3.3 V or 5 V supply to VDD is simpler, more reliable, and avoids these complications.
- How do I read multiple DS18B20 sensors on one wire?
- Each DS18B20 has a unique 64-bit ROM code (laser-programmed at manufacture). To read multiple sensors: (1) send the Search ROM command to enumerate all ROM codes on the bus; (2) issue a Match ROM command followed by the specific sensor's ROM code to address one device; (3) then issue the Convert T and Read Scratchpad commands to get that sensor's temperature. Alternatively, issue Convert T to all devices simultaneously (Skip ROM + Convert T), wait the full 750 ms for 12-bit conversion, then read each sensor individually using Match ROM. The DS18B20's datasheet and the OneWire library for Arduino implement this protocol. For more than 5–8 sensors on a single bus, verify that the parasitic current sum doesn't exceed your pullup supply capability — 3.3 kΩ to 4.7 kΩ pullup to 3.3 V is standard for up to 8 sensors.
- How does the MCP9808 alert pin work?
- The MCP9808 has a configurable ALERT output pin (open-drain, requires a pullup resistor to MCU logic level). The chip compares its measured temperature against three programmable threshold registers: TUPPER, TLOWER, and TCRIT. You can configure ALERT to fire when: temperature rises above TUPPER; drops below TLOWER; or exceeds TCRIT (a non-maskable critical threshold). In comparison mode (default), ALERT goes active-low when any threshold is violated and returns high when the temperature moves back inside the window (with hysteresis if set). In interrupt mode, ALERT latches on violation and requires a register read to clear. This alert function enables temperature-triggered interrupts on an MCU GPIO pin, allowing a low-power embedded system to sleep and only wake when temperature deviates from a normal range — more efficient than polling the I2C register every second.
References
Related Questions
What Is a Thermistor and How Do You Use One?
Covers NTC thermistor voltage divider circuits, Steinhart-Hart equation, self-heating error, ADC interface, and selection for temperature measurement.
How Do PT100 and RTD Temperature Sensors Work?
Covers PT100/PT1000 RTD sensors: 2-wire, 3-wire, and 4-wire connections, excitation current, Wheatstone bridge conditioning, and when to choose an RTD.
How Do You Use the MAX31865 RTD-to-Digital Converter with a PT100 Sensor?
Learn how to wire a PT100 RTD to the MAX31865, configure its SPI registers for 2-, 3-, or 4-wire connections, and convert the ADC reading to temperature.
Sensor Signal Conditioning Basics
Covers signal conditioning for sensors: op-amp gain, offset, 4-20mA interface, anti-aliasing filter design, ADC input protection, and two-point calibration.
What Is I2C (Inter-Integrated Circuit)?
I2C is a two-wire serial bus for addressing multiple peripherals over shared SDA/SCL lines. Learn how addressing, speed grades, and pull-up resistors work.
What Is a Microcontroller (MCU)?
A microcontroller (MCU) combines a CPU, flash, RAM, and peripherals on one chip. Learn how MCUs work and how they differ from microprocessors and FPGAs.
Related Forum Discussions
STM32 GPIO interrupt configured but ISR never fires — what am I missing?
Trying to use a button on PA0 to trigger an interrupt on an STM32F411 Nucleo board. Using HAL, generated the init code with CubeMX. The GPIO
Can't decide between FreeRTOS and bare-metal for a simple sensor node — what's the tipping point?
Working on a temperature and humidity monitoring node — STM32F103 target, BME280 over I2C, reports data every 60 seconds over UART to a Rasp