Pages: [1] 2   Go Down
Author Topic: MicroSD not able to initialize often  (Read 5468 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have several mega's and WL5100 ethernet shields and sd cards.
but the have all the same issue.
when i upload my code the SD card isn't working any more.
when i disconnect it from my laptop and connect it again. it usualy works after sd card failed message.
but sometimes i need to power off and on several time before it start up.

if it works and i dont restart my arduino it keeps working without any problem.
but if i disconnect the power and power it on the issue is back.

any idea whats wrong
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this. It disables the w5100 SPI while you are setting up the SD.
Code:
#include <SD.h>

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

  // disable w5100 SPI wile setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print("Starting SD...");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");
}

void loop() {
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I am experiencing a similar issue using the Latest HW for Ethernet shield and Mega 2560 -

I had intermittent failures, where the SD card would init and I could list all files on the card - but not read any file contents. Sometimes a re-start could cause the card init to fail.

I think it is related (in part) to the SPI IO.

At the time, I was setting pin 10 as an output, and high.
The mistake I was making was that I thought 'SS' (pin 53), was the SD chip_select, and I was passing SS to the sd.init function.

It seemed when I did this the operation was flaky at best - though it did work in part.

Does anyone have a definitive description of what the following pins do:
Pin 4         = CS_SD   ?
Pin 10       = CS_Ethernet ?
Pin 53 (SS) = Must be set as an output, and preferably pulled high ?

I plan on trying to trace the PCB scheamtics, but if anyone has that done, it would be great.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Digital pin 4 is the SD slave select.
Digital pin 10 is the w5100 slave select.
Digital pin 53 is the default slave select.

The SD.begin(4) will set the default slave select as OUTPUT and HIGH.

If you are using a Mega, you must disable the w5100 slave select before initializing the SD. If you don't, the w5100 will trash up the SPI, and chances are the setup will fail.

The SPI data lines are on the ICSP pins as well as on pins 50-52 on the Mega and pins 11-13 on the Uno. The ICSP connector is how the ethernet shield gets those SPI lines from either model, not the digital pins.

This post has code that I use to initialize both devices if I want to use them together.
http://arduino.cc/forum/index.php/topic,125359.msg943368.html#msg943368
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks SurferTim, I'll give that a shot.
Hopefully it will be the end of my reset-related woes!

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hopefully it will be the end of my reset-related woes!
It was for me! I've had no problems with the reset and the SD since.

I forgot to mention that on the Uno, digital pin 10 is the default slave select as well as the w5100 slave select. Since the SD.begin(4) call sets the default slave select as OUTPUT and HIGH, the w5100 is disabled in the SD setup on the Uno. But on the Mega, this is not the case. It must be disabled by your setup code.
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see your code but is the code correct
because you mention you have to disable the W5100 chip. but you set pin 10 high before initializing the SD card.
after the initializing you set pin 10  high again.

logical i think

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

   // disable w5100 SPI while setting up SD
   pinMode(10,OUTPUT);
   digitalWrite(10,LOW);

   Serial.print("Starting SD...");
   if(!SD.begin(4)) Serial.println("failed");
   else Serial.println("ok");
   // SD.begin() returns with its SPI disabled. Good SD library!

   Serial.print("Starting w5100...");

   if(!Ethernet.begin(mac)) Serial.println("failed");
   else Serial.println("ok");

   // Ethernet.begin() returns with the SPI enabled (bug?), so disable it. Bad Ethernet library!
   digitalWrite(10,HIGH);
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Ethernet.begin() function is a bad puppy!  smiley-sad It returns with the w5100 SPI enabled. I think this is a bug. I did not say the functions were totally bug free. To patch the bug, you must disable it after that call.

The SD.begin(4) function returns with its SPI disabled like all SPI-based libraries should. It is a good puppy!  smiley
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SurferTim, thx

added the lines like example below and it works like a charm.
I had the initialisation issue almost every boot but now only one time before it continues.
after i moved it before the while it's completly gone.


  pinMode(10,OUTPUT);
  while (!SD.begin(chipSelect)) {
  digitalWrite(10, HIGH);
  Serial.println("Card failed, or not present");}
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 // enable w5100 SPI
  pinMode(10,OUTPUT);
  // now enable the SD SPI at the same time
  while (!SD.begin(chipSelect)) {
  // now disable the w5100 SPI after the SD fail
  digitalWrite(10, HIGH);
  Serial.println("Card failed, or not present");}
That will cause problems. You are enabling the w5100 SPI, then calling the SD.begin() function using the SPI.

I think this is what you said worked.
Code:
  pinMode(10,OUTPUT);
  digitalWrite(10, HIGH);

  while (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
  }
« Last Edit: October 08, 2012, 04:00:29 pm by SurferTim » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code works on an Arduino Due with an Arduino Ethernet shield and micro SD card:

Quote
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,9);

void setup()
{

  pinMode(10, OUTPUT);
  digitalWrite(10,LOW);

    
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 // start the Ethernet connection and the server:

  Ethernet.begin(mac, ip);
  server.begin();
  
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
  if (!SD.begin(4))
      Serial.println("SD initialization failed."); 
}


Note the digitalWrite(10, LOW).
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Note the digitalWrite(10, LOW).
Not on the ethernet shield. All other SPI slave selects must be HIGH when running another SPI device setup. You are leaving the SD slave select (digital pin 4) potentially active. With pin 4 floating, it is difficult to tell how the SD will react to the setup commands intended for the w5100.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not quite sure I understand what's going on here.  But with the digitalWrite LOW it works and with a digitalWrite HIGH it definitely doesn't.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can I ask exactly how you would rewrite my setup() code above to make it rock solid?  I would be extremely grateful.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not quite answering my own question, the following:

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

  pinMode(10, OUTPUT);
  digitalWrite(10,LOW);   

  Ethernet.begin(mac, ip);
  server.begin();
 
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
  digitalWrite(10,HIGH);
 
  if (!SD.begin(4))
     Serial.println("SD initialization failed.");
   
  digitalWrite(10,LOW);
     
  Serial.print("server is (still?) at ");
  Serial.println(Ethernet.localIP());   
     
}

Seems to work most reliably.   Which is to say, set pin 10 LOW when talking to the ethernet, and HIGH when initialising the SD (it seems it can be left LOW when opening and reading SD files which in my case contain some HTML that's being served up; I haven't tried writing to the SD).  Any other pattern of LOWs and HIGHs mostly stops either the SD or the ethernet or both from working, but you can sometimes leave out the digitalWrite(10,HIGH); before the SD initialisation and things will still work.  Setting 10 HIGH before doing anything ethernet always stops the ethernet in its tracks.

I think I am confused (not for the first time...)
Logged

Pages: [1] 2   Go Up
Jump to: