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

Understanding and Implementing OneWire Communication with Arduino

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:

  • Arduino Uno (1)
  • DS18B20 Temperature Sensor (1)
  • 4.7k Ohm Resistor (1)
  • Breadboard (1)
  • Jumper Wires (several)

Examples:

  1. Wiring the DS18B20 Sensor:

    • Connect the VCC pin of the DS18B20 to the 5V pin on the Arduino.
    • Connect the GND pin of the DS18B20 to the GND pin on the Arduino.
    • Connect the DATA pin of the DS18B20 to digital pin 2 on the Arduino.
    • Place a 4.7k Ohm resistor between the VCC and DATA pins of the DS18B20.
  2. 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:

  • #include <OneWire.h>: This line includes the OneWire library, which provides the necessary functions to communicate with OneWire devices.
  • #define ONE_WIRE_BUS 2: This line defines the pin number where the DS18B20 sensor is connected.
  • OneWire oneWire(ONE_WIRE_BUS);: This line initializes the OneWire instance on the specified pin.
  • void setup(): This function initializes serial communication at a baud rate of 9600.
  • void loop(): This function runs repeatedly, reading the temperature from the DS18B20 sensor and printing it to the Serial Monitor.

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.