0

Feather M0 LoRa + Micro SD breakout Board
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Fri May 19, 2017 1:53 pm

Hi guys,

I'm trying to run the read/write SD example from the example sketches:

Code: Select all | TOGGLE FULL SIZE
/*
  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:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header

 
 created   Nov 2010  by David A. Mellis
 modified 9 Apr 2012  by Tom Igoe
 
 This example code is in the public domain.
    
 */
 
#include <SPI.h>
#include <SD.h>

File myFile;

// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 10;

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


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(10, OUTPUT);
   
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  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
}





I've wired it as follows:

SD Reader Mo
3V 3V
Gnd GND
Do Mosi
Di Miso
CLK CLK
CS Pin 10

I'm using a PNC 32GB card that I did a full format using the SDformatter program. The breakout board lights up solid red when I try running the "cardinfo" example.

I've used a continuity checker to make sure I have continuity from the pad on the breakout board to the feather pins. There are no shorts and good contact is made. I've changed Chip Select to 10, but i'm wondering if I'm missing something since this is an M0 and not a simple uno. I know for the mega I had to use:

if (!SD.begin(8, 11, 12, 13)) {

per the recomendation of Adafruit forums.

Any thoughts?

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Fri May 19, 2017 11:16 pm

More info:

M0 + LoRa radio
Micro SD breakout board
GPS wing

The code I loaded is purely SD read/write so I'm assuming having the Lora and GPS attached it doesn't really matter.

I do think eventually i'll need to Mux another SPI interface. From the schematics, it looks like LoRa uses SPI and SD uses SPI. However I wouldn't expect this to be an issue if I load only SD code to the assembly.

Still need ideas. I guess I can order a plain old M0 feather and another breakout board to truly test everything. However if I don't have to spend the extra cash just to test if the SD card reader works, i'd appreciate it.

Thanks
Jim

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by adafruit_support_mike on Sat May 20, 2017 4:59 am

The LoRa modules are SPI devices and use pin 8 for their CS signal. That pin is pulled high by default, so you have to set pin 8 low before you communicate with any other SPI device, then send it high when you want to talk to the radio again.

adafruit_support_mike
 
Posts: 43183
Joined: Thu Feb 11, 2010 2:51 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Sat May 20, 2017 9:03 am

Thanks mike.

So let me see if I have it right. Micro sd is spi, Lora is spi. If I load the micro sd card code to an M0 Lora board and the code has nothing in it to use the Lora card, you still have to pull pin 8 low for any external SPI device to work?

In the end I'll have Lora, SD, and GPS working in tandem. I think GPS isn't an spi device so it's not an issue. I read that the M0 can be set up with an extra SPI port.

Is it better to do that or do I just need to make sure I pull pin 8 low before I use an sdwrite command and send pin 8 high again after the write is finished so, in theory, the Lora can be used again?

This must also mean I need to pull pin 8 low right before the sd card initializes and then pulled high immediately after. If I'm running only the sd read/write example, then I need to define pin 8 as an output first since there are no LoRa libraries included. However, if I'm running Lora code (Radiohead examples) and adding in sd code, then they must define pin 8 as an output in their libraries so I won't need to define pin 8 as an output in my initial setup.

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Sat May 20, 2017 1:54 pm

Mike,

I applied the following CardInfo example, added pinMode(8, OUTPUT) and pulled it low in the setup section. Still failing card initialization. The SD card access light is blinking so I know i'm not completely wrong here. I'm assuming having a GPS wing stacked on top shouldn't cause an issue since it's always communicating via Serial?

Double checking wiring:

DO ->Mis
DI -> Mos
CS -> Pin 10 (sixth pinup from the bottom on right hand side)
CLK -> SCK
Gnd -> gnd
3v -> 3v (I assume this is the 3v in pin and I wouldn't hook the 3.3v output of the feather to the 5v pin?)


Here is the updated code:

Code: Select all | TOGGLE FULL SIZE
/*
  SD card test
   
 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.
    
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
       Pin 4 used here for consistency with other Arduino examples

 
 created  28 Mar 2011  by Limor Fried
 modified 9 Apr 2012   by Tom Igoe
 */
 // include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10;   

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


pinMode(8,OUTPUT);
delay(100);
digitalWrite(8,LOW);
delay(100);

  Serial.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  while (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
  }
 
  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
 
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

 
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
 
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {
 
}

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Sat May 20, 2017 2:33 pm

Update:

I just tried using a stand alone 32U4 feather + Packet Radio with a new Micro SD breakout board and I still get initialization failed.

Guess I'll try another SD card sometime. Not sure how I can screw up 6 wires in a breadboard.

So unless there's something wrong with my code earlier where I pulled pin 8 low, I'm wondering if the SD card is too big/not compatible. The card worked in my Pi's without issue. hmmmmm.....

Here are two pictures of my test setup if you need to quadruple check wiring:

IMG_0452.JPG
IMG_0452.JPG (183.18 KiB) Viewed 205 times

IMG_0451.JPG
IMG_0451.JPG (187.61 KiB) Viewed 205 times

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Sat May 20, 2017 9:58 pm

Just tried a 2GB micro SD card reformatted and still getting failing to initialize. When it tries to initialize, the red light on the SD breakout turns on but fails initialization.

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by adafruit_support_mike on Sun May 21, 2017 12:06 am

I'm sorry, I have gave you the wrong signals: SPI devices become active when you pull their CS pin low, and are disabled when you send their CS pins high.

Send pin 8 high when you want to talk to the SD card, and low when you want to talk to the radio.

adafruit_support_mike
 
Posts: 43183
Joined: Thu Feb 11, 2010 2:51 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Sun May 21, 2017 10:46 am

Swapped pin low for pin 8 high. Sadly no difference. Fails to initialize. Tried two different SD cards, 32GB and 2GB. I guess I should order a plain 32U4 feather with no extras and see if that makes a difference.

The breakout still lights up when the code starts and turns off when initialization fails.


Code is below:

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <SD.h>

File myFile;

// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 10;

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


pinMode(8, OUTPUT);

digitalWrite(8, HIGH);

delay(1000);

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(SS, OUTPUT);
   
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  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
}


Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by adafruit_support_mike on Mon May 22, 2017 12:45 am

Hang on.. I just double-checked your photos above against photos of the MicroSD breakout and the LoRa M0 Feather: I think you might have the MOSI and MISO pins swapped.

The pin markings on the SD breakout correspond to the last two letters in the SPI signal names: DI is MOSI and DO is MISO.

The fourth pin from the end on the Feather is MISO, but it looks like that's connected to DI in the photo above.

adafruit_support_mike
 
Posts: 43183
Joined: Thu Feb 11, 2010 2:51 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by Jim2386 on Mon May 22, 2017 3:19 pm

Welp, this is why i'm not a fan of the SD card on this platform.

Changed nothing, came in the next morning and programmed it and it initialized.

Used the SD card for an hour and it worked great. Run the M0 again without a reprogram and after inserting the chip and I get a fail to initialize. Put SD card into laptop and format again and it initializes again.

I'm not sure what's going on inside the smart chip, but the system needs to be a little more reliable if a file sector or system is corrupt.

Wonder why the PC has no issues reading it ever but the card reader can be hit or miss?

Jim2386
 
Posts: 188
Joined: Fri Nov 14, 2014 11:58 pm

Re: Feather M0 LoRa + Micro SD breakout Board

by adafruit_support_mike on Tue May 23, 2017 2:55 am

Try replacing your jumpers, and moving the SD breakout to another position on your breadboard. Connections that show continuity at DC might still be flaky in the megahertz range, and breadboards are notorious for parasitics that cause problems at high frequency.

adafruit_support_mike
 
Posts: 43183
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.