ArduinoArduino Projects

1-Wire DS18B20 Digital Waterproof Temperature Sensor with Arduino

Description:

Welcome to our article on the DS18B20 digital temperature sensor. In this article, we will show you how to set up and use this sensor with an Arduino board. The 1-wire DS18B20 temperature sensor is a great tool for measuring temperature in a wide range of applications, from home brewing to industrial control. We will go over the wiring, the code, and how to read and display the temperature data on SSD1306 OLED display. Additionally, we will also show you how to install the necessary libraries if you haven’t done it already. By the end of this article, you will have all the knowledge you need to start using the DS18B20 temperature sensor in your own projects. So sit back, relax and enjoy the article.




Purchase the Required parts from amazon

The parts required here are as follows.
It’s very simple as all you have to do is buy a resistor, ssd1306 oled, temperature sensor, Arduino, and a breadboard.

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Ds18b20 Temperature sensor

DS18B20 Temperature sensor

The DS18B20 temperature sensor is a digital temperature sensor manufactured by Dallas Semiconductor (now part of Maxim Integrated). It is a 1-Wire device, which means it can communicate with a microcontroller using just one data pin. The sensor uses a thermistor to measure temperature, and its output is a digital signal that can be read by a microcontroller. The 1-wire DS18B20 temperature sensor has a wide temperature measurement range of -55°C to +125°C, and it also has a high accuracy of ±0.5°C in the range of -10°C to +85°C. Additionally, it uses a unique 64-bit ID that allows multiple sensors to be connected to a single 1-Wire bus.



Main specifications

・Power supply voltage: 3.0 to 5.5V
・Measurement temperature range: -55℃ to +125℃
・Output: 1wire digital interface
・Accuracy: ±0.5℃ (-10℃ to +85℃)
・Drift: ±0.2 ℃
・Outline: TO-92 package

The DS18B20 temperature sensor can measure temperatures ranging from -55C to 125C. And it accepts a wide range of input voltages from 3 volts to 5.5 volts, meaning this temperature sensor can be easily used with both 5V and 3.3V compatible controller boards such as the Arduino Uno, Arduino Nano, and 3.3V controller boards like the ESP8266, ESP32, STM32, RASPBERRY PI PICO, and so on.”

Features

  • The DS18B20 digital thermometer measures 9-bit to 12-bit degrees Celsius and have a non-volatile, user-programmable upper and lower demarcation point alarm function. 
  • The DS18B20 communicates over a 1-Wire bus, requiring only one data line and a GND line to communicate with the microprocessor. – Has an operating temperature range of -55°C to +125°C with an accuracy of ±0.5°C from -10°C to +85°C. 
  • Furthermore, the DS18B20 can omit the external power supply and obtain power directly from the data line.(Parasite Power) – Each DS18B20 has its own 64-bit serial number. (Multiple DS18B20s can function on the same 1-Wire bus.)
  • Therefore, one microprocessor can be used to easily control many DS18B20s distributed over a wide area.
  • Applications that benefit from this feature include HVAC environmental controls, temperature monitoring systems in buildings, equipment or machines, and process monitoring and control systems.



What is 1-Wire?

By the way, this DS18B20 is a sensor that uses a technology called 1-Wire. It is useful to know a rough overview because it is often seen in the sensor area. 1-Wire is a technology that transmits signals using only one power line. If you’re new to electronics, it may seem counterintuitive, but sensors require at least one wire for power, one for ground, and one for signal. As the name suggests, the power supply is the line that supplies electricity to the sensor. Grounding is the negative side of electricity + and – Electricity flows only when + and – are connected. Finally, a signal is a wire that carries data, such as temperature, obtained by a sensor. 1-Wire is a technology that combines this power supply line and signal line into one. However, in retrospect, in the DS18B20 photo I introduced earlier, there were three lines extending from the sensor. This may sound like a preposterous story, but sharing a power supply and a signal causes problems such as unstable power supply, so a power supply line is added in addition to the signal line.

