DEV Community

Cover image for Arduino MKR WiFi 1010 - SD Card
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino MKR WiFi 1010 - SD Card

If you want your Arduino MKR WiFi 1010 to log data or read config files instead of just streaming them over WiFi, a Micro SD card module is the simplest way to get real storage onto the board. It talks over SPI, so wiring is minimal, and once it is set up you can create, write, read, append and overwrite files just like you would on a computer, using file paths and standard calls.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MKR WiFi 1010 - SD Card

What you need

Wiring

Arduino MKR WiFi 1010 - SD Card wiring diagram

The Micro SD card module has six pins. VCC takes 5V power and GND is ground. MISO, MOSI and SCK are the SPI data and clock lines that let the module and the MKR WiFi 1010 exchange bits, and SS (slave select) is the pin the Arduino pulls low to tell the module it's being addressed. In the example sketch, SS is wired to pin 4, set with PIN_SPI_CS. Before wiring anything, insert the Micro SD card into a USB reader on your computer and format it as FAT16 or FAT32, since the Arduino's SD library expects one of those filesystems.

How it works

SPI is a protocol where one device (the Arduino) is the controller and the SD module is the peripheral. The controller sends data out on MOSI, reads data back on MISO, drives the shared clock on SCK, and uses SS to pick which peripheral is currently active. The SD library wraps all of this into familiar file operations: SD.begin() initializes the card, SD.exists() checks for a file, SD.open() opens or creates one, and closing it with .close() flushes your writes to the card.

Code

The sketch starts the serial connection, then calls SD.begin(PIN_SPI_CS) to initialize the card on pin 4, halting in an infinite loop if that fails. It then checks whether arduino.txt already exists with SD.exists(). If it doesn't, the code opens it with SD.open(..., FILE_WRITE), which creates the file, and immediately closes it. Finally it re-checks for the file's existence and prints the result to the Serial Monitor, confirming the file was created on the card.

arduino-mkr-wifi-1010-sd-card.ino

/*
 * This Arduino MKR WiFi 1010 code was developed by newbiely.com
 *
 * This Arduino MKR WiFi 1010 code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-sd-card
 */

#include <SD.h>

#define PIN_SPI_CS 4

File myFile;

void setup() {
  Serial.begin(9600);

  if (!SD.begin(PIN_SPI_CS)) {
    Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
    while (1); // don't do anything more:
  }

  Serial.println(F("SD CARD INITIALIZED."));

  if (!SD.exists("arduino.txt")) {
    Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file..."));
    // create a new file by opening a new file and immediately close it
    myFile = SD.open("arduino.txt", FILE_WRITE);
    myFile.close();
  }

  // recheck if file is created or not
  if (SD.exists("arduino.txt"))
    Serial.println(F("arduino.txt exists on SD Card."));
  else
    Serial.println(F("arduino.txt doesn't exist on SD Card."));
}

void loop() {
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If the Serial Monitor reports the SD card failed or isn't present, double check the SPI wiring and confirm the card is formatted as FAT16 or FAT32. A 'file not found' error usually means the file was never created or the card itself has an issue. If nothing gets written, check that the card has free space and that you're actually calling the write functions before closing the file. Garbled output in the Serial Monitor is almost always a baud rate mismatch, so make sure the monitor's speed matches the Serial.begin() value in the sketch.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-sd-card

Top comments (0)