Menu

Recent Posts

aki-technical


Saturday, October 31, 2026

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor

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.

 

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor
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.

 

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor
Arduino simple.ino Sketch
 

However I modify this example to work with a 128x64 OLED display, SH1106. So it needs these libraries:

  1. OneWire
  2.  DallasTemperature
  3.  Wire
  4.  Adafruit_GFX
  5.  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.

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor
DS18B20 Pin Configurations
 

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:

  1. // Include the libraries we need
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>

  4. /* Data wire is plugged into
  5. port 2 on the Arduino */
  6. #define ONE_WIRE_BUS D0

  7. /* Setup a oneWire instance to communicate
  8. with any OneWire devices
  9. (not just Maxim/Dallas temperature ICs) */
  10. OneWire oneWire(ONE_WIRE_BUS);

  11. /* Pass our oneWire reference
  12. to Dallas Temperature. */
  13. DallasTemperature sensors(&oneWire);

  14. // OLED Section
  15. #include <Wire.h>
  16. #include <Adafruit_GFX.h>
  17. #include <Adafruit_SH110X.h>

  18. /* Uncomment the initialize the I2C address ,
  19. uncomment only one, If you get a totally blank
  20. screen try the other*/
  21. #define i2c_Address 0x3c
  22. /*initialize with the I2C addr 0x3D
  23. Typically Adafruit OLED's */
  24. //#define i2c_Address 0x3d

  25. #define SCREEN_WIDTH 128
  26. #define SCREEN_HEIGHT 64
  27. #define OLED_RESET -1
  28. Adafruit_SH1106G display =
  29. Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  30. /*
  31. * The setup function. We only start the sensors here
  32. */
  33. void setup(void)
  34. {
  35. // start serial port
  36. Serial.begin(9600);
  37. Serial.println("Dallas Temperature IC Control Library Demo");

  38. // Start up the library
  39. sensors.begin();

  40. delay(250); // wait for the OLED to power up
  41. display.begin(i2c_Address, true); // Address 0x3C default
  42. // Clear the buffer.
  43. display.clearDisplay();
  44. display.setTextColor(SH110X_WHITE);
  45. display.setCursor(0, 8);
  46. display.setTextSize(3);
  47. display.println("ESP8266");
  48. display.println("NodeMCU");
  49. display.display();
  50. delay(5000);
  51. display.clearDisplay();
  52. display.setCursor(10, 8);
  53. display.setTextSize(3);
  54. display.println("SH1106");
  55. display.println(" OLED");
  56. display.display();
  57. delay(5000);
  58. display.clearDisplay();

  59. display.setCursor(10, 8);
  60. display.setTextSize(3);
  61. display.println("Temp");
  62. display.println("Sensor");
  63. display.display();
  64. delay(5000);
  65. display.clearDisplay();
  66. }

  67. /*
  68. * Main function, get and show the temperature
  69. */
  70. void loop(void)
  71. {
  72. Serial.print("Requesting temperatures...");
  73. sensors.requestTemperatures();
  74. Serial.println("DONE");
  75. delay(1500);
  76. float tempC = sensors.getTempCByIndex(0);

  77. // Check if reading was successful
  78. if (tempC != DEVICE_DISCONNECTED_C)
  79. {
  80. Serial.print("Temperature for the device 1 (index 0) is: ");
  81. Serial.println(tempC);
  82. }
  83. else
  84. {
  85. Serial.println("Error: Could not read temperature data");
  86. }
  87. display.setTextColor(SH110X_WHITE);
  88. display.setCursor(16, 0);
  89. display.setTextSize(2);
  90. display.println("Temp (C):");
  91. display.setCursor(16, 32);
  92. display.setTextSize(3);
  93. display.println(tempC);
  94. display.display();
  95. delay(900);
  96. // Clear the buffer.
  97. display.clearDisplay();
  98. }

Pictures:

I use my own ESP8266 TWI Prototype Board to test this example.

 

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor
ESP8266 TWI Prototype Board

 

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor
ESP8266 TWI Prototype Board

 

ESP8266 TWI SH1106 and DS18B20 Temperature Sensor
ESP8266 TWI Prototype Board

 Video:

 


 



 

Search This Blog

Electronics and Programming Tutorials

DIY Electronic Projects And Instruments

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (40) Analog Comparator (1) Arduino (17) ARM (32) AT89C52 (7) ATMega32 (59) ATMega644P (27) AVR (88) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (5) Display (162) Distance Sensor (3) dot matrix (1) ds1307 (11) DS18B20 (7) ds3231 (2) ds3232 (2) dsPIC (16) dsPIC30F1010 (3) dsPIC30F2010 (15) EEPROM (5) Environment Sensor (6) ESP-12E (8) ESP32 (1) esp8266 (9) Graphical LCD (3) HD44780 (14) I2C (40) ILI9341 (1) Input/Output (85) Interrupt (22) IoT (3) IoT PCB (1) Keil (5) Keypad (16) KS0108 (3) LCD (92) LM35 (4) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) MikroC ARM (1) MikroC dsPIC (11) MikroC PIC (2) Module PCB (2) Motor (15) MPLABX (73) Nokia 5110 LCD (5) OLED (5) One-Wire (8) Oscillator (8) PCB (13) PCD8544 (3) PCF8574 (15) PIC (122) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (14) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PLL (1) PWM (12) RTC (16) SBN0064G (1) Sensor (15) SH1106 (7) Shift Register (15) Shift Registers (19) SN74HC595N (9) Software SPI (1) Software TWI (3) SPI (44) sprinf (3) ST7735 (1) STM32 (29) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (23) STM32CubeIDE (32) STM32F103C8T6 (9) STM32F103R6 (18) STM32F401CCU6 (1) SysTick (7) temperature sensor (15) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (16) UART (11) Ultrasonic (4) Voltmeter (7) WDT (1) Web Server (1) Wi-Fi (3) XC16 (2) XC8 (96)

aki-technical