Using DS18B20 Digital Thermometer With Arduino
DS18B20 Sesnor Overview
The DS18B20 digital thermometer is a high-resolution (9-bit to 12-bit) temperature sensor with a built-in alarm and user-programmable alarm level. The sensor uses a 1-Wire bus to communicate to a microcontroller that requires data, power, and ground. Each sensor features a unique ID, allowing them to be configured and identified on the same bus.
Sensor temperature range: -67 °F to +257 °F (-55 °C to + 125 °C), comes packaged in a 3-pin TO-92 case and operates from 3 to 5.5 VDC.
The DS18B20 digital thermometer is a high-resolution (9-bit to 12-bit) temperature sensor with a built-in alarm and user-programmable alarm level. The sensor uses a 1-Wire bus to communicate to a microcontroller that requires data, power, and ground. Each sensor features a unique ID, allowing them to be configured and identified on the same bus.
Sensor temperature range: -67 °F to +257 °F (-55 °C to + 125 °C), comes packaged in a 3-pin TO-92 case and operates from 3 to 5.5 VDC.
DS18B20 Options
Through our site www.pcboard.ca, we offer individual DS18B20 sensors along with pre-wired 1m and 2m cable assemblies.
DS18B20 Connection To An Arduino UNO
Wiring for the DS18B20 to an Arduino UNO is straightforward. Power (Red wire) and Ground (Black wire) are supplied from the UNO to pins one and three of the sensor. The signal lead is attached to Digital Port 2 of the UNO from pin two of the sensor.
Additionally, a 4.7K Ω resistor (available separately) is connected across pin two (DQ) and pin three (Vdd) of the sensor. This resistor is necessary to pull up the DQ lead while reading the sensor. Failure to include the resistor can result in erroneous readings or temperature readings of -127. The resistor should be as close as possible to the sensor when used with longer-length sensor cables.
Arduino IDE Setup
The Arduino IDE requires libraries to read and process the temperature data from the DS18B20 sensor. The libraries are freely available directly through the IDE Library Manager.
From the Arduino IDE, select the Sketch option, Include Library option, and use the Manage Libraries selection. From the Library Manager, enter DallasTemperature and Install it.
During the install, you may also be prompted to install the OneWire library; you will want to select Install All, and this will set up all the necessary libraries.
Simple Arudino Program For Reading DS18B20 Temperature
Now upload the sketch below to your Arduino UNO, and temperature readings will be read from the DS18B20. The results will display to the Serial Monitor. The temperature values are displayed in both degrees Farenheight and Celcius.
The sketch can be easily modified for additional sensors or send data to external displays such as LCD 2×16 and 4×20 displays or small OLED displays.
Have other questions on other Arduino topics? See our collection of Arduino articles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/********************************************************************/ // // PCBoard.ca - Make-It.ca Technical Note // // Dallas DS18B20 1-Wire Digital Thermometer Example Sketch // IMPORTANT: Sketch requires the DallasTemperature library // // See: www.make-it.ca/arduino-ds18b20 for full notes // // First we include the libraries #include <OneWire.h> #include <DallasTemperature.h> /********************************************************************/ // Data wire is plugged into pin 2 on the Arduino #define ONE_WIRE_BUS 2 /********************************************************************/ // 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); /********************************************************************/ void setup(void) { // start serial port Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library Demo"); // Start up the library sensors.begin(); } void loop(void) { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus /********************************************************************/ Serial.print(" Requesting temperatures..."); sensors.requestTemperatures(); // Send the command to get temperature readings delay(1000); // Wait 1 second for temperature grab to stabilize Serial.println("done"); /********************************************************************/ Serial.print("Temperature is: "); // Why "byIndex"? // You can have more than one DS18B20 on the same bus. // 0 refers to the first IC on the wire Serial.print(sensors.getTempCByIndex(0)); Serial.print("° C ("); Serial.print(sensors.getTempFByIndex(0)); Serial.print("° F) -"); // Pause 1 second between readings delay(1000); } |