[SOLVED]is it possible to run Teensy 4.1 standalone(no computers)?

GeoffC

New member
Hi all,
I'm new to teensy.
I am working on a project involving Teensy 4.1 and a accelerometer and a GPS sensor. More sensors will be added if needed. Data are saving in CSV files in the teensy built-in micros-SD card and sampling rate is 50Hz. The system can be powered on by an external battery power supply. We want to make the whole system as portable as possible.

Issues I am facing:
Power by an external power supply(5Vdc to Vin/5V pin, ground to GND). Data saving stopped and data file is no even created.​
Connected the USB to a battery power bank. Data saving stopped and data file is no even created.​
Connected the USB to a computer without Arduino. Data saving stopped and data file is no even created.​

I am wondering whether the system can be run standalone, i.e., without a computer/OS/Arduino? Has anyone already done this? I do have a Pi5 which can be integrated to the system. However, I would like to eliminate Pi5 if possible.

I posted partial codes which includes the initialization of micro-SD card and data writing to CSV text file. Data will be saved to file every second.


Thanks you very much.

Code:
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <Arduino.h>
#include <SD.h>
#include <cassert>

const int chipSelect = BUILTIN_SDCARD;
// I2C
Adafruit_LIS3DH lis = Adafruit_LIS3DH();  //=============================================ACC1

/* Or....get a new sensor event, normalized */
sensors_event_t event;
//lis.getEvent(&event);

Adafruit_GPS GPS(&Serial1);  //==================================================================GPS

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences.
#define GPSECHO true

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean);  // Func prototype keeps Arduino 0023 happy




//Temporary code segment switch for easy coding
boolean b_timetag = true;  //=======================================serial print swtich
boolean b_gps = true;
boolean b_acc1 = true;

boolean b_serialprint = false;
//boolean b_serialprint = true;

//*****************************************************************************************freq setup
const int n_freq = 50;         //sampling rate. i.e., data reading rate
const int t_period=(int)(1000 / n_freq);
const int n_write = 50;  //data file write rate  every 20 data samples

//-============================data file definition

File dataFile;
const char* fileName = "Traildata_0708C.txt";  //------------------------------------------------------TEXTFILE***************************

//----------------------------------------------------------------------------------------setup function
void setup() {


   Serial.begin(115200);
  //Serial1.begin(115200);
 
  //--=============GPS  SETUP
 

  //=====================================================================I2C
  while (!Serial) delay(10);  // will pause Zero, Leonardo, etc until serial console opens
                              // Serial.println("LIS3DH test!");
  if (!lis.begin(0x18)) {     // change this to 0x19 for alternative i2c address
    //if (b_serialprint) Serial.println("Couldnt start");
    while (1) yield();
  }

 

  //===============================================BUILT-IN  micro-SD card
 
 
  //if (b_serialprint) Serial.print("Initializing the SD card....");
  if (!SD.begin(chipSelect)) {
    //  if (b_serialprint) Serial.println("Initialization failed!");
    return;
  }
  // if (b_serialprint) Serial.println("Initialization done... Begin the Simulation.");
  dataFile = SD.open(fileName, FILE_WRITE);
  if (dataFile) {
    dataFile.println("index,date, time, latitute, longitude, speed, acc_x1, acc_y1, acc_z1, t_data, t_file, t_cycle,t_delay\n");
    // btoth PRINT &   PRINTLN works
    // if (b_serialprint) Serial.println("Ready to Receive Data from the Simulator");
    dataFile.close();

  } else {
    //if (b_serialprint) Serial.println("Error opening the file");
  }


  delay(1000);
}
//end of setup



void loop()  // run over and over again
{
  t_loopstart = millis();
  i_counter++;

     // timetag and GPS***************************************************************
    if (b_timetag) {
        if (b_gps){
            
            //GPS***************************************************************
        }

    } 
      

    // accelerometer1 ***************************************************************
    if (b_acc1) {

    }  //if (b_acc1)

    //--------------------------------------------------------------------------------------------------------------------------------------------str_result

    t_result_end = millis();
    t_result = (int)t_result_end - (int)t_result_start;
    if (t_result >= t_period) {  //   save file or NOT
      snprintf(str_result, sizeof(str_result), "%s%s%s%s%s  %s%s%s%s%s   %s%s%s%s%s %s%s%s",
               str_i_counter, ",", str_date, ",", str_time,
               ",", str_gps, ",", str_acc1, ",",
               str_t_data, ",", str_t_file, ",", str_t_loop,
               ",", str_t_delay, "\n");
      strcat(str_file, str_result);

      t_data = (int)(millis() - t_loopstart);
      if (i_result % n_freq == 0) {    //===============================write file==================================
        dataFile = SD.open(fileName, FILE_WRITE);
        if (dataFile) {
          t_file_start = millis();
          dataFile.println(str_file);
          t_file= (int)(millis())-(int)t_file_start;
          //Serial.println(str_file);
          //if (b_serialprint) {
           Serial.print("   iResult=  ");
           Serial.print(i_result);
            Serial.print("   ----filewrite------");
          //}
          str_file[0] = '\0';
          dataFile.close();         

        } else {  // if the file isn't open, pop up an error:
               t_file=-99;
        }
        t_file_start = millis();
        // if (b_serialprint) {
        Serial.print(" cur_reading=");
        Serial.print(str_result);
        //}
      }else {
        t_file=-10; 
      }
      str_result[0] = '\0';
      i_result++;
      t_result_start = millis();
    
    }   
    t_loop = (int)(millis()- t_loopstart);
    t_delay = t_period - t_loop;
    if (t_delay > 0) delayMicroseconds(500);
  
  }
Image (3).jpg
Image (2).jpg
 
This line is why it's not working when not connected to a PC:
Code:
 while (!Serial) delay(10);  // will pause Zero, Leonardo, etc until serial console opens
If the USB port isn't connected to anything, the serial port will never be ready.
 
This line is why it's not working when not connected to a PC:
Code:
 while (!Serial) delay(10);  // will pause Zero, Leonardo, etc until serial console opens
If the USB port isn't connected to anything, the serial port will never be ready.
Thank you so much. Everything works as expected.
 
Generally, you want to limit the time to a few seconds. That way if you have the Teensy hooked up to the computer, you won't miss the first line or so immediately the Teensy starts, but you won't wait forever. For example:

Code:
while (!Serial && millis () < 3000u)  // wait up to 3 seconds for the serial console to be available
  delay (10);
 
Back
Top