HomeArduino TutorialsArduino DS18B20 Temperature Sensor Tutorial: Single and Multiple Sensors with SSD1306 OLED

Interfacing DS18B20 Temperature Sensor with Arduino (Single and Multiple Sensors)

The DS18B20 is a very popular digital temperature sensor for Arduino projects. It is small, accurate, and easy to use in both indoor and outdoor applications. The sensor works on the One-Wire protocol, which means you can read temperature using just one data pin. This makes it perfect for projects where you want to connect many sensors on the same line.

In this tutorial, you will learn how to interface a single DS18B20 sensor with Arduino, and then expand it to multiple sensors on the same data pin. We will cover the sensor pinout, wiring, and complete Arduino code. The temperature data will be printed on the Serial Monitor and also displayed on a 0.96-inch SSD1306 I2C OLED screen. If you are new to OLED displays, you can check my earlier guide on Arduino SSD1306 OLED Display Tutorial for more details.

Interfacing DS18B20 Temperature Sensor with Arduino (Single and Multiple Sensors)

What is the DS18B20 Temperature Sensor

The DS18B20 is a digital temperature sensor used in many Arduino and IoT projects. It provides accurate temperature readings without the need for complex circuits. The sensor comes in a small TO-92 package, and it can also be found in a waterproof version for outdoor use. Because it uses digital communication, the readings stay stable even over long wires. This makes the DS18B20 a reliable choice for hobby and professional projects.

Image showing different types of DS18B20 sensor Modules.

DS18B20 Features and Benefits

The DS18B20 is a versatile and reliable temperature sensor, ideal for both hobbyist and industrial applications. Its digital output, flexible wiring options, and accurate measurements make it a popular choice for projects that require precise temperature monitoring.

Key features of the DS18B20:

  • Easy to expand and integrate into systems like weather stations, data loggers, and industrial monitors
  • Measures temperatures from –55°C to +125°C with good accuracy
  • Provides digital output, reducing noise and interference
  • Supports 9 to 12-bit resolution for adjustable precision
  • One-Wire interface allows multiple sensors on a single data line
  • Can operate with standard 3-wire connection or parasite power mode

How DS18B20 Works with the One-Wire Protocol

The DS18B20 communicates with Arduino using the One-Wire protocol. In this protocol, a single data line handles both communication and sensor identification. Each DS18B20 has a unique 64-bit address stored inside it. When multiple sensors share the same data line, the Arduino can ask each sensor for its temperature using this unique address.

The One-Wire protocol sends data in small time-based pulses. The Arduino controls when to read or write these pulses. The sensor then responds with the temperature data in digital format. Because the protocol uses only one pin, it keeps the wiring simple, even when several sensors are connected together. This method is efficient and perfect for projects where you need many temperature readings with minimum wiring.

DS18B20 Pinout Explained

The DS18B20 temperature sensor uses a simple 3-pin interface, which makes it extremely easy to connect with an Arduino. It communicates using the One-Wire protocol, meaning only one data pin is needed for communication. The other two pins are for power and ground.

Image showing pinout of DS18B20 sensor. 3 pins are shown with VCC, GND and Data.

Here’s the pin description of the DS18B20 sensor:

Pin NameFunctionDescription
VDDPower SupplyConnects to 3.3V or 5V from the Arduino (depending on wiring mode).
DQData LineOne-Wire data pin used for communication. Requires a 4.7kΩ pull-up resistor to VCC.
GNDGroundConnects to the Arduino ground.

The DATA pin is the heart of the DS18B20. It carries all communication between the sensor and the Arduino. This pin works on the One-Wire protocol, so it handles sending and receiving data through a single line.

To make the DATA pin work correctly, you must place a 4.7kΩ pull-up resistor between the DATA pin and the VCC pin. This resistor keeps the data line stable and prevents false signals. Without this pull-up resistor, the Arduino may fail to detect the sensor or read wrong temperatures. The same rule applies whether you use one sensor or many sensors on the same line.

