Go Down

Topic: Trying to make Serial monitor data to SD card (Read 2461 times) previous topic - next topic

VoldemarsB

Hello.

I am a new arduinoer. And i am using serial monitor, and i would like to know how to save Serial monitor data to MicroSD card. I am using a MicroSD card adapter, not Ethernet shield which has a built in MicroSD card adapter.

 :)


gilshultz

That is an interest question with lots of answers. I would suggest before starting go on line and read the many different posts on how to do it. YouTube has a lot of tutorials as well.  What you do and how you do it is dependent on your application, it sounds maybe like a data logger, you can short circuit the process and get a data logger shield.  This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil
This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

ieee488

Have you run a simple example sketch that verifies you are able to access the microSD card?
I suspect you will have problems with that.


12Stepper

Have you run a simple example sketch that verifies you are able to access the microSD card?
I suspect you will have problems with that.
Why would you say that?

12Stepper

I am a new arduinoer. And i am using serial monitor, and i would like to know how to save Serial monitor data to MicroSD card. I am using a MicroSD card adapter, not Ethernet shield which has a built in MicroSD card adapter.

When working with input from serial, it's a good idea to read forum member Robin2's serial tutorial.

Among other things, he advocates the separation of data's input from its use, and in example 1 does that in 2 functions called from loop():

Code: [Select]
void loop() {
    recvOneChar();
    showNewData();
}


So, the code below is an amalgamation of Robin2's Example1, and the datalogger example from the SD card library. The SD stuff is in the second function, which I renamed showAndSaveNewData(), which both shows the character captured in recvOneChar() and saves it to the SD.

This is the dialog from when I entered data from the monitor:

Code: [Select]
Initializing SD card...card initialized.
<Arduino is ready>
This just in ... a
This just in ... b
This just in ... c


Here's the SD card opened in notepad:

Code: [Select]
a
b
c


Here's the code:

Code: [Select]

// https://forum.arduino.cc/index.php?topic=651051.0
// serial to sd

/*
  BASED ON SD card datalogger https://www.arduino.cc/en/Reference/SD
  BASED ON serial input basics http://forum.arduino.cc/index.php?topic=396450 example 1
  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 (for MKRZero SD: SDCARD_SS_PIN)
*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;
char receivedChar;
boolean newData = false;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // 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.");
  Serial.println("<Arduino is ready>");
}

void loop()
{
  recvOneChar();
  showAndSaveNewData();
}


void recvOneChar()
{
  if (Serial.available() > 0)
  {
    receivedChar = Serial.read();
    newData = true;
  }
}//recvOneChar

void showAndSaveNewData()
{
  if (newData == true)
  {
    Serial.print("This just in ... ");
    Serial.println(receivedChar);
    newData = false;

    //now the sd card stuff
    // 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(receivedChar);
      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");
    }
  }
}//showAndSaveNewData



Robin2's Example1 is for one character at a time; have a look at the other ones. But the foregoing at least shows you that what you want to do (although you don't give details) is feasible.




ieee488

Why would you say that?

Because it always happens to newbies such as yourself who jump into the deep end of the pool first.

.

12Stepper

#6
Dec 02, 2019, 05:36 am Last Edit: Dec 02, 2019, 05:54 am by 12Stepper
In the years I've used SD cards, either on the Ethernet shield or my standalone adafruit one, it's never not worked. The example code with the SD library has always worked.

So why would you (ieee488) for a moment expect the OP (VoldemarsB) not to be able to successfully run example code that is known good?



Go Up