Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 21 of 21

Thread: Playing audio file from audio shield for Teensy 3.2

  1. #1

    Playing audio file from audio shield for Teensy 3.2

    I am trying to play an audio file from a micro SD card while simultaneously record an audio signal to pin 14 a(0). I am having trouble communicating with the SD card. The board is selected on Teensy 3.2 which shows up on port 6 in my device manager. I select port 6 in the IDE and began with an example code found under the tab file>>examples>>SD>>cardinfo. The only line changed was from
    const int chipSelect = 4;

    to

    const int chipSelect = 10;

    However the code wouldn't even compile giving me:
    error compiling for board Teensy 3.2/3.1.

    So next I try an example found on the forum:
    https://forum.pjrc.com/threads/540-C...for-Teensy-3-0

    which directs me to download a library, SdFatBeta20130621.zip. I placed the SDfat in libraries folder found in documents>>Arduino>>libraries

    the SdFatBeta20130621.zip download can also be found here:
    https://code.google.com/archive/p/beta-lib/downloads

    I then try the SDfat example, exactly as is, through file>>examples>>SDfat>>quickstart

    It compiled an uploaded via com 6 to the Teensy. Opening the serial monitor it asked me to enter the chip select pin number in which I entered 10.
    The feedback read:
    SD initialization failed.
    Do not reformat the card!
    Is the card correctly inserted?
    Is chipSelect set to the correct value?
    Is there a wiring/soldering problem?
    errorCode: 0x1, errorData: 0x0

    To not confuse my wiring I have inserted a picture that shows the Teensy placed on top of the shield match the left and right 14 pins.
    Click image for larger version. 