Interfacing a Single DS18B20 Sensor with Arduino

Connecting a single DS18B20 to an Arduino is simple because the sensor uses only one data line. In this part, you will learn how to wire the sensor, upload the Arduino code, and display the temperature on both the Serial Monitor and the SSD1306 OLED display.

Connection Diagram for One DS18B20

The DS18B20 connects to Arduino using only three wires. The image below shows how to connect the sensor to Arduino UNO.

Image shows the connection between DS18B20 sensor and Arduino uno. They are connected using one wire. Also connect resistor between data and VCC.

The data pin must have a 4.7kΩ pull-up resistor for stable communication. Below is the wiring table for a single DS18B20 sensor. This single resistor ensures proper communication. If you skip this step, the sensor may not respond or may show random readings.

DS18B20 PinArduino PinDescription
VDD5V or 3.3VPowers the sensor depending on module type.
DQ (Data)Digital Pin 2Used for One-Wire data. Connect a 4.7kΩ resistor between DQ and VCC.
GNDGNDCommon ground connection.

Required Libraries for DS18B20

To read temperature from the DS18B20 sensor, you need two Arduino libraries:

  • OneWire
  • DallasTemperature

These libraries make the One-Wire communication easy. They also provide simple functions to get temperature directly from the sensor. Therfore you do not need to handle any low-level timing or protocol code.

Follow these steps to install them:

  1. Open the Arduino IDE.
  2. Go to Sketch → Include Library → Manage Libraries…
  3. In the search bar, type “OneWire” and install it.
  4. Then search “DallasTemperature” and install it as well.
Install one wire library on Arduino IDE
Install Dalla Temperature library on Arduino IDE

After installing both libraries, your Arduino is ready to read temperature from a DS18B20.


Arduino Code to Read Temperature from One DS18B20

Below is the code to read the temperature from one DS18B20 sensor using the One-Wire protocol.

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

#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);
}

Code Explanation:

1. Including Libraries

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

We include the OneWire library to handle One-Wire communication. And the DallasTemperature library to read temperature easily from the DS18B20 sensor.

2. Defining the Data Pin

#define ONE_WIRE_BUS 2

The DS18B20 data pin is connected to Arduino digital pin 2. This macro helps us use the pin easily throughout the code.

3. Creating OneWire and DallasTemperature Objects

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

The first line creates a OneWire object on pin 2. The second line passes this OneWire object to the DallasTemperature library so it can talk to the sensor.

4. Setup Function

void setup() {
  Serial.begin(9600);
  sensors.begin();
}

We start the Serial Monitor at 9600 baud rate. We also initialize the DS18B20 sensor using sensors.begin().

5. Loop Function

void loop() {
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

We ask the sensor to take a new temperature reading. Then we read the temperature in Celsius from the first sensor on the bus.

6. Printing the Reading

  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" °C");

We print the temperature value on the Serial Monitor.

7. Delay Between Readings

  delay(1000);
}

Display Temperature on Serial Monitor

After uploading the code, open the Arduino Serial Monitor. Set the baud rate to 9600. You will see the temperature value update automatically.

The image below shows the Temperature readings printed on the serial monitor of Arduino IDE.

Image showing the temperature data obtaibned from the DS18B20 sensor is printed on the Arduino Serial console.

The DS18B20 provides a stable reading, so the temperature should not fluctuate rapidly. If you see errors or “-127°C”, it usually means there is a wiring issue.

Display Temperature on SSD1306 OLED (I2C)

You can also show the temperature from the DS18B20 sensor on a 0.96″ SSD1306 OLED display. This I2C display uses only two wires, which makes it easy to connect and perfect for real-time temperature monitoring. In this section, we will use the Adafruit SSD1306 library to print the temperature clearly on the screen.

If you want to get more information about how to interafce the 0.96″ Oled display with Arduino, check out this guide: Interface 0.96″ SSD1306 Oled with Arduino.

