myFile.print to Arduino wireless SD shield

Hi XD— Right now I’m trying to use an Arduino wireless SD shield to transmit temperature data to and Xbee(1) on a breakout board to the computer. The sketch is listed below.

  1. See the comment at top of the sketch. The SD card isn’t printed to, but it is working for the Examples sketch, SD card read/write. I was extremely careful in comparing the Examples’ read/write and the commands in my sketch.

Two more questions came up, but these can be deferred 'till later. The SD card is the important problem right now.

  1. The X-CTU terminal prints the data OK, but there are no “mySerial.print” commands needed when the shield atop the UNO is connected to the computer. Yet when I use another power source, no data is sent to the X-CTU terminal. This is true, also, when I do use “mySerial.print” to send data, as shown in the sketch.

3)File myFile; is declared globally and in void loop(). If it’s declared globally only, I get an error message that it isn’t declared in this scope. Why?

/* This sketch compiles and prints OK to Serial monitor and to 
 but doesn't print to the Micro SD card in the Arduino SD 
 wireless shield. The card initialization fails. However, The 
 example, "read/write" does print OK to this same Micro SD on 
 this shield. Sketch setup for the SD card is between two "===="lines. 

#include <Wire.h>
#include "RTClib.h"
RTC_Millis RTC;
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(0,1);
#include <SD.h>

const int chipSelect = 10;

const int sensPin = 0;
const int ledPin = 8;

void setup() {
  pinMode(sensPin, INPUT);
  pinMode(ledPin, OUTPUT);
  mySerial.print("Hello World");
  //=============================================SD setup
  Serial.print("initializing SD card --");
  if(!SD.begin(chipSelect)) {
    Serial.println("Card Failed");
  Serial.print("Card Initializeds");
  pinMode(10, OUTPUT);
  Serial.print("Card Initialized");

  if (SD.exists("Datalog.txt")) {
    Serial.println("Datalog.txt exists");
  else {
    Serial.println("Datalog.txt doesn't exist");

  // ===========================================SD done 
  DateTime now =;
  RTC.begin(DateTime(__DATE__, __TIME__));

  Serial.print("OK thus far");

void loop(){
  File myFile;
  myFile ="Datalog.txt", FILE_WRITE);   // Culprit

  int reading = analogRead(sensPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float tempC = (voltage - .44) * 100;
  float tempF = (tempC * 9.0/5.0) + 32;

  Serial.print("     ");
  DateTime now =;

  Serial.print(now.hour(), DEC);
  Serial.print(now.minute(), DEC);
  Serial.print(now.second(), DEC);
  Serial.print("     ");
  Serial.print("     ");
  Serial.print("     ");

  mySerial.print(now.hour(), DEC);
  mySerial.print(now.minute(), DEC);
  mySerial.print(now.second(), DEC);
  mySerial.print("     ");
  mySerial.print("     ");
  mySerial.print("     ");
   if (mySerial.available())
  if (Serial.available())

  if (myFile) {
    myFile.print(now.hour(), DEC);
    myFile.print(now.minute(), DEC);
    myFile.print(now.second(), DEC);
    myFile.print("     ");
    myFile.print("     ");
    myFile.print("     ");



The SD card stuff in the Setup section looks kosher. I have the line

File myFile;

in the preamble immediately after the includes. You don't have this. Maybe that is the problem. I don't have it in the loop.

HI Nick,

Thanks for your reply. Actually, if you got to my third question, you'd see that I did add "File myFile" in the preamble, but I got the "not in this scope" error and had to put it i the loop, as well. I left it out in the preamble to test the sketch and it wound up in the forum sketch.

Are you using an Arduino wireless SD shield successfully? Or are you working with SD without the Xbee? Thanks again.


Hi OldGuy

No, no wireless, bluetooth suffices for my local operations. I have SD cards in the Ethernet shields for my Megas and my Uno is an EtherTen, which has SD and Ethernet built-in. This is not really relevant as the SD works the same wherever it is located.

I really can’t see what your problem is. It’s probably something stupid that newbies do and will only happen twice!

The main differences between what you are doing and what I am doing is essentially just the degree and type of remoteness. Here is my current code. It’s rather voluminous and a bit rough and ready in places, but the SD stuff is all kosher and you might find something useful in it.


Working 20-01-13.txt (8.68 KB)

Hi NIck, :smiley:
To give you some idea how much I treasure your sketch, I’ll tell you my long-term goal. I wish to gather data close to the eye and send it to storage in the SD card. This might involve temperature, but the main data would be skin conductivity and movement. Part of the aim is to use an lcd monitor to look at early data. I envision the remote sensor(s) on a lily pad/Xbee and the base as an Arduino wireless SD shield. These are formidable barriers to a beginner, partly because of difficult conflicting writing in some tutorials and cookbooks. So thanks a ton for that sketch. I’ll do my best to restrain myself from questions.

I’m intrigued by certain small sketch items by one with a much higher level of sophistication. One is
“char filename = “00000000.CSV”;” after “File myFile”, and the .CSV extension. Presently, I just collect X-CTU terminal data and paste it into Excel and wondered how to get data directly into Excel. (This isn’t a question for you, as this isn’t a top priority.) Studying you sketch might be the answer.

Recently, I ran across a comment in the Arduino website, teaching that it’s better to use “const int” than “#define”; apparently it frees up memory. I think it was in the “learning” link on the web page. Perhaps this isn’t relevant in your work.

Finally, it seems that you don’t print to the SD card unless K>9 and K returns to 0 after printing. Are you really waiting for K=9 and, if so, why set it to 0? This is a question I can’t resist asking.



It sounds like you are into some pretty serious stuff there…

Essentially what I am doing is catering for four simultaneous logging operations

  1. real-time very local with eyeball. This is LCD updated every second

  2. real-time slightly less local with laptop. This is the same signal to Bluetooth

  3. more-or-less real-time remote. This is sent to , and updated every 10 seconds

  4. long-term backup. To SD card every ten seconds, same data as to cosm.

This is all with two Megas, hundreds of kilometres away and in opposite directions, and an EtherTen here at home. The rig here will have the Internet stuff removed. It won’t fit in the 32k available.

About the “char filename = “00000000.CSV”;”
This is the setup to establish the variable for naming the file on the SD. On the first loop after midnight a new file is created and named with today’s date. Needless to say, this absolves me from storing the date in the data. This is the main area where the code is messy. You might have noticed that I am using two clock libraries. This is because I haven’t yet worked out how to do two jobs, name the file and record the time, with one library.

About the k
The loop takes one second and k=k+1 in line 183 advances it every time around. Every tenth time, the following if sends data to the SD and cosm, and k is reset. I’m glad you brought this up as I see I have forgotten to remove the date from the myfile prints.

Actually, getting data into Excel is a priority and the code is set up to use PLX-DAQ.

This is not actually designed for Arduino but that really involves no more than sending the right serial commands, and it works just fine. This is another place where the code is messy as it was last used to send data to RealTerm via Bluetooth rather than Excel. I will eventually standardise on Excel over PLX via Bluetooth, as its real-time graph should impress the natives.

Hi Nick, Thanks so much for the link to parallax and I've learned much with your help; even more when I carefully read your two responses. I thought I could write to the SD card if I changed everything from "myFile.print" to "myFile.write", but I get an error that says I can't change uint8_t to "const uint8_t, whatever that means. No need to answer; I'm looking into Arduino "learning", etc. Also, I'm writing the sketch from ground 0 to print to the SD card alone. I have Adafruit dataloggers that work, but I don't want to wear the UNO on my head for collecting eye sensor data.

Communicating with Arduinos many kilometers apart opens up new unknown vistas for me and I wish you well.