Arduino Weather Data Logger with SD Card

In the last Arduino project I’ve built a temperature data logger using SD card and DS18B20 digital temperature sensor.
This post shows how to make an Arduino weather data logger (& station) that logs temperature (in °C), humidity (in %RH) and pressure (in hectopascal) using SD card, BME280 barometric pressure & humidity & temperature sensor.
The logged data is saved to a text file stored on the SD card. DS3231 real time clock board is used to get time and date information.
Time, date, temperature, humidity and pressure values are displayed on 20×4 LCD screen and sent to PC serial monitor.

To see how to interface Arduino with SD card, visit the following post:
Arduino and SD card example – Read and write files

To see how to interface Arduino with BME280 sensor (I2C mode) for the first time, take a look at this post:
Arduino with BME280 pressure, temperature and humidity sensor

And the link below shows how to build Arduino temperature data logger:
Arduino Temperature Data Logger with SD Card

Hardware Required:
This is a list of all components required to build this project.

  • Arduino UNO board   —> Board details  —> Atmega328P datasheet
  • micro SD card with FAT16 or FAT32 file system
  • micro SD card module
  • 20×4 LCD screen
  • BME280 sensor module (with built-in 3.3V regulator and level shifter)   —->  BME280 datasheet
  • DS3231 module   —->  DS3231 datasheet
  • 330 ohm resistor
  • 2 x push button
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • Breadboard
  • Jumper wires

Arduino weather datalogger & station using BME280 sensor and micro-SD card

Arduino Weather Data Logger with SD Card Circuit:
The image below shows project circuit diagram.
In this project I used micro SD card module, this module is supplied from circuit 5V source that comes from the Arduino UNO board. This module contains AMS1117-3V3 voltage regulator which is used to supply the SD card with 3.3V. Also this module contains an IC which is 74LVC125A and it’s used as level translator (from 5V to 3.3V).

Hint:
The BME280 chip works with maximum voltage of 3.6V (supply voltage range is from 1.71 to 3.6V) which means we’ve to use a 3V3 voltage regulator to supply it from a 5V source.
Also, if we’re working with a 5V system (development board, microcontroller …) like the Arduino UNO board (ATmega328P microcontroller), we’ve to use a voltage level shifter (level converter) which converts the 3.3V (comes from the BME280 chip) into 5V (goes to the ATmega328P) and vice versa. This level shifter is for the I2C bus lines (clock and data).
The BME280 module shown in project circuit diagram has a built-in 3.3V regulator and level shifter.

SD card Arduino weather data logger BME280 sensor

The microSD card module is connected to the Arduino as follows (from left to right):
The first pin of the micro SD card module (GND) is connected to Arduino GND.
The second pin of the micro SD card module (VCC) is connected to Arduino 5V.
The third pin of the micro SD card module (MISO) is connected to Arduino digital pin 12.
The fourth pin of the micro SD card module (MOSI) is connected to Arduino digital pin 11.
The fifth pin of the micro SD card module (SCK) is connected to Arduino digital pin 13.
The last pin of the micro SD card module (CS) is connected to Arduino digital pin 10.

The digital pins 10, 11, 12 and 13 are hardware SPI module pins of ATmega328P microcontroller (Arduino UNO microcontroller).

The DS3231 RTC module SDA (serial data) and SCL (serial clock) pins are respectively connected to Arduino A4 and A5 pins (ATmega328P hardware I2C module pins).
VCC is connected to Arduino 5V and GND is connected to Arduino GND.

The BME280 sensor module has at least 4 pins because it can work in SPI mode or I2C mode. For the I2C mode we need 4 pins: GND, VCC, SDA and SCL where:
GND (ground) is connected to Arduino GND pin,
VCC is the supply pin which is connected to Arduino 5V pin,
SDA is I2C bus serial data line, connected to Arduino analog pin 4 (A4),
SCL is I2C bus serial clock line, connected to Arduino analog pin 5 (A5).

The DS3231 RTC and the BME280 sensor are connected to the same I2C bus (slave devices), the I2C slave address of the DS3231 chip differs from the one of the BME280 sensor, that allows the master device (Arduino) to talk to one of them (only one at a time).

The 20×4 LCD screen (4 rows and 20 columns) is used to display time, date, pressure, humidity and temperature values where:
RS —> Arduino digital pin 2
E   —> Arduino digital pin 3
D4 —> Arduino digital pin 4
D5 —> Arduino digital pin 5
D6 —> Arduino digital pin 6
D7 —> Arduino digital pin 7
VSS, RW, D0, D1, D2, D3 and K are connected to Arduino GND,
VEE to the 10k ohm variable resistor (or potentiometer) output,
VDD to Arduino 5V and A to Arduino 5V through 330 ohm resistor.

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In the circuit there are two push buttons: B1 and B2, they are respectively connected to Arduino analog pins 1 (A1) and 2 (A2), these buttons are used to set time and date of the real time clock.

Arduino Weather Data Logger with SD Card Code:
The following Arduino code requires 3 libraries from Adafruit Industries:

