Guide for DS18B20 Temperature Sensor with Arduino

This guide shows how to use the DS18B20 temperature sensor with the Arduino board. You’ll learn how to wire the sensor, install the required libraries and get temperature from one or multiple DS18B20 sensors.

Guide for DS18B20 Temperature Sensor with Arduino

You might also like reading other DS18B20 guides:

Introducing DS18B20 Temperature Sensor

The DS18B20 temperature sensor is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with the Arduino.

It can be powered by an external power supply or it can derive power from the data line (called “parasite mode”), which eliminates the need for an external power supply.

DS18B20 Temperature Sensor Pinout Pins Diagram

The following table shows how you should wire the DS18B20 sensor to your Arduino board:

DS18B20Arduino
GNDGND
DQAny digital pin (with 4.7k Ohm pull-up resistor)
VDD5V (normal mode) or GND (parasite mode)

Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one Arduino digital pin.

The DS18B20 temperature sensor is also available in waterproof version.

DS18B20 temperature sensor waterproof version

Here’s a summary of the most relevant specs of the DS18B20 temperature sensor:

  • Communicates over one-wire bus communication
  • Power supply range: 3.0V to 5.5V
  • Operating temperature range: -55ºC to +125ºC
  • Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)

For more information consult the DS18B20 datasheet.

Parts Required

To show you how the sensor works, we’ll build a simple example that reads the temperature from the DS18B20 sensor with the Arduino and displays the values on the Arduino Serial Monitor.

Here’s a list of parts you need to complete this tutorial

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic

The sensor can operate in two modes:

  • Normal mode: 3-wire connection is needed. You provide power to the VDD pin. Here’s the schematic you need to follow:
DS18B20 temperature sensor schematic wiring diagram normal mode
  • Parasite mode: You only need data and GND. The sensor derives its power from the data line. In this case, here’s the schematic you need to follow:
DS18B20 temperature sensor schematic wiring diagram parasite mode

You can read the temperature of more than one sensor at the same time using just one Arduino digital pin. For that, you just need to wire together all the sensors data pins to an Arduino digital pin.

Upload Code – Single DS18B20

To interface with the DS18B20 temperature sensor, you need to install the One Wire library by Paul Stoffregen and the Dallas Temperature library. Follow the next steps to install those libraries.

Installing 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.

Installing OneWire library Arduino IDE

3. Then, search for “Dallas” and install the Dallas Temperature library by Miles Burton.

Installing Dallas temperature DS18B20 library Arduino IDE

After installing the needed libraries, upload the following code to your Arduino board. This sketch is based on an example from the Dallas Temperature library.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
  Based on the Dallas Temperature Library example
*********/

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup(void)
{
  // Start serial communication for debugging purposes
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
}

void loop(void){ 
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  
  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  Serial.print(sensors.getTempCByIndex(0)); 
  Serial.print(" - Fahrenheit temperature: ");
  Serial.println(sensors.getTempFByIndex(0));
  delay(1000);
}

View raw code

There are many different ways to get the temperature from DS18B20 temperature sensors. If you’re using just one single sensor, this is one of the easiest and simplest ways.

Arduino with DS18b20 Temperature Sensor

How the Code Works

Start by including the OneWire and the DallasTemperature libraries.

#include <OneWire.h>
#include <DallasTemperature.h>

Create the instances needed for the temperature sensor. The temperature sensor is connected to Pin 4.

// Data wire is conntec to the Arduino digital pin 4
const int oneWireBus = 4;          
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

In the setup(), initialize the Serial Monitor at a baud rate of 9600.

Serial.begin(9600);

Initialize the DS18B20 temperature sensor:

sensors.begin();

In the loop() is where you’ll get the temperature. You need to call the requestTemperatures() method before getting the actual temperature value.

sensors.requestTemperatures(); 

Then, get and print the temperature in Celsius. To get the temperature in Celsius, use the getTempCByIndex() method :

