If you want to measure the temperature of a liquid, a freezer, or a machine, a standard DHT11 sensor won't survive. You need something rugged and waterproof.
Enter the DS18B20. It’s an industry-standard digital sensor that can measure from -55°C to +125°C using just one single data wire.
Why is it special? (1-Wire Protocol)
Unlike analog sensors (like the LM35) where the voltage drops over long cables, the DS18B20 sends digital data. This means you can run a 10-meter cable and still get perfect accuracy.
Plus, every single sensor has a unique 64-bit ID. You can connect 10 sensors to the SAME pin on your Arduino, and read them all individually!
Wiring: Don't Forget the Resistor!
This is the #1 mistake beginners make. The DS18B20 requires a Pull-Up Resistor on the data line to work.
- Red Wire (VCC): Connect to 5V or 3.3V.
- Black Wire (GND): Connect to GND.
- Yellow Wire (Data): Connect to Digital Pin (e.g., D4).
Crucial Step: Connect a 4.7kΩ Resistor between the YELLOW wire and the RED wire. Without this, your Arduino will just read "0.00" or "-127".
The Code
You will need two libraries: `OneWire` and `DallasTemperature`. Here is the basic sketch to read the temperature:
#include <DallasTemperature.h>
// Data wire is connected to Arduino Pin 4
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(void) {
Serial.begin(9600);
sensors.begin();
}
void loop(void) {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(tempC);
delay(1000);
}
Building an IoT Device?
Connecting a sensor to an Arduino is step one. Connecting it to the Cloud securely is step two. Let us help you design your custom IoT PCB.
Get IoT Design Quote →