TinyCircuits Forum

General Category => General Discussion => Topic started by: Rydez on July 21, 2022, 03:40:42 PM

Title: MicroSD fails to initialize
Post by: Rydez on July 21, 2022, 03:40:42 PM
Hello! I'm having some trouble with reading an SD card. I have a TinyZero with a MicroSD TinyShield on top. I got my SD card here: https://tinycircuits.com/products/microsd-card-and-adapter-8gb (https://tinycircuits.com/products/microsd-card-and-adapter-8gb). I'm following the tutorial here: https://learn.tinycircuits.com/Memory/MicroSD_TinyShield_Tutorial/. I've formatted the SD card using the prescribed SD formatter (FAT32), and I downloaded the zip that contains the CardInfo sketch that uses chipSelect = 10 and uses SerialUSB. Unfortunately, when run the SD card fails to initialize, and it outputs:

Initializing SD card...initialization failed. Things to check:
* is a card inserted?
* is your wiring correct?
* did you change the chipSelect pin to match your shield or module?

I've scoured the internet in an attempt to find a solution, but nothing I've tried has worked.

Any advice would be great!
Title: Re: MicroSD fails to initialize
Post by: lennevia on July 21, 2022, 04:04:11 PM
Hiya,

First thing I would check - is the SD Card fully inserted? It hangs out a bit, but it does need pushed in all the way to the connector. The boards should also be flatly connected with the tan 32-pin connector between the TinyZero and SD Shield.

When you connect the SD card and open it on just your computer, I assume that works just fine? Can you drop files onto it? Make sure to safely eject it when you unplug it - some data corruption could cause issues when trying to read the info from the card.

Let me know if that helps!

Cheers,
Réna


Title: Re: MicroSD fails to initialize
Post by: Rydez on July 21, 2022, 04:12:47 PM
Hi Réna,

Here are a couple pictures to show the connections. They seem good to me. It seems that the SD card is in as far as possible. Also, yes, the SD card works on my computer. I can move files onto it.

https://ibb.co/G3DHdTD
https://ibb.co/W0XsQQC
Title: Re: MicroSD fails to initialize
Post by: Rydez on July 22, 2022, 11:21:41 AM
Not sure if this would cause a problem, but I originally formatted the SD using Ubuntu's OS, then later I went to a Windows computer to use the SD formatter from https://www.sdcard.org/downloads/formatter/.
Title: Re: MicroSD fails to initialize
Post by: lennevia on July 22, 2022, 12:07:03 PM
Did you test the card before formatting it with Ubuntu? I'm curious if the formatter you used could have mismanaged the protected memory of the SDHC SD Cards we distribute.

The windows formatter you linked from our site has some warnings that formatters from other operating systems could lower performance, but it doesn't mention that the card would be unusable. It still works with dragging and dropping files, so it's possible the Ubuntu formatter could have formatted the protected memory of the SD Card making the card info unreadable. Can you share what tool you used, or are you able to look into how it reformats SDHC cards?

Could you try writing a .txt file to the SD Card to see if that works? You can use this example, you'll just have to change the Serial object to SerialUSB: https://github.com/arduino-libraries/SD/blob/master/examples/DumpFile/DumpFile.ino - This example uses SD.begin(chipSelect) rather than the card.init(SPI_HALF_SPEED, chipSelect) in the example that's not working so it handles initialization a little differently.
Title: Re: MicroSD fails to initialize
Post by: Rydez on July 22, 2022, 12:45:08 PM
If I'm remembering correctly, I did not test the card before formatting it with Ubuntu. The initial formatting was done with the UI shown here: https://itsfoss.com/format-usb-drive-sd-card-ubuntu/.

I attempted to run the sketch you sent, but I'm getting the same output because SD.begin(chipSelect) fails. I also attempted using SD.begin() in the past while trying to figure this out myself.
Title: Re: MicroSD fails to initialize
Post by: Rydez on July 22, 2022, 12:55:09 PM
Thinking about going and getting a fresh SD card to test. Are the SD cards distributed by TinyCircuits special in some way? Or, will any SD card work?
Title: Re: MicroSD fails to initialize
Post by: Rydez on July 23, 2022, 09:59:43 AM
Started doing some deeper debugging within the SD library. I've found that the card fails here: https://github.com/arduino-libraries/SD/blob/a61e84b97b7742024ea2c8545a2b797aa6d551f4/src/utility/Sd2Card.cpp#L305 (https://github.com/arduino-libraries/SD/blob/a61e84b97b7742024ea2c8545a2b797aa6d551f4/src/utility/Sd2Card.cpp#L305). Timeout is reached while attempting to go into idle state. The timeout is set to 2,000 ms. I tried waiting 100,000 ms, but it didn't work. The status_ variable in Sd2Card is 0x20 before running card.init(), and 0xFF after.
Title: Re: MicroSD fails to initialize
Post by: Rydez on July 23, 2022, 10:46:51 AM
Just went out and bought a new microSDHC card. I tried it straight out of the package, and that didn't work. Then I formatted it with the sdcard.com formatter, and that didn't work.
Title: Re: MicroSD fails to initialize
Post by: lennevia on July 25, 2022, 03:05:08 PM
It sounds like something could be faulty with the hardware if the chip select at pin 10 isn't working. I'll email you about getting a replacement!
Title: Re: MicroSD fails to initialize
Post by: dblewis76 on August 14, 2022, 11:01:43 PM
I have the same issue using the MicroSD with the TinyScreen+. I aslo have the
older TinyScreen and I can write and read the SD card from the TinyScreen. I
created the TinyScreen+ "stack" as follows:

Top card: TinyScreen+
Next card: TinyGPS
Next card: MicroSD
(I have also tried this scenario without the TinyGPS with no difference)