The first library is for the DS3231 RTC, download link is below:
Adafruit RTC library                   —->  direct link

The 2nd one is for the BME280 sensor:
Adafruit BME280 Library            —->  direct link

You may need to install the Adafruit Unified Sensor library if it’s not already installed:
Adafruit Unified Sensor library   —->    direct link

After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).
The same thing for the other library files.

Programming hints:
There are 4 libraries included in the Arduino code as shown below.
The first library is for the SD card,
the 2nd one is for the LCD display,
the 3rd is for the DS3231 RTC,
and the last library is for the BME280 sensor.

The default I2C address of the BME280 library is defined as 0x77 and my device I2C address is 0x76.
In the code, the definition of BME280 sensor I2C slave address and the initialization of its library are as shown below:

The Arduino reads pressure, temperature & humidity values from the BME280 sensor and saves them (with time and date) to the SD card every 1 second, for that I used the following if condition:

where the variable p_second is used to save only one value every 1 second.

Functions used in the code:
SD Card functions:
SD.begin(): this function initializes the SD card as well as the file system (FAT16 or FAT32), it returns 1 (true) if OK and 0 (false) if error.

SD.exists(“Log.txt”): tests whether the file “Log.txt” exists on the SD card, returns 1 if the file already exists and 0 if not.

SD.open(“Log.txt”, FILE_WRITE): opens the file “Log.txt” and moves the cursor to the end of the file. This function will create the file if it doesn’t already exist. Returns 0 if error.

dataLog.close(): closes the file associated with dataLog.

DS3231 Functions:
bool debounce (): this function is for button B1 debounce, returns 1 if button is debounced.

void RTC_display(): displays day of the week, date and time on the display.

byte edit(byte parameter): this function is for setting the real time clock, returns the edited parameter.

BME280 Functions:
bme280.begin(BME280_I2C_ADDRESS): this function initializes the BME280 sensor, returns 1 if OK and 0 if error.

Reading the values of temperature, humidity and pressure is done as shown below:

Temperature, humidity and pressure values (with time and date) are displayed on the 20×4 LCD screen.
If there is a problem with the BME280 sensor (for example wrong device address) the LCD will display BME Sensor Error! on the 4th row.
Also and instead of the 3 quantities (temperature, humidity and pressure), the Arduino will print Error in the text file “Log.txt”.

1 bar = 10000 Pa = 100 hPa. ( 1 hPa = 100 Pa = 1 millibar)
Pa: Pascal
hPa: hectoPascal

Full Arduino code:

This project was tested in real hardware circuit using original Samsung microSD card with capacity of 32GB. The following image shows data logger file (Log.txt) created by the hardware circuit:

Arduino weather data logger SD card created file

And the following one shows serial monitor output:

weather datalogger Arduino IDE serial monitor


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

7 thoughts on “Arduino Weather Data Logger with SD Card”

  1. Hi Tanjapm,

    Thank you for this interesting and informative project. It works like a dream.

    I do have one question: How do I change the software to only write the data to the SD Card once a third button is pressed? The button is wired to A3. I don’t need the data to be written to the SD card unnecessarily.

  2. Hi dear friend,
    I don`t know your name but I think it´s not so important. I have built your projekt “Arduino Weather Data Logger with SD Card” and it runs well. I´m a newbie in programming micros and I would like to realise two modifikations in this.
    I would like to change the logtime from one second for example to 10 minutes and further use the LCD via I2C connection to the micro. Please be so kind and give me some help or hints to modify your sketh in order to let run these two changes.in your projekt
    Best regards
    Johannes Sock
    written from Germany on July 16th 2020

    1. Hi Johannes,

      I assume you might have found out by now, but for anyone else stumbling upon this:
      you can adapt the code starting in line 114 (in reference to the above posted ‘full Arduino Code’.

      void loop()
      { p_second = rtc.now().second();

      if( p_second == rtc.now().second() )
      { // all your code here

      p_second = p_second+delta_t;
      if(p_second >59){p_second = p_second-60;
      }

      you set the variable to the current second, since this condition is fulfilled you enter the if-loop and print the data, before you exit , you set the variable to the current second plus a delay of your choice. For example 10 seconds. That means the next iteration waits until delta_t has passed. If you prefer working in minutes, set your variables p_second to rtc.now().minute(). To be consistent I would rename the variable as well…

      hope that helps.

      1. Hi tanjapm,
        cause I`m momentary hanndycaped I´ll try your proposal in a few weeks.
        On there I´ll report into this community wether it runs or any other result.
        best regards
        johannes

      2. Dear TanjapM

        I enjoy using the Arduino sketch to measure temperature and humidity. I also have a problem with the short time of 1 second and would like to set it to 1, 5 and eg 10 minutes.
        When making the changes after line 114 I get a lot of error messages that I can’t get right after many tries.
        Could you do me a favor and send me the complete sketch with the changes already made?

        I would be very happy with it.
        JanW

        Arduino Weather Data Logger with SD Card
        http://simple-circuit.com/arduino-weather-data-logger-sd-card/

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top