Connection Diagram for SSD1306 OLED Display

The 0.96″ SSD1306 OLED uses the I2C interface, which means it only needs two data wires along with power and ground. The wiring is simple and works with most Arduino boards, including the Arduino UNO.

Image showing the connection between DS18B20, 0.96" Oled and Arduino UNO.

Make sure the display is powered correctly. Most SSD1306 OLED modules work with both 3.3V and 5V, but some versions are 3.3V only. Always check your module printing before powering it.

Below is the wiring table for the SSD1306 I2C OLED display:

OLED PinArduino PinDescription
VCC3.3VPowers the OLED display (depends on module).
GNDGNDCommon ground connection.
SDAA4I2C data line for communication.
SCLA5I2C clock line for communication.

Installing Adafruit SSD1306 and GFX Libraries

To begin, open your Arduino IDE and follow these steps:

  1. Go to the Sketch menu → Include LibraryManage Libraries…
  2. In the Library Manager, type “SSD1306” in the search bar.
  3. Find Adafruit SSD1306 by Adafruit and click Install.
  4. Next, search for “Adafruit GFX” and install Adafruit GFX Library as well.
Install Adafruit SSD1306 library on Arduino
Install Adafruit GFX library on Arduino

Arduino Code to Display Temperature on 0.96″ Oled

Here is the OLED version of the code:

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

#define ONE_WIRE_BUS 2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}

void loop() {
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.print("Temp: ");
  display.print(tempC);
  display.print(" C");
  display.display();

  delay(1000);
}

Code Explanation:

This example reads temperature from one DS18B20 sensor and shows it on a 0.96″ SSD1306 OLED display using I2C.

1. Including Required Libraries

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
  • OneWire.h is used for One-Wire communication with the DS18B20.
  • DallasTemperature.h makes temperature reading easy.
  • Adafruit_GFX.h provides basic graphics functions for the OLED.
  • Adafruit_SSD1306.h is the main library for the SSD1306 OLED display.

2. Defining Pins and Screen Size

#define ONE_WIRE_BUS 2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
  • The DS18B20 data pin is connected to D2.
  • The OLED has a resolution of 128×64 pixels.

3. Creating Display and Sensor Objects

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
  • The first line creates the OLED display object using I2C.
  • The OneWire object is created on pin 2.
  • The DallasTemperature object uses this OneWire object to talk to the sensor.

4. Setup Function

void setup() {
  Serial.begin(9600);
  sensors.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}
  • Serial Monitor starts at 9600 baud.
  • The DS18B20 sensor is initialized.
  • The OLED display starts with I2C address 0x3C.
  • The screen is cleared before writing anything.

5. Reading Temperature

sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
  • We request a fresh temperature reading.
  • Then we read the Celsius value from the first sensor.

6. Displaying Temperature on OLED

display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.print("Temp: ");
display.print(tempC);
display.print(" C");
display.display();
  • Clear the screen before updating.
  • Set text size to 2 for easy visibility.
  • Set text color to white.
  • Move the cursor to start writing text.
  • Print temperature on the OLED.
  • display.display() updates the screen.

7. Delay Between Updates

delay(1000);
  • Wait 1 second before taking the next reading.

Output on the 0.96″ Oled

Once the code is uploaded successfully, the oled should print the Temperature value as shown in the image below.

Image showing the temperature data, acquired from DS18B20, is printed on the Oled display.

This value will update every second.


Interfacing Multiple DS18B20 Sensors with Arduino

Connecting multiple DS18B20 temperature sensors with Arduino is easy because all sensors share the same data line. Each sensor has a unique 64-bit address. This allows the Arduino to read temperatures from all connected sensors one by one. In this part, we will see the connection, Arduino code, Serial Monitor output, and how to show multiple readings on a 0.96″ SSD1306 I2C OLED display.

If you want to get more information about how to interafce the 0.96″ Oled display with Arduino, check out this guide: Interface 0.96″ SSD1306 Oled with Arduino.