Serial.print(sensors.getTempCByIndex(0)); 

Or use the getTempFByIndex() to get the temperature in Fahrenheit.

Serial.println(sensors.getTempFByIndex(0));

The getTempCByIndex() and the getTempFByIndex() methods accept the index of the temperature sensor. Because we’re using just one sensor its index is 0. If you have more than one sensor, you use index 0 for the first sensor, index 1 for the second sensor, and so on.

New temperature readings are requested every second.

delay(5000);

Demonstration

After uploading the code, open the Arduino IDE Serial Monitor at a 9600 baud rate. You should get the temperature displayed in both Celsius and Fahrenheit:

Testing DS18b20 Temperature Sensor with Arduino IDE Serial Monitor

Getting Temperature from Multiple DS18B20 Sensors

Getting Temperature from Multiple DS18B20 Sensors

The DS18B20 temperature sensor communicates using one-wire protocol and each sensor has a unique 64-bit serial code, so you can read the temperature from multiple sensors using just one single Arduino digital Pin.

Schematic

To read the temperature from multiple sensors, you just need to wire all data lines together as shown in the following schematic diagram:

Arduino Multiple DS18B20 Temperature sensors wiring schematic diagram

Upload Code – Multiple DS18B20

Then, upload the following code. It scans for all devices on Pin 4 and prints the temperature for each one. This sketch is based on the example provided by the DallasTemperature library.

/*
 * Rui Santos
 * Complete Project Details https://randomnerdtutorials.com
*/

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// 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);

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

void setup(void) {
  // start serial port
  Serial.begin(9600);
  
  // Start up the library
  sensors.begin();
  
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");

  // Loop through each device, print out address
  for(int i=0;i<numberOfDevices; i++) {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
		} else {
		  Serial.print("Found ghost device at ");
		  Serial.print(i, DEC);
		  Serial.print(" but could not detect address. Check power and cabling");
		}
  }
}

void loop(void) { 
  sensors.requestTemperatures(); // Send the command to get temperatures
  
  // Loop through each device, print out temperature data
  for(int i=0;i<numberOfDevices; i++) {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i)){
		
		// Output the device ID
		Serial.print("Temperature for device: ");
		Serial.println(i,DEC);

    // Print the data
    float tempC = sensors.getTempC(tempDeviceAddress);
    Serial.print("Temp C: ");
    Serial.print(tempC);
    Serial.print(" Temp F: ");
    Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
    } 	
  }
  delay(5000);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
  }
}

View raw code

How the code works

The code uses several useful methods to handle multiple DS18B20 sensors.

You use the getDeviceCount() method to get the number of DS18B20 sensors on the data line.

numberOfDevices = sensors.getDeviceCount();

The getAddress() method finds the sensors addresses:

