#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; // CS pin of the SD card module
const int analogPin = A0; // Analog pin to read sensor data
File dataFile;
void setup() {
Serial.begin(9600);
pinMode(chipSelect, OUTPUT);
// Initialize the SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("Card initialized.");
}
void loop() {
// Read sensor data
int sensorValue = analogRead(analogPin);
// Open or create a file
dataFile = SD.open("datalog.txt", FILE_WRITE);
// If the file is available, write to it
if (dataFile) {
dataFile.println(sensorValue);
dataFile.close();
Serial.println(sensorValue);
} else {
Serial.println("Error opening datalog.txt");
}
// Delay for a while before the next reading (adjust as needed)
delay(1000);
}
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; // CS pin of the SD card module
const int analogPin = A0; // Analog pin to read sensor data
File dataFile;
void setup() {
Serial.begin(9600);
pinMode(chipSelect, OUTPUT);
// Initialize the SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("Card initialized.");
}
void loop() {
// Read sensor data
int sensorValue = analogRead(analogPin);
// Open or create a file
dataFile = SD.open("datalog.txt", FILE_WRITE);
// If the file is available, write to it
if (dataFile) {
dataFile.println(sensorValue);
dataFile.close();
Serial.println(sensorValue);
} else {
Serial.println("Error opening datalog.txt");
}
// Delay for a while before the next reading (adjust as needed)
delay(1000);
}
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; // CS pin of the SD card module
const int analogPin = A0; // Analog pin to read sensor data
File dataFile;
void setup() {
Serial.begin(9600);
pinMode(chipSelect, OUTPUT);
// Initialize the SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("Card initialized.");
}
void loop() {
// Read sensor data
int sensorValue = analogRead(analogPin);
// Open or create a file
dataFile = SD.open("datalog.txt", FILE_WRITE);
// If the file is available, write to it
if (dataFile) {
dataFile.println(sensorValue);
dataFile.close();
Serial.println(sensorValue);
} else {
Serial.println("Error opening datalog.txt");
}
// Delay for a while before the next reading (adjust as needed)
delay(1000);
}