Arduino Project : Weather station

 Arduino Weather Station




Get project source code below the pdf:👇



Project Source Code

 
#include <Wire.h> //for I2C communication
#include <Adafruit_GFX.h> // include library for the OLED
#include <Adafruit_SSD1306.h> // include library for the OLED
#include <SFE_BMP180.h> // include library for the pressure sensor
#include <DHT.h> //include library for the temperature and humidity sensor

#define DHTPIN 2     // pin that DTH11 is connected
#define DHTTYPE DHT11   // DHT 11
#define OLED_RESET 4    //OLED
#define DS3231_I2C_ADDRESS 0x68
Adafruit_SSD1306 display(OLED_RESET);
#define ALTITUDE 100 // set with your altitude

SFE_BMP180 pressure;

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// Convert normal decimal numbers to binary coded decimal (used to convert time)
byte decToBcd(byte val){
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
  return( (val/16*10) + (val%16) );
}

void setup(){
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else {
    // Oops, something went wrong, this is usually a connection problem,
    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
  Wire.begin();  // initialize wire communication
  dht.begin();  //initialize  dht sensor 
  Serial.begin(9600); // initialize  serial monitor
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize display
  
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // uncomment the following line after setting up the time once
  setDS3231time(33,21,11,5,29,9,16);
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){
  //set the time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year){
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

// Displays data and time on the OLED display
void displayTime(){ 
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  display.clearDisplay(); //clearing the display
  display.setTextColor(WHITE); //setting the color to white
  display.setTextSize(1); //setting the color size
  display.setCursor(0,0); //set the cursor coordinates
  // dispaying the day of the week
  switch(dayOfWeek){ 
    case 1:
      display.print("Sunday");
      break;
    case 2:
      display.print("Monday");
      break;
    case 3:
      display.print("Tuesday");
      break;
    case 4:
      display.print("Wednesday");
      break;
    case 5:
      display.print("Thursday");
      break;
    case 6:
      display.print("Friday");
      break;
    case 7:
     display.print("Saturday");
      break;
  }
  display.setCursor(0,20); // setting the cursor coordinates
  display.print(hour, DEC); //display the hour
  // convert the byte variable to a decimal number when displayed
  
  display.print(":");
  if (minute<10){
    display.print("0");
  }
  display.print(minute, DEC);
  display.print(":");
  if (second<10){
    display.print("0");
  }
  display.print(second, DEC);
  display.setCursor(0,10);
  display.print(dayOfMonth, DEC);
  display.print("/");
  display.print(month, DEC);
  display.print("/");
  display.print(year, DEC);
}
// Displays the temperature and humidity
void displayTempHumid(){

  delay(2000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
   display.clearDisplay();
   display.setTextColor(WHITE);
   display.setTextSize(1);
   display.setCursor(5,0);
   display.print("Failed to read from DHT sensor!");
   return;
  }
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Humidity: "); 
  display.print(h);
  display.print(" %\t");
  
  // Comments this section and uncomment below if you prefer to display temperature in Fahrenheit
  display.setCursor(0,10);
  display.print("Temp: "); 
  display.print(t);
  display.print(" C"); 
  
  // Uncomment this part if you want to display the temperature in Fahrenheit
  /*display.setCursor(0,10);
  display.print("Temp: "); 
  display.print(t*1.8+32);
  display.print(" F"); */
}

// Displays pressure
void displayPressure(){ 
  char status;
  double T,P,a;
  status = pressure.startTemperature();
  if (status != 0){
    // Wait for the measurement to complete:
    delay(status);
    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.
    status = pressure.getTemperature(T);
    if (status != 0){
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0){
        // Wait for the measurement to complete:
        delay(status);

        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P,T);
        if (status != 0){
          // Print out the measurement:
          display.setTextColor(WHITE);
          display.setTextSize(1);
          display.setCursor(0,20);
          display.print("Pressure: ");
          display.print(P,2);
          display.print(" mb ");
        }
        else Serial.println("error retrieving pressure measurement\n"); //for debugging
      }
      else Serial.println("error starting pressure measurement\n"); //for debugging
    }
    else Serial.println("error retrieving temperature measurement\n"); //for debugging
  }
  else Serial.println("error starting temperature measurement\n"); //for debugging
}
void loop(){
  displayTempHumid();
  displayPressure();
  display.display();
  delay(3000);
  displayTime(); 
  display.display();
  delay(3000);
}


