Write .txt file to Sd Card on audio shield using Teesny 4.0

rsh50

Member
Hi All,
I am working on this toy project where i want to write a text file to sd card on audio shield (Rev D) sd card holder through teensy 4.0.
The goal: Every time I press the reboot button on teensy board the data should be written to the Sd card (without printing anything to monitor).
Current Issue: The data is being written to Sd card but only when I plug it into power source. But when i press reboot button the teensy doesn't reboot and the red led light stays on and no data is written to the Sd card.

I used the example SdCard test code and modified to write to Sd card. Can anyone help me find what's wrong or what is the logic to achieve my goal. I have added image of my setup if that helps

Code:
// SD Card Test
//
// Check if the SD card on the Audio Shield is working,
// and perform some simple speed measurements to gauge
// its ability to play 1, 2, 3 and 4 WAV files at a time.
//
// Requires the audio shield:
//   http://www.pjrc.com/store/teensy3_audio.html
//
// Data files to put on your SD card can be downloaded here:
//   http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
//
// This example code is in the public domain.

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

// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7   // Teensy 4 ignores this, uses pin 11
#define SDCARD_SCK_PIN   14  // Teensy 4 ignores this, uses pin 13

// Use these with the Teensy 3.5 & 3.6 & 4.1 SD card
//#define SDCARD_CS_PIN    BUILTIN_SDCARD
//#define SDCARD_MOSI_PIN  11  // not actually used
//#define SDCARD_SCK_PIN   13  // not actually used

// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN    4
//#define SDCARD_MOSI_PIN  11
//#define SDCARD_SCK_PIN   13
File Test;
int i=1;
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Sd2Card card;
  SdVolume volume;
  File f1, f2, f3, f4;
  // char buffer[512];
  boolean status;
  // unsigned long usec, usecMax;
  elapsedMicros usecTotal, usecSingle;
  // int i, type;
  int type;
  float size;


  // wait for the Arduino Serial Monitor to open
  // while (!Serial) ;
  // delay(50);

  // Configure SPI
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);

  // Serial.begin(11000);
  // Serial.println("SD Card Test");
  // Serial.println("------------");

  // First, detect the card
  status = card.init(SPI_FULL_SPEED, SDCARD_CS_PIN);
  if (status) {
    // Serial.println("SD card is connected :-)");
  } else {
    // Serial.println("SD card is not connected or unusable :-(");
    return;
  }

  type = card.type();
  if (type == SD_CARD_TYPE_SD1 || type == SD_CARD_TYPE_SD2) {
    // Serial.println("Card type is SD");
  } else if (type == SD_CARD_TYPE_SDHC) {
    // Serial.println("Card type is SDHC");
  } else {
    // Serial.println("Card is an unknown type (maybe SDXC?)");
  }

  // Then look at the file system and print its capacity
  status = volume.init(card);
  if (!status) {
    // Serial.println("Unable to access the filesystem on this card. :-(");
    return;
  }

  size = volume.blocksPerCluster() * volume.clusterCount();
  size = size * (512.0 / 1e6); // convert blocks to millions of bytes
  // Serial.print("File system space is ");
  // Serial.print(size);
  // Serial.println(" Mbytes.");

  // Now open the SD card normally
  status = SD.begin(SDCARD_CS_PIN);
  if (status) {
    // Serial.println("SD library is able to access the filesystem");
  } else {
    // Serial.println("SD library can not access the filesystem!");
    // Serial.println("Please report this problem, with the make & model of your SD card.");
    // Serial.println("  http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports");
  }



}


void loop() {
  // do nothing after the test

  if (i==1)
  {
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(1000);                      // wait for a second
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
    delay(1000);

    writeheaders();

    // Serial.println("headers have been printed, writing data");

    writedata();

    // Serial.println("SD card has been written to and file is closed.");
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
      delay(1000);                      // wait for a second
      digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
      delay(1000);
    i=0;
  }
}

