Chipselect in Metro M4 (ATSAMD51)

I am trying to log data using a Metro M4 (ATSAMD51) and adalogger using the following example code. It failed at "SD.begin(chipSelect)" and I am wondering if I had the wrong number for chipSelect. Thanks for any help in advance!

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

const int chipSelect = 4;

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

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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  Serial.println("card initialized.");

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";

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

  // if the file is available, write to it:
  if (dataFile) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");

What connections have you made between your M4 and the SD card module?

Which SD card module do you have?

I think the Metro M4 runs at 3.3V. If that's correct, you should use an SD card module that has just the SD card holder on it, rather than one with additional electronics that is used with a 5V system such as an UNO.

I connected M4 and Adalogger FeatherWing with power pins (3.3v and GND), SCL, SDA, SCK, MOSI, MISO, SDCS. I think that should be all the pins need to be connected?

I am using the Adalogger FeatherWing without any additional microcontroller on it.

Ok, so you got the MOSI, MISO & SCK signals from the 6-pin ICSP header on the M4?

Where did you connect the SDCS signal to on the M4?

Yes, that is right.

I tried connecting SDCS to pin 10 because it is mentioned on the datasheet that the SDCS pin is on GPIO 10. I also tried connecting to pin4. None of them works.

It will be great to know which pin I should connect and specify in the code. Thank you!

If you use pin 4 - like in your code in #1, then I would have thought that you would connect the SDCS signal to the digital header pin marked 4.

I guess you have tried the example SD card code from the IDE?

Yes. I specified pin 4 in the code and connected SDCS to pin 4. I also tried changing the pin (both in the code and connection) to 10. Neither works.

This is actually the example sd card code from IDE and it doesn't work.

Ok, stupid question, but did you connect:

M4 MOSI -> AdaLogger MOSI
M4 MISO -> AdaLogger MISO
M4 SCK  -> AdaLogger SCK

Another thought, what SD card are you using? Is it greater than 32Gb (I think that's the size).

Did you format it using your PC format command or one of the SD formatting utilities. See the notes at the top of the Storage section of the forum about formatting SD cards.

I am using a 64Gb SanDisk Ultra micro sd.

I didn't change the format of the sd card so it should be at its default setting.

I wonder if the issue is with the size of your SD card? See reply#4 of this discussion as it suggests that you might need to use a different SD library for 64Gb cards.

If you have a 32Gb card or one with less capacity, the standard library should work with it.