And the TinyScreen stack:

Top card: TinyScreen
Next card: Ribbon cable
Next card: TinyUSB
Next card: TinyDuino

The ribbon cable is connected between the two stacks, both microprocessors are
powered off.

I compiled the simple SD_Write sketch below with these settings for the ATmega328P
(I am using the Eclipse plugin Sloeber):

Platform folder: .../arduino/hardware/avr/1.8.3
Board: Arduino Nano
Upload Protocol: Default
Port: /dev/cu.usbserial-DO00BU5U
Processor: ATmega328P (Old Bootloader)

I plugged the serial USB into my Mac and the TinyScreen.
I inserted the SD card in the MicroSD and powered on the TinyScreen.
I loaded the ATmega328P sketch into the TinyDuino and was able to
write a file to the SD card. I was then able to read the file back on my Mac.

I compiled the simple SD_Write sketch below with these settings for the SAMD21:
Platform folder: .../arduino/hardware/samd/1.8.11
Board: Arduino Zero (Native USB Port)
Upload Protocol: Default
Port: /dev/cu.usbmodem14101

Then I powered off the TinyScreen, removed the ribbon cable, moved the serial cable to the TinyScreen+ and
powered on the TinyScreen+. I loaded the SAMD21 sketch to the TinyScreen+, but
it failed to recognize the SD card and/or reader.

To summarize, the sketch is compiled for the TinyScreen (ATmega328P) and it is able
to write the SD card. The file written can be read back on my Mac. The exact same sketch is
compiled for the TimyScreen+ (SAMD21) simpy by changing the board configuration in the IDE.
It does not recognize the MicroSD card.

Output from TinyScreen:

   dataFile: 1
   Write error: 0
   Wrote header: Date,Time,Latitude,Longitude,Alt,Speed,Course
   bytes written: 47
   dataFile: 1
   Write error: 0
   Wrote header: Date,Time,Latitude,Longitude,Alt,Speed,Course
   bytes written: 47

Data written to the file GPS.csv:

   Date,Time,Latitude,Longitude,Alt,Speed,Course
   1
   Date,Time,Latitude,Longitude,Alt,Speed,Course
   2
   Date,Time,Latitude,Longitude,Alt,Speed,Course
   3


Output from TinyScreen+:

   No SD card
   dataFile: 0
   SD card failed


Simple SD_Write sketch:

//-------------------------------------------------------------------------------
//  TinyCircuits GPS TinyShield Display Example
//  Last updated 7 April 2016
// 
//  This example uses the TinyGPS library, originally written by Mikal Hart and
//  currently forked and updated at https://github.com/florind/TinyGPS to support
//  Glonass, slightly modified to work with the GPS TinyShield.
//
//  Some GPS modules have been shipped with 4800 baud instead of 9600- try this if
//  you see bad data.
//
//  TinyCircuits, http://TinyCircuits.com
//
//-------------------------------------------------------------------------------


#include <SPI.h>
#include <SD.h>
#include <errno.h>
#include <string.h>
#include <time.h>

const int RXPin = A1, TXPin = A0;
const int chipSelect = 10;
bool cardPresent   = false;
bool doClearScreen = false;
bool logInfo       = true;
bool logHeader     = true;
const uint32_t SerialBaud = 9600*2;

const char* header = "Date,Time,Latitude,Longitude,Alt,Speed,Course";
//                    00/00/2000 21:04:02 ,33.303665,-111.982444,1157.81,-1.00,****

// Make program compatibile for all TinyCircuits processor boards
#if defined(ARDUINO_ARCH_SAMD)
  #define SerialMonitor SerialUSB
#else
  #define SerialMonitor Serial
#endif

SDLib::File dataFile;

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
  } while (millis() - start < ms);
}

// The serial connection to the GPS device
//SoftwareSerial gpsDev(RXPin, TXPin);
void setup()
{
  SerialMonitor.begin(SerialBaud);//Note: Set Serial Monitor rate to 57600
  delay(100);

  // see if the card is present and can be initialized:
  if (SD.begin(chipSelect)) {
    cardPresent = true;
    smartdelay(1000);
  }
  else
  {
   SerialMonitor.println("No SD card");
   smartdelay(1000);
  }

}

time_t now = 0;
void loop()
{
   dataFile = SD.open("gps.csv", FILE_WRITE );
   SerialMonitor.print("dataFile: ");
   SerialMonitor.println(dataFile);
   if( ! dataFile )
   {
      SerialMonitor.println("SD card failed");
      while(1);
   }
   size_t written = dataFile.println(header);
   ++now;
    dataFile.println(now);

   SerialMonitor.print("Write error: ");
   SerialMonitor.println(dataFile.getWriteError());
   SerialMonitor.print("Wrote header: ");
   SerialMonitor.println(header);
   SerialMonitor.print("bytes written: ");
   SerialMonitor.println(written);

   dataFile.close();

   smartdelay(500);
}
Title: Re: MicroSD fails to initialize
Post by: lennevia on August 15, 2022, 02:37:39 PM
Hi there,

The first two things I would advise you to correct:

- The TinyDuino board selection should be Arduino Pro or Pro Mini with a (3.3V, 8MHz) Processor selection
- The TinyScreen+ needs a separate boards package downloaded and then you can select "TinyScreen+" as a board option.

Check out the Getting Started tutorials for some direction:
- TinyDuino: https://learn.tinycircuits.com/Processors/TinyDuino_Setup_Tutorial/
- TinyScreen+: https://learn.tinycircuits.com/Processors/TinyScreen%2B_Setup_Tutorial/

Cheers,
Réna
Title: Re: MicroSD fails to initialize
Post by: dblewis76 on August 17, 2022, 04:03:54 PM
Thank you Réna. That worked and I can now write to the SD card.