This super simple code, that saves data to a csv file, works perfectly fine on my Arduino Uno (I am using the default SPI pins).
However, for my project I need to use an ESP8266. Does the library automatically use the default SPI pins for any other boards?
In my code I have tried changing the CS pin from 10 to 15 for the ESP8266. All I get is "Initialization failed"! I figure, I need to somehow change the other SPI pins like MOSI, MISO and SCK. Is there a way to do this? I'm an absolute noob so sorry in advance if I oversaw some kind of easy fix.
#include <SdFat.h>
SdFat sd;
SdFile file;
void setup() {
Serial.begin(9600);
if (!sd.begin(10, SD_SCK_MHZ(50))) {
Serial.println("Initialization failed!");
return;
}
if (!file.open("data.csv", O_RDWR | O_CREAT | O_AT_END)) {
Serial.println("Error opening file!");
return;
}
file.println("Sensor1,Sensor2,Sensor3");
}
void loop() {
float sensorReading1 = 1;
float sensorReading2 = 1;
float sensorReading3 = 1;
file.print(sensorReading1);
file.print(",");
file.print(sensorReading2);
file.print(",");
file.println(sensorReading3);
file.sync();
delay(1000);
}
1 Answer 1
So I just tried again and somehow I happened to replace the SD_SCK_MHZ(50), that worked on my arduino Uno, with SPI_HALF_SPEED. Now the same code as above works perfectly fine on my esp8266.