void writeheaders(){
  Test = SD.open("test.txt", FILE_WRITE);

  if(Test){
      // Serial.print("Writing to file");

      Test.println("NUM1, NUM2, NUM3");

      Test.close();
      
    
 

  }
else{
  // Serial.println("The SD card failed to initialize");
  while(1);
}

}

void writedata(){
  Test = SD.open("test.txt", FILE_WRITE);
  if(Test){

    // while(int i = 0; i < 50; i++)
    // {
      // Serial.println(" writing data");

       int num1 = 1;
       int num2 = 2;
       int num3 = 3;
       Test.println(num1 + ',' + num2 + ',' + num3 + ',');
       Test.close();

      
    // }
  }
  else{
    // Serial.println("The SD card failed to initialize");
    while(1);
  }

}
 

Attachments

  • 6CB2B70A-E910-4BBC-8B08-6A9D941C2C7C_1_201_a.jpg
    6CB2B70A-E910-4BBC-8B08-6A9D941C2C7C_1_201_a.jpg
    218.6 KB · Views: 18
Hi All,
I am working on this toy project where i want to write a text file to sd card on audio shield (Rev D) sd card holder through teensy 4.0.
The goal: Every time I press the reboot button on teensy board the data should be written to the Sd card (without printing anything to monitor).
Current Issue: The data is being written to Sd card but only when I plug it into power source. But when i press reboot button the teensy doesn't reboot and the red led light stays on and no data is written to the Sd card.

I used the example SdCard test code and modified to write to Sd card. Can anyone help me find what's wrong or what is the logic to achieve my goal. I have added image of my setup if that helps
It's NOT a reboot button but a Teensy (re)program button.
 
No it will simply clear all the memory and reprogram the Teensy.
Why not simply use a button attached to a Teensy pin?
 
No it will simply clear all the memory and reprogram the Teensy.
Why not simply use a button attached to a Teensy pin?
I see, appreciate your quick replies helped a lot.
Is there a tutorial link or article that you suggest for connecting button to my quad channel teensy setup.
Even 2 channel links works for me if i can simply extend the connections. I am not very familiar with circuits
 
You could watch this video. It will give you an idea how to connect a momentary switch.
It's for an Arduino Uno, but the same applies to the Teensy EXCEPT when they mention using an External Pull Up Resistor to 5V.
DO NOT connect an external resistor to 5V but ONLY to 3V. Using 5V will DESTROY, at the least that pin, at the worst the complete Teensy.
 
You could watch this video. It will give you an idea how to connect a momentary switch.
It's for an Arduino Uno, but the same applies to the Teensy EXCEPT when they mention using an External Pull Up Resistor to 5V.
DO NOT connect an external resistor to 5V but ONLY to 3V. Using 5V will DESTROY, at the least that pin, at the worst the complete Teensy.
Sounds good. Thanks for advise about resistor, will try it and see if it works.
 
Here is a Teensy specific tutorial for connecting a pushbutton. The info is same as all other Arduino compatible boards.

However, you might consider using the Bounce library instead of digitalRead(). The Bounce library gives you automatically handling of mechanical chatter in the switch. There isn't a nice tutorial, but you can find examples in Arduino IDE. Click File > Examples > _Teensy > USB_Keyboard > Buttons. Hopefully the example code is easy to understand and just copy the relevant parts into your own program.
 
Here is a Teensy specific tutorial for connecting a pushbutton. The info is same as all other Arduino compatible boards.

However, you might consider using the Bounce library instead of digitalRead(). The Bounce library gives you automatically handling of mechanical chatter in the switch. There isn't a nice tutorial, but you can find examples in Arduino IDE. Click File > Examples > _Teensy > USB_Keyboard > Buttons. Hopefully the example code is easy to understand and just copy the relevant parts into your own program.
Appreciate it, will keep these things in mind when I make the switch connections.
 
Back
Top