Measuring temperature using DS18B20, ESP8266 and GLCD

In this article I will show how to read DS18B20 sensor with ESP8266 and display values on GLCD.Now that we have GLCD set with our nice custom font, we can start measuring temperature.

First we need to connect our temperature sensors to ESP8266. There are two ways to do this:

  • traditional with multiple DS18B20 sensors (DS sensor from here on) on one pin of microcontroller
  • and the non-traditional method of one sensor per pin using multiple pins – this is what we’ll do.

You ask why? Because if one of the sensors stops working we can just connect new one without complications, because each of them have unique ID and we would need to somehow change this in software.

After connecting GLCD to NodeMCU we have D3 and D4 free. This is where we will connect two sensors.

We will use 3 wire connection. There also exists 2 wire connection (so called parasitic connection).

We connect pin 1 (left pin when looking DS sensor from fromt) of DS sensor to GND, pin 2 to microcontroller and pin 3 to +3V3. We also need 4,7kohm resistor between the +3V3 and middle pin of DS sensor.

Now we are ready for software.

We will be using OneWire and DallasTemperature libraries, so we need to add include for them:

Next we need to declare a few variables so everything can work:

As you can see they are just before the setup.

For sensors to start working we need to run begin method in setup:

This would be different if we would be using just one pin for all sensors. We use last_ temperatures because we need them to delete previous displayed temperature from display. This is because we use custom font (we overwrite the previous value with background color and display new value).

To get values from sensor we use requestTemperature method. Depending on what temperature are you using Celsius/Fahrenheit we call getTempCByIndex/getTempFByIndex respectively. We get float value of the temperature which we print over Serial and display on the LCD with displayRefresh method.

Below is the displayRefresh method with comments explaining code:

Here are values nicely displayed with our custom font (different from previous tutorials):

This is it for this time. Next time I will show you how to create nice web server with gauges for showing values instead of showing them on GLCD.

Thank you for reading. If you like this post please share it.

Slemi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.