0
0
AutocadHow-ToBeginner · 4 min read

Arduino Project for Weather Station: Simple Guide & Code

An Arduino weather station project uses sensors like DHT11 to measure temperature and humidity, then displays data on a serial monitor or LCD. You connect the sensor to the Arduino, write code to read sensor data, and output the results for monitoring weather conditions.
📐

Syntax

The basic syntax for reading a DHT11 sensor in Arduino involves including the sensor library, defining the sensor pin, initializing the sensor, and then reading temperature and humidity values inside the loop() function.

Key parts:

  • #include <DHT.h>: Includes the sensor library.
  • DHT dht(pin, type);: Creates sensor object with pin number and sensor type.
  • float temp = dht.readTemperature();: Reads temperature.
  • float hum = dht.readHumidity();: Reads humidity.
  • Serial.print(): Prints data to serial monitor.
arduino
#include <DHT.h>

#define DHTPIN 2     // Pin where the sensor is connected
#define DHTTYPE DHT11   // Sensor type

DHT dht(DHTPIN, DHTTYPE);

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

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

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");

  delay(2000); // Wait 2 seconds before next reading
}
💻

Example

This example shows a simple Arduino weather station using a DHT11 sensor to measure temperature and humidity. The data is printed to the serial monitor every 2 seconds.

arduino
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

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

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

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");

  delay(2000);
}
Output
Humidity: 45.00 % Temperature: 23.00 *C Humidity: 46.00 % Temperature: 23.50 *C Humidity: 45.50 % Temperature: 23.20 *C
⚠️

Common Pitfalls

Common mistakes when building an Arduino weather station include:

  • Not connecting the sensor to the correct pin or power supply.
  • Forgetting to install or include the DHT library.
  • Not checking if sensor readings are valid (using isnan() to detect errors).
  • Using the wrong sensor type in code (e.g., DHT22 vs DHT11).
  • Not adding delays between readings, causing sensor errors.
arduino
// Wrong: Missing sensor initialization
void setup() {
  Serial.begin(9600);
  // dht.begin(); // Forgot this line
}

// Right: Proper sensor start
void setup() {
  Serial.begin(9600);
  dht.begin();
}
📊

Quick Reference

Tips for your Arduino weather station:

  • Use DHT11 or DHT22 sensors for temperature and humidity.
  • Connect sensor power to 5V and ground correctly.
  • Use a pull-up resistor (4.7kΩ) on the data pin if needed.
  • Check sensor readings with isnan() before using them.
  • Print data to serial monitor or display on LCD for easy viewing.

Key Takeaways

Use a DHT sensor with Arduino to measure temperature and humidity easily.
Always initialize the sensor with dht.begin() in setup().
Check sensor readings for errors using isnan() before printing or using data.
Add delays between readings to avoid sensor communication issues.
Connect sensor pins correctly and use pull-up resistors if required.