SD card not working on external power teensy 4.1

DIYaerospace

New member
Hi,
I am working on making a datalogger that will log data from a BMP 388 and a MPU6050. I have the sketch printing values correctly but when I try to log the data onto the internal micro SD card I encounter a few problems. When I test the sketch with a usb cable the teensy logs the sensor data correctly. But when I use a 9 volt battery and a 7805 to power the teensy the file is not created on the micro SD card and nothing is logged. I have tried using different SD cards, an external micro SD card slot, a different teensy 4.1, and SDfat and SD.h libraries. I am using "BUILTIN_SDCARD" as the chip select and still nothing works. I am using the teensy 4.1 and am on mac OS, The SD cards I have tried are a 32 gigabit TF card and 64 gigabit San disk card. Both of these are able to store information on other hardware.
Any help would be appreciated,
Thanks!
Code




/*
SD card datalogger

This example shows how to log data from three analog sensors
to an SD card using the SD library.

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

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;

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(BUILTIN_SDCARD)) {
Serial.println("Card failed, or not present");
while (1) {
// No SD card, so don't do anything more - stay stuck here
}
}
Serial.println("card initialized.");
}

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

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}

// 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);
} else {
// if the file isn't open, pop up an error:
Serial.println("error opening datalog.txt");
}
delay(100); // run at a reasonable not-too-fast speed
}
 
what exactly means "nothing works"?
Serial port does not print?
File on sd card is empty?
SD card is not detected?
etc. etc.

The way you programmed setup, you NEED a usb line connected
Code:
while (!Serial) {
; // wait for serial port to connect.
}
 
Agreed, you need to remove the wait for Serial when you run from only a battery.

Maybe also add pinMode and some digitalWrite to pin 13 to distinctively blink the orange LED at startup and for the various error conditions, so you can visually see if the hardware is starting up and if it encounters any errors.
 
what exactly means "nothing works"?
Serial port does not print?
File on sd card is empty?
SD card is not detected?
etc. etc.

The way you programmed setup, you NEED a usb line connected
Code:
while (!Serial) {
; // wait for serial port to connect.
}

The way that I usually code this is to wait up to 3 seconds for the USB Serial line to be created, and if it doesn't just continue on. This way if you do connect the USB serial, you won't miss the first line or so if the serial connection has not been set up, but it won't hang forever if you are running on battery. I.e.:

Code:
while (!Serial && millis () < 3000UL) {
  ; // wait for serial port to connect for a maximum of 3 seconds.
}

As Paul says it can be good to have a blinking light indication for various error conditions, which can make it easier to identify issues if you are running on battery only.
 
Back
Top