Go Down

Topic: Sd logger problem (Read 1 time) previous topic - next topic


I made myself a sd shield to use with the mega it has a micro sd going to a sn74ahc125n and then to the SPI lines on the Mega.  It also has a 3.3v regulator.

MOSI -> 51
MISO -> 50
SCK  -> 52
CS    -> 10

I can not get any of the example sketches to work.  I have also tried a shield from adafruit with the same problem   http://www.ladyada.net/make/logshield//  I tried the sd library, and the sdfatlib.  The card wont initialise.  I had it working on a breadboard with no problems.  I am totally stumped.  I changed the chipselect in the sketch.

I have used the card with the one on a breadboard, so I know its formatted right. (I also tried 3 different sd cards)

Also I just upgraded to IDE 1.0.1  Could this have done anything?

Any help would be greatly appreciated. Here is the code. Thanks!

Code: [Select]
// Quick hardware test
#include <SdFat.h>
// Test with reduced SPI speed for breadboards.
// Change spiSpeed to SPI_FULL_SPEED for better performance
// Use SPI_QUARTER_SPEED for even slower SPI bus speed
const uint8_t spiSpeed = SPI_HALF_SPEED;
// Normally SdFat is used in applications in place
// of Sd2Card, SdVolume, and SdFile for root.
Sd2Card card;
SdVolume volume;
SdFile root;

// Serial streams
ArduinoOutStream cout(Serial);

// input buffer for line
char cinBuf[40];
ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));

// SD card chip select
int chipSelect;

void cardOrSpeed() {
  cout << pstr(
    "Try another SD card or reduce the SPI bus speed.\n"
    "The current SPI speed is: ");
  uint8_t divisor = 1;
  for (uint8_t i = 0; i < spiSpeed; i++) divisor *= 2;
  cout << F_CPU * 0.5e-6 / divisor << pstr(" MHz\n");
  cout << pstr("Edit spiSpeed in this sketch to change it.\n");

void reformatMsg() {
  cout << pstr("Try reformatting the card.  For best results use\n");
  cout << pstr("the SdFormatter sketch in SdFat/examples or download\n");
  cout << pstr("and use SDFormatter from www.sdcard.org/consumer.\n");

void setup() {
  cout << pstr(
    "\nSD chip select is the key hardware option.\n"
    "Common values are:\n"
    "Arduino Ethernet shield, pin 4\n"
    "Sparkfun SD shield, pin 8\n"
    "Adafruit SD shields and modules, pin 10\n");

bool firstTry = true;
void loop() {
  // read any existing Serial data
  while (Serial.read() >= 0) {}

  if (!firstTry) cout << pstr("\nRestarting\n");
  firstTry = false;

  cout << pstr("\nEnter the chip select pin number: ");
  if (cin >> chipSelect) {
    cout << chipSelect << endl;
  } else {
    cout << pstr("\nInvalid pin number\n");
  if (!card.init(spiSpeed, chipSelect)) {
    cout << pstr(
      "\nSD initialization failed.\n"
      "Do not reformat the card!\n"
      "Is the card correctly inserted?\n"
      "Is chipSelect set to the correct value?\n"
      "Is there a wiring/soldering problem?\n");
    cout << pstr("errorCode: ") << hex << showbase << int(card.errorCode());
    cout << pstr(", errorData: ") << int(card.errorData());
    cout << dec << noshowbase << endl;
  cout << pstr("\nCard successfully initialized.\n");
  cout << endl;

  uint32_t size = card.cardSize();
  if (size == 0) {
    cout << pstr("Can't determine the card size.\n");
  uint32_t sizeMB = 0.000512 * size + 0.5;
  cout << pstr("Card size: ") << sizeMB;
  cout << pstr(" MB (MB = 1,000,000 bytes)\n");
  cout << endl;

  if (!volume.init(&card)) {
    if (card.errorCode()) {
      cout << pstr("Can't read the card.\n");
    } else {
      cout << pstr("Can't find a valid FAT16/FAT32 partition.\n");
  cout << pstr("Volume is FAT") << int(volume.fatType());
  cout << pstr(", Cluster size (bytes): ") << 512L * volume.blocksPerCluster();
  cout << endl << endl;

  if (!root.openRoot(&volume)) {
    cout << pstr("Can't open root directory.\n");
  cout << pstr("Files found (name date time size):\n");
  root.ls(LS_R | LS_DATE | LS_SIZE);

  if ((sizeMB > 1100 && volume.blocksPerCluster() < 64)
    || (sizeMB < 2200 && volume.fatType() == 32)) {
    cout << pstr("\nThis card should be reformatted for best performance.\n");
    cout << pstr("Use a cluster size of 32 KB for cards larger than 1 GB.\n");
    cout << pstr("Only cards larger than 2 GB should be formatted FAT32.\n");
  // read any existing Serial data
  while (Serial.read() >= 0) {}
  cout << pstr("\nSuccess!  Type any character to restart.\n");
  while (Serial.read() < 0) {}


Sep 22, 2012, 07:12 am Last Edit: Sep 22, 2012, 07:36 am by Houser636 Reason: 1
I just built the circuit on a bread board again it has a micro sd broken out to the buffer chp, and the spi lines going to the buffer chip.  I tripple checked wiring.  Looks ok.  Now every once in a while I get -

Can't determine the card size.
Try another SD card or reduce the SPI bus speed.
The current SPI speed is: 2.00 MHz
Edit spispeed in this sketch to change it.

I changed the speed down to quarter, and still nothing. Also tried other SD cards.

I just put a resistor from GND to the middle of the chipselect line, and know it has that same error every time.  It initializes.  I hope I'm headed in the right direction, but still can't get it past that.


I just used the same sketch a couple days ago, the differences though is that I used a 74HC4050 and my CS pin is on 53 running full speed on a breadboard, neither should matter though.

what connections do you have on the SD card, they can trick you up ... if your looking at the pins with the angled edge facing left its




I went back and checked the sd card pins.  Turns out i just did a terrible soldering job.  I resoldered it and it works perfect.  Thanks for your help!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131