In this experiment, we will learn how to use DS18B20 module test the environmental temperature and
make a thermometer.Since the previous temperature sensor output is analog, we need to add additional A/D and D/A chip into the circut. These cause a big challenge. So we are create the Ds18b20 module.
The new DS18B20 Temperature Sensor Module is very good solve the problem. is economical, has a
unique 1-wire bus and is fully compatible with the Arduino platform .Users can easily form a sensor
network with this module.
A module with a digital ‘One Wire’ temperature sensor (DS18B20). A 4.7Kohm pullup resistor is included for the bus signal. Additional sensors can be added to the bus and individually addressed. Only one pullup resistor should be connected to the bus, irrespective of the number of sensors connected.
• Temperature range: -55 to +125°C
• Typical accuracy: 0.5°C
• Resolution: 9-12Bit, depending on the program
DS18B20 module uses a single bus. The power supply voltage range of 3.0 V to 5.5 V and no standby
power supply. It can Measure temperature range from-55 degree to +125 degree with accuracy
of+/-0.5°C. The temperature sensor features programmable DPI which can be set between 9 and 12.
The temperature itself is represented by 12 bits and can be reported at a maximum rate of once
every 750 milliseconds.
Each DS18B20 contains a unique serial number so that multiple DS18B20 chips can exist in abus.
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the board.
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire.h:Implements the OneWire communication protocol for devices like DS18B20
DallasTemperature.h: High-level library specifically for Dallas/Maxim temperature sensors
You can download the required library files by following Steps 1 to 3 shown in the figure. Search for the target library and complete the download and installation. If online download fails, click Step 4 and select the local library file provided in the supporting folder of this tutorial for manual installation.
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(void)
{
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
sensors.begin();
}
sensors.begin()
Purpose : Initialize the sensor library and search for all DS18B20 devices on the bus
Execution Process :
void loop(void)
{
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
}
sensors.requestTemperatures()
Core Purpose : Trigger all connected DS18B20 sensors to perform a temperature measurement
sensors.getTempCByIndex(0) //Sensor index, starting from 0
Core Purpose : Read the Celsius temperature value from the specified sensor
After correctly wiring the DS18B20 temperature sensor to the mainboard and keeping the pin defined in the code consistent with the actual wired pin, upload the program and open the serial monitor. You will see printed data similar to the example shown in the figure. When you bring your hand close to or hold the sensor probe, you can observe that the real-time temperature value rises gradually.