Read Temperature with DS18B20 Sensor and Arduino
Read Temperature with DS18B20 Sensor and Arduino
DS18B20 is a temperature sensor that uses the 1-Wire protocol. It’s reasonably accurate and comes in a waterproof version, making it suitable for measuring water temperature or outdoor environments.
Required Parts
| Component | Quantity |
|---|---|
| Arduino UNO R3 | 1 |
| DS18B20 TO-92 | 1 |
| Resistor 4.7kΩ | 1 |
| Breadboard MB-102 | 1 |
| Jumper wires M-M, M-F | several |
| Power Adapter 9V 2A | optional |
Wiring the Circuit
Follow this straightforward wiring:
| Arduino | DS18B20 |
|---|---|
| Pin 2 | Middle pin (Data) |
| 5V | Right pin (VCC) |
| GND | Left pin (GND) |
| 5V and Pin 2 | 4.7kΩ resistor (pull-up) |
Make sure to orient DS18B20 with the flat side facing you: left = GND, middle = Data, right = VCC.
Installing Libraries
You need 2 libraries:
- Open Arduino IDE → Sketch → Include Library → Manage Libraries
- Search and install OneWire (by Paul Stoffregen)
- Search and install DallasTemperature (by Miles Burton)
Code for Reading Temperature
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 connected to Arduino pin 2
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" °C");
delay(1000);
}
Upload this code to your board, then open Serial Monitor (Tools → Serial Monitor) and set baud rate to 9600.
If wired correctly, you’ll see temperature readings every second. Try blowing warm air near the sensor—the value should increase.
Reference Video
Search for “Arduino DS18B20 tutorial” on YouTube if you want to see the wiring process in action.
Summary
- DS18B20 wiring: Data → Pin 2, VCC → 5V, GND → GND
- Add 4.7kΩ resistor between Data and VCC
- Install OneWire + DallasTemperature libraries
- Read temperature with
getTempCByIndex(0)
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย