Arduinio sd on Ethernet shield not working at all

久未见 提交于 2019-12-22 18:28:45

问题


I am new to Arduino, and I have an ethernet shield with an SD socket on top, but it not seems to be working. I am just trying to run a simple sketch taken from the SD libraries example to get infos about the card, but the "card.init(SPI_HALF_SPEED, chipSelect)" part always fails.

I have set the ChipSelect pin to 4, and set pin 10 to output, still nothing.

My code:

#include <SD.h>

Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = 4;    

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


  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);     // change this to 53 on a mega


  if (!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?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present."); 
  }

  // 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");
  }

}


void loop(void) {

}

What I get:

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

I am using Arduino Uno R3, Ethernet Shield (not the official one). I have tried with several SD cards: SD/SDHC, 2/4/16 Gb, Sandisk/Kingston, formatted with FAT16/FAT32

I am afraid something is bad with the shield itself (though the ethernet part is working). How can I identify the source of the problem? Please Help!


回答1:


Check here:

https://electronics.stackexchange.com/questions/67212/how-to-avoid-sd-card-and-w1500-spi-mixup-on-the-ethernet-shield/93868#93868

short overview of the answer from the link above:

#define SS_SD_CARD 4 #define SS_ETHERNET 10

digitalWrite(SS_SD_CARD, HIGH); // SD Card not active
digitalWrite(SS_ETHERNET, HIGH); // Ethernet not active

digitalWrite(SS_SD_CARD, LOW); // SD Card ACTIVE

//do SD-Card stuff here

digitalWrite(SS_SD_CARD, HIGH); // SD Card not active

digitalWrite(SS_ETHERNET, LOW); // Ethernet ACTIVE

//do Ethernet stuff here

if you have a Arduino Ethernet / SD Shield with the Wiznet 5100 Chip on it, you have exactly that known W5100 bug - as my Shield has. There are more informations about that bug by googling it.

When you connect this shield with the arduino the ethernet function is active and will not work if a sd-card is inserted in the slot. By using one of the Ethernet Examples from the standard library you will always get a DHCP failure (Failed to configure Ethernet using DHCP). By removing the SD-Card and restarting arduino (reset) it will work.

When you have to use both functions as I like to do you will have to struggle within the code in order to turn ethernet off and sd on and vice versa.




回答2:


pinMode(4, OUTPUT);

or to be correct

pinMode(chipSelect,OUTPUT);

Add this in the setting pin 10. hope this helps.

Some times in life it is the little things that mess us up.




回答3:


Put the following line of code after you set the pin 10 to output:

digitalWrite(10, High);

This should do the trick.




回答4:


FYI for anyone experiencing similar issues, i.e. using the Ethernet shield SD card and essentially the Arduino website SD card sample code and having inexplicable issues initializing the SD card. The above solution allowed the initialization for me.




回答5:


I ran your code and fixed it by initializing SD.begin() before the line Serial.print("\n Initializing SD card...");

Something like this:

SD.begin();
Serial.print("\nInitializing SD card..."); 


来源:https://stackoverflow.com/questions/17503094/arduinio-sd-on-ethernet-shield-not-working-at-all

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!