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

Wire Library

The Wire library in Arduino provides a simple and efficient way to communicate with I2C devices. I2C (Inter-Integrated Circuit) is a widely used communication protocol for connecting multiple devices on the same bus. The Wire library abstracts the low-level details of I2C communication, making it easier for Arduino users to interface with I2C devices.

The Wire library is important for Arduino users because it allows them to easily connect and communicate with a wide range of I2C devices such as sensors, displays, and other peripherals. By using the Wire library, Arduino users can take advantage of the functionalities provided by these devices without having to deal with the complexities of the I2C protocol.

To align the Wire library with the Arduino environment, the library provides a set of functions that are specifically designed for Arduino boards. These functions abstract the hardware-specific details of I2C communication and provide a simple and consistent interface for Arduino users.

Project: For this example, let's create a project that reads data from a temperature and humidity sensor (DHT11) and displays the readings on an OLED display (SSD1306) using the Wire library.

Components List:

  • Arduino Uno (1)
  • DHT11 Temperature and Humidity Sensor (1)
  • SSD1306 OLED Display (1)

Examples:

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
  Serial.begin(9600);
  dht.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("Temperature:");
  display.println("Humidity:");
  display.display();
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  display.setCursor(80, 0);
  display.println(temperature, 1);
  display.setCursor(80, 16);
  display.println(humidity, 1);
  display.display();

  delay(2000);
}

In this example, we include the necessary libraries: Wire, Adafruit_SSD1306, and DHT. We define the pin for the DHT11 sensor and the type of sensor (DHTTYPE). We create instances of the DHT and Adafruit_SSD1306 classes.

In the setup function, we initialize the serial communication, start the DHT sensor, and initialize the SSD1306 display. We set up the display by clearing it, setting the text color to white, setting the text size to 1, and positioning the cursor at the top-left corner. We print the labels for temperature and humidity readings on the display and then display it.

In the loop function, we read the temperature and humidity values from the DHT sensor. We update the display by positioning the cursor at the appropriate locations and printing the temperature and humidity values. Finally, we display the updated content on the OLED display and introduce a delay of 2 seconds before repeating the process.

This example demonstrates how the Wire library can be used to communicate with the DHT11 sensor and the SSD1306 display, allowing us to read temperature and humidity values and display them on the OLED display.

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.