if(sensors.getAddress(tempDeviceAddress, i)){

The address is unique for each sensor. So each sensor can be identified by its address.

Then, you use the getTempC() method that accepts as argument the device address. With this method you can get the temperature from a specific sensor:

float tempC = sensors.getTempC(tempDeviceAddress);

To get the temperature in Fahrenheit degrees, you can use the getTemF(). Alternatively, you can convert the temperature in Celsius to Fahrenheit as follows:

DallasTemperature::toFahrenheit(tempC)

Wrapping Up

The DS18B20 temperature sensor is a one-wire digital sensor. To use this sensor with the Arduino, you need the OneWire and the DallasTemperature libraries. You can use one sensor or multiple sensors on the same data line because you can identify each sensor by its unique address.

Now, you can take this project further and display your sensor readings in an OLED display, for example.

We have more tutorials for other Arduino compatible sensors that you may find useful:

We hope you’ve found this guide useful.

If you want to learn more about Arduino, take a look at our resources:

Thanks for reading!

Updated July 2, 2019



Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

45 thoughts on “Guide for DS18B20 Temperature Sensor with Arduino”

  1. Hello J uses your tutorial . J have errors at compile on dallastemperature.h Can you help me by giving me the dallastemperature library works
    Best regards
    Pierre

    Reply
  2. Hi Rui,

    Thanks for your tutorial.

    I need to compare two temperature sensors, lets say T1 and T2 and control a relay when T1 e higher than T2.
    How can i achieve this?

    Kind regards,
    Rodrigo Catarino

    Reply
    • Hi Rodrigo. You need an if statement that compares both temperatures and turns the relay on when T1 > T2.
      Controlling a relay is the same thing as controlling an LED but with inverted logic.
      digitalWrite(relay, HIGH); –> turns the relay off
      digitalWrite(relay, LOW); –> turns the relay on

      You may want to take a look at this tutorial to see how to wire the relay to the Arduino: https://randomnerdtutorials.com/guide-for-relay-module-with-arduino/
      I hope this helps.
      Regards,
      Sara 🙂

      Reply
  3. Hello,
    I’m working on a project using an Arduino Zero that as an Operating Voltage at 3.3 V. Do you think it may varied the pull-up resistor value?
    Thanks you !

    Reply
    • i have the same problem, if you have found a fix please share. i have tried couple of temperature sensors. but the temp. is still -127. and the circuit wiering is ok.

      Reply
    • Yes, I have solved this problem.
      Make sure that the power of 5 v, 300mA reaches well to ds18b20. and use a small wire-like Telephone-cable to 5 miter

      Reply
  4. Dear Sara, thank you for the program, it works great!
    I was wondering if there is a way to increase the speed of the functions.
    Either requestTemperatures or getTempCbyIndex works rather slowly and i’d prefer something quicker.
    Cheers,

    Reply
  5. hi
    have tried 3xDS18B20 i works but i get 3 different temperaturens with about a 4 degrees one each 16, 24,20 degrees i dont understand why that is ???

    Reply
  6. Hi, I’m an engineering student and was hoping to use this code to collect temperature averages which can trigger a relay command, I tried changing the if loop to give each sensor reading its own individual tempC1, tempC2, tempC3, which will then be added into an average. Could someone help me with me please?

    Reply
      • Hi, I managed to get it working, I had failed to add the needed } at the end and caused a failure to verify. I have posted my code below, I will now try and find a way to take the average of each Temp(0-2) C entries and if that average is above a specified temp, activate a relay.

        /*
        * Rui Santos
        * Complete Project Details https://randomnerdtutorials.com
        */

        #include <OneWire.h>
        #include <DallasTemperature.h>

        // Data wire is plugged into port 4 on the Arduino
        #define ONE_WIRE_BUS 4
        // 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);

        int numberOfDevices; // Number of temperature devices found

        DeviceAddress tempDeviceAddress; // We’ll use this variable to store a found device address

        void setup(void) {
        // start serial port
        Serial.begin(9600);

        // Start up the library
        sensors.begin();

        // Grab a count of devices on the wire
        numberOfDevices = sensors.getDeviceCount();

        // locate devices on the bus
        Serial.print(“Locating devices…”);
        Serial.print(“Found “);
        Serial.print(numberOfDevices, DEC);
        Serial.println(” devices.”);

        // Loop through each device, print out address
        for(int i=0;i<numberOfDevices; i++) {
        // Search the wire for address
        if(sensors.getAddress(tempDeviceAddress, i)) {
        Serial.print(“Found device “);
        Serial.print(i, DEC);
        Serial.print(” with address: “);
        printAddress(tempDeviceAddress);
        Serial.println();
        } else {
        Serial.print(“Found ghost device at “);
        Serial.print(i, DEC);
        Serial.print(” but could not detect address. Check power and cabling”);
        }
        }
        }

        void loop(void) {
        sensors.requestTemperatures(); // Send the command to get temperatures

        // Loop through each device, print out temperature data
        for(int i=0;i<numberOfDevices; i++) {
        // Search the wire for address
        if(sensors.getAddress(tempDeviceAddress, i=0)){

        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // Print the data
        float tempC0 = sensors.getTempC(tempDeviceAddress);
        Serial.print("Temp0 C: ");
        Serial.print(tempC0);
        Serial.print(" Temp0 F: ");
        Serial.println(DallasTemperature::toFahrenheit(tempC0)); // Converts tempC to Fahrenheit

        }

        if(sensors.getAddress(tempDeviceAddress, i=1)){

        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // Print the data
        float tempC1 = sensors.getTempC(tempDeviceAddress);
        Serial.print("Temp1 C: ");
        Serial.print(tempC1);
        Serial.print(" Temp1 F: ");
        Serial.println(DallasTemperature::toFahrenheit(tempC1)); // Converts tempC to Fahrenheit

        }

        if(sensors.getAddress(tempDeviceAddress, i=2)){

        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // Print the data
        float tempC2 = sensors.getTempC(tempDeviceAddress);
        Serial.print("Temp2 C: ");
        Serial.print(tempC2);
        Serial.print(" Temp2 F: ");
        Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit

        }

        }
        delay(5000);
        }

        // function to print a device address
        void printAddress(DeviceAddress deviceAddress) {
        for (uint8_t i = 0; i < 8; i++) {
        if (deviceAddress[i] < 16) Serial.print(“0”);
        Serial.print(deviceAddress[i], HEX);
        }
        }

        Reply
        • I suggest you read up on indenting your code, it will make it easier to find those kinds of errors. You should have received an error from the compiler and no code would have been generated. There is also no need for the uint8_t, int will work.

          Here is an example:

          // function to print a device address
          void printAddress(DeviceAddress deviceAddress)
          {
          for (int i = 0; i < 8; i++)
          {
          if (deviceAddress[i] < 16)
          Serial.print(“0”);
          Serial.print(deviceAddress[i], HEX);
          }
          }

          Reply
          • Thanks for the advice, i hadnt messed with that part. That uint8_t is from the articles code. Ill make the suggested change, whats the difference between int and uint8_t?

          • int is a signed value and its size (number of bits) depends on the CPU. Usually 16, 32 or 64.
            uint8_t is unsigned (cannot have a negative value) and is always 8 bits, so the largest value it can represent is 2 ** 8 – 1. You only need to use it when you really care about the size and sign.

          • I’ve tried creating the average by using float as shown on the code below, but im getting a message saying tempC0, tempC1, tempC2 are not declared, how can i keep the variables outside of their if statements?

            if(sensors.getAddress(tempDeviceAddress, i=2)){

            // Output the device ID
            Serial.print("Temperature for device: ");
            Serial.println(i,DEC);

            // Print the data
            float tempC2 = sensors.getTempC(tempDeviceAddress);
            Serial.print("Temp2 C: ");
            Serial.print(tempC2);
            Serial.print(" Temp2 F: ");
            Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit

            }

            float x = tempC0+tempC1+tempC2;

            }
            delay(5000);
            }

    • // Put this above all refs to the tempCns
      float tempC1, tempC2, tempC3;

      if(sensors.getAddress(tempDeviceAddress, i=2)){

      // Output the device ID
      Serial.print("Temperature for device: ");
      Serial.println(i,DEC);

      // Print the data
      // Remove the floats here
      tempC2 = sensors.getTempC(tempDeviceAddress);
      Serial.print("Temp2 C: ");
      Serial.print(tempC2);
      Serial.print(" Temp2 F: ");
      Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit

      }

      float x = tempC0+tempC1+tempC2;

      }
      delay(5000);
      }

      Reply
      • thank you for your help, ive tried using the float before the if statements but it returns x as 0 because the tempCns are 0 values out side the if statements, the full for statement is below. The if statements are not “sharing” their variables with the for statements.

        void loop(void) {
        sensors.requestTemperatures(); // Send the command to get temperatures

        // Loop through each device, print out temperature data
        for(int i=0;i<numberOfDevices; i++) {

        float tempC0, tempC1, tempC2;

        // Search the wire for address
        if(sensors.getAddress(tempDeviceAddress, i=0)){

        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // Print the data
        float tempC0 = sensors.getTempC(tempDeviceAddress);
        Serial.print("Temp0 C: ");
        Serial.println(tempC0);

        }

        if(sensors.getAddress(tempDeviceAddress, i=1)){

        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // Print the data
        float tempC1 = sensors.getTempC(tempDeviceAddress);
        Serial.print("Temp1 C: ");
        Serial.println(tempC1);

        }

        if(sensors.getAddress(tempDeviceAddress, i=2)){

        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // Print the data
        float tempC2 = sensors.getTempC(tempDeviceAddress);
        Serial.print("Temp2 C: ");
        Serial.println(tempC2);

        }
        float x = tempC0+tempC1+tempC2;
        Serial.println(x);

        }
        delay(5000);
        }

        Reply
        • You missed one change and I left out one detail:

          float tempC1, tempC2, tempC3 = 0.0;

          if(sensors.getAddress(tempDeviceAddress, i=2)){

          // Output the device ID
          Serial.print(“Temperature for device: “);
          Serial.println(i,DEC);

          // Print the data
          // Remove the floats here
          /* no float here */ tempC2 = sensors.getTempC(tempDeviceAddress);
          Serial.print(“Temp2 C: “);
          Serial.print(tempC2);
          Serial.print(” Temp2 F: “);
          Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit

          }

          Reply
          • Thanks for all the help, got the code working with the relay and OLED screen, ive added my code below for anyone who wants it in the future.

            /*
            * Rui Santos
            * Complete Project Details https://randomnerdtutorials.com
            * Modified by Hector Isava so it averages 3 thermistors
            * triggers a relay if above a set temperature
            * and displays each temperature on an OLED screen
            */

            #include <OneWire.h>
            #include <DallasTemperature.h>
            #include <Adafruit_GFX.h>
            #include <Adafruit_SSD1306.h>

            // Data wire is plugged into port 4 on the Arduino
            #define ONE_WIRE_BUS 4

            #define OLED_RESET 4
            #define SCREEN_WIDTH 128 // OLED display width, in pixels
            #define SCREEN_HEIGHT 64 // OLED display height, in pixels
            #define NUM_PAGE 8
            #define NUMFLAKES 10

            #define XPOS 0
            #define YPOS 0
            #define LOGO16_GLCD_HEIGHT 16
            #define LOGO16_GLCD_WIDTH 16

            // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
            Adafruit_SSD1306 display(OLED_RESET);

            // 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);

            int numberOfDevices; // Number of temperature devices found

            DeviceAddress tempDeviceAddress; // We’ll use this variable to store a found device address

            // Add relay pin on arduino
            int RELAY_PIN = 3;

            void setup(void) {
            // start serial port
            Serial.begin(9600);

            // Start up the library
            sensors.begin();

            // Grab a count of devices on the wire
            numberOfDevices = sensors.getDeviceCount();

            // locate devices on the bus
            Serial.print(“Locating devices…”);
            Serial.print(“Found “);
            Serial.print(numberOfDevices, DEC);
            Serial.println(” devices.”);

            // Loop through each device, print out address
            for(int i=0;i<numberOfDevices; i++) {
            // Search the wire for address
            if(sensors.getAddress(tempDeviceAddress, i)) {
            Serial.print(“Found device “);
            Serial.print(i, DEC);
            Serial.print(” with address: “);
            printAddress(tempDeviceAddress);
            Serial.println();
            } else {
            Serial.print(“Found ghost device at “);
            Serial.print(i, DEC);
            Serial.print(” but could not detect address. Check power and cabling”);
            }
            }
            // Set relay as digital output
            pinMode(RELAY_PIN, OUTPUT);

            // Initializing OLED display
            display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

            display.clearDisplay();
            display.setTextSize(2);
            display.setTextColor(WHITE);
            display.setCursor(0,0);
            display.println(“License to Chill”);
            display.display();

            delay(2000);
            }

            void loop(void) {
            sensors.requestTemperatures(); // Send the command to get temperatures

            // Loop through each device, print out temperature data
            for(int i=0;i<numberOfDevices; i++) {

            float tempC0, tempC1, tempC2 = 0.0;

            // Search the wire for address
            if(sensors.getAddress(tempDeviceAddress, i=0)){

            // Output the device ID
            Serial.print("Temperature for device: ");
            Serial.println(i,DEC);

            // Print the data
            tempC0 = sensors.getTempC(tempDeviceAddress);
            Serial.print("Temp0 C: ");
            Serial.println(tempC0);

            }

            if(sensors.getAddress(tempDeviceAddress, i=1)){

            // Output the device ID
            Serial.print("Temperature for device: ");
            Serial.println(i,DEC);

            // Print the data
            tempC1 = sensors.getTempC(tempDeviceAddress);
            Serial.print("Temp1 C: ");
            Serial.println(tempC1);

            }

            if(sensors.getAddress(tempDeviceAddress, i=2)){

            // Output the device ID
            Serial.print("Temperature for device: ");
            Serial.println(i,DEC);

            // Print the data
            tempC2 = sensors.getTempC(tempDeviceAddress);
            Serial.print("Temp2 C: ");
            Serial.println(tempC2);

            }
            float x = (tempC0+tempC1+tempC2)/3;
            Serial.print("Average Temperature: ");
            Serial.println(x);
            Serial.println();

            if(x > 22){ // change number for desired refrigeration temperature
            digitalWrite(RELAY_PIN, HIGH);
            } else {
            digitalWrite(RELAY_PIN, LOW);
            }

            //clear display
            display.clearDisplay();

            // display average temperature
            display.setTextSize(1);
            display.setCursor(0,0);
            display.print("Average Temperature: ");
            display.setTextSize(2);
            display.setCursor(0,10);
            display.print(x);
            display.print(" ");
            display.setTextSize(1);
            display.cp437(true);
            display.write(167);
            display.setTextSize(2);
            display.print("C");
            display.display();
            delay(2000);

            // display temperature from thermistor 0
            display.clearDisplay();
            display.setTextSize(1);
            display.setCursor(0,0);
            display.print("Temperature 0: ");
            display.setTextSize(2);
            display.setCursor(0,10);
            display.print(tempC0);
            display.print(" ");
            display.setTextSize(1);
            display.cp437(true);
            display.write(167);
            display.setTextSize(2);
            display.print("C");
            display.display();
            delay(2000);

            // display temperature from thermistor 1
            display.clearDisplay();
            display.setTextSize(1);
            display.setCursor(0,0);
            display.print("Temperature 1: ");
            display.setTextSize(2);
            display.setCursor(0,10);
            display.print(tempC1);
            display.print(" ");
            display.setTextSize(1);
            display.cp437(true);
            display.write(167);
            display.setTextSize(2);
            display.print("C");
            display.display();
            delay(2000);

            // display temperature from thermistor 2
            display.clearDisplay();
            display.setTextSize(1);
            display.setCursor(0,0);
            display.print("Temperature 2: ");
            display.setTextSize(2);
            display.setCursor(0,10);
            display.print(tempC2);
            display.print(" ");
            display.setTextSize(1);
            display.cp437(true);
            display.write(167);
            display.setTextSize(2);
            display.print("C");
            display.display();
            delay(2000);

            }
            }

            // function to print a device address
            void printAddress(DeviceAddress deviceAddress) {
            for (int i = 0; i < 8; i++) {
            if (deviceAddress[i] < 16) Serial.print(“0”);
            Serial.print(deviceAddress[i], HEX);
            }
            }

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.