• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

HUM: DS18B20 waterproof digital thermometer

You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Croduino and the basic code. Everything else is left to your imagination.

Introduction

In this tutorial we will deal with a waterproof digital thermometer by the Maxim Integrated company which has become very popular among Maker communities due to its functionalities. It has previously been calibrated to the Celsius scale, and the maximum temperature error ranging from -10°C to +85°C is +/-0,5°C, while the maximum measuring range is between -55°C and +125°C. In addition to good precision and high measuring range, this thermometer has the ability to change measurement resolution, set the alarm and parasitic power supply. This type of sensor is most commonly used in industry, consumer electronics and sensitive temperature systems.

 

Characteristics:
Power supply voltage: 3,3-5V
Measuring range: -55°C do +125°C
Measurement error: +/-0,5°C
Measurement resolution: 9-12 bits
Communication: One-wire
Number of sensors on the bus: 127
Wire length: 100 cm

How does it work?

If we look at the datasheet of this device, we will notice a block diagram that seems somewhat more complicated than that of other temperature sensors, and that is because of additional functionalities.

The device may have a special external power supply or it can be supplied via parasitic power using data line, about which you can find more in a separate tutorial.

Since we have several devices our microcontroller can communicate with on the same data line, there must be a way to ensure that only one device "speaks" in each moment. In order to ensure reliable communication, we use One-Wire protocol because of which all devices that use it have a unique 64bit address, using which we address a certain device on the bus.

Measurement precision can be set to 9, 10, 11 or 12 bits. This value will be adjusted according to what is more important to you, faster temperature reading or precision. For example, if we choose 12bits we will get a maximum precision, but the slowest reading time, up to 750 milliseconds.

In the block diagram, we can still notice two registers scheduled for alarms, into which the temperature value is entered. One register is for the lower limit, and the other for upper. This feature is used in the realization of protective functions. For example, if you want to protect a device from overheating to the upper limit register, you will enter the maximum temperature of a device you are protecting. If the device exceeds the specified temperature, this thermometer wil generate an alarm that the microcontroller detects and takes certain measures.
When the microcontroller gives a temperature reading command, the thermometer goes from idle state and starts the measurement. By the end of measuring the temperature is saved to memory (Scratchpad) and the thermometer goes into sleep mode in order to reduce energy consumption.

How to connect it?

Connecting is very simple since we only need to connect three wires and one 4,7kΩ resistor. The red wire of the thermometer is connected to Croduino's +5V power supply, and the black wire to the GND. The data wire can be connected to any digital pin. Between the data wire and +5V it is necessary to connect a pull up resistor. We have chosen digital pin 1 in this example, which we will later on use in the program code. The connection scheme is shown in the following picture.

 

Arduino code

As we do not want to get caught up in programming One-Wire protocol, we will use two libraries: OneWire and DallasTemperature. If you do not know how to install the library, check our tutorial.

#include "OneWire.h"
#include "DallasTemperature.h"

#define ONE_WIRE_BUS      1    //digital pin to which sensors are connected

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define nu  1                     //if you connect more than one sensor, it is necessary to change the number 

uint8_t deviceCount = 0;     

struct
{
  int id;
  DeviceAddress addr;
} T[nu];               


void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
  
  // checks how many devices are on the bus
  deviceCount = sensors.getDeviceCount();
  Serial.print("#uredaja: ");
  Serial.println(deviceCount);

  for (uint8_t index = 0; index < deviceCount; index++)
  {
    // go through all sensors
    sensors.getAddress(T[index].addr, index);
    T[index].id = sensors.getUserData(T[index].addr);
  }

  for (uint8_t index = 0; index < deviceCount; index++)
  {
    Serial.println();
    Serial.println(T[index].id);
    printAddress(T[index].addr);
    Serial.println();
  }
  Serial.println();

}


void loop(void)
{
  Serial.println();
  sensors.requestTemperatures();
  Serial.println("\tDohvati temperature ");
  for (int i = 0; i < nu; i++)
  {
    Serial.print("\t temp:\t");
    Serial.println(sensors.getTempC(T[i].addr));
  }

  delay(1000);
}


Leave a Reply