How to start and stop writing to SD card

I am working on my project. So, for that i am collecting data from analog sensors(1-3). All I want is to save the data over SD card. I am using teensy 3.6 which is capable enough. I want to give command from serial monitor such as Start to start writing data and then Stop to stop writing data to Sd card. Is it possible to do that.


Yes, sure.

Visit Robin2’s serial input basics.

Is it possible to do that.

Assuming that question (assuming it's a question with no ?) warrants more than this useless answer:

Yes, sure.

The way I would do it is (if you're starting with one of the SD library examples, Datalogger say) is move all the data logging code that is in loop() into a function of its own, say logData(). Then to test, call the function unconditionally in loop(). (So at that stage, no new functionality, just tidy code.)

Then as suggested use one or other of the examples in Robin2's tutorial to read input; do a test on the input to set or clear a flag called say enableDataLogging.

Last change your call to logData() to be be conditional based on enableDataLogging.

The code below is what I outlined above. I also added millis() to each record, so you can tell at a glance that logging was turned off where there’s a gap in the times.

It’s tested, but not guaranteed foolproof, YMMV.

(I did add a safety net to close the file when logging gets turned off, just in case it was open at that instant.)

Type an a or a b in the monitor…

//  turn logging off and on via serial
//  this example just takes in a single character, a=on, b=off.
//  saves 3x analogs and millis() per record

  BASED on SD card datalogger
  and incorporating Robin2's serial technique Example 1 (single character)

  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 (for MKRZero SD: SDCARD_SS_PIN)

  created  24 Nov 2010
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.


#include <SPI.h>
#include <SD.h>
File dataFile; //needs to be global since it's used in more than one function here

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

void setup()
  pinMode(2, OUTPUT); //willuse to indicate logging or not
  digitalWrite(2, LOW);
  // Open serial communications and wait for port to open:
  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:
  Serial.println("card initialized.");
  Serial.println("a turns logging on, b turns it off...");


void loop()
  if (enableDataLogging) logData();

void recvOneChar()
  if (Serial.available() > 0) {
    receivedChar =; //although there may be more behind it in the buffer
    newData = true;

void useNewData()
  if (newData == true)
    //Serial.print("This just in ... ");
    if (receivedChar == 'a')
      enableDataLogging = true;
      Serial.println("Data logging enabled");
      digitalWrite(LED_BUILTIN, HIGH);
    if (receivedChar == 'b')
      enableDataLogging = false;
      Serial.println("   Data logging disabled");
      digitalWrite(LED_BUILTIN, LOW);
      //We might have hit stop while file was open, so closing just in case
      if (dataFile) dataFile.close();
    newData = false;

void logData()
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string, then append millis
  for (int analogPin = 0; analogPin < 3; analogPin++)
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    dataString += ",";
  dataString += String(millis());

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  dataFile ="newtest.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening new.txt");

Thank you all for replying and helping me.
I will test your provided solution and if required will post more problems.