Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Temperature Measurement with DS18B20 Sensor and Arduino

The DS18B20 is a digital temperature sensor popular for its accuracy and simplicity. It communicates over a 1-Wire bus, making it easy to interface with microcontrollers like Arduino. This article will guide you through setting up the DS18B20 sensor with an Arduino to measure temperature. Understanding how to use this sensor is crucial for projects involving environmental monitoring, home automation, and other applications requiring precise temperature readings.

Project: In this project, we will create a simple temperature monitoring system using the DS18B20 sensor and an Arduino. The system will read the temperature from the DS18B20 sensor and display it on the Serial Monitor of the Arduino IDE. This project aims to familiarize you with interfacing the DS18B20 sensor with Arduino, reading temperature data, and displaying it.

Components List:

  1. Arduino Uno - 1
  2. DS18B20 Temperature Sensor - 1
  3. 4.7kΩ Resistor - 1
  4. Breadboard - 1
  5. Jumper Wires - Several

Examples:

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

// Data wire is connected to pin 2 on the Arduino
#define ONE_WIRE_BUS 2

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

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

void setup(void) {
  // Start serial communication for debugging
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

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

void loop(void) {
  // Request temperature readings from all devices on the bus
  sensors.requestTemperatures();

  // Fetch and print the temperature in Celsius
  float temperatureC = sensors.getTempCByIndex(0);
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.println(" °C");

  // Wait 1 second before repeating the loop
  delay(1000);
}

Explanation:

  • Libraries: We include the OneWire and DallasTemperature libraries to handle communication with the DS18B20 sensor.
  • Pin Definition: We define the data pin for the DS18B20 sensor (pin 2 in this case).
  • OneWire Instance: We create an instance of the OneWire class to communicate with the sensor.
  • DallasTemperature Instance: We pass the OneWire instance to the DallasTemperature library.
  • Setup Function: Initializes serial communication and starts the DallasTemperature library.
  • Loop Function: Requests temperature readings, retrieves the temperature in Celsius from the sensor, and prints it to the Serial Monitor every second.

Common Challenges:

  1. Incorrect Wiring: Ensure the DS18B20 sensor is correctly wired, with the data pin connected to the defined Arduino pin, and the 4.7kΩ pull-up resistor between the data pin and Vcc.
  2. Library Installation: Make sure the OneWire and DallasTemperature libraries are installed in the Arduino IDE.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.