Temperature Measurement Operation

  • The core feature of the DS18B20 is an orthogonal digital temperature sensor. 
  • Temperature sensor resolution is user selectable to 9, 10, 11, and 12 bits corresponding to 0.5°C, 0.25°C, 0.125°C, and 0.0625°C increments. 
  • The default resolution at power-on is 12 bits.
  • The DS18B20 powers up in a low-power idle state. 
  • To initiate temperature measurement and A/D conversion, the master must issue a “CONVERT T [44h]” command. 
  • The resulting temperature data after conversion is stored in a 2-byte temperature register in scratchpad memory and the DS18B20 temperature sensor returns to the idle state. If the DS18B20 temperature sensor is powered by an external power source, the master may prepare a “read time slot” the DS18B20 temperature sensor will respond by sending [0] when the temperature conversion is in progress and [1] when the conversion is finished. 
  • If the DS18B20 temperature sensor is powered by parasite power, this notification method cannot be used. 



Download DS18B20 DataSheet:

ds18b20 datasheet

LM35 Vs DS18b20 Temperature Sensor:

The LM35 and DS18B20 are both temperature sensors, but they have some key differences:

  • The LM35 is an analog temperature sensor, meaning its output is an analog voltage that is proportional to the temperature. The DS18B20, on the other hand, is a digital temperature sensor, meaning its output is a digital signal that needs to be read by a microcontroller.
  • The LM35 has a lower resolution than the DS18B20, typically 10mV/°C vs 0.5°C, so it is less accurate but still acceptable for general-purpose temperature measurement.
  • The LM35 requires more components to interface with a microcontroller than the DS18B20, as the former has an analog output and the latter has a digital output, so it needs an ADC(Analog to digital converter) to interface.
  • The LM35 is less expensive than the DS18B20, however, the later has a wider temperature range with the ability to measure from -55°C to +125°C, where the LM35 is limited to typically -55°C to +150°C
  • The DS18B20 supports multiple sensor connections in one bus, with a unique 64-bit ID for each sensor, which allows for greater flexibility and scalability in applications where multiple temperature measurements are needed.

In summary, the DS18B20 is a more precise and versatile sensor while the LM35 is less expensive but less precise, so the choice between the two will depend on the specific requirements of your application.




Interfacing DS18b20 temperature sensor with Arduino

DS18B20 Temperature sensor

This is a simple circuit diagram. The data wire of the DS18B20 temperature sensor is connected with the digital pin D4 of Arduino. And the red and black wires are connected with the 5V and GND of Arduino.

The SDA and SCL pins of the SSD1306 Oled display module are connected with the I2C pins of Arduino. The I2C pins in Arduino are A4 and A5. A4 is the SDA and A5 is the SCL. And its voltage and GND pins are connected with the 5V and GND pins of Arduino.

DS18B20 Temperature sensor

As you can see, I have made all the connections as per the circuit diagram. You won’t get confused with this development board. You can make the same connections on a breadboard.”

DS18B20 Temperature sensor

You can see that this sensor has three wires. The red wire is the voltage wire and it can be connected with 3.3V or 5 volt. The black wire is the ground wire and the yellow wire is the data wire. You can see that I have also placed a 10K resistor between the voltage wire and data wire, although the recommended value is 4.7K ohm, but a 10k ohm works well enough. I am going to use I2C supported SSD1306 Oled display Module to display temperature.

Before, you start the programming, first of all, make sure you download all the necessary libraries the download links are given above.



Download Libraries:

DallasTemperature

OneWire

Adafruit_SSD1306-master

Adafruit-GFX-Library-master

How to install Libraries for ds18b20 temperature sensor:

if you want to install the libraries yourself, I am teaching you how to do it.

Open the Arduino IDE.

DS18B20 Temperature sensor

