Environmental sensors are of great importance in embedded applications. Many temperature sensors measure the ambient temperature or the temperature of a surface. To measure the temperature of water and other fluids, waterproof temperature sensors are required. One of these temperature sensors is the DS18B20. This sensor can measure the temperature of air, liquids such as water and soil. The sensor comes in two formats, one of which is a waterproof module. It can be used to detect temperature in applications such as electric steam stoves, electric kettles, and temperature-controlled water storage.
In this project, we demonstrate how the DS18B20 works by interfacing with Arduino. The sensor does not require any external components to interact with a controller/computer. It uses a single-wire interface for two-way data communication with a controller which makes its interface hassle-free. The sensor comes in a TO-92 package. It is available in two formats – in one it comes in a simple transistor-like package and in the other it is enclosed in a waterproof probe.
For a demonstration on a breadboard, we are using the transistor-like form factor in this project. The sensor's temperature readings are read by the Arduino, which are displayed on the SSD1306 OLED display.
Required components
- Arduino UNO x1
- 1-wire temperature sensor DS18B20 x1
- SSD1306 x1 OLED screen
- 4.7K resistor x1
- Test board
- Connecting wires/jumper wires
DS18B20 1-Wire Temperature Sensor
DS18B20 is a 1-wire digital thermometer from Dallas Semiconductor Corp. It is based on a 1-wire interface that requires only one pin for circuit connections. The sensor has a unique 64-bit serial code to address a 1-wire interface. It has multidrop capability, allowing the interface of many DS18B20 sensors on a single data line as a distributed network. It is also possible to power the sensor from the data line itself.
The sensor produces a temperature measurement with resolution scales of 9 to 12 bits. The operating temperature range of the DS18B20 is -55˚C to 125˚C with an accuracy of +/-0.5˚C. The default resolution of the sensor is 12 bits, which allows it to measure temperature with an accuracy of 0.0625˚C. This temperature sensor takes less than 750 ms to convert a reading. Therefore, it is easily possible to fetch temperature measurements at 1-second intervals from the sensor network.
The operating voltage of DS18B20 is 3.3~5V and the current consumption is about 1mA. Therefore, it can be easily connected to any microcontroller or microcomputer as long as a software library for 1-wire interface is available for that platform. With minimal current consumption and a simple 1-wire interface, it is even possible to interface the DS18B20 with low-power microcomputers such as the Raspberry Pi.
DS18B20 has the following pin diagram:
On the waterproof version of the sensor, the pins are identified by color coding. The GND, Data and VDD lines are identified by black, yellow and red wires. It should be noted that there are also some Chinese models of the sensor available. In these models the pin configuration is inverted, that is, in the front view the left pin is VDD, then Data and the rightmost pin is GND.
Circuit Connections
In this project, we connect an OLED DS18B20 and SSD1306 with Arduino UNO. To interface the DS18B20, connect the sensor's GND and VDD pins to ground and the Arduino's 5V output pins, respectively. The sensor data pin can be connected to any GPIO. In this project, the data pin is connected to D2 of Arduino. To stabilize the data line, a 4.7K pull-up resistor is recommended for communication between the data pin and power pin. Arduino's built-in pull-up resistors are insufficient to implement a 1-wire protocol. If an external resistor is not connected when interfacing with the Arduino, the board may not correctly read the sensor. It is also important to ensure the correct connections for voltage supply. A reverse voltage applied to the sensor can easily heat it to permanent rupture.
The OLED display is connected to show temperature readings. The SSD1306 interfaces with the Arduino using the Arduino's physical SPI port. To interface the SSD1306 OLED via physical SPI port, connect the D0/SCK and D1/MOSI pins of the SSD1306 OLED to the D13 and D11 pins of the Arduino, respectively. Connect the DC, RESET and CS pins of the SSD1306 to the D9, D10 and D8 pins of the Arduino respectively.
Arduino Libraries for DS18B20
Firstly, to work with DS18B20, a wire library is required. It can be found in the Arduino IDE library manager. It can also be downloaded manually as a ZIP from this link. The one-wire library is used to manage data communication over a 1-wire interface. The other library required for DS18B20 is a hardware-specific library for implementing the Dallas 1-wire protocol. It can be found in the library manager as DallasTemperature or downloaded as a ZIP file from this link.
Arduino Sketch
How it works
The sensor has its hardware specific 1-wire protocol, implemented by the Dallas Temperature library. The Arduino communicates with the DS18B20 via 1-wire protocol. The protocol can be implemented on any GPIO. The Arduino reads the temperature of the sensor by detecting the protocol-specific signals on its data pin and storing the value in a variable. The read temperature measurement is then displayed on the SSD1306 OLED.
Programming guide
The sketch begins by importing the OneWire and DallasTemperature libraries to work with the DS18B20 temperature sensor. Then, the SPI, Wire, Adafruit_GFX and Adafruit_SSD1306 libraries are imported to work with OLED display. A constant is defined to indicate the temperature sensor data line. An object of the OneWire class is instantiated, and the same object is used to instantiate an object of the DallasTemperature class. Constants for screen resolution and OLED display pin connections are defined. An object of class Adafruit_SSD1306 is instantiated with SPI explicitly specified as the interface protocol. A global variable 'temp' is declared to store temperature values. A bitmap is stored in PROGMEM for the site log and an array is defined for the same.
In the setup function, the DS18B20 is initialized by calling the sensors.begin method. The baud rate for data communication is 9600 bps by calling the Serial.begin method. The OLED display is initialized by calling the display.begin method, and the website logo is displayed on the OLED screen by calling the display.drawBitmap method.
In the loop function, the DS18B20 temperature reading is done by calling the Sensors.requestTemperatures method and the sought value is accessed in a variable through the Sensors.getTempCByIndex(0) method. The read temperature measurement is transferred to the serial port and displayed on the OLED display.
Result
Demo video