Hi,
Basically I want to know if I am headed in the right direction or am I screwing up right off the bat?
I bought 2 SD card reader modules from ebay
http://www.ebay.ca/itm/400497455183?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
I connected the reader as per the diagram on the seller’s product page. (see attached pic)
I understand that it is using the resistors as a voltage divider to supply 3.3V to the inputs of the SD card. I read on some articles that you need to supply 3.3 V inputs (MOSI, CS, CLK) or you can burn your card.
I measured the voltage level going into the card on some of those pins and it is 3.3V
The diagram says to use 3.3k with 1.8k on MOSI and CS, 1k with 2.2k with SCK. I did not have exactly those so I used 1.5k and 3k everywhere which gives me the almost exact 3.3V as mentionned above.
I am using pin 10 for slave select, reflected in my code.
I know some people have used a level shifter instead for this in certain articles.
The setup section fails on init, I tried 2 cards on it.
I checked my wiring, and I find nothing wrong with it so far.
I still have some troubleshooting to do, but want to know if this setup has a shot at working or not.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_Pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int BTN1_pin = 4;
int LED_1_pin = 13;
int SD_SS_pin = 10;
byte BTN_Val, PREV_BTN_Val = LOW;
int num_pressed=0;
File myFile;
LiquidCrystal_I2C lcd (I2C_ADDR, En_Pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
void setup()
{
Serial.begin(9600);
pinMode(BTN1_pin, INPUT);
pinMode(LED_1_pin, OUTPUT);
digitalWrite(LED_1_pin, LOW);
lcd.begin(20,4);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SD Test");
//lcd.setCursor(0,1);
//lcd.print("Button to work");
//delay(2000);
delay(4000);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(SD_SS_pin)) {
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()
{
//lcd.clear();
//lcd.setCursor(0,1);
//lcd.print(ii++,DEC);
//lcd.setBacklight(LOW);
//delay(3000);
//lcd.setBacklight(HIGH);
//delay(3000);
if ( BTN_Val == HIGH && PREV_BTN_Val == LOW )
{
delay(25);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Send #");
lcd.print(++num_pressed,10);
// tx msg
//startM = millis();
}
PREV_BTN_Val = BTN_Val;
//delay(1000);
}