Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
OneWire is a communication protocol that enables data transfer between a master device and multiple slave devices using a single data line. This protocol is particularly useful in scenarios where minimizing wiring is critical, such as in temperature sensing with DS18B20 sensors. In the Arduino environment, the OneWire library simplifies the implementation of this protocol, making it accessible for hobbyists and professionals alike. This article will guide you through the basics of OneWire communication, its importance, and how to use it with Arduino.
Project: In this project, we will create a temperature monitoring system using the DS18B20 temperature sensor. The objective is to read temperature data from the sensor and display it on the Serial Monitor. This project demonstrates the simplicity and efficiency of the OneWire protocol in gathering data from multiple sensors using minimal wiring.
Components List:
Examples:
Wiring the DS18B20 Sensor:
Arduino Code:
// Include the OneWire library
#include <OneWire.h>
// Define the pin where the DS18B20 is connected
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
void setup() {
// Start the serial communication
Serial.begin(9600);
}
void loop() {
// Reset the oneWire bus
oneWire.reset();
// Send the command to read the temperature
oneWire.write(0x44, 1); // Start temperature conversion
// Wait for the conversion to complete
delay(1000);
// Reset the oneWire bus again
oneWire.reset();
// Select the sensor
oneWire.write(0xCC); // Skip ROM command
// Read the temperature
oneWire.write(0xBE); // Read Scratchpad command
// Read the 9 bytes of data
byte data[9];
for (int i = 0; i < 9; i++) {
data[i] = oneWire.read();
}
// Convert the data to temperature
int16_t rawTemperature = (data[1] << 8) | data[0];
float celsius = rawTemperature / 16.0;
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" °C");
// Wait before the next reading
delay(2000);
}
Code Explanation: