Introduction:

In this tutorial we are going to learn How to interface DS18B20 Temperature sensor to Arduino UNo, the Arduino Uno is a popular for many hobbyists. Its simplicity, versatility, and extensive community support make it an ideal platform for a wide range of projects. One common task in temperature monitoring projects is interfacing a temperature sensor, such as the DS18B20, with the Arduino Uno. In this blog post, we will provide a step-by-step guide on how to interface the DS18B20 sensor with an Arduino Uno, allowing you to measure and monitor temperature with ease.

Components Needed:

Before we dive into the tutorial, let’s make sure we have all the necessary components:

  1. Arduino Uno: The Arduino Uno board will serve as the main microcontroller for our project.
  2. DS18B20 Temperature Sensor: The DS18B20 is a digital temperature sensor with a built-in 1-wire interface. It provides high-precision temperature readings.
  3. Breadboard and Jumper Wires: These are essential for creating the necessary connections between the Arduino Uno and the DS18B20 sensor.
  4. 4.7kΩ Resistor: A 4.7kΩ pull-up resistor is required to connect the DS18B20 sensor to the Arduino Uno.
  5. LCD 16X 2 OR Shield of LCD compatible for Arduino

Circuit:

Step 1: Wiring the Circuit: Start by connecting the components on the breadboard as follows:

Step 2: Installing the Libraries: To interface the DS18B20 sensor with the Arduino Uno, we need to install the OneWire and DallasTemperature libraries. Here’s how you can do it:

  1. Launch the Arduino IDE on your computer.
  2. Go to “Sketch” -> “Include Library” -> “Manage Libraries”.
  3. In the “Library Manager” window, search for “OneWire” and click the “Install” button to install the library.
  4. Similarly, search for “DallasTemperature” and install the library.

Step 3: Code: write the code to read temperature data from the DS18B20 sensor. Open a new sketch in the Arduino IDE and copy the following code:

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 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);


#include <LiquidCrystal.h>
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("DS18b20 Demo");
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Temp:");
  // Start up the library
  sensors.begin();
}

/*
 * Main function, get and show the temperature
 */
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 temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  float tempC = sensors.getTempCByIndex(0);

  // Check if reading was successful
  if(tempC != DEVICE_DISCONNECTED_C) 
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
    lcd.setCursor(6,0);
    lcd.print(tempC);
    lcd.write(223);
    lcd.print("C");
  } 
  else
  {
    Serial.println("Error: Could not read temperature data");
  }
  delay(1000);
}

Step 4:

Uploading the Code: Connect your Arduino Uno to your computer using a USB cable. Make sure you have selected the correct board and port in the Arduino IDE. Then, click on the “Upload” button to upload the code to the Arduino Uno.

Step 5:

Testing the Setup: After the code is successfully uploaded to the Arduino Uno, open the Serial Monitor in the Arduino IDE. You should see the temperature readings printed on the Serial Monitor and LCD Display also

Photos

Working Video

Leave a Reply

Your email address will not be published. Required fields are marked *