Read Temperature with DS18B20
- Page ID
- 290608
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)You will need
- ESP8266-NodeMCU Development Board
- DS18B20 Waterproof Temperature Probes
- 4.7kΩ Resistor
- Bradboard
- Jumper Wires
- USB to MicroUSB Cable
Connect DS18B20 to Node MCU
DS18B20 Temperature probes use 1-Wire protocol, which means that several devices can be connected to the same pin.
Arduino Sketch
We will use a two step approach. In the first step we will create a loop that will go through all connected Ds18B20 probes and print out their addresses. Each DS18B20 probe has its unique address (sometimes also called ID) and it doesn’t change no matter what pin it is connected to or even what microcontroller it is connected to. You can think of it as a fingerprint.
Step 1: Get Device Address
Open Arduino IDE and create a new Sketch. Copy the code below and upload it to your ESP8266-NodeMCU microcontroller. Make sure to adjust the pin that your probes are connected to.
#include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS D2 // Data wire is connected to pin D2 OneWire oneWire(ONE_WIRE_BUS); //We need to setup 1-Wire protocol DallasTemperature sensors(&oneWire); //And then pass it to DallasTemperature DeviceAddress Address; //Variable to store addresses int deviceCount = 0; //Start at zero void setup(void) { Serial.begin(115200); sensors.begin(); //Initiate the sensors // Locate all connected devices Serial.println("Locating devices..."); Serial.print("Found "); deviceCount = sensors.getDeviceCount(); Serial.print(deviceCount, DEC); Serial.println(" devices."); Serial.println(""); // Loop through all connected devices and print their addresses Serial.println("Printing addresses..."); for (int i = 0; i < deviceCount; i++) { Serial.print("Sensor "); Serial.print(i+1); Serial.print(" : "); sensors.getAddress(Address, i); printAddress(Address); //Call void printAddress (see below) } } void loop(void){ } void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { Serial.print("0x"); if (deviceAddress[i] < 0x10) Serial.print("0"); Serial.print(deviceAddress[i], HEX); if (i < 7) Serial.print(", "); } Serial.println(""); }
Note: The void loop() in the code is empty, but it is still present since the code won’t work if either void setup() or void loop() is missing.
Once you uploaded the code you need to open Serial Monitor and push the “RST” button. This will reboot it and print the addresses. Save the addresses, we will need them in the next step.
Step 2: Get Temperature Readings
We will use the addresses in the following code to get temperature readings. The advantage of this two-step approach over just looping through the sensors to get readings is that now you can easily assign variables to each sensor and it is very useful if you are sending this data to a Google Sheet or a website.
Create a new Sketch and paste the following code. Replace the addresses with your own and upload it to the microcontroller.
#include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS D2 // Data wire is connected to pin D2 OneWire oneWire(ONE_WIRE_BUS); //We need to setup 1-Wire protocol DallasTemperature sensors(&oneWire); //And then pass it to DallasTemperature float tempSensor1, tempSensor2; //Add more variables as needed uint8_t sensor1[8] = { 0x28, 0xBE, 0x8E, 0x16, 0xA8, 0x01, 0x3C, 0x58 }; // Paste the first address here uint8_t sensor2[8] = { 0x28, 0xD7, 0x8B, 0x16, 0xA8, 0x01, 0x3C, 0xDF }; // Paste the second address here void setup() { Serial.begin(115200); sensors.begin(); //Initiate the sensors } void loop() { sensors.requestTemperatures(); tempSensor1 = sensors.getTempC(sensor1); // Get temperaure reading from probe#1 tempSensor2 = sensors.getTempC(sensor2); // Get temperaure reading from probe#2 Serial.print("Temperature Probe #1: "); Serial.print(tempSensor1); Serial.println("C"); Serial.print("Temperature Probe #2: "); Serial.print(tempSensor2); Serial.println("C"); delay(5*1000); // Wait 5 seconds between readings }
Note: You can add more sensors any time. You will need to repeat both steps and add new variables for additional sensors.
Contributors and Attributions
Robert E. Belford (University of Arkansas Little Rock; Department of Chemistry). The breadth, depth and veracity of this work is the responsibility of Robert E. Belford, rebelford@ualr.edu. You should contact him if you have any concerns. This material has both original contributions, and content built upon prior contributions of the LibreTexts Community and other resources, including but not limited to:
- Elena Lisitsyna