If you are searching for an arduino temp sensor, the direct answer depends on your physical environment: use the DS18B20 for liquids and pipes, the DHT22 for ambient air and humidity, and the TMP36 for low-cost analog classroom demos. For 95% of general-purpose maker projects, the default pick is the waterproof DS18B20 due to its digital accuracy, long cable runs, and open-drain bus architecture.

Below is a complete decision framework, a step-by-step build guide for the DS18B20 on an Arduino Uno R3, production-ready C++ code with error handling, and a debugging checklist for when the serial monitor throws errors.

The Arduino Temp Sensor Decision Matrix

Do not guess which sensor to buy. Use this decision tree to select the exact part number based on your environmental constraints and bus requirements.

Criteria DS18B20 (Digital) DHT22 / AM2302 (Digital) TMP36 (Analog) BME280 (I2C)
Best For Liquids, pipes, outdoor weather stations Indoor ambient air, HVAC monitoring Basic classroom demos, simple analog reads High-precision environmental chambers
Protocol OneWire (1 digital pin) Single-bus proprietary (1 digital pin) Analog Voltage (1 analog pin) I2C (SDA/SCL)
Accuracy ±0.5°C (from -10°C to +85°C) ±0.5°C ±1°C to ±2°C ±1.0°C
Resolution 0.0625°C (12-bit) 0.1°C ~0.5°C (depends on ADC) 0.01°C
Waterproof? Yes (stainless steel probe variants) No (exposed die, ruins if wet) No (TO-92 plastic package) No (breakout board)
Approx Cost (2026) $3.00 - $5.00 $4.50 - $6.00 $1.50 - $2.00 $7.00 - $10.00
Decision Path Verdict: If your sensor will touch water, soil, or a metal pipe → Buy the DS18B20. If you need relative humidity alongside air temp → Buy the BME280 (skip the DHT22 in 2026; the BME280 is faster, more stable, and uses standard I2C). If you just need to read a voltage on an analog pin for a school assignment → Buy the TMP36.

Project Build: Waterproof DS18B20 on Arduino Uno R3

We will build the most common and robust configuration: a waterproof DS18B20 probe connected to an Arduino Uno R3. This setup uses the OneWire protocol, which requires a specific pull-up resistor to function correctly.

Parts List

  • Microcontroller: Arduino Uno R3 (ATmega328P, 5V logic)
  • Sensor: DS18B20 Waterproof Probe (Maxim/Analog Devices or reputable clone with pre-attached 1m cable)
  • Resistor: 4.7kΩ (472) through-hole resistor, 1/4W
  • Hardware: Half-size breadboard, male-to-male jumper wires

Pin Mapping Table

DS18B20 Wire Color Function Arduino Uno R3 Pin Notes
Red VCC (Power) 5V Do not use 3.3V; the Uno is a 5V board.
Black GND (Ground) GND Must share common ground with the Uno.
Yellow (or White) Data (OneWire) Digital Pin 2 Requires 4.7kΩ pull-up to 5V.

Wiring Steps

  1. Connect Power: Insert the red wire into the 5V rail and the black wire into the GND rail on your breadboard. Connect these to the corresponding 5V and GND pins on the Uno.
  2. Connect Data: Insert the yellow (data) wire into a breadboard row. Run a jumper wire from this row to Digital Pin 2 on the Arduino.
  3. Install the Pull-Up Resistor: This is where most beginners fail. The OneWire bus is open-drain. Insert one leg of the 4.7kΩ resistor into the same row as the yellow data wire, and the other leg into the 5V rail. This pulls the bus HIGH when the sensor is not actively pulling it LOW.
  4. Install Libraries: In the Arduino IDE, go to Sketch > Include Library > Manage Libraries. Search for and install OneWire (by Paul Stoffregen) and DallasTemperature (by Miles Burton).

Complete Compilable Code (Target: Arduino Uno R3)

The following C++ code is fully compilable for the Arduino Uno R3 (AVR architecture). It includes explicit pin definitions, sets the sensor to maximum 12-bit resolution, and implements error handling to catch disconnected sensors rather than logging garbage data.

#include <OneWire.h>
#include <DallasTemperature.h>

// Pin definitions - change if wiring to a different digital pin
#define ONE_WIRE_BUS 2
#define SENSOR_RESOLUTION 12 // 9 to 12 bits. 12-bit = 0.0625°C resolution

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature library
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing DS18B20...");
  
  sensors.begin();
  
  // Verify at least one sensor is on the bus
  int deviceCount = sensors.getDeviceCount();
  if (deviceCount == 0) {
    Serial.println("FATAL: No DS18B20 sensors found on Pin 2. Check wiring.");
    while(1); // Halt execution
  }
  
  Serial.print("Found ");
  Serial.print(deviceCount);
  Serial.println(" sensors.");
  
  // Set resolution for the first sensor on the bus
  sensors.setResolution(0, SENSOR_RESOLUTION);
}

