I had been stuck on this issue for a few days and tried most of the solutions online but with no avail. I am using Arduino Mega 2560 with my recently purchase 1.8" TFT Display Shield. I stacked the shield directly over the Arduino Mega following this tutorial Overview | 1.8" TFT Display Breakout and Shield | Adafruit Learning System . Graphics test and joystick tests are able to run perfectly. However, the problem comes with I wanted to try reading the image from the SD card. The Initializing SD card always failed. The SD_Chip selected is defined as pin 4. I also added the line to upon the note that even if it's not used as the CS pin, the hardware SS pin 53 on the Mega must be left as an output (pinMode(53, OUTPUT) or the SD library functions will not work. But it still fail to initialize.
Partial Code
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins. For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_RST 9 // Reset line for TFT (or see below...)
#define TFT_DC 8 // Data/command line for TFT
#define SD_CS 4 // Chip select line for SD card
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// Option 2: use any pins but a little slower!
#define TFT_SCLK 13 // set these to be whatever pins you like!
#define TFT_MOSI 11 // set these to be whatever pins you like!
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup(void) {
Serial.begin(9600);
pinMode(53, OUTPUT);
// Use this initializer if you're using a 1.8" TFT
tft.initR(INITR_BLACKTAB);
// Use this initializer (uncomment) if you're using a 1.44" TFT
//tft.initR(INITR_144GREENTAB);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println(SD_CS);
Serial.println("failed!");
return;
}
Serial.println("OK!");
// change the name here!
bmpDraw("parrot.bmp", 0, 0);
// wait 5 seconds
delay(5000);
}
void loop has nothing much inside and bmpDraw() is as default. But those are not impt for connecting the SD Card I guess.
I had been stuck on this issue for a few days and tried most of the solutions online but with no avail. I am using Arduino Mega 2560 with my recently purchase 1.8" TFT Display Shield. I stacked the shield directly over the Arduino Mega following this tutorial Overview | 1.8" TFT Display Breakout and Shield | Adafruit Learning System . Graphics test and joystick tests are able to run perfectly. However, the problem comes with I wanted to try reading the image from the SD card. The Initializing SD card always failed. The SD_Chip selected is defined as pin 4. I also added the line to upon the note that even if it's not used as the CS pin, the hardware SS pin 53 on the Mega must be left as an output (pinMode(53, OUTPUT) or the SD library functions will not work. But it still fail to initialize.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins. For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_RST 9 // Reset line for TFT (or see below...)
#define TFT_DC 8 // Data/command line for TFT
#define SD_CS 4 // Chip select line for SD card
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// Option 2: use any pins but a little slower!
#define TFT_SCLK 13 // set these to be whatever pins you like!
#define TFT_MOSI 11 // set these to be whatever pins you like!
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup(void) {
Serial.begin(9600);
pinMode(53, OUTPUT);
// Use this initializer if you're using a 1.8" TFT
tft.initR(INITR_BLACKTAB);
// Use this initializer (uncomment) if you're using a 1.44" TFT
//tft.initR(INITR_144GREENTAB);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println(SD_CS);
Serial.println("failed!");
return;
}
Serial.println("OK!");
// change the name here!
bmpDraw("parrot.bmp", 0, 0);
// wait 5 seconds
delay(5000);
}
void loop has nothing much inside and bmpDraw() is as default. But those are not impt for connecting the SD Card I guess.
Anybody got any idea how to fix this? Please help(:
Most software is written for UNO rather than MEGA2560. Likewise the most popular Shields.
This means that you have to bit-bang SPI on the MEGA2560 because the shields expect you to be using the UNO SPI pins (10, 11, 12, 13).
The Adafruit examples generally contain constructors for bit-banging the "graphics" sketches.
If they use the SD disk, they are just using the conventional hardware SPI because no one thinks that you would want to bit-bang <SD.h> library.
My advice would be: buy a UNO or clone.
If you are determined to use the MEGA2560, you need to use SofwareSPI with the <SD.h> library. Yes, it can be done. But it is not straightforward.
David.
p.s. some shields have specific connector to mate with the ISP header. This lets them pick up the hardware SPI from both a MEGA and a UNO. Is your shield like this ?
I guess I'm quite constrained to use the Mega2560. Apparently the shield that I am using doesn't have the ICSP headers. Am I right to say that I have to manually connect the Mega's hardware SPI pins to the shield's SPI pin? Still don't quite understand this whole software SPI thing, so confusing.
Do you know if 2 peripherals can share the same SPI pins?
Yes, multiple device can share SCK, MOSI, MISO pins. Each device needs its own unique chip select pin.
3.3V devices like SD cards need SCK, MOSI, CS buffered down to 3.3V level (cd74HC4050 is good for that), and its good to have MISO coming back buffered as well so other 5V devices aren't dragged down by 3.3V clamp diodes on the3.3V devices (I used 74HC125 for that).
SD cards can also be power hungry, the 3.3V from the Mega can not supply enough current.
You can likely use the Uno based shield on the mega by adding some jumpers - 10-11-12-13 to 50-51-52-53 - check the schematics to match up CS (10), MOSI (11), MISO (12), and SCK (13) with the same Mega pins.
Pin 10 may be pin 4, that seems to be a popular choice too. I use pin 10 as the SS pin must be an output to be SPI master.
Then in your sketch, do not use 10-11-12-13 on the Mega, just leave them as inputs (which is the default state).
Hardware SPI libraries always uses 11-12-13, and the equivalent on the Mega. Software SPI doesn't make sense for an SD card, too slow.
I connected the pins 11-12-13 to 51-52-53 respectively and the SD card initialized without any problems! All 3 wires had to be present, missing of any one of them will result in the SD not being able to initialize.
Those was missing for the tutorial I was following.
I'm sorry, but I'm having exactly the same problem! I make the same connections and I still have the same SD reading error! The only change in @nicholasnbx code is to add
SD.begin(SD_CS, 11, 12, 13) in the setup() function?