top of page
Writer's pictureRamesh G

DS18B20 Digital Thermometer with Bluetooth Interface

Updated: Nov 27, 2021


Here to learn Arduino based bluetooth temperature sensor measures the temperature via the DS18B20 integrated digital temperature sensor and makes it available via bluetooth. This creating a kind of remote temperature monitor and temperature reading is transmitted via the Arduino’s serial port to the Arduino bluetooth module. Then the android application receive celsius & fahrenheit temperature reading and displays digital reading and therometer gauge display in mobile screen.


Circuit Diagram:

Components Required:

Arduino Nano Board - 1 no

DS18B20 with Probe - 1 no

Resistor 4k7- 1 no

HC-05 Bluetooth Module- 1no



DS18B20


The DS18B20 is a 1-wire programmable Temperature sensor from maxim integrated. It is widely used to measure temperature in hard environments like in chemical solutions, mines or soil etc. The constriction of the sensor is rugged and also can be purchased with a waterproof option making the mounting process easy. It can measure a wide range of temperature from -55°C to +125° with a decent accuracy of ±5°C. Each sensor has a unique address and requires only one pin of the MCU to transfer data so it a very good choice for measuring temperature at multiple points without compromising much of your digital pins on the microcontroller.


How to use the DS18B20 Sensor

The sensor works with the method of 1-Wire communication. It requires only the data pin connected to the microcontroller with a pull up resistor and the other two pins are used for power as shown below.

The pull-up resistor is used to keep the line in high state when the bus is not in use. The temperature value measured by the sensor will be stored in a 2-byte register inside the sensor. This data can be read by the using the 1- wire method by sending in a sequence of data. There are two types of commands that are to be sent to read the values, one is a ROM command and the other is function command. The address value of each ROM memory along with the sequence is given in the datasheet below. You have to read through it to understand how to communicate with the sensor.

Applications


  • Measuring temperature at hard environments

  • Liquid temperature measurement

  • Applications where temperature has to be measured at multiple points

DS18B20 Sensor Specifications


  • 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

  • Programmable alarm options

  • Available as To-92, SOP and even as a waterproof sensor


Bluetooth HC-05 Module


HC-05 is a Bluetooth module which is designed for wireless communication. This module can be used in a master or slave configuration. Bluetooth serial modules allow all serial enabled devices to communicate with each other using Bluetooth.

It has 6 pins,

1.Key/EN: It is used to bring Bluetooth module in AT commands mode. If Key/EN pin is set to high, then this module will work in command mode. Otherwise by default it is in data mode. The default baud rate of HC-05 in command mode is 38400bps and 9600 in data mode.

HC-05 module has two modes,

Data mode: Exchange of data between devices.

Command mode: It uses AT commands which are used to change setting of HC-05. To send these commands to module serial (USART) port is used.


2. VCC: Connect 5 V or 3.3 V to this Pin.


3. GND: Ground Pin of module.


4. TXD: Transmit Serial data (wirelessly received data by Bluetooth module transmitted out serially on TXD pin)


5. RXD: Receive data serially (received data will be transmitted wirelessly by Bluetooth module).


6. State: It tells whether module is connected or not.


HC-05 module Information

HC-05 has red LED which indicates connection status, whether the Bluetooth is connected or not. Before connecting to HC-05 module this red LED blinks continuously in a periodic manner. When it gets connected to any other Bluetooth device, its blinking slows down to two seconds.

This module works on 3.3 V. We can connect 5V supply voltage as well since the module has on board 5 to 3.3 V regulator.

As HC-05 Bluetooth module has 3.3 V level for RX/TX and microcontroller can detect 3.3 V level, so, no need to shift transmit level of HC-05 module. But we need to shift the transmit voltage level from microcontroller to RX of HC-05 module.

HC-05 Default Settings


  • Default Bluetooth Name: “HC-05”

  • Default Password: 1234 or 0000

  • Default Communication: Slave

  • Default Mode: Data Mode

  • Data Mode Baud Rate: 9600, 8, N, 1

  • Command Mode Baud Rate: 38400, 8, N, 1

  • Default firmware: LINVOR

Installing the Arduino Library

Installing Libraries for DS18B20


To interface with the DS18B20 temperature sensor, you need to install the One Wire library . Follow the next steps to install those libraries.


1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.


2. Type “onewire” in the search box and install the OneWire library by Paul Stoffregen.


In your Arduino IDE, to install the libraries go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.


After installing the required libraries, copy the following code to your Arduino IDE.


Subscribe and Download code.


After a successful upload, open the Serial Monitor at a baud rate of 9600. Press the “EN/RST” button on the Arduino Uno board and see the result in monitor.



Mobile Output

First open Mobile application Thermometer_BT and select Bluetooth Connect button, after that select bluetooth HC-05 device to connect and enter Password as mentioned above (0000 or 1234).

Demo Video:


Subscribe and Download code.

Android Download


Arduino code

#include <OneWire.h>


// OneWire DS18S20, DS18B20, DS1822 Temperature Example


// http://www.pjrc.com/teensy/td_libs_OneWire.html


OneWire ds(5); // on pin 10 (a 4.7K resistor is necessary)


String temperature;



void setup(void) {


Serial.begin(9600);



}


void loop(void) {


byte i;


byte present = 0;


byte type_s;


byte data[12];


byte addr[8];


float celsius, fahrenheit;



if ( !ds.search(addr)) {




ds.reset_search();


delay(250);


return;


}





for( i = 0; i < 8; i++) {


//// Serial.write(' ');




}


if (OneWire::crc8(addr, 7) != addr[7]) {




return;


}





// the first ROM byte indicates which chip


switch (addr[0]) {


case 0x10:




type_s = 1;


break;


case 0x28:




type_s = 0;


break;


case 0x22:




type_s = 0;


break;


default:



return;


}


ds.reset();


ds.select(addr);


ds.write(0x44, 1); // start conversion, with parasite power on at the end



delay(1000); // maybe 750ms is enough, maybe not


// we might do a ds.depower() here, but the reset will take care of it.



present = ds.reset();


ds.select(addr);


ds.write(0xBE); // Read Scratchpad



for ( i = 0; i < 9; i++) { // we need 9 bytes


data[i] = ds.read();




}



// Convert the data to actual temperature


// because the result is a 16 bit signed integer, it should


// be stored to an "int16_t" type, which is always 16 bits


// even when compiled on a 32 bit processor.


int16_t raw = (data[1] << 8) | data[0];


if (type_s) {


raw = raw << 3; // 9 bit resolution default


if (data[7] == 0x10) {


// "count remain" gives full 12 bit resolution


raw = (raw & 0xFFF0) + 12 - data[6];


}


} else {


byte cfg = (data[4] & 0x60);


// at lower res, the low bits are undefined, so let's zero them


if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms


else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms


else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms


//// default is 12 bit resolution, 750 ms conversion time


}


celsius = (float)raw / 16.0;


fahrenheit = celsius * 1.8 + 32.0;



temperature = (String) celsius + "," + (String) fahrenheit ;

Serial.println(temperature);

}





Recent Posts

See All
bottom of page