void loop() {
  // Command all sensors on the bus to perform a temperature conversion
  sensors.requestTemperatures();
  
  // Read temperature from the first sensor (index 0)
  float tempC = sensors.getTempCByIndex(0);
  
  // Error handling: DEVICE_DISCONNECTED_C is defined as -127.0
  if (tempC <= DEVICE_DISCONNECTED_C) {
    Serial.println("ERROR: Sensor disconnected, shorted, or missing pull-up resistor.");
  } else {
    float tempF = tempC * 9.0 / 5.0 + 32.0;
    Serial.print("Temp: ");
    Serial.print(tempC, 2);
    Serial.print(" °C  |  ");
    Serial.print(tempF, 2);
    Serial.println(" °F");
  }
  
  // Wait 1 second. DS18B20 takes ~750ms for a 12-bit conversion.
  delay(1000);
}

Debugging: First Three Things to Check When It Fails

When your serial monitor outputs garbage, halts, or throws compiler errors, follow this ranked troubleshooting path. These are the exact failure modes encountered on the bench.

1. Serial Monitor Prints: -127.00 °C or ERROR: Sensor disconnected

  • Root Cause: The Arduino is communicating on the bus, but the sensor is not responding with valid data. The DallasTemperature library returns -127.0 (defined as DEVICE_DISCONNECTED_C) when the CRC check fails or the bus stays HIGH.
  • Fix: Check the 4.7kΩ pull-up resistor. If it is missing, the data line floats, causing CRC failures. Second, check for a broken wire inside the probe's stainless steel casing (common in cheap clones if bent sharply).

2. Serial Monitor Prints: FATAL: No DS18B20 sensors found on Pin 2

  • Root Cause: The OneWire library scanned the bus and found zero ROM addresses.
  • Fix: You have likely swapped the Data and VCC wires, or the Data wire is plugged into the wrong pin (e.g., Pin 3 instead of Pin 2). Verify the yellow wire is on Digital Pin 2 and the #define ONE_WIRE_BUS 2 matches your physical wiring.

3. Compiler Throws: fatal error: OneWire.h: No such file or directory

  • Root Cause: The required libraries are not installed in your Arduino IDE environment.
  • Fix: Open the Library Manager (Ctrl+Shift+I or Cmd+Shift+I), search for "OneWire" and install Paul Stoffregen's version. Then search for "DallasTemperature" and install Miles Burton's version. Restart the IDE if the error persists.
Clone Warning: If you bought a 10-pack of DS18B20 sensors for $8 online, be aware that many are counterfeit chips. They often fail the OneWire CRC check at higher temperatures or when the cable exceeds 2 meters. For critical applications, source genuine Analog Devices (formerly Maxim) chips from authorized distributors like Digi-Key or Mouser.

Extending and Simplifying the Build

How to Extend: Daisy-Chaining Multiple Sensors

The OneWire protocol allows you to connect up to 127 DS18B20 sensors on a single digital pin. You do not need multiple pins. Simply wire the red, black, and yellow wires of all sensors in parallel. You only need one single 4.7kΩ pull-up resistor for the entire bus, regardless of how many sensors you add (up to about 10-15 sensors; beyond that, bus capacitance degrades the signal and you may need to drop the resistor to 2.2kΩ).

To read them, use sensors.getTempCByIndex(1), getTempCByIndex(2), etc. For production deployments, read the unique 64-bit ROM address of each sensor using the oneWire.search() function so you can map specific physical probes to specific variables, as index numbers can shift on reboot.

How to Simplify: Switching to Analog (TMP36)

If you are out of digital pins, or you are working with a microcontroller that lacks the processing overhead for OneWire timing (like some basic ATTiny cores without optimized libraries), strip the build back to a TMP36 analog sensor.

Wire the TMP36 VCC to 5V, GND to GND, and the middle Vout pin to Analog Pin A0. No pull-up resistor is needed. Replace the loop code with this simplified math:

int reading = analogRead(A0);
float voltage = reading * (5.0 / 1023.0);
float tempC = (voltage - 0.5) * 100.0;
Serial.println(tempC);

Note: The TMP36 resolution is limited by the Uno's 10-bit ADC. At 5V, each ADC step is ~4.88mV. Since the TMP36 outputs 10mV/°C, your effective resolution is roughly 0.5°C, and it is highly susceptible to USB voltage noise. Use a 0.1µF ceramic capacitor between VCC and GND as close to the sensor as possible to stabilize the analog read.

For further reading on OneWire protocol timing and bus capacitance limits, refer to Paul Stoffregen's OneWire documentation. For comparing environmental sensors like the DHT series and BME280, review Adafruit's DHT sensor guide.