DS18B20 temperature sensor with Arduino setup

1. Required Components

For best results, ensure all components are compatible with your Arduino model. The DS18B20 comes in TO-92 package or as a waterproof probe, depending on your application (e.g., liquid temperature monitoring).

2. DS18B20 Pinout

Follow these connections to interface the DS18B20 sensor with your Arduino:

Note: Place a 4.7kΩ resistor between the DQ and VDD pins for proper operation. This pull-up resistor ensures stable communication over the OneWire protocol.

For parasitic power mode (optional), connect VDD to GND and rely on the DQ pin for both power and data, though this may reduce reliability in long-term use.

3. Code Explanation

The code utilizes the OneWire and DallasTemperature libraries to manage communication with the DS18B20 sensor. Here's an overview of the main functions:

Installing Required Libraries

Before uploading the code, install the required libraries:

  1. In the Arduino IDE, navigate to Sketch > Include Library > Manage Libraries....
  2. Search for OneWire by Paul Stoffregen and click "Install."
  3. Search for DallasTemperature by Miles Burton and click "Install."
  4. Ensure versions are up-to-date to avoid compatibility issues.

These libraries simplify the process of reading data from the DS18B20, abstracting the low-level OneWire protocol timing requirements.

4. Arduino Code

Use the following code to read the temperature data from the DS18B20 sensor and display it in both Celsius and Fahrenheit:


// Include necessary libraries
#include 
#include 

// Pin to which the DS18B20 data line is connected
#define ONE_WIRE_BUS 2

// Initialize OneWire instance and pass it to DallasTemperature library
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  // Start serial communication
  Serial.begin(9600);
  Serial.println("DS18B20 Temperature Sensor Demo");
  
  // Initialize the temperature sensor library
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount());
  Serial.println(" sensor(s)");
}

void loop() {
  // Request temperature data from the sensor
  sensors.requestTemperatures();
  
  // Get temperature in Celsius and Fahrenheit
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);

  // Check if reading is valid
  if (temperatureC == DEVICE_DISCONNECTED_C) {
    Serial.println("Error: Sensor disconnected");
    return;
  }

  // Print the temperature to the serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.print(" °C | ");
  Serial.print(temperatureF);
  Serial.println(" °F");

  // Wait for 1 second before the next reading
  delay(1000);
}
            

This enhanced version includes error checking and displays temperatures in both units for broader usability.

5. Upload and Test

Follow these steps to upload the code and test your setup:

  1. Connect your Arduino to your PC using a USB cable.
  2. Open the Arduino IDE and paste the code above into a new sketch.
  3. Install the required libraries if you haven’t already.
  4. Select the correct board (e.g., Arduino Uno) and port under Tools in the IDE.
  5. Click the upload button to program your Arduino.
  6. Open the Serial Monitor (set baud rate to 9600) to see real-time temperature readings.
  7. Test the sensor by warming it (e.g., with your fingers) or cooling it (e.g., with ice) to verify responsiveness.

Expected output should show consistent temperature updates every second, reflecting ambient conditions.

6. Additional Tips

7. Troubleshooting

If you encounter issues, check the following:

Use the Serial Monitor to debug by adding extra Serial.println() statements to trace variable values.

8. Conclusion

This experiment demonstrated how to interface the DS18B20 sensor with an Arduino to measure and display temperature data. This simple yet powerful setup serves as a foundation for numerous applications, including weather stations, home automation systems, industrial temperature monitoring, or even aquaponics systems for precise water temperature control.

With its low cost, high accuracy, and expandability, the DS18B20 is an excellent choice for hobbyists and professionals alike. Experiment further by adding data visualization (e.g., via Processing or Python) or integrating with IoT platforms like Blynk or ThingSpeak.

Contact Us

If you have any questions or inquiries, feel free to reach out to us at Microautomation.no@icloud.com .

Follow our Socials for the newest updates!