Micro SD card works in arduino ide but not in platformio

So I recently created a custom board based around the samd21e18 and it works great! I got the project intergrated in platformio and so far everything works except for the microsd card reader. Basically, I tested the microsd card out a few times in arduino ide using the Rapid Prototypes arduino Tau board and it worked. However, using the exact same variant.cpp and variant.h files in platformio, the microsd card reader does not work.

here is the code I have been using accross arduino ide and pio:

/*
  SD card read/write

  This example shows how to read and write data to and from an SD card file
  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Its possible that the variant.cpp or variant.h files are causing trouble. I have used the adafruit trinket m0 as the board in pio and then added the variant.cpp and variant.h files directly into the src folder. That is the only change I made and It works because I tested it with a neoPixle LED and it was able to use the LED_BUILTIN that i defined in the new variant files and didnt work if I made changes to those files. The hope was that the variant.cpp and variant.h files would automatically overide the default ones for the trinket m0. Any thoughts as to what might be happening?

So another issue popped up. For some strange reason, a simple sketch for the BMI088 wont print anything while the same exact code works totally fine in arduino IDE. These may be related? Honestly im out here pulling my hair. I can’t really find anyone else who has had this issue and im getting close to giving up :confused:

So you’re trying to get a custom Arduino core and variant supported? What are the exact project files? One missing build flag or wrong configuration and it could be dead in the water.


Hey! So I actually got most of the things working since I later realized that the Sparkfun Samd21 Mini board actually uses the same pins for i2c and spi even though it uses the samd21g18 and not the e18. Previously, I had selected the adafruit trinket m0 board and just included the variant.cpp and variant.h files in the src and thats it. That approach kinda worked for things like setting digital outputs but never really worked with things like spi. The sparkfun board however is not a 100% match to my pinout and I am wondering if I could use the same approach to include the variant.cpp and variant.h files because I need to change a single pin (named AREF) to an analog input.

If you have a custom variant you should use the options to control the variant directory and name of the board variant, as well as any identifiyng macros. Basically, a new board JSON should be created. It’s basically the same concept as in https://github.com/maxgerhardt/pio-custom-stm32duino-variants.

Thanks so much! I got every part of my board to work and basically I only had to change one pin in the new variant.cpp file.

Hi there!

I’m currently working on a project where I’m using PlatformIO to build firmware for an ESP8266 (Feather Huzzah). My goal is to read data from a GPS module (via serial) and save it to an SD card (via SPI). Although I have successfully written firmware with the Arduino IDE that writes to the SD card, I’m struggling to parse the NMEA messages from the GPS module within that environment.

I am attempting to build a custom component to handle writing to the SD card using PlatformIO, but so far, I haven’t had any success. Does anyone know of a repository with a working example of writing to an SD card using PlatformIO and ESP8266, or any resources that could guide me in handling the SD card writing?

Any guidance or pointers would be greatly appreciated. Thanks for your time and help!

All the best,

If it works in the ArduinoIDE and not in PlatformIO, it means that there are differences.
Therefore, the differences would have to be determined.
This can be achieved by comparing the settings in the ArduinoIDE and the settings in platformio.ini.
You would also need to compare the platform/framework versions used.

Thanks sivar2311 for the reply. Using Arduino IDE 2.3.2 I can use the SPI sdcard module with platform Feather huzzah ESP8266 using #include <SPI.h> and #include <SD.h>. I then have my functions for creating a file and log data. When using esphome, which is quite new system to me, I have a .yaml file where the header is:

esphome:
  name: testname
  platform: ESP8266
  board: huzzah
  libraries:
    - "SD"
    - "SPI"
...

I have a platformio.ini file and its content is:

[env:esp8266]
platform = espressif8266
board = huzzah
framework = arduino
lib_deps =
	greiman/SdFat@^2.2.3
	greiman/SdFs@^2018.6.24
	arduino-libraries/SD@^1.2.4
        SPI

I then try to compile from terminal using esphome compile configuration.yaml. But the compilation fails… One error is “fatal error: SDFS.h: No such file or directory”

I have the feeling I am missing something big…

The Arduino IDE Version doesn’t tell the used Arduino Core Version.

Unfortunatley, I don’t know anything about esphome and the YAML configuration file.
I thought it is a standard Arduino project.

In this case I suggest to file an issue to the esphome-team: Issues · esphome/issues · GitHub

This doesn’t look good at all because the Arduino-ESP8266 core has the “SD” library built in:
https://github.com/esp8266/Arduino/tree/master/libraries/SD

So you shouldn’t use another version of it. Have yeou tried deledting the entire lib_deps statement and just running the reference sketch? Core-builtin libraries are detected automatically without lib_deps.

Dear sivar2311 and maxgerhardt, thanks for your replies. I changed strategy and here is a running example of a main.cpp file compiled with pio run. Here is the content of main.cpp:

#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

// Define the chip select pin for the SD card module
const int chipSelect = 16;

// Define the GPS RX pin
const int gpsRxPin = 2;

// Create a SoftwareSerial object for GPS communication
SoftwareSerial gpsSerial(gpsRxPin, -1); // RX, TX (TX is not used)

// Create a TinyGPS++ object
TinyGPSPlus gps;

void setup() {
    // Initialize serial communication at 115200 baud for debugging
    Serial.begin(115200);
    
    // Initialize the secondary serial port for GPS at 9600 baud
    gpsSerial.begin(9600);
    
    // Initialize SPI communication
    SPI.begin();
    
    // Attempt to initialize the SD card
    Serial.print("Initializing SD card...");
    
    // Check if the SD card initialization is successful
    if (!SD.begin(chipSelect)) {
        Serial.println("initialization failed!");
        return;
    }
    Serial.println("initialization done.");
    
    // Open a file for writing
    File dataFile = SD.open("example.txt", FILE_WRITE);
    
    // Check if the file opened successfully
    if (dataFile) {
        Serial.println("Writing to example.txt...");
        dataFile.println("This is a test.");
        dataFile.close();
        Serial.println("Done.");
    } else {
        // If the file didn't open, print an error
        Serial.println("error opening example.txt");
    }
    
    // Open the file for reading
    dataFile = SD.open("example.txt");
    if (dataFile) {
        Serial.println("example.txt:");
        
        // Read from the file until there's nothing else in it
        while (dataFile.available()) {
            Serial.write(dataFile.read());
        }
        dataFile.close();
    } else {
        // If the file didn't open, print an error
        Serial.println("error opening example.txt");
    }
}

void loop() {
    // Check if there are any GPS data available
    while (gpsSerial.available() > 0) {
        gps.encode(gpsSerial.read());
    }
    
    // Check if a valid location is available
    if (gps.location.isUpdated()) {
        // Print latitude and longitude to the serial monitor
        Serial.print("Latitude: ");
        Serial.println(gps.location.lat(), 6);
        Serial.print("Longitude: ");
        Serial.println(gps.location.lng(), 6);
    }
    
    // Wait for 5 seconds before the next reading
    delay(5000);
}

and here is the content of platformio.ini:

[env:esp8266]
platform = espressif8266
board = huzzah
framework = arduino
lib_deps = 
    SD
    SPI
    ESP8266SdFat
    TinyGPSPlus
    EspSoftwareSerial

Thanks for your help as that was pointing me in the right direction. Btw: the output from serial monitor looks ok, but I might update here the comment when I will be able to test GPS antenna outside the building. All the best