Introduction to Arduino

DIY Temperature, Relative Humidity and Barometric Pressure Logger

Arduino UNO R3 Pin Setup


    Have you ever wanted to create a DIY Temperature, Relative Humidity, and Barometric Pressure data logger? Well, I have and here’s how I did it.

    I figured I would need 4 main components to make this project work: sensors to collect data (BME280 Temperature, Pressure and Humidity Sensor), a clock for the time stamp (DS1307 Clock Module), an SD Card Module to write the data to a Micro SD card and finally a microcontroller to make all the magic happen (Arduino UNO).

    First I needed to figure out how to wire up all the components to the Arduino UNO (image shown above).

    If you have not already done so, you need to install a few libraries. These are easy to install and only need to be installed once. Go to Tools / Manage Libraries and search for these libraries:

      DS1307RTC (Real Time Clock Library)
      Time (Time Library)
      Adafruit_Sensor (Adafruit Sensor Library)
      Adafruit_BME280 (BME280 Temperature, Humidity and Pressure Sensor Library)
      SPI (Serial Peripheral Interface Library)
      SD (SD Card Library)

    After wiring up our components, we need to set the clock. If your Real Time Clock module has an internal battery you only needs to preform this process once, but if the battery dies or is removed you will need to reset the clock again. If you are using a module without a battery, the module will keep time as long as power is maintained to the module, if the power is removed the clock will need to be reset again. I would suggest you use a module with an internal battery (code to set the time can be found at the bottom of this page).

    Now that we have our modules connected, our Libraries installed and the time set on our Real Time Clock Module, we can upload our code (found below).

    This code allows you to determine the logging interval by designating what part of the Day, Hour or Minute you wish to log. When the clock reaches the indicated time, an entry will be written to the file. The “||” symbol indicates “or” which allows you to choose multiple logs during a minute or hour. The code below indicated a new log will be recorded at the beginning of each minute (second 0), then at 15, 30 and 45 seconds. If you want to log every 15 minutes, just replace “tm.Second” with “tm.Minute”. You can alter the times and number of logging intervals to suit your application.

      if (tm.Second == 0 || tm.Second == 15 || tm.Second == 30 || tm.Second == 45){

    I have found it best to install a SD card in the module and then apply power to the Arduino (if you want the header sentence to be written to the file). After the code has been uploaded to the UNO, the logger will add a header to the text file and then log data at set intervals. If the text file doesn’t exist on the card, it will be created and if it already does exist, the new data will be appended to the existing data in the file.

    I have included two LEDs to indicate if the UNO can access the SD card. If it cannot, a RED LED will light. A GREEN LED will light when data is being written to the card.

Example of the "data.txt" file created:

    Year:Month:Day,Hour:Minute:Second,Temperature(C),Relative Humidity(%),Pressure(Pascals)

Parts List:

    1 BME280 Temperature, Pressure and Humidity Sensor
    1 DS1307 Clock Module
    1 SD Card Module
    1 SD card
    2 LEDs (one red and one green)
    2 220 Ohm Resistos
    1 Arduino UNO R3
    1 Breadboard
    Connector Wires

Powerup Sequence: Insert the SD Card then powerup your Arduino.

Data Logger Code:

    //------------- Code Starts Here ----------------------------
    //Published by
    //Created by Paul Illsley (
    //Please use and share so others can enjoy
    //Include the Wire library
    #include <Wire.h>
    //Include the Time library
    #include <Time.h>
    //Include the DS1307RTC library
    #include <DS1307RTC.h>
    //Include the Adafruit_Sensor library
    #include <Adafruit_Sensor.h>
    //Include the Adafruit_BME280 library
    #include <Adafruit_BME280.h>
    //Include the SPI library
    #include <SPI.h>
    //Include the SD library
    #include <SD.h>
    // Creating bme from Adafruit_BME280 sensor (I2C)
    Adafruit_BME280 bme;
    // Reference SD card to Pin 4
    const int chipSelect = 4;
    // Defining the LED Pins
    #define LED_RED 9
    #define LED_GREEN 10
    void setup(){
    //Activate SD Card Module
    // Set Pin Modes
    pinMode(LED_RED, OUTPUT);
    pinMode(LED_GREEN, OUTPUT);
    // if the SD card is not present, turn on the RED LED
    if (!SD.begin(chipSelect)) {
    digitalWrite(LED_RED, HIGH);
    // Print a Header Line in the data.txt File
    File dataFile ="data.txt", FILE_WRITE);
    dataFile.println("Year:Month:Day,Hour:Minute:Second,Temperature(C),Relative Humidity(%),Pressure(Pascals)");
    void loop(){
    //Activate SD Card Module
    // if the SD card is not present, turn on the RED LED
    if (!SD.begin(chipSelect)) {
    digitalWrite(LED_RED, HIGH);
    //Start the BME280 sensors
    // Initializing the Time tool
    tmElements_t tm;
    // Read the humidity value from the sensor
    float humidity = bme.readHumidity();
    // Read the Temperature value from the sensor
    float temperature = bme.readTemperature();
    // Read the Pressure value from the sensor
    float pressure = bme.readPressure();
    //Read the clock information as "tm";
    // Open "data.txt" file on the SD Card and write data to it every 15 seconds.
    if (tm.Second == 0 || tm.Second == 15 || tm.Second == 30 || tm.Second == 45){; 
    File dataFile ="data.txt", FILE_WRITE);
    digitalWrite(LED_GREEN, HIGH);
    delay (2000);
    // Turn off LED's
    digitalWrite(LED_GREEN, LOW);
    digitalWrite(LED_RED, LOW);
    //------------- Code Stops Here ----------------------------

Code for setting the DS1307 Clock Module's date and time:
Run this code one time before you start logging to make sure the time stamp is accurate.
This will synchronize the DS1307 Clock Module's date and time with the clock on your computer.
You will need to install the Time.h and DS1307RTC.h libraries if you haven't already installed them.

    //------------- Code Starts Here ----------------------------
    #include <Wire.h>
    #include <Time.h>
    #include <DS1307RTC.h>
    const char *monthName[12] = {
      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    tmElements_t tm;
    void setup() {
      bool parse=false;
      bool config=false;
      // get the date and time the compiler was run
      if (getDate(__DATE__) && getTime(__TIME__)) {
        parse = true;
        // and configure the RTC with this info
        if (RTC.write(tm)) {
          config = true;
      while (!Serial) ; // wait for Arduino Serial Monitor
      if (parse && config) {
        Serial.print("DS1307 configured Time=");
        Serial.print(", Date=");
      } else if (parse) {
        Serial.println("DS1307 Communication Error :-{");
        Serial.println("Please check your circuitry");
      } else {
        Serial.print("Could not parse info from the compiler, Time=\"");
        Serial.print("\", Date=\"");
    void loop() {
    bool getTime(const char *str)
      int Hour, Min, Sec;
      if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
      tm.Hour = Hour;
      tm.Minute = Min;
      tm.Second = Sec;
      return true;
    bool getDate(const char *str)
      char Month[12];
      int Day, Year;
      uint8_t monthIndex;
      if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
      for (monthIndex = 0; monthIndex < 12; monthIndex++) {
        if (strcmp(Month, monthName[monthIndex]) == 0) break;
      if (monthIndex >= 12) return false;
      tm.Day = Day;
      tm.Month = monthIndex + 1;
      tm.Year = CalendarYrToTm(Year);
      return true;
    //------------- Code Stops Here ----------------------------

Created by Paul Illsley

Return to