- All Products
- All Products
- Temperature Sensor DS18B20
Temperature Sensor DS18B20
The DS18B20 is a highly popular digital temperature sensor manufactured by Maxim Integrated, previously known as Dallas Semiconductor. Renowned for its widespread usage, this sensor provides a reliable temperature reading with a reasonable accuracy of 0.5 °C across an extensive temperature range from -55 °C to +125 °C. Its versatility shines through as it can seamlessly interface with various platforms, including the Arduino (5 V), ESP32, and Raspberry Pi (3.3 V GPIO pins). With an operating voltage spanning from 3.0 to 5.5 V, the DS18B20 ensures compatibility with different electronic systems.
Package Includes:
- 1 x DS18B20 Temperature Sensor IC
Terms and Conditions
30-day money-back guarantee
Shipping: 2-3 Business Days
Features:
- Digital Interface: The DS18B20 uses a 1-Wire digital interface, enabling multiple sensors to share a single data line, simplifying wiring and reducing pin requirements in microcontrollers.
- High Accuracy: The sensor offers a high level of accuracy with ±0.5 °C precision, ensuring reliable temperature measurements in various scenarios.
- Wide Temperature Range: The DS18B20 can measure temperatures ranging from -55 °C to +125 °C, making it suitable for applications in extreme environments.
- Programmable Resolution: Users can choose from 9 to 12-bit resolution for temperature measurements. Higher resolution provides more precise readings but may take longer to acquire.
- Unique 64-Bit Serial Code: Each DS18B20 sensor has a unique 64-bit serial code burned into it during manufacturing. This allows multiple sensors to be connected to the same bus, and their individual addresses can be used to distinguish them.
- Parasite Power Mode: The sensor can operate in a "parasite power mode," drawing power from the data line during temperature conversion. This mode requires only two wires (data and ground) for communication and power, simplifying the circuitry.
- Low Power Consumption: The DS18B20 is designed for energy efficiency, making it suitable for battery-powered applications where power consumption is critical.
- Conversion Time: The time required for temperature conversion depends on the chosen resolution. It can vary from 94 ms (9-bit resolution) to 750 ms (12-bit resolution).
- Alarm Function: The DS18B20 has a built-in programmable alarm function, allowing users to set upper and lower temperature thresholds. When the temperature crosses these limits, the sensor can trigger an alarm signal.
- Digital Output: The temperature data is transmitted in a digital format, eliminating the need for external analog-to-digital converters and ensuring robust signal transmission.
- Multiple Device Integration: Many DS18B20 sensors can be connected in parallel to the same data bus, enabling temperature monitoring at multiple points within a system.
- Waterproof Option: The sensor is available in waterproof versions, encased in stainless steel, making it suitable for outdoor and underwater applications.
- Compatibility: The DS18B20 is widely supported across various platforms, including Arduino, Raspberry Pi, ESP32, and other microcontrollers, with libraries and code examples readily available.
- Calibrated Output: Each DS18B20 sensor is individually calibrated in the manufacturing process, ensuring accurate and consistent temperature readings.
-
Description:
The DS18B20 is an advanced digital temperature sensor manufactured by Maxim Integrated (formerly Dallas Semiconductor). Widely recognized as one of the most popular temperature sensors available, it excels in providing exceptional accuracy with a precision of 0.5 °C, making it ideal for a wide temperature range from -55 °C to +125 °C. This versatile sensor is compatible with both the Arduino (5 V) and other devices like ESP32 and Raspberry Pi, which operate at 3.3 V GPIO pins, due to its flexible operating voltage range of 3.0 to 5.5 V. One of the primary advantages of the DS18B20 is its seamless communication with the Arduino using just one digital pin, thanks to its implementation of the Dallas Semiconductor 1-Wire® protocol. Although similar to I2C in operation, this sensor's 1-Wire® protocol offers a greater operating range and slower data rates. The DS18B20's unique 64-bit serial code assigned to each sensor allows multiple DS18B20 sensors to function efficiently on a single 1-Wire bus. As a result, you can effortlessly read data from multiple interconnected sensors using just one Arduino pin, enhancing system scalability and simplicity. Additionally, the sensor's resolution is programmable, providing users with the flexibility to adjust it to 9, 10, 11, or 12 bits. These settings correspond to temperature increments of 0.5, 0.25, 0.125, and 0.0625 degrees Celsius, respectively, with the 12-bit resolution being the initial default setting. With these capabilities and features, the DS18B20 proves to be an excellent choice for a wide array of applications, including industrial automation, environmental monitoring, HVAC systems, and scientific research, where precise and reliable temperature measurements are crucial.
Principle of Work:
The DS18B20 temperature sensor operates based on the principle of digital temperature conversion. It utilizes a semiconductor-based temperature sensor element that changes its electrical resistance with changes in temperature. The sensor's internal circuitry converts this resistance into a digital signal, allowing for easy communication with external devices like microcontrollers:
- Semiconductor Sensor Element: At the core of the DS18B20, there is a semiconductor-based temperature sensor element. This sensor element is typically made of a material like silicon, which exhibits predictable changes in electrical resistance as the temperature varies.
- Oscillator and Counter: The sensor contains an oscillator that generates a specific frequency signal, and a counter tracks the duration of this signal. This oscillator and counter pair work together during the temperature conversion process.
- Temperature Conversion: When the DS18B20 receives a command from an external device (e.g., a microcontroller) to measure the temperature, it starts the temperature conversion process. The oscillator begins generating pulses, and the countermeasures the time it takes for the pulses to traverse through the semiconductor sensor element.
- Digital-to-Analog Conversion: The sensor's internal circuitry processes the data from the counter and performs a digital-to-analog conversion. The result is an analog representation of the temperature value.
- Analog-to-Digital Conversion: The analog temperature value is further converted into a digital signal. The DS18B20 then encodes this digital value into a specific data format that follows the 1-Wire® protocol.
- Serial Communication: After the temperature conversion is complete and the digital temperature value is ready, the DS18B20 uses the 1-Wire® protocol to communicate with the external device (e.g., a microcontroller) to transmit the temperature data.
- 64-Bit Serial Code: Each DS18B20 sensor has a unique 64-bit serial code burned into its ROM during the manufacturing process. This code acts as an address, enabling multiple sensors to coexist on the same 1-Wire® bus without conflicts.
- Parasite Power Mode: As mentioned earlier, the DS18B20 can operate in a "parasite power mode" where it draws power from the data line during temperature conversion. This mode eliminates the need for a separate power supply pin, simplifying the wiring.
- Programmable Resolution: The DS18B20 allows users to set the resolution of the temperature measurement. Higher resolution settings provide more precise temperature readings but require longer conversion times.
- Calibration: Each DS18B20 sensor undergoes individual calibration during the manufacturing process, ensuring accurate and consistent temperature readings across various units.
Pinout of the Board:
DS18B20 | Description |
GND | GND |
DQ | Any digital pin (with 4.7k Ohm pull-up resistor) |
VDD | 5V (normal mode) or GND (parasite mode) |
Applications:
- Environmental Monitoring: The DS18B20 is widely used in weather stations and environmental monitoring systems to measure ambient temperatures and track temperature changes over time.
- Home Automation: It is employed in smart home systems for temperature control, and energy efficiency, and to trigger actions like turning on or off heating or cooling systems based on temperature thresholds.
- Industrial Automation: In industrial settings, the DS18B20 is used to monitor and control temperatures in manufacturing processes, warehouses, and storage facilities.
- HVAC Systems: Heating, Ventilation, and Air Conditioning (HVAC) systems utilize the DS18B20 to maintain optimal temperatures in buildings for comfort and energy efficiency.
- Aquariums and Pet Habitats: The module is used to monitor and regulate temperatures in aquariums, reptile enclosures, and other pet habitats to ensure the well-being of the animals.
- Medical Devices: In medical equipment and devices, the DS18B20 is utilized for precise temperature monitoring, such as in incubators and laboratory equipment.
- Automotive: The sensor is employed in automotive applications for monitoring engine temperatures, coolant systems, and cabin climate control.
- Data Centers: In data centers and server rooms, the DS18B20 is used to monitor equipment temperatures and prevent overheating.
- Process Control: It is utilized in industrial process control systems to ensure the optimal temperature is maintained for specific manufacturing processes.
- Food and Beverage Industry: In the food and beverage industry, the sensor is used for temperature monitoring in refrigerators, freezers, and during food processing.
- Greenhouses: The DS18B20 helps monitor and control temperatures in greenhouses to ensure optimal conditions for plant growth.
- Weather Balloons: In weather balloon applications, the module measures temperatures at different altitudes to gather atmospheric data.
- Cold Chain Management: The sensor plays a critical role in cold chain logistics, monitoring and maintaining the required temperature conditions during the transportation of temperature-sensitive goods, such as pharmaceuticals and perishable foods.
- Scientific Research: Researchers use the DS18B20 in various scientific experiments where precise temperature measurements are essential.
Circuit:
- Locate the Sensor Pins: Identify the DS18B20 sensor's pins. When looking at the flat side of the sensor (the side with the text printed on it) and the pins facing down, pin 1 (GND) is on the leftmost side.
- Wiring Connections: Connect the rightmost pin (DQ - Data) of the DS18B20 sensor to digital pin 2 on your microcontroller. This will be the middle pin when looking at the pins, with GND on the left and VDD (power) on the right.
- To ensure the 1-Wire bus remains in an idle state (high), add a 4.7k ohm pull-up resistor between the DQ pin and the 5V power supply on your microcontroller.
Library:
-
Download the Library:
- Click on the link to download the library: Download DS18B20 Library
- Save the downloaded library file (usually a .zip file) to a location on your computer where you can easily find it.
-
Open Arduino IDE:
- Launch the Arduino IDE on your computer.
-
Install the Library:
- In the Arduino IDE, navigate to the menu: Sketch -> Include Library -> Add .ZIP Library.
-
Locate the Downloaded Library File:
- A file browser window will appear. Browse to the location where you saved the downloaded DS18B20 library .zip file.
- Select the .zip file and click "Open" or "Choose" (depending on your operating system).
-
Library Installation:
- The Arduino IDE will automatically install the library from the .zip file.
- After installation, you will see a confirmation message in the Arduino IDE's status bar.
Code:
This code is a program for reading temperature data from one or more DS18B20 temperature sensors and displaying the temperature values in both Celsius and Fahrenheit on the Serial Monitor.
// Include the required libraries for the DS18B20 temperature sensor: #include "OneWire.h" #include "DallasTemperature.h" // Define the pin number to which the 1-Wire bus is connected: #define ONE_WIRE_BUS 2 // Create a new instance of the OneWire class to communicate with the DS18B20 sensor: OneWire oneWire(ONE_WIRE_BUS); // Pass the OneWire reference to the DallasTemperature library: DallasTemperature sensors(&oneWire); void setup() { // Start serial communication with the computer at a baud rate of 9600: Serial.begin(9600); // Start communication with the DS18B20 temperature sensor: sensors.begin(); } void loop() { // Command all connected DS18B20 sensors to measure temperature: sensors.requestTemperatures(); // Read the temperature in degrees Celsius from the first connected sensor: float tempC = sensors.getTempCByIndex(0); // Convert temperature from Celsius to Fahrenheit: float tempF = sensors.toFahrenheit(tempC); // Print the temperature in Celsius to the Serial Monitor: Serial.print("Temperature: "); Serial.print(tempC); Serial.print(" \xC2\xB0C | "); // Display degree symbol and "C" for Celsius // Print the temperature in Fahrenheit to the Serial Monitor: Serial.print(tempF); Serial.print(" \xC2\xB0F"); // Display degree symbol and "F" for Fahrenheit // Add a new line for the next reading: Serial.println(); // Wait for 1 second before taking the next temperature reading: delay(1000); }
-
It includes the necessary libraries, "OneWire.h" and "DallasTemperature.h," for interfacing with the DS18B20 temperature sensor.
-
The code defines the Arduino pin number (ONE_WIRE_BUS) to which the 1-Wire bus of the DS18B20 sensor is connected. In this case, it is connected to digital pin 2 on the Arduino.
-
It creates a new instance of the OneWire class named "oneWire" to communicate with the DS18B20 sensor using the 1-Wire protocol.
-
The OneWire reference is passed to the DallasTemperature class instance named "sensors." This allows the DallasTemperature library to communicate with the DS18B20 sensor using the OneWire interface.
-
In the "setup" function, it starts serial communication with the computer at a baud rate of 9600 and initiates communication with the DS18B20 temperature sensor using "sensors.begin()".
-
In the "loop" function, the following steps are executed repeatedly:
a. It sends a command to all connected DS18B20 sensors to measure the temperature using "sensors.requestTemperatures()".
b. It reads the temperature in degrees Celsius from the first connected DS18B20 sensor using "sensors.getTempCByIndex(0)" and stores it in the variable "tempC".
c. The Celsius temperature value is then converted to Fahrenheit using "sensors.toFahrenheit(tempC)" and stored in the variable "tempF".
d. The temperature values in Celsius and Fahrenheit are printed to the Serial Monitor using "Serial.print()". The degree symbol (°) and units ("C" for Celsius and "F" for Fahrenheit) are also displayed.
e. A new line is added to separate the readings for the next loop iteration using "Serial.println()".
f. The code then waits for 1 second using "delay(1000)" before taking the next temperature reading. This delay prevents rapid and unnecessary temperature readings.
Technical Details:
- Programmable Digital Temperature Sensor
- Communicates using 1-Wire method
- Operating voltage: 3V to 5V
- Temperature Range: -55°C to +125°C
- Accuracy: ±0.5°C
- Output Resolution: 9-bit to 12-bit (programmable)
- Unique 64-bit address enables multiplexing
- Conversion time: 750ms at 12-bit
Resources:
Comparisons:
The DS18B20 temperature sensor and the DHT11 module are both popular sensors used for measuring temperature in various applications. but they cater to different needs. If you require highly accurate temperature readings over a wide range, the DS18B20 is an excellent choice. On the other hand, if you need to measure both temperature and humidity, the DHT11 offers a cost-effective solution. Your specific project requirements will determine which sensor is the best fit:
-
Type of Sensor:
- DS18B20: The DS18B20 is a digital temperature sensor that provides accurate temperature readings with a precision of 0.5°C.
- DHT11: The DHT11 is a digital temperature and humidity sensor that measures both temperature and relative humidity.
-
Temperature Measurement Range:
- DS18B20: It has a wide temperature measurement range of -55°C to +125°C.
- DHT11: It has a more limited temperature range of 0°C to +50°C.
-
Accuracy:
- DS18B20: It offers higher accuracy compared to the DHT11 with a precision of 0.5°C.
- DHT11: The DHT11 has a lower accuracy, typically around ±2°C for temperature readings.
-
Communication Protocol:
- DS18B20: It uses the 1-Wire protocol for communication, allowing multiple sensors to be connected to the same data bus.
- DHT11: The DHT11 uses a proprietary 1-wire-like protocol with a single data line.
-
Supply Voltage:
- DS18B20: It operates over a wide supply voltage range from 3.0V to 5.5V.
- DHT11: The DHT11 typically works with a supply voltage of 3.3V to 5.5V.
-
Sample Rate:
- DS18B20: It has a faster sample rate compared to the DHT11, as it can provide a temperature reading within a few milliseconds.
- DHT11: The DHT11 has a slower sample rate, typically taking a couple of seconds to provide accurate readings.
-
Additional Capability:
- DS18B20: The DS18B20 only measures temperature but provides highly accurate readings over a wide temperature range.
- DHT11: In addition to temperature, the DHT11 can measure relative humidity, making it suitable for applications that require humidity monitoring as well.
-
Application Scenarios:
- DS18B20: The DS18B20 is commonly used in applications where precise temperature measurements are critical, such as environmental monitoring, industrial automation, and scientific research.
- DHT11: The DHT11 is often used in projects that require both temperature and humidity measurements, like weather stations, home automation systems, and greenhouse monitoring.
DHT11 | Specifications | DS18B20 |
Humidity and temperature | Measurement | Temperature |
0 – 50° C / ± 2° C | Temperature Range | -55 – 125° C / ± 0.5° C |
20 – 80 / ± 5 % | Humidity Range | Nil |
1 wire | Communication Protocol | 1 wire |
1 Hz one reading every second | Sampling Rate | |
3 – 5 V | Operating Voltage | 3 – 5 V |
2.5 mA | Minimum current | 10 mA |
Features:
- Digital Interface: The DS18B20 uses a 1-Wire digital interface, enabling multiple sensors to share a single data line, simplifying wiring and reducing pin requirements in microcontrollers.
- High Accuracy: The sensor offers a high level of accuracy with ±0.5 °C precision, ensuring reliable temperature measurements in various scenarios.
- Wide Temperature Range: The DS18B20 can measure temperatures ranging from -55 °C to +125 °C, making it suitable for applications in extreme environments.
- Programmable Resolution: Users can choose from 9 to 12-bit resolution for temperature measurements. Higher resolution provides more precise readings but may take longer to acquire.
- Unique 64-Bit Serial Code: Each DS18B20 sensor has a unique 64-bit serial code burned into it during manufacturing. This allows multiple sensors to be connected to the same bus, and their individual addresses can be used to distinguish them.
- Parasite Power Mode: The sensor can operate in a "parasite power mode," drawing power from the data line during temperature conversion. This mode requires only two wires (data and ground) for communication and power, simplifying the circuitry.
- Low Power Consumption: The DS18B20 is designed for energy efficiency, making it suitable for battery-powered applications where power consumption is critical.
- Conversion Time: The time required for temperature conversion depends on the chosen resolution. It can vary from 94 ms (9-bit resolution) to 750 ms (12-bit resolution).
- Alarm Function: The DS18B20 has a built-in programmable alarm function, allowing users to set upper and lower temperature thresholds. When the temperature crosses these limits, the sensor can trigger an alarm signal.
- Digital Output: The temperature data is transmitted in a digital format, eliminating the need for external analog-to-digital converters and ensuring robust signal transmission.
- Multiple Device Integration: Many DS18B20 sensors can be connected in parallel to the same data bus, enabling temperature monitoring at multiple points within a system.
- Waterproof Option: The sensor is available in waterproof versions, encased in stainless steel, making it suitable for outdoor and underwater applications.
- Compatibility: The DS18B20 is widely supported across various platforms, including Arduino, Raspberry Pi, ESP32, and other microcontrollers, with libraries and code examples readily available.
- Calibrated Output: Each DS18B20 sensor is individually calibrated in the manufacturing process, ensuring accurate and consistent temperature readings.
-
Description:
The DS18B20 is an advanced digital temperature sensor manufactured by Maxim Integrated (formerly Dallas Semiconductor). Widely recognized as one of the most popular temperature sensors available, it excels in providing exceptional accuracy with a precision of 0.5 °C, making it ideal for a wide temperature range from -55 °C to +125 °C. This versatile sensor is compatible with both the Arduino (5 V) and other devices like ESP32 and Raspberry Pi, which operate at 3.3 V GPIO pins, due to its flexible operating voltage range of 3.0 to 5.5 V. One of the primary advantages of the DS18B20 is its seamless communication with the Arduino using just one digital pin, thanks to its implementation of the Dallas Semiconductor 1-Wire® protocol. Although similar to I2C in operation, this sensor's 1-Wire® protocol offers a greater operating range and slower data rates. The DS18B20's unique 64-bit serial code assigned to each sensor allows multiple DS18B20 sensors to function efficiently on a single 1-Wire bus. As a result, you can effortlessly read data from multiple interconnected sensors using just one Arduino pin, enhancing system scalability and simplicity. Additionally, the sensor's resolution is programmable, providing users with the flexibility to adjust it to 9, 10, 11, or 12 bits. These settings correspond to temperature increments of 0.5, 0.25, 0.125, and 0.0625 degrees Celsius, respectively, with the 12-bit resolution being the initial default setting. With these capabilities and features, the DS18B20 proves to be an excellent choice for a wide array of applications, including industrial automation, environmental monitoring, HVAC systems, and scientific research, where precise and reliable temperature measurements are crucial.
Principle of Work:
The DS18B20 temperature sensor operates based on the principle of digital temperature conversion. It utilizes a semiconductor-based temperature sensor element that changes its electrical resistance with changes in temperature. The sensor's internal circuitry converts this resistance into a digital signal, allowing for easy communication with external devices like microcontrollers:
- Semiconductor Sensor Element: At the core of the DS18B20, there is a semiconductor-based temperature sensor element. This sensor element is typically made of a material like silicon, which exhibits predictable changes in electrical resistance as the temperature varies.
- Oscillator and Counter: The sensor contains an oscillator that generates a specific frequency signal, and a counter tracks the duration of this signal. This oscillator and counter pair work together during the temperature conversion process.
- Temperature Conversion: When the DS18B20 receives a command from an external device (e.g., a microcontroller) to measure the temperature, it starts the temperature conversion process. The oscillator begins generating pulses, and the countermeasures the time it takes for the pulses to traverse through the semiconductor sensor element.
- Digital-to-Analog Conversion: The sensor's internal circuitry processes the data from the counter and performs a digital-to-analog conversion. The result is an analog representation of the temperature value.
- Analog-to-Digital Conversion: The analog temperature value is further converted into a digital signal. The DS18B20 then encodes this digital value into a specific data format that follows the 1-Wire® protocol.
- Serial Communication: After the temperature conversion is complete and the digital temperature value is ready, the DS18B20 uses the 1-Wire® protocol to communicate with the external device (e.g., a microcontroller) to transmit the temperature data.
- 64-Bit Serial Code: Each DS18B20 sensor has a unique 64-bit serial code burned into its ROM during the manufacturing process. This code acts as an address, enabling multiple sensors to coexist on the same 1-Wire® bus without conflicts.
- Parasite Power Mode: As mentioned earlier, the DS18B20 can operate in a "parasite power mode" where it draws power from the data line during temperature conversion. This mode eliminates the need for a separate power supply pin, simplifying the wiring.
- Programmable Resolution: The DS18B20 allows users to set the resolution of the temperature measurement. Higher resolution settings provide more precise temperature readings but require longer conversion times.
- Calibration: Each DS18B20 sensor undergoes individual calibration during the manufacturing process, ensuring accurate and consistent temperature readings across various units.
Pinout of the Board:
DS18B20 | Description |
GND | GND |
DQ | Any digital pin (with 4.7k Ohm pull-up resistor) |
VDD | 5V (normal mode) or GND (parasite mode) |
Applications:
- Environmental Monitoring: The DS18B20 is widely used in weather stations and environmental monitoring systems to measure ambient temperatures and track temperature changes over time.
- Home Automation: It is employed in smart home systems for temperature control, and energy efficiency, and to trigger actions like turning on or off heating or cooling systems based on temperature thresholds.
- Industrial Automation: In industrial settings, the DS18B20 is used to monitor and control temperatures in manufacturing processes, warehouses, and storage facilities.
- HVAC Systems: Heating, Ventilation, and Air Conditioning (HVAC) systems utilize the DS18B20 to maintain optimal temperatures in buildings for comfort and energy efficiency.
- Aquariums and Pet Habitats: The module is used to monitor and regulate temperatures in aquariums, reptile enclosures, and other pet habitats to ensure the well-being of the animals.
- Medical Devices: In medical equipment and devices, the DS18B20 is utilized for precise temperature monitoring, such as in incubators and laboratory equipment.
- Automotive: The sensor is employed in automotive applications for monitoring engine temperatures, coolant systems, and cabin climate control.
- Data Centers: In data centers and server rooms, the DS18B20 is used to monitor equipment temperatures and prevent overheating.
- Process Control: It is utilized in industrial process control systems to ensure the optimal temperature is maintained for specific manufacturing processes.
- Food and Beverage Industry: In the food and beverage industry, the sensor is used for temperature monitoring in refrigerators, freezers, and during food processing.
- Greenhouses: The DS18B20 helps monitor and control temperatures in greenhouses to ensure optimal conditions for plant growth.
- Weather Balloons: In weather balloon applications, the module measures temperatures at different altitudes to gather atmospheric data.
- Cold Chain Management: The sensor plays a critical role in cold chain logistics, monitoring and maintaining the required temperature conditions during the transportation of temperature-sensitive goods, such as pharmaceuticals and perishable foods.
- Scientific Research: Researchers use the DS18B20 in various scientific experiments where precise temperature measurements are essential.
Circuit:
- Locate the Sensor Pins: Identify the DS18B20 sensor's pins. When looking at the flat side of the sensor (the side with the text printed on it) and the pins facing down, pin 1 (GND) is on the leftmost side.
- Wiring Connections: Connect the rightmost pin (DQ - Data) of the DS18B20 sensor to digital pin 2 on your microcontroller. This will be the middle pin when looking at the pins, with GND on the left and VDD (power) on the right.
- To ensure the 1-Wire bus remains in an idle state (high), add a 4.7k ohm pull-up resistor between the DQ pin and the 5V power supply on your microcontroller.
Library:
-
Download the Library:
- Click on the link to download the library: Download DS18B20 Library
- Save the downloaded library file (usually a .zip file) to a location on your computer where you can easily find it.
-
Open Arduino IDE:
- Launch the Arduino IDE on your computer.
-
Install the Library:
- In the Arduino IDE, navigate to the menu: Sketch -> Include Library -> Add .ZIP Library.
-
Locate the Downloaded Library File:
- A file browser window will appear. Browse to the location where you saved the downloaded DS18B20 library .zip file.
- Select the .zip file and click "Open" or "Choose" (depending on your operating system).
-
Library Installation:
- The Arduino IDE will automatically install the library from the .zip file.
- After installation, you will see a confirmation message in the Arduino IDE's status bar.
Code:
This code is a program for reading temperature data from one or more DS18B20 temperature sensors and displaying the temperature values in both Celsius and Fahrenheit on the Serial Monitor.
// Include the required libraries for the DS18B20 temperature sensor: #include "OneWire.h" #include "DallasTemperature.h" // Define the pin number to which the 1-Wire bus is connected: #define ONE_WIRE_BUS 2 // Create a new instance of the OneWire class to communicate with the DS18B20 sensor: OneWire oneWire(ONE_WIRE_BUS); // Pass the OneWire reference to the DallasTemperature library: DallasTemperature sensors(&oneWire); void setup() { // Start serial communication with the computer at a baud rate of 9600: Serial.begin(9600); // Start communication with the DS18B20 temperature sensor: sensors.begin(); } void loop() { // Command all connected DS18B20 sensors to measure temperature: sensors.requestTemperatures(); // Read the temperature in degrees Celsius from the first connected sensor: float tempC = sensors.getTempCByIndex(0); // Convert temperature from Celsius to Fahrenheit: float tempF = sensors.toFahrenheit(tempC); // Print the temperature in Celsius to the Serial Monitor: Serial.print("Temperature: "); Serial.print(tempC); Serial.print(" \xC2\xB0C | "); // Display degree symbol and "C" for Celsius // Print the temperature in Fahrenheit to the Serial Monitor: Serial.print(tempF); Serial.print(" \xC2\xB0F"); // Display degree symbol and "F" for Fahrenheit // Add a new line for the next reading: Serial.println(); // Wait for 1 second before taking the next temperature reading: delay(1000); }
-
It includes the necessary libraries, "OneWire.h" and "DallasTemperature.h," for interfacing with the DS18B20 temperature sensor.
-
The code defines the Arduino pin number (ONE_WIRE_BUS) to which the 1-Wire bus of the DS18B20 sensor is connected. In this case, it is connected to digital pin 2 on the Arduino.
-
It creates a new instance of the OneWire class named "oneWire" to communicate with the DS18B20 sensor using the 1-Wire protocol.
-
The OneWire reference is passed to the DallasTemperature class instance named "sensors." This allows the DallasTemperature library to communicate with the DS18B20 sensor using the OneWire interface.
-
In the "setup" function, it starts serial communication with the computer at a baud rate of 9600 and initiates communication with the DS18B20 temperature sensor using "sensors.begin()".
-
In the "loop" function, the following steps are executed repeatedly:
a. It sends a command to all connected DS18B20 sensors to measure the temperature using "sensors.requestTemperatures()".
b. It reads the temperature in degrees Celsius from the first connected DS18B20 sensor using "sensors.getTempCByIndex(0)" and stores it in the variable "tempC".
c. The Celsius temperature value is then converted to Fahrenheit using "sensors.toFahrenheit(tempC)" and stored in the variable "tempF".
d. The temperature values in Celsius and Fahrenheit are printed to the Serial Monitor using "Serial.print()". The degree symbol (°) and units ("C" for Celsius and "F" for Fahrenheit) are also displayed.
e. A new line is added to separate the readings for the next loop iteration using "Serial.println()".
f. The code then waits for 1 second using "delay(1000)" before taking the next temperature reading. This delay prevents rapid and unnecessary temperature readings.
Technical Details:
- Programmable Digital Temperature Sensor
- Communicates using 1-Wire method
- Operating voltage: 3V to 5V
- Temperature Range: -55°C to +125°C
- Accuracy: ±0.5°C
- Output Resolution: 9-bit to 12-bit (programmable)
- Unique 64-bit address enables multiplexing
- Conversion time: 750ms at 12-bit
Resources:
Comparisons:
The DS18B20 temperature sensor and the DHT11 module are both popular sensors used for measuring temperature in various applications. but they cater to different needs. If you require highly accurate temperature readings over a wide range, the DS18B20 is an excellent choice. On the other hand, if you need to measure both temperature and humidity, the DHT11 offers a cost-effective solution. Your specific project requirements will determine which sensor is the best fit:
-
Type of Sensor:
- DS18B20: The DS18B20 is a digital temperature sensor that provides accurate temperature readings with a precision of 0.5°C.
- DHT11: The DHT11 is a digital temperature and humidity sensor that measures both temperature and relative humidity.
-
Temperature Measurement Range:
- DS18B20: It has a wide temperature measurement range of -55°C to +125°C.
- DHT11: It has a more limited temperature range of 0°C to +50°C.
-
Accuracy:
- DS18B20: It offers higher accuracy compared to the DHT11 with a precision of 0.5°C.
- DHT11: The DHT11 has a lower accuracy, typically around ±2°C for temperature readings.
-
Communication Protocol:
- DS18B20: It uses the 1-Wire protocol for communication, allowing multiple sensors to be connected to the same data bus.
- DHT11: The DHT11 uses a proprietary 1-wire-like protocol with a single data line.
-
Supply Voltage:
- DS18B20: It operates over a wide supply voltage range from 3.0V to 5.5V.
- DHT11: The DHT11 typically works with a supply voltage of 3.3V to 5.5V.
-
Sample Rate:
- DS18B20: It has a faster sample rate compared to the DHT11, as it can provide a temperature reading within a few milliseconds.
- DHT11: The DHT11 has a slower sample rate, typically taking a couple of seconds to provide accurate readings.
-
Additional Capability:
- DS18B20: The DS18B20 only measures temperature but provides highly accurate readings over a wide temperature range.
- DHT11: In addition to temperature, the DHT11 can measure relative humidity, making it suitable for applications that require humidity monitoring as well.
-
Application Scenarios:
- DS18B20: The DS18B20 is commonly used in applications where precise temperature measurements are critical, such as environmental monitoring, industrial automation, and scientific research.
- DHT11: The DHT11 is often used in projects that require both temperature and humidity measurements, like weather stations, home automation systems, and greenhouse monitoring.
DHT11 | Specifications | DS18B20 |
Humidity and temperature | Measurement | Temperature |
0 – 50° C / ± 2° C | Temperature Range | -55 – 125° C / ± 0.5° C |
20 – 80 / ± 5 % | Humidity Range | Nil |
1 wire | Communication Protocol | 1 wire |
1 Hz one reading every second | Sampling Rate | |
3 – 5 V | Operating Voltage | 3 – 5 V |
2.5 mA | Minimum current | 10 mA |