Intro: Digital Thermometer on OLED Display Using ESP8266 ESP-12E NodeMCU and DS18B20 Temperature Sensor
In my previous instructable I gave an introduction to ESP8266, and programming it with our familiar Arduino IDE.
In this instructable I will go a step further, interfacing a digital temperature sensor DS18B20 and displaying the temperature in degrees and Fahrenheit on a cheap tiny OLED display (in SPI connection mode).
**NodeMCU Dev Board is based on widely explored esp8266 System on Chip from Expressif. It combined features of WIFI accesspoint and station + microcontroller and uses simple LUA based programming language. Recently, more easier and familiar arduino IDE is being widely explored for programming ESP8266.
**The DS18B20 Measures Temperatures from -55°C to +125°C (-67°F to +257°F) with ±0.5°C Accuracy from -10°C to +85°C. A fairly reasonable temperature range for day to day applications and weather system.
This temp sensor is available at around 2USD on aliexpress.
Step 1: A Bit About DS18B20 Digital Temperature Sensor IC
DS18B20 is a temperature sensor IC capable of measuring temperature and giving direct digital signal i.e. unlike other temperature sensors such as LM35, it can send temperature reading directly to any microprocessor or microcontroller or a personal computer.
**Measures Temperatures from -55°C to +125°C (-67°F to +257°F) ±0.5°C Accuracy from -10°C to +85°C
**The precision of measurements can be programmed between 9-bit to 12-bit digital signals (with compromise of reading time).
**It has an alarm function with nonvolatile user-programmable upper and lower trigger points. i.e. it can be programmed to trigger an external alarm or thermostat to indicate or control respectively, any external appliance when temperature reaches certain lower or higher threshold set-points. These set-points remain in its memory even when the sensor is powered off.
**It communicates over a 1-Wire® bus, that by definition requires only one data line (and ground) for communication with a microprocessor or microcontroller.
**In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.
**If we want to run using external power supply, it can be operated between +3.0 V to +5.5V. This makes it compatible with 3.3V ICs such as Arduino Due, esp8266 etc AND ALSO 5V ICs such as Arduino Uno,Nano etc.
**Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area.
**No external circuitry is required, except in some cases a 10k pull-up resistor is required.
Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.
Step 2: DS18B20 Pin Connections
DS18B20 comes in different forms, packages. It comes as IC or on ebay,aliexpress etc we can find very cheap encapsulated versions. The one I have used is with steel encapsulation ordered from aliexpress.com (links given on my blog and and the end of this instructable)
**If you are using TO-92 packaging i.e. Bare IC form shown in attached image, the pin out is - Left-Gnd, Middle-DQ or Data, Right-VCC (I have coded those lines as Black, Yellow, Red respectively to match the wire coding of encapsulated form)
**In general the black wire is ground line, red is +ve VCC (+3.0 to +5.5V) and Yellow line is the DQ or data line.In our case red wire will go to +3.3V of NodeMCU or an external power supply with regulated 3.3V.
**A pull up resistor of approx 4.7K Ohm (any other value near 4.7K can be used) is connected between yellow-DQ wire and +VCC (sometimes, it is not required)
**DQ or Data (yellow) wire should go to our microcontroller i.e. our NodeMCU digital input pin. in our case it is connected to D1 or GPIO 5 [Note- The pin mapping of NodeMCU board and actual ESP8266 IC are different, please refer my previous instructable here for details of pin mapping between NodeMCU, ESP8266 and Arduino IDE]
Another important note - avoid connecting DQ or Data line to D0 pin of NodeMCU, I noticed that it gives error while programming/uploading code if we connect our DQ line with external pull-up at D0 pin/GPIO16.
Step 3: Interfacing OLED Display (in SPI Mode) With NodeMCU
Initially, i struggled a lot to make my tiny OLED display work with NodeMCU. After a lot of struggle finally found the correct pin connections as shown in the figure.
Note- i tried several times to use my favourite U8GLIB library for OLED display, but failed. Finally i read somewhere that there is some issue with U8GLIB library interfacing with ESP8266 using arduino IDE. If you are using other IDE such as ESPLORER then u8GLIB works perfectly - as shown in my video here.
Do not forget to insert correct pin numbers in the constructor called in the code..
#define OLED_SDA D7 //MOSI
#define OLED_SCL D5 //CLK
#define OLED_DC D4 //DC
#define OLED_CS 12 // no need of connecting anything to this pin, just use some pin number
#define OLED_RESET D3 //RES
// this is the constructor to call the OLED display where we specify exact pin numbers.
Adafruit_SSD1306 display(OLED_SDA,OLED_SCL, OLED_DC, OLED_RESET, OLED_CS);
Step 4: Circuit Diagram of Entire System
The final complete circuit diagram is as shown in the figure.
I have powered my DS18B20 and OLED display both from 3.3V output of NodeMCU. You can use external power supply too.
Some tricks for problems encountered sometimes-
Since, esp8266 is power hungry device, if you face problem of sketch not getting uploaded, just disconnect the DS18B20 VCC pin until sketch is uploaded completely and then reconnect once sketch is uploaded.
I noticed, few times, even after sketch is uploaded successfully, the display dosent show any temperature. A solution to this is just power reset your NodeMCU after uploading the sketch. Power reset can be done by pressing reset button on your NodeMCU or just removing VCC or USB cable and reconnecting it.
Step 5: Code/Sketch, Libraries and Links
- The working code (with comments-explanation) for arduino IDE is available here on my blogpost or is attached below as .ino file.
All libraries used in this project are also attached in .rar file below
(or you can use following links to download libraries)
- The latest Arduino IDE is available here
- The library for arduino IDE and DS18B20 is available here as Dallas temperature library.
Install all of these three libraries before uploading sketch to nodemcu.
The DS18B20 was purchased from aliexpress and nodeMCU from banggood.com
Step 6: Final Examples and Comparison With Commercial Temp Sensor
I checked the temperature displayed on OLED display measured by our system and compared it with temperature read by commercial temperature sensor. The temperature measurement are quite accurate over wide range of temperature scale. Obviously i could not go to very low temperatures.
Attached images are readings with sensor kept into a bowl containing ice (1.1 °C) and temp displayed when sensor heated using cigarette lighter (127 °C). Please note working range of DS18B20 is between -55°C to +125°C (-67°F to +257°F)
Good luck and have fun.... suggestions and comments are most welcomed.
In upcoming instructables i will log the measured temperature data, then upload online, so on.... stay tuned by following me here and subscribing on my youtube channel "Electromania" by clicking here