Arduino Data Logger Shield Documentation
The Arduino Data Logger Shield is a versatile expansion board designed for Arduino boards, enabling users to log data from various sources to a microSD card. This shield is ideal for IoT projects that require data collection, monitoring, and analysis.
MicroSD card slot for data storage
Real-time clock (RTC) for timestamping data
Battery-backed RTC for maintaining time during power outages
Supports FAT16 and FAT32 file systems
Compatible with most Arduino boards
To use the Arduino Data Logger Shield, connect it to an Arduino board as follows:
1. Align the shield's pins with the Arduino board's headers.
2. Gently push the shield onto the Arduino board, ensuring all pins are securely connected.
In your Arduino sketch, include the following libraries:
`SD` library for microSD card operations
`RTC` library for real-time clock operations
### Example 1: Logging Temperature Data to a CSV File
This example demonstrates how to log temperature data from a DS18B20 sensor to a CSV file on the microSD card.
```c++
#include <SD.h>
#include <RTC.h>
#include <DallasTemperature.h>
// Define DS18B20 sensor pin
#define DS18B20_PIN 2
// Initialize DS18B20 sensor and RTC
DallasTemperature ds(DS18B20_PIN);
RTC_DS1307 rtc;
void setup() {
// Initialize SD card
SD.begin();
// Initialize RTC
rtc.begin();
// Set RTC to current date and time (optional)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Create a new CSV file on the SD card
File dataFile = SD.open("temperature_log.csv", FILE_WRITE);
// Write CSV header
dataFile.println("Date,Time,Temperature (C)");
dataFile.close();
}
void loop() {
// Read temperature data from DS18B20 sensor
ds.requestTemperatures();
float temperature = ds.getTempCByIndex(0);
// Get current date and time from RTC
DateTime now = rtc.now();
// Open the CSV file in append mode
File dataFile = SD.open("temperature_log.csv", FILE_WRITE);
// Write temperature data to the CSV file
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, 2);
dataFile.println();
dataFile.close();
// Wait 1 minute before logging again
delay(60000);
}
```
### Example 2: Logging Humidity and Pressure Data to a Text File
This example demonstrates how to log humidity and pressure data from a BME280 sensor to a text file on the microSD card.
```c++
#include <SD.h>
#include <RTC.h>
#include <Wire.h>
#include <BME280.h>
// Define BME280 sensor I2C address and pin
#define BME280_ADDRESS 0x76
#define BME280_PIN 5
// Initialize BME280 sensor and RTC
BME280 bme(BME280_ADDRESS);
RTC_DS1307 rtc;
void setup() {
// Initialize SD card
SD.begin();
// Initialize RTC
rtc.begin();
// Set RTC to current date and time (optional)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Create a new text file on the SD card
File dataFile = SD.open("humidity_pressure_log.txt", FILE_WRITE);
// Write file header
dataFile.println("Date,Time,Humidity (%),Pressure (hPa)");
dataFile.close();
}
void loop() {
// Read humidity and pressure data from BME280 sensor
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100;
// Get current date and time from RTC
DateTime now = rtc.now();
// Open the text file in append mode
File dataFile = SD.open("humidity_pressure_log.txt", FILE_WRITE);
// Write humidity and pressure data to the text file
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(humidity, 2);
dataFile.print(",");
dataFile.print(pressure, 2);
dataFile.println();
dataFile.close();
// Wait 10 minutes before logging again
delay(600000);
}
```
These examples demonstrate the basic usage of the Arduino Data Logger Shield for logging data to a microSD card. You can modify the code to suit your specific project requirements and adapt it to work with various sensors and data formats.