Skip to content

Temperature measurement with the DS18B20

    The DS18B20 is a digital temperature sensor that is used to accurately measure temperatures in a wide range from -55°C to +125°C. It communicates via a 1-wire protocol, which means that it can be operated with just one data line (plus ground and supply voltage). This makes it easy to connect multiple sensors to a single bus.

    Important features of the DS18B20:

    Measuring range: -55°C to +125°C

    Accuracy: ±0.5°C in the range from -10°C to +85°C

    Resolution: Configurable from 9 to 12 bits (corresponds to 0.5°C to 0.0625°C)

    1-Wire interface: Enables data communication and power supply via a single cable

    Unique 64-bit serial number: Each sensor has a unique address, making it possible to distinguish between several sensors on the same bus

    Parasitic power supply: Can be supplied with power via the data line if required


    Setup of a measuring circuit with a sensor and the ESP8266

    Parts required:

    ESP8266 (here NodeMCU)

    DS18B20 (in TO-92 housing or encapsulated)

    Resistance approx. 4.7 KOhm

    Breadboard

    Wires (Jumper)


    The construction of the circuit is quite simple:
    I like to use breadboards with header pins to build experimental circuits. This structure can then be used again and again, e.g. to include further components. The circuit of the DS18B20 is also shown (G=Ground; D=data line; V=supply voltage)

    Fritzing circuit ESP32 with DS18b20
    Logo Fitzing.org
    Structure on PCB - Front
    Structure on PCB - back

    Programming with the Arduino IDE

    Prerequisite -> Installed Arduino IDE
    Install the required libraries.

    The code requires the 2 libraries OneWire.h and DallasTemperature.h. These must be installed if not already done. The following screenshots show you how to install them.
    Please go to the library manager and search for “OneWire”. The OneWire will be listed as I have marked it. You will see the current version and the “Install” button. Click on this too.

    Integrating the Onewire Library

    Proceed in the same way with the 2nd library. Search for DallasTemperature and find this library.
    Please install this as well. Then restart the Arduino IDE once.

    Including the Dallas Library

    The code for reading the temperature


    Upload the tested code

    Checking the code with buttons

    The code is checked syntactically with the left button. To compile the code and upload it to the board, use the arrow to the right.

    Output of the compile and upload process
    Output of the queried sensor value

    And here we have the output of our sketch.
    A measurement is triggered and output every 10 seconds


    Adding a sensor to the circuit

    It is very easy to integrate another sensor into the circuit. I’ll show you the schematic on the breadboard.

    Extension of the circuit to 2 sensors
    Logo Fitzing.org

    Here I present the loop loop from the above code with the extension for reading another sensor.

    void loop()
    {
      // call sensors.requestTemperatures() to trigger a global temperature measurement
      // all sensors on the bus are queried
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // Trigger temperature measurement 
      Serial.println("DONE");
      Serial.print("Temperature for the device 1 (index 0) is: ");
      Serial.println(sensors.getTempCByIndex(0)); // Warum "byIndex"?
      // We can operate more than one sensor on the bus.  
      // 0 refers to the first sensor
      Serial.print("Temperature for the device 2 (index 1) is: ");
      Serial.println(sensors.getTempCByIndex(1));  
      // Index 1 refers to the second sensor
      delay(10000);
    }

    And with further small changes, it is also possible that we do not have to worry about the number of connected sensors. A For loop can do this for us.

    void loop()
    {
      // call sensors.requestTemperatures() to trigger a global temperature measurement
      // all sensors on the bus are queried
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // Trigger temperature measurement
      Serial.println("DONE");
    
      // Query the sensors in a loop
      for (int i = 0; i < sensors.getDeviceCount(); i++) {
        Serial.print("Temperature for device ");
        Serial.print(i);
        Serial.print(" is: ");
        Serial.println(sensors.getTempCByIndex(i)); 
      }
    
      delay(10000); // Wait 10 seconds
    }


    Briefly about the structure of the for loop.

    for (int i = 0; i < sensors.getDeviceCount(); i++) {
    …………….
    }


    i is the counter and also represents the index number of the sensor.
    With sensors.getDeviceCount() we ask for the number of connected sensors.
    And with i++, i is increased by one with each loop pass so that we can read out the next sensor.
    With the “<” operator, we ensure that the loop is terminated as soon as all available sensors have been read out.

    The result is the output of the measured values from both sensors. The accuracy of the sensors can be seen very clearly. Both measured values are the same until I briefly touched one sensor.

    Output of the queried sensor values

    Here we are at the end of our DIYTechAdventure. I hope you were able to follow everything and come to similar conclusions.

    If you have any questions, please use the opportunity to comment.


    0 Kommentare
    Inline Feedbacks
    View all comments