Explaination:

Arduino Simple Weather Station: A Fun and Easy DIY Project

Have you ever wanted to build your own weather station to track temperature and humidity at home? With an Arduino, you can create a simple weather station that displays live weather data. This is a great project for beginners interested in electronics, coding, and weather tracking. In this blog post, we’ll walk you through how the weather station works and what you’ll need to build it.

What Is a Weather Station?

A weather station is a device that collects information about the environment, such as temperature, humidity, air pressure, and more. With this data, you can track changes in the weather over time. For our project, we’ll focus on measuring temperature and humidity, two key weather indicators. By using a few simple components with an Arduino, you can create a weather station that shows real-time data on a display.

How Does the Arduino Weather Station Work?

The core idea of this weather station is to measure temperature and humidity and display the data on an LCD screen. You will use a sensor to capture the weather data, and the Arduino will process the information and show it on the screen.

Here’s how it works in simple terms:

  1. The sensor: A temperature and humidity sensor (like the DHT11 or DHT22) collects data from the surrounding environment.
  2. The Arduino: It reads the data from the sensor and converts it into temperature and humidity values.
  3. The display: An LCD screen connected to the Arduino shows the temperature and humidity readings so you can easily see the weather conditions in real time.

Components You Will Need

To build this simple weather station, you will need the following components:

  1. Arduino Uno: The brain of the project that processes the data.
  2. DHT11 or DHT22 sensor: This sensor measures both temperature and humidity.
    • DHT11: Cheaper, but less accurate.
    • DHT22: Slightly more expensive, but more accurate.
  3. LCD display (16x2): A small screen that will show the temperature and humidity.
  4. Resistors: These help limit the current to protect your components.
  5. Jumper wires: To connect everything together.
  6. Breadboard: A platform for assembling your circuit.
  7. Power supply: You can power your Arduino through a USB cable or a battery.

Step-by-Step Guide to Building the Weather Station

Here’s an easy guide to putting together your Arduino weather station:

  1. Set up the Arduino and Sensor:

    • First, connect your temperature and humidity sensor (DHT11 or DHT22) to the Arduino. This sensor has three pins: one for power (VCC), one for ground (GND), and one for data. The data pin connects to a digital pin on the Arduino to read the sensor's output.
  2. Connect the LCD Display:

    • Attach the LCD display to the Arduino using jumper wires. You will connect several pins to power (VCC and GND), while others connect to the Arduino’s digital pins. This will allow the Arduino to control what is displayed on the screen.
    • You can also add a potentiometer to adjust the brightness of the LCD if you want.
  3. Upload the Code:

    • Once the hardware is set up, you'll need to write a simple code (or use a pre-existing library) that tells the Arduino to read data from the DHT sensor and display it on the LCD screen. While we won't dive into code here, you can find plenty of tutorials online that walk you through the coding part.
  4. Test Your Weather Station:

    • After everything is connected and the code is uploaded, power up the Arduino and watch the weather station in action! The LCD display should show both the temperature (in degrees Celsius) and humidity (in percentage). You can now place the weather station in different rooms or outside to see how the readings change.

Why Build an Arduino Weather Station?

There are several reasons why building a weather station is a fun and educational project:

  1. Learn new skills: You’ll get hands-on experience with Arduino, sensors, and electronics. Plus, you’ll gain coding experience as you write or modify the Arduino code to make the station work.
  2. Monitor your home environment: By building a weather station, you can track the temperature and humidity inside or outside your home. This can be useful for things like ensuring plants get the right conditions or improving your comfort at home.
  3. Expand the project: Once you’ve built this basic weather station, you can expand it by adding more sensors to measure other factors like air pressure or light levels. You can even connect your weather station to the internet to track weather data remotely!

Final Thoughts

Building a simple weather station with Arduino is an easy and rewarding project for beginners. You only need a few basic components, and you can track real-time temperature and humidity in no time. Plus, you’ll learn valuable skills in electronics and coding that can be applied to many other fun projects. Give it a try and see how much fun DIY weather monitoring can be!

Please leave your comment below

Post a Comment

0 Comments