I have arduino uno and sd card module.
I need to measure the voltage by A0 and A1 and save data with the time on SD module card.
The main porblem - I still get information that initialization is failed.
I added digitalWrite(pinCS, HIGH); line but it still doesn't work.
#include <SD.h>
#include <SPI.h>
File myFile;
int pinCS = 10;
int readValue1 = 0; //Odczytana wartość z ADC
float voltage1 = 0; //Wartość przeliczona na napięcie w V
int readValue2 = 0; //Odczytana wartość z ADC
float voltage = 0; //Wartość przeliczona na napięcie w V
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
digitalWrite(pinCS, HIGH); // Add this line
// SD Card Initialization
if (SD.begin(pinCS))
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Writing to file...");
// Write to file
myFile.println("Testing text 1, 2 ,3...");
myFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
// Reading the file
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Read:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
}
void loop() {
// put your main code here, to run repeatedly:
readValue1 = analogRead(A0); //Odczytujemy wartość napięcia
voltage1 = readValue1 * (5.0 / 1024.0); //Przeliczenie wartości na napięcie
Serial.println(voltage1); //Wysyłamy zmierzone napięcie
delay(2000); //Czekamy, aby wygodniej odczytywać wyniki
readValue2=analogRead(A1); //Odczytujemy wartość napięcia
voltage2=readValue2 * (5.0/1024.0); //Przeliczenie wartości na napięcie
Serial.println(voltage2); //Wysyłamy zmierzone napięcie
delay(200); //Czekamy, aby wygodniej odczytywać wyniki
}