Go Down

Topic: SD Card and SPI don't work on Arduino ATMEGA 2560 (Read 5311 times) previous topic - next topic

sebassilvap

Good afternoon gentlemen from the Arduino community I have an issue. I need to connect an arduino ATMEGA 2560, a SD Card module and a WIFI shield to save some data in a SD card. This is my example code
Code: [Select]
#include <SPI.h>
#include <SD.h>
/*
 * MISO - 50 - 12
 * SCK - 52 - 13
 * MOSI - 51 - 11
 * CS - 53 - 10
 */
const int chipSelect = 53;
int contador = 0;
int i = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.print("Inicializando sd card...");
  pinMode(chipSelect,OUTPUT);
  delay(2000);
  if (!SD.begin(chipSelect))
  {
    Serial.print("fallo lectura de tarjeta");
    delay(2000);
    return;
  }
  Serial.print("SD card inicializada ok");
  delay(2000);
 
}

void loop() {
  // put your main code here, to run repeatedly:
  for(i=0; i<100; i++)
  {
    contador ++;
    sdcard();
    delay(1000);
  }
}

void sdcard() {
  String dataString = "";
  dataString += String("Contador");
  dataString +=",";
  dataString += String(contador);
  dataString +=",";
  File dafile = SD.open("datalog.txt",FILE_WRITE);
  if (dafile) {
    dafile.print(",");
    dafile.println(dataString);
    dafile.close();
    Serial.println(dataString);
  }
  else {
    Serial.println("error al abrir datalog.txt");
  }
}


When I try this on my arduino uno it works putting the connections in the pins 12, 13, 11, 10, but when I try this on the Arduino ATMEGA 2560 with the WIFI shield it does not. I took out the WIFI shield and I tried just the arduino with the SD card module and still does not work. I saw a forum about this but without solution. Do I need another library for making it work? The pins 50, 52, 51 and 53 are the right ones for the SPI connection?
Please help me.
Best regards,
Sebastian

sebassilvap


6v6gt

Maybe you should say which SD card you are using. I guess you've checked that it is pin tolerant of 5Volts.


6v6gt

It appears that the model you have does not have level shifters (but just resistors) to handle the 5v/3.3v translations required for the basic SD chip.
I've also had a quick look at other peoples' experience and found all sorts of horrible hacks to attempt to stabilise these cheaper modules including library parameter changes and soldering additional components on the board (German: ) http://www.mikrocontroller.net/topic/273121

sebassilvap

Hi I have reviewed a lot of tutorials, I found this:

http://tech.memoryimprintstudio.com/access-sd-cards-from-tft-lcd-shield-using-arduino-mega-2560-soft-spi/#comment-1696

Which basically says that if I want to use the SD card module with arduino mega 2560 I have to do the following changes:

1) In Sd2Card.h and Sd2Card.cpp comment this

// #define USE_SPI_LIB

2) In Sd2Card.h

#define MEGA_SOFT_SPI 1

change to 1

3) Additionally I made the following change

Code: [Select]
#else  // SOFTWARE_SPI
// define software SPI pins so Mega can use unmodified GPS Shield
/** SPI chip select pin */
uint8_t const SD_CHIP_SELECT_PIN = 53;
/** SPI Master Out Slave In pin */
uint8_t const SPI_MOSI_PIN = 51;
/** SPI Master In Slave Out pin */
uint8_t const SPI_MISO_PIN = 50;
/** SPI Clock pin */
uint8_t const SPI_SCK_PIN = 52;
#endif  // SOFTWARE_SPI


I am using the pins 50, 51, 52 and 53 for my case (instead of the pins 10, 11, 12, 13 that are used on arduino UNO).

But still it does not work. Can anybody please give me a hand?

Best regards,
Sebastian.

Delta_G

In addition to changing the pins in the code, have you also changed the connections on the shield?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.


Delta_G

Code: [Select]
#else  // SOFTWARE_SPI

Wait, you might have changed the pin numbers in the wrong spot.  Are you really using software SPI?  Why in the world would you be doing that?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

sebassilvap

#9
Nov 25, 2016, 08:41 pm Last Edit: Nov 26, 2016, 11:51 am by sebassilvap
Hi @Delta_G I used this pin numeration stated in the same web page of arduino:

https://www.arduino.cc/en/Reference/SPI

What do you mean with software SPI?

In a desperate behavior I uninstalled the arduino IDE and installed it again. Something strange happened. First I tried the sketch with arduino UNO and it worked. Second I tried in Arduino Atmega 2560 and it worked too. Then I installed the rest of my circuit which includes two sensors, the WIFI shield (which by the way has a SD card module) and a IIC Liquid Crystal and it didn't work. Fourth I removed this part of my circuit and again tried with only with the arduino atmega 2560 and at the beginning it worked but then it stopped working. Fifth I tried again and it only saved the first value and then it stopped working. Can anybody please tell me what is happening?

sebassilvap

Please anybody that can give me a clue with this it is the third day that I spend and it does not work!

Delta_G

The use of the String class may be making swiss cheese out of your RAM and crashing your Arduino.  It is usually best to leave resource hungry crutches like the String class behind when you move to resource limited environments like the microcontroller.  Instead you should be using char arrays. 

Right now on each pass of loop you use the += operator on the String class 400 times.  Each one of those fragments the heap.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

sebassilvap

My question is why does it work perfectly on the arduino UNO and not on the arduino mega 2560?

sebassilvap

I had an experience. I kept the sd card pressed after the program was uploaded for 30 seconds and suddenly it started working. I closed arduino IDE I started the program again and it was working! I disconnected the usb cable connected it again and it stopped working. Does anybody have an idea of what is happening?

sebassilvap


Go Up