Arduino Mega 2560 + DHT11 Sensor Module Documentation
The Arduino Mega 2560 is a microcontroller board based on the ATmega2560 chip, while the DHT11 sensor module is a low-cost, digital temperature and humidity sensor. Combining these two components enables the creation of IoT projects that can measure and respond to environmental conditions.
Arduino Mega 2560:
+ Microcontroller: ATmega2560
+ Operating Voltage: 5V
+ Input Voltage: 7-12V
+ Digital I/O Pins: 54
+ Analog Input Pins: 16
DHT11 Sensor Module:
+ Temperature Range: 0-50C (32-122F)
+ Humidity Range: 20-80% RH
+ Accuracy: 2C (3.6F) for temperature, 5% RH for humidity
+ Communication Protocol: Digital serial interface
Arduino Mega 2560:
+ Digital Pins: D0-D53
+ Analog Pins: A0-A15
DHT11 Sensor Module:
+ VCC: Connect to Arduino's 5V pin
+ GND: Connect to Arduino's GND pin
+ SIG: Connect to any digital pin on Arduino (e.g., D2)
### Example 1: Temperature and Humidity Monitoring
This example demonstrates how to read temperature and humidity data from the DHT11 sensor module using the Arduino Mega 2560.
#define DHT_PIN 2 // Connect DHT11 SIG pin to Arduino's D2 pin
void setup() {
Serial.begin(9600);
}
void loop() {
int chk = DHT.read11(DHT_PIN);
if (chk == DHTLIB_OK) {
Serial.print("Temperature: ");
Serial.print(DHT.temperature, 1);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(DHT.humidity, 1);
Serial.println(" %");
} else {
Serial.println("Error reading DHT11 sensor");
}
delay(2000);
}
```
### Example 2: Smart Home Automation with Temperature and Humidity Control
This example demonstrates how to use the DHT11 sensor module with Arduino Mega 2560 to control a relay module, which can be used to switch on/off devices (e.g., fans, heaters) based on temperature and humidity levels.
#define DHT_PIN 2 // Connect DHT11 SIG pin to Arduino's D2 pin
#define RELAY_PIN 13 // Connect relay module to Arduino's D13 pin
#define TEMPERATURE_THRESHOLD 25 // Set temperature threshold (C)
#define HUMIDITY_THRESHOLD 60 // Set humidity threshold (%)
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int chk = DHT.read11(DHT_PIN);
if (chk == DHTLIB_OK) {
float temperature = DHT.temperature;
float humidity = DHT.humidity;
if (temperature > TEMPERATURE_THRESHOLD || humidity > HUMIDITY_THRESHOLD) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay (e.g., turn on fan)
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the relay (e.g., turn off fan)
}
} else {
Serial.println("Error reading DHT11 sensor");
}
delay(2000);
}
```
DHT11 sensor module datasheet: [https://www.mouser.com/ds/2/758/DHT11-281116.pdf](https://www.mouser.com/ds/2/758/DHT11-281116.pdf)
Arduino Mega 2560 datasheet: [https://www.arduino.cc/en/Main/arduinoBoardMega2560](https://www.arduino.cc/en/Main/arduinoBoardMega2560)
DHT library for Arduino: [https://github.com/adafruit/DHT-sensor-library](https://github.com/adafruit/DHT-sensor-library)