The DS18B20 is a temperature sensor that provides 9-bit to 12-bit values of temperature. Temperature readings from a specific device can be shown here. This sensor can communicate with an internal microprocessor via a one-wire bus protocol that employs only one data line. It is also possible to eliminate the requirement for an additional power supply by using this sensor, which receives its power directly from the data line.
Working Principle of DS18B20 temperature sensor
The DS18B20 temperature sensor works in the same way as a tmp36 temperature sensor. This sensor’s resolution can vary from 9 bits to 12 bits. However, when the computer is first powered on, the resolution is set to 12-bit. A low-power inactive state provides power for this sensor. A convert-T command can measure temperature and convert A to D. The temperature readings can be saved in the sensor’s two-byte register, and the sensor returns to its dormant state.
The master can offer read time slots alongside the Convert T instruction if an external power supply powers the sensor. It will supply 0 when the temperature change is improving and 1 when the temperature change is complete, even though the difference is small.
The following are some of the applications for the DS18B20 sensor.
- This sensor is used to measure the temperature of the liquid.
- Use it in the thermostat controls system.
- Used in thermally sensitive equipment
- HVAC systems make use of them.
- Multiple temperature measurements.
Features of DS18B20 temperature sensor
The following are some details of this sensor:
- This temperature sensor is programmable and digital.
- This sensor can be communicated via a 1-Wire approach.
- The supply voltage is in the 3.0V–5.5V range.
- Fahrenheit is the temperature range between -67°F and +257°F.
- This sensor’s precision is 0.5°C.
- This resolution ranges from 9-bit to 12-bit for the input/output.
- Within 750 ms, it converts the 12-bit temperature to a digital word.
- Customizable alarm settings
- It is possible to compute the temperature range of -55 to +125 degrees Celsius.
- These sensors are available in SOP, To-92, and even as waterproof sensors.
Pinout of the DS18B20 Temperature Sensor
Digital Out is one of the three pins on the DS18b20 Temperature Sensor. It is impossible to get analog signals from this sensor because it only outputs digital data. The DS18b20 Temperature Sensor’s pinout is as follows:
- VCC is the supply pin of the DS18b20 Temperature Sensor.
- GND is the DS18b20 Temperature Sensor IC’s ground pin and should be connected to the Arduino’s ground pin.
- DOUT is the IC’s Digital Output pin. The 1-Wire protocol is used to send temperature data through this pin.
DS18B20 Temperature Sensor Arduino Connection
The connections are quite straightforward.
Connect the Arduino pin number 4 to the sensor data pin(yellow wire). Sensor supply wire(red wire) to Arduino 5v and sensor GND wire (black wire) to Arduino GND.
Installing Library
Install the DS18B20 temperature sensor library in Library Manager.
- Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
- Type “OneWire” in the search box and install the OneWire library by Paul Stoffregen.
- Then, search for “Dallas” and install the Dallas Temperature library by Miles Burton.
Arduino Code for DS18B20 single sensor
Use the Arduino board to upload the following code.
This code will display the temperature on the serial monitor.
//electronicsmith.com
#include <OneWire.h>
#include <DallasTemperature.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);
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Start serial communication for debugging purposes
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop(void){
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
delay(1000);
}
Connect for multiple sensors
Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one Arduino digital pin.
here is the schematics.
Arduino Code for multiple DS18B20 sensors.
upload the code and this code will give the temperature output of each sensor.
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup(void) {
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void loop(void) {
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
}
delay(5000);
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
Code explanation
To get the number of DS18B20 sensors on the data line.
numberOfDevices = sensors.getDeviceCount();
The address of each sensor is unique. This method finds the sensor address.
if(sensors.getAddress(tempDeviceAddress, i))
This method gives the temperature of a specific sensor.
float tempC = sensors.getTempC(tempDeviceAddress);
2 thoughts on “DS18B20 Temperature Sensor and Arduino Interfacing: what you need to know”