Connection Diagram for Multiple Sensors

When using multiple DS18B20 sensors, all sensors connect in parallel:

DS18B20 PinArduino PinDescription
VDD5V or 3.3VPower for all sensors (connected together).
DQ (Data)Digital Pin 2Shared by all sensors. Use a 4.7kΩ pull-up resistor between DQ and VCC.
GNDGNDGround for all sensors (connected together).

The image below shows how Three DS18B20 sensors are connected in parallel on the same line and are powered by the same supply.

Image showing multiple DS18B20 sensors are connected using same pin to the Arduino.

Just connect the VCC pins from All the sensors together, GND pins together and data pins together. Then connect the combined pins to the Arduino.
This wiring supports 2, 5, or even 10 sensors on the same data line.

How Multiple DS18B20 Sensors Share One Data Line

All DS18B20 sensors use the One-Wire protocol. This lets many sensors work on the same wire without conflict. Each sensor has a unique ROM address. The Arduino reads each address and then fetches the temperature for that specific sensor.

This makes wiring simple, reduces pin usage, and works well for projects like room monitoring or multi-point temperature tracking.


Arduino Code for Multiple DS18B20 Sensors

Here is the code to read temperatures from multiple DS18B20 sensors and display all readings on the OLED screen:

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

#define ONE_WIRE_BUS 2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}

void loop() {
  sensors.requestTemperatures();
  int deviceCount = sensors.getDeviceCount();

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  for (int i = 0; i < deviceCount; i++) {
    float tempC = sensors.getTempCByIndex(i);

    // Print on serial
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(tempC);
    Serial.println(" °C");

    // Print on OLED
    display.print("S");
    display.print(i);
    display.print(": ");
    display.print(tempC);
    display.println(" C");
  }

  display.display();
  Serial.println("------------------");

  delay(1000);
}

Code Explanation for Multiple DS18B20 Sensors with OLED Display

1. Including All Required Libraries

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
  • OneWire.h handles the One-Wire communication.
  • DallasTemperature.h makes reading multiple DS18B20 sensors easy.
  • Adafruit_GFX.h provides basic graphics functions.
  • Adafruit_SSD1306.h controls the OLED display.

2. Defining Pin and OLED Screen Size

#define ONE_WIRE_BUS 2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
  • The DS18B20 sensors are connected to Arduino Pin 2.
  • The OLED display resolution is 128 x 64 pixels.

3. Creating Sensor and Display Objects

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
  • Creates the OLED display object using the I2C interface.
  • Creates a One-Wire object for communication on pin 2.
  • The DallasTemperature object uses the One-Wire bus to detect and read multiple sensors.

4. Setup Function

void setup() {
  Serial.begin(9600);
  sensors.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}
  • Starts the Serial Monitor at 9600 baud rate.
  • Detects and initializes all DS18B20 sensors connected to the bus.
  • Initializes the OLED display at I2C address 0x3C.
  • Clears the display before writing text.

5. Requesting Temperatures and Counting Sensors

sensors.requestTemperatures();
int deviceCount = sensors.getDeviceCount();
  • Requests fresh readings from all attached sensors.
  • Gets the total number of DS18B20 sensors detected.

6. Preparing the OLED Display

display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
  • Clears the screen.
  • Sets the text size to 1 so all readings fit.
  • Sets the text color to white.
  • Positions the cursor at the top-left corner.

7. Looping Through All Sensors

