The Adafruit Datalogger Shield v1 is a versatile shield designed to fit on top of an Arduino UNO or similar boards. It provides an easy way to log data from sensors and other inputs to an SD card, making it ideal for remote sensing, weather stations, and other projects where data collection over time is crucial. The shield also includes a real-time clock (RTC) for timestamping the logged data, ensuring accurate timekeeping even when the Arduino is powered off.
Pin Number | Function | Description |
---|---|---|
D10 | CS (Chip Select) | Selects the SD card slot when driven low |
D11 | MOSI (Master Out) | SPI Master Out, Slave In for SD card communication |
D12 | MISO (Master In) | SPI Master In, Slave Out for SD card communication |
D13 | SCK (Clock) | SPI Clock for SD card communication |
A4 | SDA (Data) | I2C Data for RTC |
A5 | SCL (Clock) | I2C Clock for RTC |
#include <Wire.h>
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
RTC_DS1307 rtc; // Create an RTC object
const int chipSelect = 10; // SD card chip select pin
void setup() {
Serial.begin(9600);
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Set the date and time at compile time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize the SD card
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
return;
}
// Create or open the log file
File logFile = SD.open("datalog.txt", FILE_WRITE);
if (logFile) {
DateTime now = rtc.now(); // Get current time
// Write a header with the current date and time
logFile.print("Log started on: ");
logFile.print(now.year(), DEC);
logFile.print('/');
logFile.print(now.month(), DEC);
logFile.print('/');
logFile.print(now.day(), DEC);
logFile.print(" ");
logFile.print(now.hour(), DEC);
logFile.print(':');
logFile.print(now.minute(), DEC);
logFile.print(':');
logFile.print(now.second(), DEC);
logFile.println();
logFile.close(); // Close the file
Serial.println("Initialization done.");
} else {
Serial.println("Error opening datalog.txt");
}
}
void loop() {
// Log data at regular intervals
// This example assumes a sensor reading function called readSensor()
File logFile = SD.open("datalog.txt", FILE_WRITE);
if (logFile) {
DateTime now = rtc.now(); // Get current time
int sensorValue = readSensor(); // Replace with actual sensor reading
// Write data with a timestamp
logFile.print(now.unixtime()); // Unix time as a simple timestamp
logFile.print(", ");
logFile.println(sensorValue);
logFile.close(); // Close the file after writing
} else {
Serial.println("Error opening datalog.txt");
}
delay(2000); // Wait for 2 seconds before the next log entry
}
int readSensor() {
// Placeholder for sensor reading logic
return analogRead(A0); // Example: read from analog pin A0
}
datalog.txt
file can be created or opened, and that the SD card is not full.Q: Can I use a microSD card with this shield? A: Yes, but you will need a microSD to SD adapter.
Q: How much data can I log? A: It depends on the size of the SD card and the frequency and size of the log entries.
Q: Can I use this shield with other Arduino boards? A: The shield is designed for the Arduino UNO form factor but may be compatible with other boards that share the same pin layout and voltage specifications.