Making micro SD card and Arduino Cloud work in the same program

I'm trying to write data to a micro SD card while also pushing data to the Arduino IoT cloud, all in the same program, so far unsuccessfully.

I have a MKR1010-WiFi with a MKR-ETH mounted on it and a 32 GB micro SD card inserted. The latter is formatted with the Tuxera SD card formatter, as recommended by Arduino.
The SD card works fine with the Datalogger example, and the Arduino IoT works using the ArduinoIoTCloud_LED_switch example.

By moving the statements from the Datalogger over to the ArduinoIoTCloud_LED_switch program, I was hoping to achieve my goal of logging data to an SD card while also sending and receiving data to and from the Arduino IoT cloud.

Compilation goes fine, the connection to the Wifi is made and the board initializes the SD card. Data are moving to and from the cloud without problems. But it fails to create and open the file on the SD and so cannot write data to it. I’m getting the error message that the logic dictates when the file doesn’t open.

I am running IDE 1.8.12, libraries are updated, board firmware is the latest, I've tried different computers, separate 5V and 3.3V power supplies, and also at some point bent away the pin4 on the ETH board so that MKR1010 pin6 could be used as the chipselect, all to no avail.

What am I missing. Code is attached.

test.ino (3.12 KB)

arduino_secrets.h (444 Bytes)

thingProperties.h (1.19 KB)

I investigated this and discovered there is a bug that causes pin 10 to be set to INPUT mode when ArduinoCloud.update(); is called before the MKR WiFi 1010 has connected to Arduino IoT Cloud. Pin 10 is the MISO pin on the SPI bus used to communicate with the SD card, so this breaks communication with the SD card.

I have a workaround for you. Right above this line in your test.ino:

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

add these lines:

// Wait for connection with Arduino IoT Cloud
while (!ArduinoCloud.connected()) {
  ArduinoCloud.update();
}

This will still set pin 10 to INPUT mode, but it does it before the call to SD.begin(), which configures the SPI pins for communication with the SD card, so it does no harm.

I'll do some further investigation to try to discover the cause of the bug so that we can get a real fix in the library and make the workaround not necessary in the future. But the workaround isn't so bad, other than delaying the start of your program while it waits for the connection.

That fixed it. Great. Also, inserting the wait for the Arduino cloud connection is much cleaner on the Serial Monitor. Thanks so much.

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

Per,

The "Wait for connection" solved an issue I was having with the Arduino MKR WiFi 1010 and the Arduino MAX31865. Standalone the code worked fine, connecting to the IoT Cloud would break the communications with the MAX31865.

Timely, and appreciated!