for (int i = 0; i < deviceCount; i++) {
    float tempC = sensors.getTempCByIndex(i);
  • Loops through each DS18B20 sensor.
  • Reads the temperature of sensor number i.

8. Printing Values on Serial Monitor

Serial.print("Sensor ");
Serial.print(i);
Serial.print(": ");
Serial.print(tempC);
Serial.println(" °C");
  • Prints the sensor number and its temperature.
  • Helps in debugging and monitoring values in real time.

9. Printing Values on the OLED Display

display.print("S");
display.print(i);
display.print(": ");
display.print(tempC);
display.println(" C");
  • Shows each sensor’s reading on the OLED.
  • Each line displays something like:
    S0: 25.4 C

10. Updating the OLED Screen

display.display();
  • Renders the new content on the screen.

Delay and Separator

Serial.println("------------------");
delay(1000);
  • Prints a separator line for better readability.
  • Waits 1 second before taking the next reading.

Output of multiple sensors on the 0.96″ Oled

Once the code is uploaded successfully, the oled should print the Temperature values from all the sensors as shown in the image below.

Image showing the temperature values, acquired from multiple DS18B20 sensors, is printed on the 0.96"mSSD1306 Oled display.

I have placed three sensors inside three cups, each containing water at a different temperature. One cup has hot water, another has ice-cold water, and the third contains water at normal room temperature. As you can see, the OLED display is showing the temperature readings from all three sensors.

The display updates every second, which makes it perfect for real-time monitoring.

Troubleshooting DS18B20 with Arduino

Even though the DS18B20 is easy to use, you may face a few common problems while testing your project. Most issues are related to wiring, missing resistors, or loose connections. In this section, you will learn how to identify and fix the most common DS18B20 errors when working with Arduino.

Common Wiring Issues

Most problems come from incorrect wiring. The DS18B20 has three pins, and mixing them up will stop the sensor from working. Make sure the middle pin is the DATA pin, the right pin goes to VCC, and the left pin goes to GND.

Another common issue is a missing or incorrect pull-up resistor. The DATA line must have a 4.7kΩ resistor connected between DATA and VCC. If the resistor is not connected, the sensor may show random values or fail to respond.

Also check for loose jumper wires or poor breadboard contact. A small loose connection can break communication and cause unstable readings.


Sensor Not Detected

If your Serial Monitor shows -127°C, it means the Arduino cannot detect the sensor.
This usually happens due to one of these reasons:

  • Wrong wiring
  • DATA pin not connected properly
  • Missing 4.7kΩ pull-up resistor
  • Using a very long cable without proper grounding
  • Bad sensor (rare)

Double-check your connections carefully. If you are using multiple sensors, make sure all sensors share the same GND.

If the sensor is still not detected, try switching to another digital pin or using shorter wires during testing.


Wrong Temperature Reading

If the sensor is detected but the values are incorrect, the issue is usually related to power or long wire length.

Here are some common fixes:

  • Ensure the sensor is powered with a stable 5V or 3.3V
  • If using long cables, avoid thin wires that cause voltage drop
  • Avoid placing the sensor near heat sources or power components
  • For waterproof DS18B20 probes, make sure the metal tip is not touching electrical parts or metal surfaces

Sometimes the sensor takes a moment to stabilize after powering up. Give it a few seconds before taking final readings.

If you see values jumping suddenly or dropping to unrealistic levels, check the pull-up resistor. A weak or missing resistor causes unstable data.

Conclusion

In this tutorial, we explored how to interface one or multiple DS18B20 temperature sensors with Arduino and display real-time readings on an SSD1306 OLED. We discussed wiring the sensor correctly with a pull-up resistor, identifying each sensor using the One-Wire bus, and using the DallasTemperature library to handle temperature requests easily. We also learned how to show the results on an OLED screen, making the entire setup more user-friendly and visually clear.

This project is highly useful for building practical temperature-monitoring systems such as home automation, HVAC monitoring, greenhouses, aquariums, and multi-point temperature logging. By supporting multiple DS18B20 sensors on a single pin, the solution becomes scalable and efficient, especially when you want to monitor different locations simultaneously. With simple wiring and reliable accuracy, this approach gives you a solid foundation for creating advanced Arduino-based IoT or data-logging applications.

Browse More Arduino Tutorials

1 2 3

Arduino DS18B20 Project Download

Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Arduino DS18B20 FAQs

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments