Hi,
I have several sensors connected to my teensy 3.6. I need to log the data on an sd card. I am using the built in micro sd holder with a SanDisk 64 GB sd card.
To test the data logger, I simply tried to save data from two analog pins that are measuring voltage and current from a battery.
I adapted example codes from two libraries. The SD library and the SdFat library. However, it seems that it cannot initialize the sd card, and therefore cannot create a file and write in it.
Here are my two codes. Do I need to somehow pre-format the sd card or is my code not correct?
Thank you for your help!
Example code 1 from SD library:
#include <SD.h>
#include <SD_t3.h>
#include <SPI.h>
// change this to match your SD shield or module
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
const int chipSelect = BUILTIN_SDCARD;
float amps; //define variable for current
float volt_bat; //define variable for battery voltage
void setup()
{
pinMode(A21, INPUT_DISABLE); //define pin A21 as input for battery voltage
pinMode(A22, INPUT_DISABLE); //define pin A22 as input for current measurement
// 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.");
delay(3000);
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
amps = map(float(analogRead(A22)), 386, 1024, 0, 37.2);
volt_bat = map(float(analogRead(A21)), 1, 1024, 0, 26.2);
dataString += String(amps);
dataString += ",";
dataString += String(volt_bat);
// open the file.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1000);
}
Example code 2 from SdFat library:
#include <SdFat.h>
#define USE_SDIO 1
const uint8_t SD_CHIP_SELECT = SS;
SdFatSdioEX sd;
SdFile myFile;
char fileName[] = "datalog.txt";
char KeyCode;
int FileData;
float amps; //define variable for current
float volt_bat; //define variable for battery voltage
void setup()
{
pinMode(A21, INPUT_DISABLE); //define pin A21 as input for battery voltage
pinMode(A22, INPUT_DISABLE); //define pin A22 as input for current measurement
Serial.begin(9600);
while(!Serial);
}
void loop()
{
String dataString = "";
amps = map(float(analogRead(A22)), 386, 1024, 0, 37.2);
volt_bat = map(float(analogRead(A21)), 1, 1024, 0, 26.2);
dataString += String(amps);
dataString += ",";
dataString += String(volt_bat);
sd.begin();
if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
Serial.println("Opening text file for write failed.");
}
else {
myFile.println(dataString);
myFile.close();
Serial.println(dataString);
}
delay(1000);
}
I have several sensors connected to my teensy 3.6. I need to log the data on an sd card. I am using the built in micro sd holder with a SanDisk 64 GB sd card.
To test the data logger, I simply tried to save data from two analog pins that are measuring voltage and current from a battery.
I adapted example codes from two libraries. The SD library and the SdFat library. However, it seems that it cannot initialize the sd card, and therefore cannot create a file and write in it.
Here are my two codes. Do I need to somehow pre-format the sd card or is my code not correct?
Thank you for your help!
Example code 1 from SD library:
#include <SD.h>
#include <SD_t3.h>
#include <SPI.h>
// change this to match your SD shield or module
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
const int chipSelect = BUILTIN_SDCARD;
float amps; //define variable for current
float volt_bat; //define variable for battery voltage
void setup()
{
pinMode(A21, INPUT_DISABLE); //define pin A21 as input for battery voltage
pinMode(A22, INPUT_DISABLE); //define pin A22 as input for current measurement
// 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.");
delay(3000);
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
amps = map(float(analogRead(A22)), 386, 1024, 0, 37.2);
volt_bat = map(float(analogRead(A21)), 1, 1024, 0, 26.2);
dataString += String(amps);
dataString += ",";
dataString += String(volt_bat);
// open the file.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1000);
}
Example code 2 from SdFat library:
#include <SdFat.h>
#define USE_SDIO 1
const uint8_t SD_CHIP_SELECT = SS;
SdFatSdioEX sd;
SdFile myFile;
char fileName[] = "datalog.txt";
char KeyCode;
int FileData;
float amps; //define variable for current
float volt_bat; //define variable for battery voltage
void setup()
{
pinMode(A21, INPUT_DISABLE); //define pin A21 as input for battery voltage
pinMode(A22, INPUT_DISABLE); //define pin A22 as input for current measurement
Serial.begin(9600);
while(!Serial);
}
void loop()
{
String dataString = "";
amps = map(float(analogRead(A22)), 386, 1024, 0, 37.2);
volt_bat = map(float(analogRead(A21)), 1, 1024, 0, 26.2);
dataString += String(amps);
dataString += ",";
dataString += String(volt_bat);
sd.begin();
if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
Serial.println("Opening text file for write failed.");
}
else {
myFile.println(dataString);
myFile.close();
Serial.println(dataString);
}
delay(1000);
}