Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Control devices are integral to various applications, from home automation to industrial systems. Using Arduino, a versatile and user-friendly microcontroller platform, you can control devices such as lights, motors, and sensors with ease. This article will guide you through the process of setting up and controlling devices using Arduino, highlighting its importance in simplifying complex control systems. Adjustments have been made to ensure compatibility with the Arduino environment, providing code examples and detailed instructions.
Project: In this example project, we will create a simple home automation system to control a light and a fan using an Arduino. The objectives of this project are to understand how to interface relays with Arduino, control devices through digital signals, and implement basic automation logic. The functionalities include turning the light and fan on or off based on specific conditions, such as a button press or a temperature threshold.
Components List:
Examples:
// Include necessary libraries
#include <DHT.h>
// Define pin connections
#define LIGHT_RELAY_PIN 2
#define FAN_RELAY_PIN 3
#define BUTTON_PIN 4
#define DHT_PIN 5
#define DHT_TYPE DHT11
// Initialize the DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set relay pins as output
pinMode(LIGHT_RELAY_PIN, OUTPUT);
pinMode(FAN_RELAY_PIN, OUTPUT);
// Set button pin as input
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize the DHT sensor
dht.begin();
// Initially turn off the relays
digitalWrite(LIGHT_RELAY_PIN, HIGH);
digitalWrite(FAN_RELAY_PIN, HIGH);
}
void loop() {
// Read the button state
int buttonState = digitalRead(BUTTON_PIN);
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Print temperature and humidity values to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Control light based on button press
if (buttonState == LOW) {
digitalWrite(LIGHT_RELAY_PIN, LOW); // Turn on the light
} else {
digitalWrite(LIGHT_RELAY_PIN, HIGH); // Turn off the light
}
// Control fan based on temperature threshold
if (temperature > 25.0) {
digitalWrite(FAN_RELAY_PIN, LOW); // Turn on the fan
} else {
digitalWrite(FAN_RELAY_PIN, HIGH); // Turn off the fan
}
// Add a small delay to avoid bouncing issues
delay(200);
}
Explanation: