The DS18B20 is a digital thermometer able to converts surrounding temperature between -55°C and +125°C. It operates between 3.0VDC to 5.5VDC which is suitable for hobbyist and industrial applications. It implements the One-Wire digital communication protocol, requires only one bi-directional I/O pin for data and command transmission or reception. Its ROM Search feature allow multiple DS18X20 sensors to connect on a single wire bus.
![]() |
| Temperature Reading via ESP8266 D0 Pin |
Commanding and decoding data from this sensor is easy. The manufacturers offer a lot of example programs written in Assembly or C language. For Arduino users, there are a lot of libraries available, created by various authors.
Using Arduino IDE, we can use this sensor from scratch by installing some related libraries. I use the OneWire and DallasTemperature library. Because its C++ function is easily readable and short. Go into the DallasTemprature and open the simple.ino file. To test this sensor and see the result over serial port.
![]() |
| Arduino simple.ino Sketch |
However I modify this example to work with a 128x64 OLED display, SH1106. So it needs these libraries:
- OneWire
- DallasTemperature
- Wire
- Adafruit_GFX
- Adafruit_SH110X
In this example, the ESP-12E read temperature data from a single DS18B20 connects to D0 pin. The result will show on the SH1106 OLED display.
There are many device packages for this sensor. For hobbyists, it's suitable to use a transistor-like TO-92 package. However, there are some Arduino modules for this sensor available at very low cost. That's below 1USD.
Its DQ or data pin is open-drain. So we need to add a weak pull up resistor between 4.7kΩ and 10kΩ depending on its range.
ESP8266 Arduino Source Code:
- // Include the libraries we need
- #include <OneWire.h>
- #include <DallasTemperature.h>
- /* Data wire is plugged into
- port 2 on the Arduino */
- #define ONE_WIRE_BUS D0
- /* Setup a oneWire instance to communicate
- with any OneWire devices
- (not just Maxim/Dallas temperature ICs) */
- OneWire oneWire(ONE_WIRE_BUS);
- /* Pass our oneWire reference
- to Dallas Temperature. */
- DallasTemperature sensors(&oneWire);
- // OLED Section
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SH110X.h>
- /* Uncomment the initialize the I2C address ,
- uncomment only one, If you get a totally blank
- screen try the other*/
- #define i2c_Address 0x3c
- /*initialize with the I2C addr 0x3D
- Typically Adafruit OLED's */
- //#define i2c_Address 0x3d
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 64
- #define OLED_RESET -1
- Adafruit_SH1106G display =
- Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- /*
- * The setup function. We only start the sensors here
- */
- void setup(void)
- {
- // start serial port
- Serial.begin(9600);
- Serial.println("Dallas Temperature IC Control Library Demo");
- // Start up the library
- sensors.begin();
- delay(250); // wait for the OLED to power up
- display.begin(i2c_Address, true); // Address 0x3C default
- // Clear the buffer.
- display.clearDisplay();
-
- display.setTextColor(SH110X_WHITE);
- display.setCursor(0, 8);
- display.setTextSize(3);
- display.println("ESP8266");
- display.println("NodeMCU");
- display.display();
- delay(5000);
- display.clearDisplay();
-
- display.setCursor(10, 8);
- display.setTextSize(3);
- display.println("SH1106");
- display.println(" OLED");
- display.display();
- delay(5000);
- display.clearDisplay();
- display.setCursor(10, 8);
- display.setTextSize(3);
- display.println("Temp");
- display.println("Sensor");
- display.display();
- delay(5000);
- display.clearDisplay();
- }
- /*
- * Main function, get and show the temperature
- */
- void loop(void)
- {
-
- Serial.print("Requesting temperatures...");
- sensors.requestTemperatures();
- Serial.println("DONE");
- delay(1500);
-
- float tempC = sensors.getTempCByIndex(0);
- // Check if reading was successful
- if (tempC != DEVICE_DISCONNECTED_C)
- {
- Serial.print("Temperature for the device 1 (index 0) is: ");
- Serial.println(tempC);
- }
- else
- {
- Serial.println("Error: Could not read temperature data");
- }
- display.setTextColor(SH110X_WHITE);
- display.setCursor(16, 0);
- display.setTextSize(2);
- display.println("Temp (C):");
- display.setCursor(16, 32);
- display.setTextSize(3);
- display.println(tempC);
- display.display();
- delay(900);
- // Clear the buffer.
- display.clearDisplay();
-
- }
Pictures:
I use my own ESP8266 TWI Prototype Board to test this example.
![]() |
| ESP8266 TWI Prototype Board |
![]() |
| ESP8266 TWI Prototype Board |
![]() |
| ESP8266 TWI Prototype Board |
Video:





