Teensy 3 MicroSD guide

Status
Not open for further replies.
@PaulStoffregen

Thanks! you're right, it's easier to have the adapter, and it's more versatile. Having a little buffer won't do any harm. it was just a technical speculation.

MJ
 
I just hooked this up but am failing to get the benchmark code to work.

I hooked up micro SD adapter board the same way and was able to the cardInfo sketch both from the original and SDFat running and it tells me info about the card and list the files, etc.

But when I run the benchmark or any sketch that writes to a file, the LED lights up and it seems to never get past the write function.

For instance, in benchSD it never gets past this code. I even lowered the filesize to just the size of the buffer so it would have to do it only once:
Code:
    if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
      error("write failed");
    }

Any ideas on what would prevent a write? I have a 32GB micro SD card class 10 SDHC formatted in FAT32.
 
Ok, I just needed to format the card with the teensy. Did you do anything to get better benchmarks?

Here's my benchmark @ 96MHz (which was not very different than 48MHz)
Free RAM: 12343
Type is FAT32
File size 5MB
Buffer size 100 bytes
Starting write test. Please wait up to a minute
Write 472.01 KB/sec
Maximum latency: 14606 usec, Minimum Latency: 9 usec, Avg Latency: 210 usec

Starting read test. Please wait up to a minute
Read 1079.21 KB/sec
Maximum latency: 1030 usec, Minimum Latency: 8 usec, Avg Latency: 91 usec
 
I just needed to format the card with the teensy
Wasn't aware that it is possible to read correctly but fail on write, but I'll add the format tip to the first-post in case anyone else experiences the same, thnx!
Did you format using SD or SdFat ? (I assume SdFat...)

Did you do anything to get better benchmarks?

Just one more thing as far as I can see: increase the buffer size

Maximum I am able to use in the bench example is 10240
Any higher and the read will get corrupted.

Post #6 in this topic shows a list of a few benchmarks under different conditions, the last one shows the max buffer use.

Using such a high buffer might not be wise outside the benchmark util though, not sure what the implications are of max buffer usage.

P.S. I have been having stable but different benchmark speeds using different brand MicroSD cards, Kingston and Sandisks seem to have max speed
 
Oh yeah, so here's my benchmark for buff size 10240 for 5MB and 25MB. Pretty good stuff. Mine is:
Samsung 32GB Micro SDHC class 10. I know it's overkill, but I'm just borrowing it from my tablet for now.
http://www.amazon.com/Samsung-microSDHC-Memory-Adapter-number/dp/B005TUQV0E/

Free RAM: 2203
Type is FAT32
File size 5MB
Buffer size 10240 bytes
Starting write test. Please wait up to a minute
Write 1510.62 KB/sec
Maximum latency: 16336 usec, Minimum Latency: 4450 usec, Avg Latency: 6750 usec

Starting read test. Please wait up to a minute
Read 2389.82 KB/sec
Maximum latency: 4993 usec, Minimum Latency: 4147 usec, Avg Latency: 4284 usec


Free RAM: 2203
Type is FAT32
File size 25MB
Buffer size 10240 bytes
Starting write test. Please wait up to a minute
Write 1545.43 KB/sec
Maximum latency: 25048 usec, Minimum Latency: 4485 usec, Avg Latency: 6619 usec

Starting read test. Please wait up to a minute
Read 2385.33 KB/sec
Maximum latency: 5142 usec, Minimum Latency: 4145 usec, Avg Latency: 4291 usec
 
Wow, thats very nice indeed
I haven't been able to reach the 2MB yet, 1999.34 KB/sec... just 1 KB/s or 49 KB/s short depending on how you count :x

Also, I don't think there is such thing as overkill when it concerns storage space ;)

(added the format advise to the firstpost)
 
Last edited:
Oh, and i did use the SdFat formatting.

And do you know if the latency is the time from the call to the return of writing a single buffer? I assume that's the case.

In my case, I think I'll probably set a fairly small buffer so that it's completely written before 1msec, to not interfere with other processing.

But, when transferring the file to the computer, I'll make the buffer as large as possible to read and send to PC. I'll try and contribute a sketch in the future that makes it easy to configure the teensy to capture signals and send it to the PC when it's done.
 
And do you know if the latency is the time from the call to the return of writing a single buffer?

