MicroSD Card Problem

Hi,

I just got 2 MicroSD Card modules :

[CATALEX, MicroSD Card Adapter v1.0 11/01/2013]

I followed as 1st example this video : Vid. 1 Youtube Video

I connected everything as in the video, uploaded the code :

#include <SD.h>
#include <SPI.h>

int CS_PIN = 10;

File file;

void setup()
{

  Serial.begin(9600);

  initializeSD();
  createFile("test.txt");
  writeToFile("This is sample text!");
  closeFile();

  openFile("prefs.txt");
  Serial.println(readLine());
  Serial.println(readLine());
  closeFile();
}
void loop()
{
}

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

int writeToFile(char text[])
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      return String(received);
    }
    else
    {
      received += ch;
    }
  }
  return "";
}

I formated the MicroSD in to NTFS, created a file as in the video " openFile("prefs.txt");"
and pluged everything and uploaded the code: I got In the serial monitor that file can't be created
and the 2nd file can'd be opened so I thought that the module is bad and i changed with the 2nd one
same thing.

Well i thought Im not doing something good so i uploaded and pluged the wires as in the
SD.h lib Example_SD_ReadWrite.
Done same as above and again i don't have any comunication, opened the serial monitor and
said "initialization failed!".

Im i doing something wrong? or my totally new modules are not working?
It's hard to believe that the totally new modules not working so that's why i came here.

So what it could be?

How did you format it?
Should be using formatter from this website

Then use SDfat.h library

Hi, CrossRoads

I formated the microSD with the website software you gave me and i downloaded the
SDFat.h lib, I opened the Example ReadWriteFat, uploaded the code and got this:

" Type any character to start
Can't access SD card. Do not reformat.
No card, wrong chip select pin, or SPI problem?
SD errorCode: 0X1,0X0 "

Im not sure exactly what's wrong, new module, good microSD card, all connected right but it's not working O_o

How do you have the card connected? May just have CS on the wrong IO pin.

Don't leave anything to chance.

// change this
  if (SD.begin())
// to this
  if (SD.begin(CS_PIN))

It's working, not sure what it was from the beginnig probably a bad wire connection, i even changed all wires
and it's working now.

I am using the ATMega1284 with the SD card library and got a problem I just cant shake

It seems I cannot move the chipselect pin away from PD4..

even with "const int chipSelect = PB4;"

and

card.init(SPI_FULL_SPEED, chipSelect);
SD.begin(chipSelect);

it wants to make PD4 the chipselect

The SD card is working, but only because the chipselect is being held low where I have it connected to PB4

(This probably answers all my problems with why my SPI RAM didnt work either !!)

Any clues anyone ??

Symbols of the form Pxn where x is a letter and n is a number are not pin numbers. These symbols are a bit number in an AVR PORT register.

When you run this program on a 1284 board

void setup() {
  Serial.begin(9600);
  Serial.println(PA4);
  Serial.println(PB4);
  Serial.println(PC4);
  Serial.println(PD4);
}
void loop() {}

It prints

4
4
4
4

If you want chip select to be port D bit 4, you must use the Arduino style digital pin number for the 1284 board you are using.

There are at least three pin maps for 1284 boards.

Pin maps for the version of the 1284 IDE that I use are here.

AVR pin D 4 maps as follows:

For "avr_developers" and also Sanguino it is digital pin 12.

For "bobuino" it is digital pin 30.

For "standard" it is digital pin 12.

Thanks, that makes alot of sense :slight_smile:

Sometimes a picture helps:

jboyton:
Sometimes a picture helps

I use that picture alot, but my SD card is still chipselected on PD4 even though the pin definition says PB4

I have decided to ignore the problem and move other pins around leaving CS on PD4 (I dont like not solving problems like this as they always come back to bite me one day !!)

mcnobby:
I use that picture alot, but my SD card is still chipselected on PD4 even though the pin definition says PB4

You mean if you code this...

const int chipSelect = 4;
card.init(SPI_FULL_SPEED, chipSelect);

...the code toggles physical pin 18 of the 1284 chip instead of physical pin 5?

That would be weird.

jboyton:
You mean if you code this...

const int chipSelect = 4;
card.init(SPI_FULL_SPEED, chipSelect);

...the code toggles physical pin 18 of the 1284 chip instead of physical pin 5?

That would be weird.

Exactly that Mr Boyton :frowning:

That seems fishy to me, Mr. McNobby.

Although it's possible, it's unlikely that something so obvious is wrong with the core. I am inclined to believe that you've done something to cause this. Whether it's a hardware oversight or a software one I can't say. But I like these kinds of bugs. I wish it were mine to solve. :slight_smile:

Good luck!

jboyton:
I am inclined to believe that you've done something to cause this.

I totally agree with you, whether it be installation of cores, version of IDE or perhaps the way I sometimes have to knife and fork bits of code to get it to work

I didnt have these kind of problems with 328/t85/t13 etc etc, maybe I might change the 1284 cores...