Hi,
I'm currently trying to use a SD Card Module and a Arduino Mega to save data collected from a sensor on a txt file, but it doesn'y save the text on the txt file.
Here's the code:
#include <SD.h>
#include <SPI.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 = 4;
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
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(chipSelect, OUTPUT);
// 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.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
int sensor = analogRead(0);
dataString = String(sensor);
// 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");
}
}
I got the following on serial monitor:
Initializing SD card...
card initialized
value from the sensor (just one time)
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
....
Any advices on what's wrong?
Thanks in advance,
José Coelho
try addig this:
pinMode(10, OUTPUT); // in setup
The SPI library sets the SS pin to an output already:
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "pins_arduino.h"
#include "SPI.h"
SPIClass SPI;
void SPIClass::begin() {
// Set SS to high so a connected chip will be "deselected" by default
digitalWrite(SS, HIGH);
// When the SS pin is set as OUTPUT, it can be used as
// a general purpose output port (it doesn't influence
// SPI operations).
pinMode(SS, OUTPUT);
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
}
I'm not sure where to find the link from Mega pin 53 to SS; setting 53 to an output in setup so the Mega is SPI master wouldn't hurt:
pinMode (53, OUTPUT);
You do have 5V to 3.3V converters for SCK, MOSI, and CS in the design someplace? (such as cd74HC4050) And a 200-250mA capable 3.3V source? The Mega 3.3V regulator is only good for 150mA.
knut_ny:
try addig this:
pinMode(10, OUTPUT); // in setup
I added that and still not working.
CrossRoads:
You do have 5V to 3.3V converters for SCK, MOSI, and CS in the design someplace? (such as cd74HC4050) And a 200-250mA capable 3.3V source? The Mega 3.3V regulator is only good for 150mA.
I though the module was ready to convert 5v to 3.3v. I saw some videos where they use the same module without any voltage converters or external sources. I'll try using a voltage converter and a better source (a capacitor will do the job or it's better to get one source)?
josecoelho:
I though the module was ready to convert 5v to 3.3v. I'll try using a voltage converter and a better source
You are probably right, I think most SD modules are good for 5v, and it will pay to check before you do anything. It should be clear enough, with a pin labelled 5v. If it doesn't have a 5v pin, it might be simpler to pay up another $2.50 and get a module that does..
My module has 3.3v and 5v pins.
In that case, looking to check was a good idea, and using a converter isn't.
The next thing to try is ignore reply#1, delete
pinMode(10, OUTPUT);
and use
pinMode(53, OUTPUT);
which is standard procedure for a Mega, and clearly shown as comment in the code you already have. Your code seems to be otherwise OK, although keeping the redundant line
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
can't be a good idea.
All this assumes you are using the the standard wiring for Mega
** MISO - 50
** MOSI - 51
** CLK - 52
** CS - 4
or the ICSP pins, which are common to Uno and Mega. You can, of course use any pin for CS, so long as you identify it in the code. A shield I have for a Mega uses pin 53, and this is probably common practice
const int chipSelect = 53;
void setup(){
pinMode(53, OUTPUT);
Serial.begin(9600);
Serial.print("\nInitializing SD card...");