If I understand the code below correct, that is correct.
Code:
  // do write test
  uint32_t n = FILE_SIZE/sizeof(buf);
  maxLatency = 0;
  minLatency = 9999999;
  totalLatency = 0;
  uint32_t t = millis();
  for (uint32_t i = 0; i < n; i++) {
    uint32_t m = micros();
    if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
      error("write failed");
    }
    m = micros() - m;
    if (maxLatency < m) maxLatency = m;
    if (minLatency > m) minLatency = m;
    totalLatency += m;
  }

The filesize is divided by the buffersize, then each write operation is timed by 'uint32_t m = micros();'
At the end of each write operation the start time is subtracted from the end time and added to the totalLatency (time for all write operations together)

After running the entire benchmark, the maxLatency and minLatency only contain a single write operation, the highest and lowest value during the entire benchmark.

Edit: The same goes for read operations, code is the same concerning how latency is calculated

And code contributions are always welcome ofcourse (but not mandatory) :)
 
Last edited:
This worked just fine for me, so it must be a really good guide :)
I did one thing different, I ignored the 3.3v thing and just hooked up the +5V on the MicroSD adapter to the Teensy3 Vin and this was okay.
They only reason I did it this way is 'cos I'm lazy.
 
Thats great to hear, kept it as simple as possible so that even somebody like myself could understand it :p

Wasn't sure if I'd blow up either the Teensy pin or the MicroSD if I didnt bridge the 3V pads, and well, had to learn how to solder anyway...
 
I'm glad to see the updates to the SDFat library--I'll have to try them out. Any news on when the SD USB mode for Teensy 3.0 might be coming?
 
So far, I haven't experienced any issues with (the latest) SdFat beta, so I would definately advise to give it a go :)

I think, but I'm not sure if 'SD USB mode' depends on 'USB host mode'
From what I understood from the Kickstarter page:
The USB port on Teensy 3.0 is capable of USB host mode. A Micro-AB connector allows future use of USB host mode. Initially, Teensy 3.0 will ship with only USB device support. USB host support, integrated in the Arduino(R)** environment with easy-to-use objects, is planned as a software-only upgrade in 2013. Programming a project using USB host mode will require a second Teensy 3.0 and a board-to-board link using the I2C pins (2 wires and ground), to provide a path for the Arduino IDE to upload code.

From posts on this forum I understood that Paul has it on his to-do list, but there are still some other things with a higher priority.

P.S. feel free to correct me if I made an incorrect assumption here ;)
 
Ah--sorry that I wasn't specific. I'm wondering about the Mass Storage USB mode emulation that mounts the SD card as a removable storage when the Teensy is connected to a host computer. It's selectable from the Teensyduino "USB Mode" menu, and Teensy 2.0 has it. Comparable to Joystick, Keyboard, and soft Serial modes.
 
Hi,
I was able to wire up the Teensy 3.0 with microSD adaptor. But I could not follow your instruction regarding the code, "cardinfo example".

I am reading data from MPU9150 (acc,gyro and mag, 9 DOF sensor) and I would like to save it to a microSD card using the micro SD adaptor.

Please help. I am a newbie. I need simpler instructions with regards to the code.
 
Hi Paul,

