กลับหน้าหลัก
views
Read Temperature with DS18B20 Sensor and Arduino
Last updated on

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.

DS18B20 pin diagram showing flat side view with VCC, Data, and GND pins labeled

Required Parts

ComponentQuantity
Arduino UNO R31
DS18B20 TO-921
Resistor 4.7kΩ1
Breadboard MB-1021
Jumper wires M-M, M-Fseveral
Power Adapter 9V 2Aoptional

Wiring the Circuit

Follow this straightforward wiring:

ArduinoDS18B20
Pin 2Middle pin (Data)
5VRight pin (VCC)
GNDLeft pin (GND)
5V and Pin 24.7kΩ resistor (pull-up)
Breadboard wiring diagram showing DS18B20 connected with 4.7kΩ resistor

Make sure to orient DS18B20 with the flat side facing you: left = GND, middle = Data, right = VCC.

Installing Libraries

You need 2 libraries:

  1. Open Arduino IDE → Sketch → Include Library → Manage Libraries
  2. Search and install OneWire (by Paul Stoffregen)
  3. 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.

Serial Monitor window displaying temperature readings in °C updating every second

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

จ้างทำโปรเจคเลย

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...