BeginnerSensors
DS18B20 Digital Thermometer
Read a digital one-wire temperature sensor and print the result to the serial monitor.
Parts from the database
- DS18B20+ × 11-Wire temperature sensor
- ATmega328P-PU × 1Use on a 5 V Arduino-compatible development board
Other supplies
- 5 V Arduino-compatible development boardCommon build supply
- 4.7 kΩ resistorCommon build supply
- breadboardCommon build supply
- USB cableCommon build supply
- jumper wiresCommon build supply
Amazon links may be affiliate links. As an Amazon Associate, Electronics Hobby Hub earns from qualifying purchases.
Connections
- With the flat face and leads toward you, confirm the exact sensor package pinout in its datasheet before wiring.
- For the common TO-92 DS18B20, connect GND to ground, VDD to 5 V and DQ to D2.
- Add a 4.7 kΩ pull-up from DQ to VDD. Do not rely on the MCU's internal pull-up for this bus.
Build steps
- Install OneWire and DallasTemperature libraries.
- Wire the sensor and pull-up resistor with power disconnected.
- Upload the sketch and view output at 9600 baud.
- Warm the sensor gently between fingers and verify that readings rise gradually.
Software and firmware setup
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2); DallasTemperature sensors(&oneWire);
void setup(){ Serial.begin(9600); sensors.begin(); }
void loop(){ sensors.requestTemperatures(); float c=sensors.getTempCByIndex(0);
if(c==DEVICE_DISCONNECTED_C) Serial.println("Sensor not found");
else Serial.printf("%.2f C\n",c); delay(1000); }Test your build
A detected sensor should return a plausible room temperature. Give it time to reach equilibrium; touching it changes its temperature slowly.
Troubleshooting
- If the library reports -127 °C, check the pin order and 4.7 kΩ pull-up.
- Long leads may need a stronger bus layout and careful grounding.
- For a waterproof probe, verify its cable colors with the seller's wiring diagram; colors are not universal.
Safety and limitations
Use only low voltage. A waterproof probe is not automatically food-safe or rated for immersion in every liquid.
Manufacturer and platform documentation
Community Notes for This Project
These are unverified visitor contributions, separate from the build guide and manufacturer specifications. Verify technical advice against the linked datasheets before relying on it.
Set up the comments service to load community notes.