Click on the Sketch menu and then go to Include Library and click on Manage Libraries. Wait a bit.

DS18B20 Temperature sensor



In the search box, type the name of the library you want to install, for example, I want to install the onewire library.

DS18B20 Temperature sensor

Then I want to install the DallasTemperature library

DS18B20 Temperature sensor

then I want to install the SSD1306 oled library

DS18B20 Temperature sensor

You can see that I already have this library installed. Similarly, you can install other libraries.

Now uploaded the below program and watch the DS18B20 digital temperature sensor in action.



Ds18b20 Temperature sensor Arduino Code:

#include <OneWire.h>

#include <DallasTemperature.h>

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

// Data wire is conntec to the Arduino digital pin 4

#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor

DallasTemperature sensors(&oneWire);

#define SCREEN_WIDTH 128 // ORelay display width, in pixels

#define SCREEN_HEIGHT 64 // ORelay display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)

#define ORelay_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, ORelay_RESET);

void setup(void)

{

  // Start serial communication for debugging purposes

  Serial.begin(9600);

  // Start up the library

  sensors.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  delay(2000);

  display.clearDisplay();

  display.setTextColor(WHITE);

}

void loop(void) {

  sensors.requestTemperatures();

  float C_Temp = sensors.getTempCByIndex(0);

  float F_Temp = sensors.getTempFByIndex(0);

  display.clearDisplay();

  display.setCursor(10, 0);

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.print(C_Temp);

  display.print((char)247);

  display.print(“C”);

  display.setCursor(10, 20);

  display.setTextSize(2);

  display.print(F_Temp);

  display.print((char)247);

  display.print(“F”);

  display.setCursor(10, 45);

  display.setTextSize(2);

  display.display();

  delay(1000);

}

DS18B20 digital temperature sensor in action

DS18B20 Temperature sensor

DS18B20 Temperature sensor




Program Explanation:

Now let’s look at the program. First I added all the required libraries namely OneWire, DallasTemperature, SPI, Wire, adafruit_GFX, and Adafruit_SSD1306

#include <OneWire.h>

#include <DallasTemperature.h>

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

Next, I define the pin that is connected to the DS18B20 Temperature sensor data, namely pin 4. You may connect to other pins.

#define ONE_WIRE_BUS 4

Next, I create the object for Onewire for communication

OneWire oneWire(ONE_WIRE_BUS);

Next, I define variables for height and width, in pixels for SSD1306 oled display 

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

Next, I define a variable for an SSD1306 display connected to I2C (SDA, SCL pins)

#define ORelay_RESET     -1

Next, I create the object for SSD1306 oled display

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, ORelay_RESET);



In the void setup, I just start the SSD1306 oled display and 1-wire ds18b20 temperature sensor.

void setup(void)

{

  // Start serial communication for debugging purposes

  Serial.begin(9600);

  // Start up the library

  sensors.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  delay(2000);

  display.clearDisplay();

  display.setTextColor(WHITE);

}

Next, in the main program, send commands to the DS18B20 temperature sensor,

 sensors.requestTemperatures();

next, I store the sensor in values in the float type variables

 float C_Temp = sensors.getTempCByIndex(0);

 float F_Temp = sensors.getTempFByIndex(0);



Then I displayed the ds18b20 temperature sensor data on ssd1306 oled display

display.clearDisplay();

  display.setCursor(10, 0);

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.print(C_Temp);

  display.print((char)247);

  display.print(“C”);

  display.setCursor(10, 20);

  display.setTextSize(2);

  display.print(F_Temp);

  display.print((char)247);

  display.print(“F”);

  display.setCursor(10, 45);

  display.setTextSize(2);

  display.display();

  delay(1000);

conclusion

The temperature sensor used this time can also be used as a water temperature sensor, so I think it would be interesting to apply it to water temperature measurements such as aquariums!

Related Articles

Leave a Reply

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

Back to top button