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

4 thoughts on “Measuring temperature using DS18B20, ESP8266 and GLCD

  1. Why do you blur the esp8266 D1 Mini? is it confidential :-O

    I like the project now it needs website with temperature over several days as a graph!

    • It is blurred because it is not part of this project and to avoid confusion for new programmers. 🙂 About the graph. I have Home Assistant for this, together with Grafana. It might come on this page some day…

      • it would be nice to have an website served by the esp8266 to show both temperatures and maybe graph it over the last 12hours?

        If you add humidity and a 1 I/O pin that is controlled by either humidity or temperature you have a very neat fan controller for air control in your shed outside or connect it to an led to show too cold/hot temperature…

        greetings from germany Moes!

        • Hi!

          Yes all that is true, but the purpose of this article was to understand how to use DS18B20 and GLCD with ESP8266.

          Regards Gregor!

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.