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

Serial Communication with Arduino

Importance and Utility of Serial Communication

Serial communication is a crucial aspect of Arduino programming and is widely used in various applications. It allows the Arduino board to communicate with other devices such as computers, sensors, and actuators. Serial communication enables data transfer between the Arduino and external devices, enabling control, monitoring, and data acquisition.

Serial communication is essential in projects that require real-time monitoring or control. It allows for bidirectional communication, enabling the Arduino to receive commands and send data to external devices simultaneously. This capability is particularly useful in applications such as home automation, robotics, and sensor networks.

Project: Serial Communication with a Computer

In this example project, we will create a simple Arduino-based system that communicates with a computer via serial communication. The objective of this project is to send sensor data from the Arduino to the computer and receive commands from the computer to control an actuator.

Functionalities:

  1. Read sensor data (e.g., temperature, humidity) using appropriate sensors connected to the Arduino.
  2. Send the sensor data to the computer via serial communication.
  3. Receive commands from the computer to control an actuator (e.g., an LED or a motor).
  4. Execute the received commands to control the actuator.

List of Components:

Examples:

  1. Reading Sensor Data and Sending it to the Computer:
    
    #include <DHT.h>

define DHTPIN 2

define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() { Serial.begin(9600); dht.begin(); }

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

Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" °C\t");

Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %");

delay(2000); }


2. Receiving Commands from the Computer and Controlling an LED:
```cpp
int ledPin = 13;
char command;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    command = Serial.read();

    if (command == '1') {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED turned ON");
    } else if (command == '0') {
      digitalWrite(ledPin, LOW);
      Serial.println("LED turned OFF");
    }
  }
}

In the first example, the Arduino reads temperature and humidity data from a sensor and sends it to the computer via serial communication. The received data is then displayed on the computer's serial monitor.

In the second example, the Arduino receives commands from the computer to control an LED. When the computer sends '1', the LED turns on, and when it sends '0', the LED turns off.

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.