I want to read the output from MPU9150 (Acc, Gyro and Mag) using a Teensy 3.0 (PJRC product - http://www.pjrc.com/store/teensy3.html) to a micro SD card on a SD adaptor (PJRC product - http://www.pjrc.com/teensy/sd_adaptor.html). I am able to read the data from the MPU9150 and display it on the serial monitor on my computer. I want to save the data on a micro SD card. I went to the following post http://forum.pjrc.com/threads/16758-Teensy-3-MicroSD-guide
I have followed all the instructions to connect the SD adaptor to the Teensy 3.0. But I could not follow how to get the script to save data from MPU9150 through the Teensy 3.0 to the Micro SD card in the SD adaptor (Teensy product). I tried attaching a photograph to convey my idea. But it seems the system does not allow me to do that.
Please help.
 
I tried attaching a photograph to convey my idea. But it seems the system does not allow me to do that.

The forum imposes a file size limit, so a high res photo directly from your digital camera is probably too large. Just open the file with your favorite image program (eg, Photoshop, Gimp, etc) and reduce the size to something like 1024 or 800 pixels. Save as JPEG format. Usually setting "quality" to 75% usually results in a smaller file with minimal loss of detail.

I'd also agree with turtle9er, you must post a small but complete sample program if you want us to help much. Take a look at this thread to see how important sample code is for resolving problems!
 
Random question: if I only have one SD card slot on the SPI bus, do I need to use a pin for Chip Select or can I simply ground it?
 
sdadaptor in the protoboard with and without boards: 20130710_213159.jpg 20130710_213401.jpg

and the benchmarck with Sandisk Extreme Pro:

=====================================================================
Free RAM: 8103
Type is FAT32
File size 5MB
Buffer size 512 bytes

Write 363.75 KB/sec
Maximum latency: 75200 usec, Minimum Latency: 1130 usec, Avg Latency: 1405 usec

Read 1224.51 KB/sec
Maximum latency: 1422 usec, Minimum Latency: 399 usec, Avg Latency: 416 usec
------------------------------------------------------------------------------------------------------------------------------
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 512 bytes

Write 362.52 KB/sec
Maximum latency: 54664 usec, Minimum Latency: 1113 usec, Avg Latency: 1410 usec

Read 1216.83 KB/sec
Maximum latency: 1398 usec, Minimum Latency: 401 usec, Avg Latency: 419 usec
=====================================================================
Free RAM: 8103
Type is FAT32
File size 5MB
Buffer size 1024 bytes

Write 436.30 KB/sec
Maximum latency: 18524 usec, Minimum Latency: 1471 usec, Avg Latency: 2344 usec

Read 1395.64 KB/sec
Maximum latency: 1731 usec, Minimum Latency: 661 usec, Avg Latency: 732 usec
------------------------------------------------------------------------------------------------------------------------------
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 1024 bytes

Write 437.88 KB/sec
Maximum latency: 19906 usec, Minimum Latency: 1472 usec, Avg Latency: 2336 usec

Read 1397.34 KB/sec
Maximum latency: 1731 usec, Minimum Latency: 637 usec, Avg Latency: 731 usec
=====================================================================
Free RAM: 8103
Type is FAT32
File size 5MB
Buffer size 2048 bytes

Write 1021.07 KB/sec
Maximum latency: 18108 usec, Minimum Latency: 1719 usec, Avg Latency: 2001 usec

Read 1834.56 KB/sec
Maximum latency: 2103 usec, Minimum Latency: 1049 usec, Avg Latency: 1114 usec
------------------------------------------------------------------------------------------------------------------------------
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 2048 bytes

Write 1026.52 KB/sec
Maximum latency: 18149 usec, Minimum Latency: 1689 usec, Avg Latency: 1992 usec

Read 1840.30 KB/sec
Maximum latency: 2097 usec, Minimum Latency: 1030 usec, Avg Latency: 1111 usec
=====================================================================
Free RAM: 4007
Type is FAT32
File size 5MB
Buffer size 4096 bytes

Write 1577.37 KB/sec
Maximum latency: 19210 usec, Minimum Latency: 2166 usec, Avg Latency: 2590 usec

Read 2164.19 KB/sec
Maximum latency: 2901 usec, Minimum Latency: 1851 usec, Avg Latency: 1890 usec
------------------------------------------------------------------------------------------------------------------------------
Free RAM: 4007
Type is FAT32
File size 10MB
Buffer size 4096 bytes

Write 1598.71 KB/sec
Maximum latency: 33052 usec, Minimum Latency: 2157 usec, Avg Latency: 2558 usec

Read 2167.43 KB/sec
Maximum latency: 2873 usec, Minimum Latency: 1821 usec, Avg Latency: 1888 usec
=====================================================================
Free RAM: -89
Type is FAT32
File size 5MB
Buffer size 8192 bytes

Write 2036.32 KB/sec
Maximum latency: 42498 usec, Minimum Latency: 3757 usec, Avg Latency: 4015 usec

Read 2351.59 KB/sec
Maximum latency: 4444 usec, Minimum Latency: 3440 usec, Avg Latency: 3482 usec
------------------------------------------------------------------------------------------------------------------------------
Free RAM: -89
Type is FAT32
File size 10MB
Buffer size 8192 bytes

Write 2076.51 KB/sec
Maximum latency: 17011 usec, Minimum Latency: 3721 usec, Avg Latency: 3940 usec

Read 2352.14 KB/sec
Maximum latency: 4480 usec, Minimum Latency: 3405 usec, Avg Latency: 3481 usec
=====================================================================

Does anyone know why "Free RAM: -89" in the last test?

regards!
 
Last edited:
It was time for me to add a SD card as well to my project, but I ran into a strange issue when using 'bench' from 'SdFatBeta20130621'.

I assume it must have something to do with UHS, which is a shame really, since it is the smallest mSD card I have, and I really had no other use for it any more, besides for this project.
Maybe it is me who is strange, but that Transcend is normally considered to be the slowest card in my possession.

Samsung UHS-1 card:
Code:
[url]http://www.samsung.com/uk/consumer/memory-cards-hdd-odd/memory-cards-accessories/micro-sdhc-pro/MB-MG8GB/EU[/url]
that I formated using SDformatter from the sdfat lib.

//
// 96 MHz, 100 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 100 bytes
Starting write test.  Please wait up to a minute
Write 197.55 KB/sec
Maximum latency: 110787 usec, Minimum Latency: 8 usec, Avg Latency: 505 usec

Starting read test.  Please wait up to a minute
Read 748.56 KB/sec
Maximum latency: 4006 usec, Minimum Latency: 8 usec, Avg Latency: 132 usec

//
// 96 MHz, 1022 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 8095
Type is FAT32
File size 10MB
Buffer size 1022 bytes
Starting write test.  Please wait up to a minute
Write 199.08 KB/sec
Maximum latency: 25521 usec, Minimum Latency: 1519 usec, Avg Latency: 5131 usec

Starting read test.  Please wait up to a minute
Read 790.70 KB/sec
Maximum latency: 4466 usec, Minimum Latency: 624 usec, Avg Latency: 1291 usec

//
// 96 MHz, 1024 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 1024 bytes
Starting write test.  Please wait up to a minute
Write 366.42 KB/sec
Maximum latency: 15439 usec, Minimum Latency: 801 usec, Avg Latency: 2792 usec

Starting read test.  Please wait up to a minute
error: read failed
SD errorCode: 0X3,0X1

//48 MHz does show similar results.

Transcend Class 10 card:
Code:
[url]http://www.bestcovery.com/transcend-ts16gusdhc10-16-gb-microsd-high-capacity-microsdhc[/url]  <--- this one I believe it is
that I formated using SDformatter from the sdfat lib.

//
// 96 MHz, 100 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 100 bytes
Starting write test.  Please wait up to a minute
Write 337.28 KB/sec
Maximum latency: 191464 usec, Minimum Latency: 8 usec, Avg Latency: 295 usec

Starting read test.  Please wait up to a minute
Read 996.71 KB/sec
Maximum latency: 2241 usec, Minimum Latency: 8 usec, Avg Latency: 99 usec

//
// 96 MHz, 1022 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 8095
Type is FAT32
File size 10MB
Buffer size 1022 bytes
Starting write test.  Please wait up to a minute
Write 370.74 KB/sec
Maximum latency: 378981 usec, Minimum Latency: 1061 usec, Avg Latency: 2754 usec

Starting read test.  Please wait up to a minute
Read 1053.00 KB/sec
Maximum latency: 3580 usec, Minimum Latency: 506 usec, Avg Latency: 969 usec

//
//96 MHz, 2048 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 8103
Type is FAT32
File size 10MB
Buffer size 2048 bytes
Starting write test.  Please wait up to a minute
Write 671.34 KB/sec
Maximum latency: 566246 usec, Minimum Latency: 1708 usec, Avg Latency: 3048 usec

Starting read test.  Please wait up to a minute
Read 1794.39 KB/sec
Maximum latency: 3758 usec, Minimum Latency: 1077 usec, Avg Latency: 1140 usec

//
// 96 MHz, 4096 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 4007
Type is FAT32
File size 25MB
Buffer size 4096 bytes
Starting write test.  Please wait up to a minute
Write 1134.62 KB/sec
Maximum latency: 564823 usec, Minimum Latency: 2293 usec, Avg Latency: 3608 usec

Starting read test.  Please wait up to a minute
Read 2091.52 KB/sec
Maximum latency: 3569 usec, Minimum Latency: 1934 usec, Avg Latency: 1957 usec

//
// 96 MHz, 6144 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 4007
Type is FAT32
File size 25MB
Buffer size 6144 bytes
Starting write test.  Please wait up to a minute
Write 1158.42 KB/sec
Maximum latency: 566788 usec, Minimum Latency: 3422 usec, Avg Latency: 5301 usec

Starting read test.  Please wait up to a minute
Read 2211.80 KB/sec
Maximum latency: 4473 usec, Minimum Latency: 2666 usec, Avg Latency: 2776 usec

//
// 96 MHz, 8192 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: -89
Type is FAT32
File size 25MB
Buffer size 8192 bytes
Starting write test.  Please wait up to a minute
Write 1573.72 KB/sec
Maximum latency: 19035 usec, Minimum Latency: 4467 usec, Avg Latency: 5202 usec

Starting read test.  Please wait up to a minute
Read 2306.76 KB/sec
Maximum latency: 6149 usec, Minimum Latency: 3529 usec, Avg Latency: 3550 usec

//
// 96 MHz, 10240 buffer
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: -89
Type is FAT32
File size 25MB
Buffer size 10240 bytes
Starting write test.  Please wait up to a minute
Write 1454.43 KB/sec
Maximum latency: 23946 usec, Minimum Latency: 5330 usec, Avg Latency: 7037 usec

Starting read test.  Please wait up to a minute
Read 2325.41 KB/sec
Maximum latency: 6096 usec, Minimum Latency: 4255 usec, Avg Latency: 4401 usec

I am using 'Arduino 1.0.5', with 'Teensy Loader 1.15'.
 
Hi everyone,

I made a micro SD adaptor that can be soldered directly to the T3, or you can use stackable headers (like the arduino shields use). Since the T3 is 3.3 Volt native, there is no need for a level shifter / buffer.
When soldered directly to the T3 the adaptor covers the reset button in the T3, that's why I added another reset button to the adaptor (and desoldered the original one on the T3).
There is also room on the PCB for a decoupling cap to stabilize the 3.3 Volts right at the sd card and also room for an LED that lights up when the card is being accessed (precisely: when the chip select is pulled low). I have not yet populated these parts of the board, only the microSD-slot itself, but I wanted to share this with you anyway.
Here are some pictures:

image.jpg

image.jpg

image.jpg

image.jpg

image.jpg

Ben
 
I took the PJRC SD adaptor and put a 7-pin right-angle header on it with the two extra pins extending beyond MISO, I then connected the +5V to the pin adjacent to MISO and the pad next to the 'V' in '3V' to the other pin. I can use the same adapter with both 5V and 3V systems without unsoldering the pads. For a Teensy2.0 and Teensy++ 2.0, you can plug it into a breadboard jumper-free and supply power and ground through the adjacent output pins.

For the Teensy 3, putting a 3.3V pin next to GND could minimize the jumpers--configure pin 9 for CS, and jumper pin 10 across to pin 13, then pinMode(8,OUTPUT);pinMode(7,OUTPUT);digitalWrite(7,HIGH) to turn the SD card on.

It might be nice to add the MISO/MOSI/SCKL/SC/GND and +5V/SW to the silkscreens for this part. And if you moved SW one hole middle-wards and connected CK to the corner pin, it could piggyback on the Teensy 3.x.
 
Last edited:
Here is a Teensy 3.x uSD shield I've made for one of my projects. It fits nicely underneath the Teensy board.

microsd_shield.jpg microsd_shield_teensy.jpg

(second picture shows it installed together with a CAN bus shield that I've made as well)
 
Cheap microSD adapter

After realizing that the Teensy3.x can talk directly to SD/microSD cards without all that level shifting business, I figured I could make an adapter pretty easily. I'm posting this here as a reference for others who might happen to be as cheap as I am. This worked with the two different cards I had readily available using both the standard arduino SD library and the SDfat library. If anybody sees any problems or has some way to make this better, fire away. P.S. I've not tested this with Teensy3.1 yet, but I see no reason why it would make any difference whatsoever between the two revisions.

Only pins 1,2,3,4,5, & 7 are used

IMG_20140613_151748_373.jpg



Female stackable headers 0.1" spacing fit with no modification. It's a little bit of a close fit, but if you are careful with the soldering, you can keep the plastic melting carnage to a minimum. I didn't bother soldering anything to pins 8 and 9

IMG_20140613_151839_890.jpg


This is how I made my connections:
Adapter ---> Teensy3.x
1 CS ---> Pin 10 (CS)
2 DataIn ---> Pin 11 (DOUT)
3 Vss (GND) ---> GND
4 Vdd (3.3v) ---> 3.3v
5 SCLK ---> Pin 13 (SCK)
7 DataOut ---> Pin 12 (DIN)

IMG_20140613_151958_942.jpg
 
Status
Not open for further replies.
Back
Top