Arduino Project : Weather Station –Data Logging to Excel

Weather Station –Data Logging to Excel



Demonstration









Project Source Code

// Example testing sketch for various DHT humidity/temperature sensors
#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#include <RTClib.h>
#include <SFE_BMP180.h>

//define DHT pin
#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

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

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10; 

//creating a file to store the data
File myFile;

//For the RTC
RTC_DS1307 rtc;

//You need to create an SFE_BMP180 object, here called "pressure":
SFE_BMP180 pressure;

#define ALTITUDE 100 // Set here your altitude

void setup() {

  //initializing the DHT sensor
  dht.begin();

  //initializing Serial monitor
  Serial.begin(9600);
  
  // setup for the RTC
  while (!Serial); // for Leonardo/Micro/Zero
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if(pressure.begin())
    Serial.println("BMP180 init success");
  else {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.
    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever. 
  }
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // if the file opened ok, write to it:
  if (myFile) {
    Serial.println("File opened ok");
    myFile.println("Date,Time,Temperature ºC,Humidity %,Pressura hPa");
  }
  myFile.close();
}

void loggingTime(){
  DateTime now = rtc.now();
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(" ");
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(",");
  }
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  delay(1000);  
}

void loggingTemperature () {

  // 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)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  //debugging purposes
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.println(" %");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
  //Serial.print(f);
  //Serial.println(" *F\t"); 
  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
    myFile.print(t);
    myFile.print(",");
    myFile.print(h);
    myFile.print(",");
  }
  myFile.close();
}
void loggingPressure(){
  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){
          myFile = SD.open("DATA.txt", FILE_WRITE);
          if (myFile) {
            myFile.println(P,2);
            Serial.println("Pressure is ok");
          }
          myFile.close();
        }
        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() {
  loggingTime();
  loggingTemperature();
  loggingPressure();
  delay(5000);
}



Detailed Explaination:-

Weather Station – Data Logging to Excel Using Arduino

Building a weather station that logs data is a fascinating project for monitoring environmental conditions like temperature, humidity, and pressure. By logging the data to Excel, you can visualize the information over time. This blog will guide you through the steps to create a simple weather station using Arduino and save the sensor data to Excel.

Components Required

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

  1. Arduino Uno – The microcontroller to process and log the data.
  2. DHT11/DHT22 Sensor – For measuring temperature and humidity.
  3. BMP180/BME280 Sensor – For measuring atmospheric pressure (optional for more accuracy).
  4. Real-Time Clock (RTC) DS3231 Module – To timestamp the data.
  5. MicroSD Card Module – For data logging to store the data locally.
  6. Jumper Wires – To connect the components.
  7. Breadboard – For wiring.
  8. USB Cable – To connect Arduino to your PC.
  9. PLX-DAQ (Parallax Data Acquisition) – Excel interface to read serial data from Arduino.
  10. Resistors (if needed) – For connections as per sensor requirements.

Circuit Diagram

Here’s how to connect the components:

  • DHT11 Sensor:
    • VCC to 5V
    • GND to GND
    • Data pin to digital pin 2
  • BMP180/BME280 Sensor:
    • VCC to 3.3V
    • GND to GND
    • SDA to A4 (for I2C communication)
    • SCL to A5
  • RTC DS3231 Module:
    • VCC to 5V
    • GND to GND
    • SDA to A4
    • SCL to A5
  • MicroSD Module:
    • VCC to 5V
    • GND to GND
    • CS to digital pin 10
    • MOSI to pin 11
    • MISO to pin 12
    • SCK to pin 13

Arduino Code

Below is the Arduino code that reads the data from the sensors, logs it to the SD card, and sends it via serial communication, which can then be captured in Excel using PLX-DAQ.

#include <DHT.h>
#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BMP085.h> #include <RTClib.h> #include <SD.h> #define DHTPIN 2 // Pin for DHT sensor #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); Adafruit_BMP085 bmp; RTC_DS3231 rtc; File dataFile; const int chipSelect = 10; void setup() { Serial.begin(9600); dht.begin(); if (!bmp.begin()) { Serial.println("Could not find BMP sensor!"); while (1) {} } if (!rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (!SD.begin(chipSelect)) { Serial.println("SD card initialization failed!"); return; } Serial.println("PLX-DAQ v2.11"); Serial.println("CLEARDATA"); // Clears previous data in Excel Serial.println("LABEL,Date,Time,Temp (°C),Humidity (%),Pressure (Pa)"); if (rtc.lostPower()) { rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } } void loop() { DateTime now = rtc.now(); float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); float pressure = bmp.readPressure(); if (isnan(temperature) || isnan(humidity)) { Serial.println("Failed to read from DHT sensor!"); return; } // Log to Serial (for PLX-DAQ) Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(','); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.print(','); Serial.print(temperature); Serial.print(','); Serial.print(humidity); Serial.print(','); Serial.println(pressure); // Log to SD card dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { dataFile.print(now.year(), DEC); dataFile.print('/'); dataFile.print(now.month(), DEC); dataFile.print('/'); dataFile.print(now.day(), DEC); dataFile.print(','); dataFile.print(now.hour(), DEC); dataFile.print(':'); dataFile.print(now.minute(), DEC); dataFile.print(':'); dataFile.print(now.second(), DEC); dataFile.print(','); dataFile.print(temperature); dataFile.print(','); dataFile.print(humidity); dataFile.print(','); dataFile.println(pressure); dataFile.close(); } else { Serial.println("Error opening datalog.txt"); } delay(2000); // Log data every 2 seconds }

Explanation of Code

  1. Libraries: The code uses libraries for handling the DHT sensor, BMP sensor, RTC, and SD card. Ensure these libraries are installed in your Arduino IDE.

  2. Initialization: Sensors and SD card are initialized in the setup() function, with error handling if any device is not detected.

  3. Data Collection: Temperature, humidity, and pressure are collected from the DHT11 and BMP180 sensors. The rtc.now() function captures the current time.

  4. Data Logging: The data is logged both to the serial monitor for real-time display and to an SD card for long-term storage.

  5. PLX-DAQ Logging: The data sent to the serial monitor is formatted in a way that PLX-DAQ can interpret it and save it directly to Excel.

Logging Data to Excel Using PLX-DAQ

To log data into an Excel spreadsheet, you'll need to install the PLX-DAQ software. PLX-DAQ reads data from the Arduino’s serial output and directly logs it into Excel. Here’s how to do it:

  1. Download and install PLX-DAQ from the Parallax website.
  2. Open the Excel sheet and launch PLX-DAQ.
  3. Connect your Arduino via USB and open the serial monitor.
  4. In PLX-DAQ, select the correct COM port and baud rate (9600 for this project).
  5. Start data logging, and you should see live updates of temperature, humidity, and pressure in Excel.

Advantages of Data Logging to Excel

  • Data Visualization: Once the data is logged, you can easily plot graphs of the temperature, humidity, and pressure over time, helping you visualize trends.
  • Data Backup: Saving data in Excel helps you maintain a historical record of your environmental conditions.
  • Ease of Analysis: You can use Excel's powerful data analysis features to run statistical analysis, calculate averages, and even generate reports.

Conclusion

This Arduino-based weather station with data logging to Excel is a great project for understanding environmental monitoring and data management. By integrating sensors with real-time logging, you can keep track of weather conditions and use the data for various applications like home automation or agriculture.

With just a few components and some coding, you’ve built a functional weather station that logs data efficiently. Happy making!

Post a Comment

0 Comments