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

Understanding the Serial Monitor in Arduino

Importance and Utility of the Serial Monitor

The Serial Monitor is a crucial tool in Arduino programming as it allows communication between the Arduino board and the computer. It provides a way to send and receive data, making it easier to debug and monitor the behavior of the Arduino program. By displaying data in real-time, the Serial Monitor helps in understanding the program's execution and aids in troubleshooting any issues that may arise.

Project: Creating a Temperature and Humidity Monitor

In this project, we will create a temperature and humidity monitor using an Arduino board and a DHT11 sensor. The objective is to read the temperature and humidity values from the sensor and display them on the Serial Monitor. This project can be useful in various applications such as home automation, greenhouse monitoring, and weather stations.

List of components:

  1. Arduino Uno board
  2. DHT11 temperature and humidity sensor
  3. Jumper wires

Examples:

Example 1: Reading Temperature and Humidity Values

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

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

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature: ");
  Serial.print(DHT.temperature);
  Serial.print(" °C\t");
  Serial.print("Humidity: ");
  Serial.print(DHT.humidity);
  Serial.println(" %");
  delay(2000);
}

Explanation:

  • We include the DHT library to work with the DHT11 sensor.
  • Define the pin to which the DHT11 sensor is connected.
  • In the setup function, we initialize the Serial communication.
  • In the loop function, we read the temperature and humidity values using the DHT library.
  • The values are then printed on the Serial Monitor with appropriate units.
  • A delay of 2 seconds is added to avoid flooding the Serial Monitor with data.

Example 2: Sending Commands to Arduino via Serial Monitor

int ledPin = 13;
char command;

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

void loop() {
  if (Serial.available()) {
    command = Serial.read();
    if (command == 'H') {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED turned ON");
    } else if (command == 'L') {
      digitalWrite(ledPin, LOW);
      Serial.println("LED turned OFF");
    }
  }
}

Explanation:

  • We define an LED pin and a variable to store the received command.
  • In the setup function, we initialize the Serial communication and set the LED pin as an output.
  • In the loop function, we check if any data is available on the Serial Monitor.
  • If the received command is 'H', the LED is turned on and a corresponding message is sent to the Serial Monitor.
  • If the received command is 'L', the LED is turned off and a corresponding message is sent 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.