Go Down

Topic: SD card problem on Mega, can a card go faulty via SPI but still work in PC? (Read 1 time) previous topic - next topic

richardtheboffin

Hi all,

I am driving myself slightly nuts trying to get an SD card going on a Mega 2560.

I've had this card working in the past and I've been using the same code (and tried all the examples) but the card refuses to play ball.

The card works fine when plugged into the PC card reader.

Is it possible for it to go faulty when being read via SPI and still work when being read from a PC?

I have these connections:
SD card    Mega
5v             5v
Gnd         Gnd
MOSI      51
MISO      50
SCLK      52
CS          53

(i've tried CS on other port pins and set 53 to output regardless)

What's bugging me is this has all worked fine before!

On the scope I can see clock and MOSI going and there are just single pulses coming back on MISO.

There's no level shifter on the SD card interface board and I've never used one in the past either. (it's an lc studio card module)

This uses pin 45 for CS


Code: [Select]
#include <SPI.h>
#include <SD.h>

void setup() {
  // put your setup code here, to run once:
 
  Serial.begin(9600);
 
  pinMode(45,OUTPUT);
  pinMode(53,OUTPUT);

  Serial.println("check for SD card");
 
  if(SD.begin(45))
    {
      Serial.println("SD card found");
    }
   else
   {
     Serial.println("SD card NOT found");
   }
 

}

void loop() {
  // put your main code here, to run repeatedly:
 
}



Any ideas or suggestions would be most welcome!

CrossRoads

Quote
lc studio card module

Got a link? Maybe it provides the 3.3V regulation and level translators.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nightcrawler218

#2
Dec 20, 2013, 08:26 pm Last Edit: Dec 20, 2013, 08:41 pm by nightcrawler218 Reason: 1
I suppose Richard is using this module


I am using the same module, having the same issue.
Differences is my board is 328 based UNO & his MEGA2560.
"Imagination is more important than knowledge."
-Einstein

CrossRoads

Could try the SDfat.h library instead of Sd.h
http://code.google.com/p/sdfatlib/

fat16lib updates it regularly and is in the forum quite frequently as well.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nightcrawler218

#4
Dec 20, 2013, 09:44 pm Last Edit: Dec 20, 2013, 10:07 pm by nightcrawler218 Reason: 1
Thanks for the link CrossRoads. Tried to format the sd card using the SdFormatter example.
Connections...
SD Module--------------------- Arduino
MOSI----------------------------D11
MISO----------------------------D12
SCK------------------------------D13
CS-------------------------------D10

The error report
Code: [Select]

This sketch can erase and/or format SD/SDHC cards.

Erase uses the card's fast flash erase command.
Flash erase sets all data to 0X00 for most cards
and 0XFF for a few vendor's cards.

Cards larger than 2 GB will be formatted FAT32 and
smaller cards will be formatted FAT16.

Warning, all data on the card will be erased.
Enter 'Y' to continue: Y
// I pressed Y
Options are:
E - erase the card and skip formatting.
F - erase and then format the card. (recommended)
Q - quick format the card without erase.
// I pressed F
Enter option: F
Card Size: 3782 MB, (MB = 1,048,576 bytes)

Erasing
..............................
All data set to 0x00
Erase done

Formatting
Blocks/Cluster: 64
FAT32
error: write MBR
SD error: 13,80

Formatted the SD using Windows.  FAT32.... Default allocation size......... Quick format.....
Then re-inserted the card in the module & run thhe sketch. still showing same error.
The card is working fine in my computer.
Perplexed indeed!
"Imagination is more important than knowledge."
-Einstein

richardtheboffin

Yes, that the module.
Crazy thing is, I had it working fine a few days ago! I had stripped the board down to do something else and today I wanted to use the SD card again.
I thought I may have blown the card but it reads and writes fine on a PC.
I must be missing something really obvious, but I have checked and triple checked wiring. I have tried 2 cards and 2 interfaces.
I know the spi port worked as I used in with a radio module.
Maybe I will have a eureka moment tomorrow!

nightcrawler218

"Imagination is more important than knowledge."
-Einstein

richardtheboffin

Just tried SDfat and the SDinfo example returns :

card.init failed
SD errorCode: 0XA
SD errorData: 0X1F

(that's ACMD41 initialization process timeout)

and with no card in the slot :

card.init failed
SD errorCode: 0X1
SD errorData: 0XFF

So it is detecting something.

richardtheboffin

OK looks like its a ground problem.

I connected both GND pins on the card module to the Gnd pins on a Nano (either side of the usb socket) and the card works!

Or maybe it had actually locked up the card because no I can put the grounds back to before - one ground wire to the GND pin next to the Vin pin and it still works!


i have been swapping cards between a Nano and a Mega 2560 and I also formatted the card again with the pc.

For some reason it's just decided to go again...

Maybe it got corrupted at some point but the PC never had any problems reading it....

Now I can get on with what I wanted to do two days ago!!! 

richardtheboffin

After some more playing, way way too late at night, I have come to the conclusion that is odd problem must just be down to the 5v SPI levels causing reliability problems.

After finally getting the reader working again by connected both grounds, It decided to stop working and then to only work with both grounds disconnected!

I'm going to ditch the cheaper SD card modules and go for the micro SD reader with the proper level shifter. It's 4 times more expensive but at least I know it will keep working from one day to the next!

It's amazing that something that worked fine for months can suddenly decide to stop, but that's what comes of using things out of spec...

In the mean time, if anyone has a source of really economical 5V compatible SD card modules, in the UK, let me know. At the moment I'm looking at the one Hobby Components have for £4.50.

nightcrawler218

Found something - http://www.instructables.com/id/Arduino-DIY-SD-Card-Logging-Shield/
Need a level shifter.
Bad product...
:(
"Imagination is more important than knowledge."
-Einstein

Helium328PU

Hi,

it definitely looks like mine problem which I tried to solve few weeks ago. Its good to know that version with level shifter is probably extremely (not "just") recommended (even these are really few % of market).

Thats why I have ordered shield from ebay which has level shifter IC. Also it has ICSP header, so it could be used on Mega an also on Uno. I am using Mega2560.
It will arrive in next days (I hope), and it costs under 10$ with shipping ;)

I may post a link of that if you want...

nightcrawler218

Sure, that'd be nice. post the link. I shall try to find it in our region.
Thanks for your response.
"Imagination is more important than knowledge."
-Einstein


nightcrawler218

Thanks for the link. This is strange but true that the seller is from our neighboring country & doesn't ship here! Bad luck... :smiley-roll-sweat:
"Imagination is more important than knowledge."
-Einstein

Go Up