Hi everyone,
I am doing a project which needs a lots of I/O pins so I use Arduino MEGA 2560. I need a sd card for storing some data! so i took 8gb card and I have 2.4" display mcufriend shield which have sd card support with separate pins ( spi pins use for sd in that shield) when I used it with UNO my sd is giving me output but when i disconnect and connected to mega as mention at ARDUINO SD i also tried to change 50 with 51 and vice versa with no result! I don't understand where I am doing wrong as my sd card is OK as uno supporting it! Can any one could help me on it? I am using SD card info example of inbuilt sd library!
Are you using pin 10 instead of pin 53 in line 51?
Nah I am using 53 only and also used LCD.begin(53)
Here is my code if I did any wrong in it causing no card detect message to show up, please let me know if any mistake you found in it!
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(53) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
Your code does not compile. It stops at line 36
if (!SD.begin(53)) {
I specifically asked you if you had declared pin 53 as output. You said yes but there is no evidence of that.
One would expect a line like
const int chipSelect = 4;
You are not using it and have no equivalent. You need to know which pin is used for select.
I am not familiar with your display/SD and I don't understand how it connects to a Mega but, since it works on a Uno, I would imagine that all you need to do to the CODE is call 53 as output for the Mega, as clearly indicated in the example, nothing else. This is what happens when you use the SD on an Ethernet shied and swap from Uno to Mega. I understand that on the latest versions of IDE, even that is not necessary.
Further to this, the Ethernet shield has connection to the six-pin ICSP cluster and may pick up SPI therefrom when used with Mega. The following is a guess. IF your LCD does not have that pin cluster AND does not have connection to pins 22>54 then you have a shield specifically made not to work with a Meg. No amount of code will fix that and it makes getting a display that will work with a Mega start looking like a good idea. Perhaps a better idea is to put a Mega proto shield between the two in order to divert the wiring.
I thought when I use SPI.h and SD.h it has pin defining output and input in it as pin 53 is default ss pin in it!( I didn't read it yet!)
I got the problem dont no why it was occurring in it I was taking supply from my UNO only and all spi pins from mega and thought as both connected to same pc there would be no problem in taking supply from diff board ( thats where I was doing wrong) when i use complete MEGA with supply pins too it worked fine with the same code!
But thanks for your help on it!
I had a similar issue. The problem turned out to be crosstalk between the MISO and MOSI lines in my wiring harness. After I separated the two signals and routed them in separate harnesses, everything became rock solid.