Using BUILTIN_SDCARD for chip select on Teensy 4.1 is not working.

dlab

Active member
I am trying to write data to an SD card connected to a Teensy 4.1 using the port that's on the PCB (not an external micro SD module). I was trying to check if the Teensy is able to recognise that there is an SD card connected to it by uploading the following code:

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

const int CS = BUILTIN_SDCARD;
// const int CS = 10;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  if (SD.begin(CS)) {
    Serial.println("SD card initialised");
  }
  else {
    Serial.println("SD card initialisation failed");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

It keeps saying that it is not able to initialise the SD card no matter what I use for Chip Select, I have tried setting chip select to the following: 4, 10, 'BUILTIN_SDCARD' but none of those worked.
What should I use for chip select? Or are there additional steps that need be performed for it to recognise the SD card? I have already formatted the card to FAT.
 
What should I use for chip select?

BUILTIN_SDCARD is correct for using the SD socket on Teensy 4.1.


Or are there additional steps that need be performed for it to recognise the SD card?

There is a known issue where very old cards (size measured in MB rather than GB) don't work with SDIO on the built in socket.

Best to run the CardInfo example to test.
 
I have tried running the CardInfo example after setting chipSelect to BUILTIN_SDCARD, it says the same thing, that card initialisation failed.
The SD card is not an old card, it is a 32 gb SanDisk Ultra micro SD HC card
 
Just tried the CardInfo example with changing only one line (below) and it worked for me. 32 GB Sandisk Ultra. Arduino 1.8.19. TeensyDuino 1.58. Windows 7 Pro.

Code:
const int chipSelect = BUILTIN_SDCARD;
 
I am using Ubuntu 22.04, Arduino IDE 2.1.0.
Are there any known issues with these?

None seen here with Windows - not likely with others on linux based on posts and that PJRC primary dev is on linux. That message reporting would not relate to OS, but the software as built or the card in use.
 
I personally use Ubuntu 20.04 and IDE 2.1.0 & IDE 1.8.19. Works fine.

I am using Ubuntu 22.04, Arduino IDE 2.1.0.
Are there any known issues with these?

No issues I know about, apart from the general need on all Linux to install udev rules. Before IDE 2.0.0 files were always delivered by an installer which checked for 00-teensy.rules. Now with IDE 2.x.x the installation process built into the Arduino IDE doesn't check or automatically install the udev rules file. Teensyduino 1.58 added an error message explaining udev rules if hardware doesn't respond and 00-teensy.rules can't be seen in /etc/udev/rules.d.

But missing udev rules would not explain a SD card plugged into Teensy 4.1 not working. Without the udev rules, your PC can't talk to Teensy (or more specifically, non-root users don't have permission to access the hardware device) so you can't even upload a program which tries to use the SD card. Even though this is unrelated, you did ask if there are any known issues with Ubuntu 22.04, Arduino IDE 2.1.0. Anyone who's been on this forum long enough can confirm Linux users regularly stumble because of missing udev rules.
 
Exactly, I have been using a T41 for a long time on this computer.

Are there other things that I could try? Perhaps using a different type of SD card, or a different brand?
What are the things that could've gone wrong for this problem to arise? Any help or advice would be very much appreciated.

I have also tried uploading a sketch that scans through Chip Select numbers from 0 - 1000 incrementally but that did not work too.
 
Power cycle the Teensy hardware, just in case the card has somehow gotten into SPI mode or some other state the SDIO hardware can't use.

Different SD card and another Teensy 4.1 would be the best things to try. Run CardInfo, so you can see the difference between hardware not detected at all versus a card without recognized filesystem or partition table.

Also look for any message Arduino IDE prints about duplicate libraries. If you have a custom SD library installed, it probably would not define BUILTIN_SDCARD and code could not even compile. But still, a problem which has come up occasionally is someone installs a SD library (perhaps while having issues with SD cards on a non-Teensy board) and then Arduino IDE forever uses that custom installed copy rather than the proper one meant for Teensy.

I have also tried uploading a sketch that scans through Chip Select numbers from 0 - 1000 incrementally but that did not work too.

BUILTIN_SDCARD is correct for the SD socket on Teensy 4.1. It also happens to be defined as 254, so a test trying 0 to 1000 would hit it. The CS pin number is only 8 bits, so it actually would have hit it a few times.

Don't waste your time using anything other than SD.begin(BUILTIN_SDCARD).
 
The few occasions I had similar problems, could be solved by reformatting with the official tool from sdcard.org
 
Here is are some screenshots that show exactly what I am running:

SD card info:
Screenshot from 2023-05-17 11-50-09.png

Would the SD Library CardInfo example sketch indicate if something was wrong with the format that the SD card is in?

SD Library example sketch CardInfo.ino and its output:
Screenshot from 2023-05-17 11-51-58.png

I have run CardInfo many times, that is what it always shows.

Is there any information that is shown in the SD card info screenshot that might indicate what might be going wrong? I think that is really the only place I could've messed something up because the example sketch from the SD library is a standard way to test if there is an SD card attached.
 