Name:	teensy w: shield.jpg 
Views:	1057 
Size:	77.4 KB 
ID:	7795

    Am I doing something wrong? How can I get the Teensy 3.2 to communicate with the SD card (preferably through the audio shield instead of another SD card adapter.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,994
    The audio shield uses pin 14 for SCK.

  3. #3
    Quote Originally Posted by PaulStoffregen View Post
    The audio shield uses pin 14 for SCK.
    Paul, for a small business you are very on top of things and I thank you for such a timely response. But with the shield parallel with the Teensy 3.2 isn't that arbitrary. Should I not stack the shield and Teensy together? Earlier I gathered that the sck of an SD card reader (3rd party) should be connected to 13 on the Teensy. Is this correct?

  4. #4
    Senior Member Ben's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    401
    You can use either Pin 13 or 14 as SCK when using SPI. But on the Audio Adaptor, Pin 13 is in use for the I2S communication (RX) to connect to the SGTL5000 IC. So only Pin 14 is left to use for SPI SCK.

    Since Pin 13 is the default SPI SCK pin, it is recommended as the "standard" SCK pin, like for 3rd-party SD-adaptors.

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Posts
    347
    Maybe I am missing the problem here....??....If you have the standard teensy 3.1 or 3.2 and the standard teensy audio shield, then connect it up as per the data sheet with nothing else connected and download and run the wav player sketch from the examples and it should work. Then try recording using a spare pin available

  6. #6
    These answers are good but they aren't solving the original problem. I cannot communicate with the micro SD inserted into the audio shield which is placed on top of the teensy 3.2, just as shown in the picture of my original post. I am assuming, after watching the audio shield demo on youtube, that the shield is supposed to work just fine in this configuration. So which pin is the sck pin that should be used is arbitrary, I think. I did realize that the version of Arduino IDE 1.6.10 has a problem with the version of Teensyduino, which is necessary in order to communicate with the Teensy. I switched computers which has Arduino IDE 1.6.9 and leaving the shield connected to the header pins of the Teensy was able to upload the simple blink example found in File>>Examples>>Teensy>>Tutorial1>>Blink. The controller started blinking its on-board LED. However when I go to File>>Examples>>SD>>cardInfo I have to change a single line to use the teensy audio shield:

    The original line - const int chipSelect = 4;

    changed to - const int chipSelect = 10;

    The serial monitor will read:

    Initializing SD card......Initialization failed. Things to check:
    is a card inserted?
    is your wiring correct?
    did you change the ChipSelect pin to match you shiled or module?

    I don't know what is going on but I have 3 of Teensy 3.2's and 2 audio shields and none of them will read the SD card. Please someone help me find an answer to this......

    Thanks in advanced

    Steven

  7. #7
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,230
    you're right; that example needs an update.

    this should work:
    Code:
    /*
      SD card test 
       
     This example shows how use the utility libraries on which the'
     SD library is based in order to get info about your SD card.
     Very useful for testing a card when you're not sure whether its working or not.
         
     The circuit:
      * SD card attached to SPI bus as follows:
     ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
     ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
     ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
     ** CS - depends on your SD card shield or module. 
             Pin 4 used here for consistency with other Arduino examples
    
     
     created  28 Mar 2011
     by Limor Fried 
     modified 9 Apr 2012
     by Tom Igoe
     */
     // include the SD library:
    #include <SD.h>
    #include <SPI.h>
    
    // set up variables using the SD utility library functions:
    Sd2Card card;
    SdVolume volume;
    SdFile root;
    
    // change this to match your SD shield or module;
    // Arduino Ethernet shield: pin 4
    // Adafruit SD shields and modules: pin 10
    // Sparkfun SD shield: pin 8
    // Teensy audio board: pin 10
    // Wiz820+SD board: pin 4
    // Teensy 2.0: pin 0
    // Teensy++ 2.0: pin 20
    const int chipSelect = 10;    
    
    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
      }
      
      SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
      SPI.setSCK(14);  // Audio shield has SCK on pin 14
    
      Serial.print("\nInitializing SD card...");
    
    
      // we'll use the initialization code from the utility libraries
      // since we're just testing if the card is working!
      if (!card.init(SPI_HALF_SPEED, chipSelect)) {
        Serial.println("initialization failed. Things to check:");
        Serial.println("* is a card inserted?");
        Serial.println("* is your wiring correct?");
        Serial.println("* did you change the chipSelect pin to match your shield or module?");
        return;
      } else {
       Serial.println("Wiring is correct and a card is present."); 
      }
    
      // print the type of card
      Serial.print("\nCard type: ");
      switch(card.type()) {
        case SD_CARD_TYPE_SD1:
          Serial.println("SD1");
          break;
        case SD_CARD_TYPE_SD2:
          Serial.println("SD2");
          break;
        case SD_CARD_TYPE_SDHC:
          Serial.println("SDHC");
          break;
        default:
          Serial.println("Unknown");
      }
    
      // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
      if (!volume.init(card)) {
        Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
        return;
      }
    
    
      // print the type and size of the first FAT-type volume
      uint32_t volumesize;
      Serial.print("\nVolume type is FAT");
      Serial.println(volume.fatType(), DEC);
      Serial.println();
      
      volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
      volumesize *= volume.clusterCount();       // we'll have a lot of clusters
      if (volumesize < 8388608ul) {
        Serial.print("Volume size (bytes): ");
        Serial.println(volumesize * 512);        // SD card blocks are always 512 bytes
      }
      Serial.print("Volume size (Kbytes): ");
      volumesize /= 2;
      Serial.println(volumesize);
      Serial.print("Volume size (Mbytes): ");
      volumesize /= 1024;
      Serial.println(volumesize);
    
      
      Serial.println("\nFiles found on the card (name, date and size in bytes): ");
      root.openRoot(volume);
      
      // list all files in the card with date and size
      root.ls(LS_R | LS_DATE | LS_SIZE);
    }
    
    
    void loop(void) {}
    can you try this, pls, and report back ?
    Last edited by Frank B; 08-04-2016 at 07:46 PM.

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,753
    Wow - that is BORING - but it worked as written in p_7 on my T_3.1 with fully pinned Audio board and an SDHC card:
    Initializing SD card...Wiring is correct and a card is present.

    Card type: SDHC

    Volume type is FAT32

    Volume size (Kbytes): 15549952
    Volume size (Mbytes): 15185

    Files found on the card (name, date and size in bytes):
    SYSTEM~1/ 2016-05-18 20:42:28
    WPSETT~1.DAT 2016-05-18 20:42:28 12
    INDEXE~1 2016-05-18 20:42:30 76
    SDTEST1.WAV 2016-03-30 13:00:00 16787550
    SDTEST2.WAV 2016-03-30 13:00:02 16425698
    SDTEST3.WAV 2016-03-30 13:00:06 13617358
    SDTEST4.WAV 2016-03-30 13:00:06 17173152
    WORKSHOP.PDF 2016-03-30 13:00:00 8894873
    TEST.TXT 2000-01-01 01:00:00 126

  9. #9
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,230
    thanks .)

    github pull request is here:

    https://github.com/PaulStoffregen/SD/pull/4/files

  10. #10
    Frank B I tried your suggested code but I'm still getting the same error:

    Initializing SD card......Initialization failed. Things to check:
    is a card inserted?
    is your wiring correct?
    did you change the ChipSelect pin to match you shiled or module?

    The last thing I haven't tried is swapping the micro SD. It is the only micro I currently have and bought it brand new so it should be just fine but who knows. I will try this tomorrow.

  11. #11
    Senior Member
    Join Date
    Apr 2014
    Posts
    347
    This works and plays the 4 named wav files.......with teensy connected to Audio adaptor as per the pjrc documentation


    #include <Audio.h>

    #include <Wire.h>

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

    // GUItool: begin automatically generated code

    AudioPlaySdWav playWav1; //xy=154,78

    AudioOutputAnalog dac2; //xy=455,609

    AudioConnection patchCord1(playWav1, 0, dac2, 0);

    // GUItool: end automatically generated code

    void setup() {


    // Audio connections require memory to work. For more
    // detailed information, see the MemoryAndCpuUsage example
    AudioMemory(5);

    SPI.setMOSI(7);
    SPI.setSCK(14);
    if (!(SD.begin(10))) {
    // stop here, put print message statement in here if reqd.
    }

    }

    void loop() {
    playWav1.play("KiloHz1.WAV");
    delay(1000);
    playWav1.play("KiloHz2.WAV");
    delay(1000);
    playWav1.play("KiloHz3.WAV");
    delay(1000);
    playWav1.play("KiloHz4.WAV");
    delay(1000);
    }

  12. #12
    Senior Member
    Join Date
    Apr 2014
    Posts
    347
    There must be something wrong with what is happening at this line in your sketch

    if (!card.init(SPI_HALF_SPEED, chipSelect)) .....??????

    Is SPI HALF_SPEED ....?????
    Is chipSelect getting set or not.......measure ir with a meter...????

  13. #13
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,994
    Quote Originally Posted by steven32bits View Post
    The last thing I haven't tried is swapping the micro SD. It is the only micro I currently have and bought it brand new so it should be just fine but who knows. I will try this tomorrow.
    Did it work?

    The wav file player example is supposed to "just work". It (and all the other audio lib examples which access the SD card) have the SPI.setSCK() and SPI.setMOSI() lines in them, so the pins are reconfigured properly for the alternate pins the audio shield uses.

    If you get really stuck, there is an easy but somewhat expensive solution. We now have a complete kit for the audio tutorial. It comes with the SD card already installed and already loaded with the 4 files. We fully test every one, including a test where we listen to it play a WAV file, so they always work right out of the box.

    http://www.pjrc.com/store/audio_tutorial_kit.html

  14. #14
    Senior Member
    Join Date
    Apr 2014
    Posts
    347
    Paul,.........it looks a very impressive tutorial...............

  15. #15
    It didn't work. I tried the wav file player and the blink while playing examples and both didn't play any audio. This is on a new micro SD with the same SDtest 1-4 files and the ones I want to use for my project. I am connecting a headphone into the headphone jack with . I took a scope to the line out outputs of the audio shield and it is showing noise around 20mV with a DC offset of about 25mV. This voltage, if an audio signal were detected should be 100mV or so preferably with no DC offset. I don't think I have audio on those pins. I failed to mention earlier but I have a 1k audio rated pot on the ports for the volume. I don't know if I have any other choices but to buy the test kit if I want it to work with ease.


    Quote Originally Posted by PaulStoffregen View Post
    Did it work?

    The wav file player example is supposed to "just work". It (and all the other audio lib examples which access the SD card) have the SPI.setSCK() and SPI.setMOSI() lines in them, so the pins are reconfigured properly for the alternate pins the audio shield uses.

    If you get really stuck, there is an easy but somewhat expensive solution. We now have a complete kit for the audio tutorial. It comes with the SD card already installed and already loaded with the 4 files. We fully test every one, including a test where we listen to it play a WAV file, so they always work right out of the box.

    http://www.pjrc.com/store/audio_tutorial_kit.html

  16. #16
    Senior Member
    Join Date
    Apr 2014
    Posts
    347
    #include <Audio.h>
    This bit of code posted earlier does NOT use the audio adaptor audio output
    "*************************
    #include <Audio.h>

    #include <Wire.h>

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

    // GUItool: begin automatically generated code

    AudioPlaySdWav playWav1; //xy=154,78 ............this is the thing that plays the wavs file from the SD card

    AudioOutputAnalog dac2; //xy=455,609 ......This is the analogue output from A14/DAC pin on teensy

    AudioConnection patchCord1(playWav1, 0, dac2, 0); ......This is the connection from the SD Card SPI output to the Teensy DAC and output on A14/DAC pin

    // GUItool: end automatically generated code
    "**************************
    You need to break you "big" problem down to several "small problems and deal with each in turn

    The code posted earlier plays wav files and sends out to DAC pin only, if you are using that then thats why you nove no audio at audio adaptor outputs.
    For that to happen you need to use the example for the audio adaptor
    It will have "AudioControlSGTL5000 AudioShield" loaded for the adaptor and the functions to control it and you need to enable the bits you want to use. you dont need a volume pot if you dont want it because it just ends up a number anyway if you read the code.
    AudioShield. enable();
    audioShiels.volume(100);
    audioShield.unmuteLineout();

    etc, etc, etc what ever you need.......

  17. #17
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,016

    Cool

    Have you tried a different micro SD card? Preferably a name brand bought from a trusted source? There are a lot of dodgy cards out there (and dodgy sellers who sell cards with fake labels attached). For my cameras, I tend to be a fan of SanDisk Ultras.

    The Audio adapter has this to say:

    Recommended SD Card

    Most SD cards are optimized for sequential access, where a camera or camcorder reads or writes a single large file. All SD cards work well for playing a single WAV file at a time.
    For simultaneous playing of 2, 3 or 4 stereo WAV files, many common "class 10" cards perform poorly. Even though they can support many megabytes per second in sequential access, they have high latency for non-sequential access.

    PJRC has tested several brands of SD cards. We recommend SanDisk Ultra for projects where multiple WAV files will be played at the same time. SanDisk Ultra is more expensive, but its non-sequential speed is much faster.

    The Arduino SD library supports up to 32 GB size. Do not use 64 & 128 GB cards.

    The audio library includes a simple benchmark to test SD cards. Open it from File > Examples > Audio > HardwareTesting > SdCardTest.

  18. #18
    Yes I have tried a different SD. I pretty much only use SanDisk for SD and thumb drives. Teenfor3, the problem is still the SD however the suggested audio example was supposed to read the SD and therefore testing if it is properly playing the audio files off of the SD, which is my ultimate goal. I did put my scope on those pins and am not reading a voltage that represents an audio signal. It was a DC voltage on the Left and Right audio output pins of the shield and a noise signal on the DAC/A14 pin. Unless you know another way to break this problem/circuit in half to test the SD communication I'm not too sure what else to do.

    Paul, if I were to purchase the test kit am I able to swap the thumb-wheel pot and the microphone for something more high performance? This was the reason I purchased them separately in the first place.

  19. #19
    Senior Member
    Join Date
    Apr 2014
    Posts
    347
    Separate what you are trying to do into individual bits with using simple examples from the teensyduino downloads not someone's examples. Test the sd only in the example for that it will list the details on the terminal and not complicate it with audio. When you get that working try the simple play sdwav example and check for audio make sure the right bits are enabled. To try the example posted above without using the detect sd bit change the if not sdbegin to just a statement sd .begin(10); that will begin sdcard even if is not there and simply go to loop and play the wavfiles and give error if not found

  20. #20
    Junior Member
    Join Date
    Jan 2017
    Location
    Indiana
    Posts
    2
    steven32bits,

    Did you ever get this to work? I am having the exact same issue. I've used three different audio boards, and three different Teensy 3.2s to test out the boards and SD cards and all gave me the same results. I did however try the code that Frank B posted and it worked for me. But when I try the ordiginal SdCardTest example I keep getting the same "SDCard is not connected or unusable :-( " error.

    /*
    SD card test

    This example shows how use the utility libraries on which the'
    SD library is based in order to get info about your SD card.
    Very useful for testing a card when you're not sure whether its working or not.

    The circuit:
    * SD card attached to SPI bus as follows:
    ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
    ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
    ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
    ** CS - depends on your SD card shield or module.
    Pin 4 used here for consistency with other Arduino examples


    created 28 Mar 2011
    by Limor Fried
    modified 9 Apr 2012
    by Tom Igoe
    */
    // include the SD library:
    #include <SD.h>
    #include <SPI.h>

    // set up variables using the SD utility library functions:
    Sd2Card card;
    SdVolume volume;
    SdFile root;

    // change this to match your SD shield or module;
    // Arduino Ethernet shield: pin 4
    // Adafruit SD shields and modules: pin 10
    // Sparkfun SD shield: pin 8
    // Teensy audio board: pin 10
    // Wiz820+SD board: pin 4
    // Teensy 2.0: pin 0
    // Teensy++ 2.0: pin 20
    const int chipSelect = 10;

    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
    }

    SPI.setMOSI(7); // Audio shield has MOSI on pin 7
    SPI.setSCK(14); // Audio shield has SCK on pin 14

    Serial.print("\nInitializing SD card...");


    // we'll use the initialization code from the utility libraries
    // since we're just testing if the card is working!
    if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
    } else {
    Serial.println("Wiring is correct and a card is present.");
    }

    // print the type of card
    Serial.print("\nCard type: ");
    switch(card.type()) {
    case SD_CARD_TYPE_SD1:
    Serial.println("SD1");
    break;
    case SD_CARD_TYPE_SD2:
    Serial.println("SD2");
    break;
    case SD_CARD_TYPE_SDHC:
    Serial.println("SDHC");
    break;
    default:
    Serial.println("Unknown");
    }

    // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
    }


    // print the type and size of the first FAT-type volume
    uint32_t volumesize;
    Serial.print("\nVolume type is FAT");
    Serial.println(volume.fatType(), DEC);
    Serial.println();

    volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
    volumesize *= volume.clusterCount(); // we'll have a lot of clusters
    if (volumesize < 8388608ul) {
    Serial.print("Volume size (bytes): ");
    Serial.println(volumesize * 512); // SD card blocks are always 512 bytes
    }
    Serial.print("Volume size (Kbytes): ");
    volumesize /= 2;
    Serial.println(volumesize);
    Serial.print("Volume size (Mbytes): ");
    volumesize /= 1024;
    Serial.println(volumesize);


    Serial.println("\nFiles found on the card (name, date and size in bytes): ");
    root.openRoot(volume);

    // list all files in the card with date and size
    root.ls(LS_R | LS_DATE | LS_SIZE);
    }


    void loop(void) {}

  21. #21
    Junior Member
    Join Date
    Jan 2017
    Location
    Indiana
    Posts
    2
    To follow up in case anyone is interested, I uninstalled Arduino 1.6.? (I don't remember what I had installed) and updated to Arduino 1.8.1. I also updated teensyduino with the beta 1.35 for Arduino 1.8.1 which can be found here. I reran the sample SdCardTest sample sketch and it worked without having to modify anything!
    Last edited by AG708; 01-17-2017 at 06:53 PM. Reason: extra word

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •