How to use the teensy 4.1 microSD card - A tutorial for beginners.

Status
Not open for further replies.
Hi everyone! I cant seem to find any tutorials on how to use the teensy 4.1 microSD so Im offering to make one for youtube if someone can help with the code.

The project will have the following:

Teensy 4.1
microsd card
2 x buttons (one wired to pin 7 and one wired to pin 8)
2 x resistors
1 x10k potentiometer.

The goal will be to read the value of the potentiometer and print it when the value is changed

Pressing the button wired to pin 7 will save the current value of the potentiometer to the SD card

Pressing the button wired to pin 8 will print the value saved to the sd card.


here is a schematic of what it'll look like (very simple)
TEENSY4-1-TUTORIAL-SD.jpg

and here's what the wiring will end up lookin like
breadboarder.jpg

Im horrible at coding but heres a starting point....
Code:
/* how to save to SSD Tutorial

   This example code is in the public domain.
*/

void setup() {                
   
  Serial.begin(38400);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
}

int val = analogRead(A0);


void loop()                     
{


 int val = analogRead(A0); // reads the pot to see the current value


Serial.print("The value is: "); //prints the value of the pot
Serial.println(val);



  if (digitalRead(7) == HIGH) {              //if the button wired to pin 7 is pressed - save the value of the pot to the sd card
   Serial.println("value saved!!!!!!!!"); 
   
 }  

 
if (digitalRead(8) == HIGH) {  // if the button wired to pin 8 is pressed - print the value of the saved value
   
   Serial.print("the saved value is:");
    
  }  
  
 
   
  delay(250);

}
 
Hopefully this is the direction you are looking for?

Look for : ...\hardware\teensy\avr\libraries\SD\examples\Datalogger\Datalogger.ino

Those Examples are under SD and use the TeensyDuino included SD library.

In those examples or when using that library specify ::
Code:
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
const int chipSelect = BUILTIN_SDCARD;

There are other examples for digital and analog if those are needed.
 
It turns out i installed my teensy drivers but did not install the teensyduino libraries.
I installed the libraries to my arduino folder using the teensyduino installer and found datalogger.ino ! ;)
Ill play around with it tonight
 
gettin' closer!
here is my code so far. It works but I feel like I could simplify things a bit more

Code:
/*
  SD card datalogger
 
 This example shows how to log data from two digital sensors and one analog sensor
 to an SD card using the SD library.

In this example you can fiddle with a potentiometer, 
press a button to save the value of the pot to the teensy 4.1 microSD 
and press a different button to display what value was saved.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11, pin 7 on Teensy with audio board
 ** MISO - pin 12
 ** CLK - pin 13, pin 14 on Teensy with audio board
 ** CS - pin 4,  pin 10 on Teensy with audio board
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
then modified again (horrible) by Andrew C. 

 This example code is in the public domain.
 	 
 */

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

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.

// 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
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
// Wiz820+SD board: pin 4
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
//===============================================================================================================================


const int chipSelect = BUILTIN_SDCARD; 


//create an integer named "pot" This integer will be used for the value of the potentiometer.
int pot; 


//================================================================================================================================
//this part sets things up!

void setup()
{


  //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
  //SPI.setSCK(14);  // Audio shield has SCK on pin 14

  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect.
  }


  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:
    return;
  }
  Serial.println("card initialized.");
}

//================================================================================================================================


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


//================================================================================================================================
      //if button wired to digital pin 7 is pressed, print "button 7 pressed - data saved!" to serial
        
        if (digitalRead(7) == HIGH) {
        Serial.println("button 7 pressed - data saved!");
        
      //read the potentiometer's value. then use this value as the new value of dataString
          int pot = analogRead(A0);
          dataString = String(pot);


      // open the file named datalog.txt on the sd card
          File dataFile = SD.open("datalog.txt", FILE_WRITE);

          // if the file is available, write the contents of datastring to it
          if (dataFile) {
          dataFile.println(dataString);
          dataFile.close();
          }  
          // if the file isn't open, pop up an error:
          else {
          Serial.println("error opening datalog.txt");
        }   
  }

//================================================================================================================================

//if button wired to digital pin 8 is pressed, print "button 8 pressed - load" to serial


        if (digitalRead(8) == HIGH) {
        Serial.println("button 8 pressed - load");

      //open up datalog2.txt and then print all of its contents  

        File dataFile = SD.open("datalog.txt");
        if(dataFile) {
          Serial.println("datalog:");
          while (dataFile.available()) {
              Serial.write(dataFile.read());
           }
      // close the file:
         dataFile.close();


//Delete the file so it can be created again at the begining of the loop
SD.remove("datalog.txt");
    
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening datalog.txt");
  }
  
}



  delay(150);
  }
 
Hey Andrew,

What software are you using above to map out the teensy with other circuit components !

I would love to do this with my teensy 3.6 !!

Thank you in advanced,
Joe
 
It turns out i installed my teensy drivers but did not install the teensyduino libraries.
I installed the libraries to my arduino folder using the teensyduino installer and found datalogger.ino ! ;)
Ill play around with it tonight

Hey Andrew,

Do I have to install the teensyduino library on my IDE before I can start using the teensy MicroSD card reader?

Also would this work within platform IO?

Thank you for reading
Joe
 
You 'get' to install the IDE and TeensyDuino to see it work and use the included examples :)

PIO can be used - seems it may arrange some of that - but not done it here - last comment I read today is that there may not be a clear step by step for that ... @someone may correct that or there may be steps in posts/threads related to that as it does have good support AFAIK.

But IDE then Teensy Installer are the best easy simple path to a functional setup for posting sketches here everyone can work with and give guidance on as the build environment and locations and output are defined.

There are many ways with or without that - none as easy to setup to working.

Depends on the OS in use as well. Here - on Windows - the Above is installed with IDE then editor of choice is used to trigger CMDLINE builds using the IDE install.
 
You 'get' to install the IDE and TeensyDuino to see it work and use the included examples :)

PIO can be used - seems it may arrange some of that - but not done it here - last comment I read today is that there may not be a clear step by step for that ... @someone may correct that or there may be steps in posts/threads related to that as it does have good support AFAIK.

But IDE then Teensy Installer are the best easy simple path to a functional setup for posting sketches here everyone can work with and give guidance on as the build environment and locations and output are defined.

There are many ways with or without that - none as easy to setup to working.

Depends on the OS in use as well. Here - on Windows - the Above is installed with IDE then editor of choice is used to trigger CMDLINE builds using the IDE install.

Thanks for getting back to me Defragster,

I brought an adaptor which does have a library for Platform IO but I will see if it works that is great b/c I love the IDE but it not I will move on and try what you talked about above !!
Thanks again,
Joe
 
Status
Not open for further replies.
Back
Top