The few occasions I had similar problems, could be solved by reformatting with the official tool from sdcard.org

Could you please provide me with the syntax for doing so? I would greatly appreciate that since I cannot find any resources that would help me use this tool.
 
Could you please provide me with the syntax for doing so? I would greatly appreciate that since I cannot find any resources that would help me use this tool.

IIRC the noted sdcard.org has a downloadable program that finds the card with a GUI interface and gives format option ...
 
If CardInfo can't find the card/display the info, the format will not matter.
First troubleshooting step should be testing with a different SD card.
 
Here is a screenshot, so you can see how the result should look when CardInfo works.

screenshot.png

This is the SD card I used.

sdcard.jpg

Even if your card isn't formatted, CardInfo should say "Wiring is correct and a card is present" and "Card type: SDHC" if the hardware is detected.
 
If CardInfo can't find the card/display the info, the format will not matter.
First troubleshooting step should be testing with a different SD card.

Perhaps the format, for some reason, is not compatible with Teensy.
Formating with the Win/Linux/Mac sdcard.org formatter
will remove that potential variable.
 
When CardInfo prints "initialization failed", you have some sort of hardware problem. No amount of fiddling with card formatting will help. This error is about connectivity to the card. It has nothing to do with the data on the card.

As @jmarsh said, the very first thing to try is a different SD card.

Also make sure your SD card is fully inserted into the socket. Teensy 4.1 has been manufactured with 2 different types of SD sockets. Both have a hole where you can visually confirm the card is fully mated.

sdcard2.jpg
 
I have used the T41 with 2 SD Cards (pictures below).
I have verified that the formatting is correct by using the tool from sdcard.org and formatting it to FAT32. I have also verified that both the SD Cards work by making sure I could write a simple text file to them on my computer.

Here are some pictures that show the SD cards, and how I have inserted them into the Teensy:
sdTeensy.jpg

bothSD.jpg

It is the same problem once again, running the CardInfo.ino example sketch and setting chipSelect to BUILTIN_SDCARD gives the same message saying that the SD card could not be initialised.
I saw some posts on the Arduino forum alluding to some brands requiring slower clock speeds, so I also tried lowering the clock speed of the T41 all the way down to 24 MHz in the Arduino IDE, still did not work.

I also put the following line in the sketch Serial.println(BUILTIN_SDCARD). It shows that the CS number is 254, is this correct?
 
I think I have finally found a way to get the Teensy to initialise and recognise the SD card!!!!

The following instructions were tested on Ubuntu 22.04 LTS

I had to use the SD Card Formatter tool downloaded from the sdcard.org website.

This is the command that you have to run to format the SD card using the tool:
'format_sd' is the name of the executable downloaded from the website.

Code:
sudo /path/to/format_sd -l <volume name> /dev/sdX

(replace 'sdX' with the corresponding device on your machine, on mine it's /dev/sdb)

When I plug the SD card into my computer, it is mounted automatically. The Formatter does not work if the SD card is mounted. So you need to unmount it using the command:

Code:
sudo umount /dev/sdX

It does not work if you unmount the card using the file manager or the Disks utility that comes preinstalled with Ubuntu (I have not tested this extensively).
You have to use 'umount' to unmount it. I do not know why.

How I figured this out:
All this while I was using the preinstalled Disks utility on Ubuntu to format the SD cards to FAT32, after doing this the SD cards worked fine, and also behaved normally, nothing seemed off.
However, the Teensy would not recognise the SD card after formatting it this way.

So I decided to use the SD Card Formatter Tool I downloaded from sdcard.org on my Windows machine as the tool only had the GUI available for Windows and I could not be bothered to read the help message that popped up when I ran ./format_sd on Ubuntu (I would've saved a lot of time If I had just read this help message carefully the first time).

After formatting it to FAT32 using the tool on Windows. The Teensy was able to recognise the SD card.

I wanted to check what this tool was doing differently so out of curiosity I opened the SD card's drive on Windows, I did not see anything here, so I switched back to Ubuntu and mounted & opened the SD card in my file manager, now I could see the 'System Volume Information' directory in the SD card.

This was one of the differences I noticed.

The other difference I noticed was in the partitions that were created on the SD card.

Partitions when formatted using Disks on Ubuntu:
Screenshot from 2023-05-17 11-50-09.png

Partitions when formatted using format_sd:
Screenshot from 2023-05-17 18-52-39.png

I find this very confusing and I do not understand what is going on, any clarifications would be greatly appreciated!
 
Last edited:
Hmm... maybe we have a bug in the CardInfo stuff? It's supposed to detect the hardware regardless of whether the card has a proper partition table. But maybe we've lost that with the change from old SD to SdFat abstraction between version 1.53 to 1.54? All SD cards are shipped with partition tables (I believe Bill has said it's part of the official SD card specs) so this is a case that probably doesn't come up most. More investigation is probably needed.... (adding this to my low priority issue list to someday investigate).
 
Back
Top