Dear all.
I am having some trouble with my SD-card and the SPI bus. I am using Arduino Uno R3 with the MP3 Player Shield from SparkFun. SparkFun MP3 Player Shield - DEV-12660 - SparkFun Electronics I am trying to use it in conjunction with a Real-Time Clock, but for now, the question is regarding the SPI bus and SD-card.
I am using the SD library included with the Arduino, and I am able to access the SD card fine, as long as nothing else access the SPI bus before it. See the following code, which is a slightly modified version of the "Datalogger" example sketch.
#include <SPI.h>
#include <RTC.h>
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 9;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
pinMode(16, OUTPUT);
digitalWrite(16, HIGH);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
SPI.begin();
RTC.setup();
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
RTC.setTime(00, 44, 10, 17, 7, 13);
}
void loop()
{
/*
Serial.print(RTC.getWeekday());
Serial.print(" ");
Serial.print(RTC.getDay());
Serial.print("/");
Serial.print(RTC.getMonth());
Serial.print("/");
Serial.print(RTC.getYear());
Serial.print(" ");
Serial.print(RTC.getHour());
Serial.print(":");
Serial.print(RTC.getMinute());
Serial.print(":");
Serial.println(RTC.getSecond());
*/
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
/*
Serial.print("PIN 6: ");
Serial.println(digitalRead(6));
Serial.print("PIN 9: ");
Serial.println(digitalRead(9));
Serial.print("PIN 10: ");
Serial.println(digitalRead(10));
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setDataMode(SPI_MODE1);
*/
//digitalWrite(10, LOW);
//SPI.transfer(0x05);
//int result = SPI.transfer(0x00);
//digitalWrite(10, HIGH);
/*SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setDataMode(SPI_MODE0);
*/
delay(1000);
}
This code works perfectly and outputs the following result:
RTC setup complete
Initializing SD card...card initialized.
42,1018,1018
42,1018,1018
42,1018,1018
42,1018,1018
42,1018,1018
42,1018,1017
42,1018,1017
41,1018,1018
42,1018,1018
42,1018,1018
I then alter the code slightly, adding a term SPI.transfer(0x00); at the end of the file. The long term goal is to communicate with the RTC as mentioned, but for now I am troubleshooting. The foll code is then:
#include <SPI.h>
#include <RTC.h>
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 9;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
pinMode(16, OUTPUT);
digitalWrite(16, HIGH);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
SPI.begin();
RTC.setup();
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
RTC.setTime(00, 44, 10, 17, 7, 13);
}
void loop()
{
/*
Serial.print(RTC.getWeekday());
Serial.print(" ");
Serial.print(RTC.getDay());
Serial.print("/");
Serial.print(RTC.getMonth());
Serial.print("/");
Serial.print(RTC.getYear());
Serial.print(" ");
Serial.print(RTC.getHour());
Serial.print(":");
Serial.print(RTC.getMinute());
Serial.print(":");
Serial.println(RTC.getSecond());
*/
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
/*
Serial.print("PIN 6: ");
Serial.println(digitalRead(6));
Serial.print("PIN 9: ");
Serial.println(digitalRead(9));
Serial.print("PIN 10: ");
Serial.println(digitalRead(10));
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setDataMode(SPI_MODE1);
*/
//digitalWrite(10, LOW);
//SPI.transfer(0x05);
int result = SPI.transfer(0x00);
//digitalWrite(10, HIGH);
/*SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setDataMode(SPI_MODE0);
*/
delay(1000);
}
The only change is the addition of a SPI.transfer(), and the output is this:
RTC setup complete
Initializing SD card...card initialized.
42,1018,1018
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
The code runs fine the first time in the loop, but after having the SPI.transfer called, it refuses to operate ever again. I have checked all the SC pins, and they are all high when this is called, so I did not think it would affect anything. I am utterly baffled as to why this happens, and would greatly appreciate some input.
Thank you all very much!