Teensy 4.1 running only with USB cable

PaoloG

New member
Hi all, I'm new in using Teensy. I'm using Teensy 4.1 with arduino IDE on windows notebook.
My project has:
a GPS (Adafruit ultimate breakout) connected on Serial1

a sparkfun I2cMux with 3 connections an Ir thermometer MLX90614, an IR camera MLX90640 and a Sprkfun triad spectral sensor.


the goal is to take measures every time I read GPS time and summing them, and at the new minute making average and write on SD 2 different files one with time MLX90614 data and spectral data and the other with time and camera pixel data.
If Teensy is connected and powered via USB port I get what I want either if I open serial monitor or not but if I try to power via Vin pin (5v or 3.3V) SD card remains empty. I need to run Teensy in standalone mode can someone help me please?
here is my code
Code:
/*
  Versione 4.0 del 03 Agosto 2021
*/

#define NUMBER_OF_SENSORS 3
#include <Wire.h>

int D_old = 0;
int D;
int M;
int Yr;
int h;
int m;
int m_old = 0;
int s;
int n = 0;
char nomefile[13] = "20200000.csv";

#include <Adafruit_MLX90614.h>
float TA90;
float TO90;
float oktas;
float sum_TA90 = 0;
float sum_TO90 = 0;
float sum_oktas = 0;
float m_TA90;
float m_TO90;
float m_oktas;
#include "MLX90640_API.h"
#include "MLX90640_I2C_Driver.h"
const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640
#define TA_SHIFT 8 //Default shift for MLX90640 in open air

float mlx90640To[768];
float sum_mlx90640To[768];
float m_mlx90640To[768];
paramsMLX90640 mlx90640;

const byte calcStart = 33; //Pin that goes high/low when calculations are complete
//This makes the timing visible on the logic analyzer
//
Adafruit_MLX90614 mlx_fov90 = Adafruit_MLX90614(0x0A);

#include "SparkFun_AS7265X.h"
AS7265X ASsensor;
float AS[18];
float sum_AS[18];
float m_AS[18];

#include <SD.h>
#include <SPI.h>
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = BUILTIN_SDCARD;

#include <timer.h>
Timer tm;

#include <Adafruit_GPS.h>
#define GPSSerial Serial1
Adafruit_GPS GPS(&GPSSerial);
#define GPSECHO false
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy

void setup()
{
  Wire.begin();
  Wire.setClock(400000); //Increase I2C clock speed to 400kHz

  Serial.begin(9600); //Fast serial as possible

  enableMuxPort(0);
  ASsensor.begin();
  ASsensor.disableIndicator();
  disableMuxPort(0);

  enableMuxPort(6);
  mlx_fov90.begin();
  disableMuxPort(6);

  enableMuxPort(7);
  pinMode(calcStart, OUTPUT);

  if (isConnected() == false)  {
    //Serial.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing.");
    while (1);
  }
  //Get device parameters - We only have to do this once
  int status;
  uint16_t eeMLX90640[832];
  status = MLX90640_DumpEE(MLX90640_address, eeMLX90640);
  //if (status != 0)
    //Serial.println("Failed to load system parameters");

    status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640);
  //if (status != 0)
    //Serial.println("Parameter extraction failed");
    //Set refresh rate
    //A rate of 0.5Hz takes 4Sec per reading because we have to read two frames to get complete picture
    //MLX90640_SetRefreshRate(MLX90640_address, 0x00); //Set rate to 0.25Hz effective - Works
    //MLX90640_SetRefreshRate(MLX90640_address, 0x01); //Set rate to 0.5Hz effective - Works
    //MLX90640_SetRefreshRate(MLX90640_address, 0x02); //Set rate to 1Hz effective - Works
  MLX90640_SetRefreshRate(MLX90640_address, 0x03); //Set rate to 2Hz effective - Works
  //MLX90640_SetRefreshRate(MLX90640_address, 0x04); //Set rate to 4Hz effective - Works
  //MLX90640_SetRefreshRate(MLX90640_address, 0x05); //Set rate to 8Hz effective - Works at 800kHz
  //MLX90640_SetRefreshRate(MLX90640_address, 0x06); //Set rate to 16Hz effective - Works at 800kHz
  //MLX90640_SetRefreshRate(MLX90640_address, 0x07); //Set rate to 32Hz effective - fails

  //Once EEPROM has been read at 400kHz we can increase to 1MHz
  disableMuxPort(7);

  GPS.begin(9600);
  GPSSerial.begin(9600);//
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  usingInterrupt = false;  //NOTE - we don't want to use interrupts on the Due

  SD.begin(chipSelect);
  tm.startTimer(3000, RunMeasures);
}

void loop(void) {
  // read data from the GPS in the 'main loop'
  char c = GPS.read();
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return;
    tm.runTimers();
  }
}

void RunMeasures(int timer) {
  Yr = GPS.year;
  M = GPS.month;
  D = GPS.day;
  h = GPS.hour;
  m = GPS.minute;
  s = GPS.seconds;
  //Serial.print(D); Serial.print("/");
  //Serial.print(M); Serial.print("/");
  //Serial.print(Yr + 2000); Serial.print("\t");
  //Serial.print(h); Serial.print(":");
  //Serial.print(m); Serial.print(":");
  //Serial.print(s); Serial.println("");

  if (m != m_old) {
    //CALCOLO DELLE MEDIE//
    for (int x = 0 ; x < 18 ; x++)
    {
      m_AS[x] = sum_AS[x] / n;
    }
    m_TA90 = sum_TA90 / n;
    m_TO90 = sum_TO90 / n;
    m_oktas = sum_oktas / n;
    for (int x = 0 ; x < 768 ; x++)
    {
      m_mlx90640To[x] = sum_mlx90640To[x] / n;
    }
    n = 0;
    m_old = m;
    sum_TA90 = 0;
    sum_TO90 = 0;
    sum_oktas = 0;
    for (int x = 0 ; x < 768 ; x++)
    {
      sum_mlx90640To[x] = 0;
    }
    for (int x = 0 ; x < 18 ; x++)
    {
      sum_AS[x] = 0;
    }
    //      //**************************************************//
    //      // SCRIVO SU FILE da riprovare
    //      //      sprintf(nomefile, "%s%04d%02d%02d.csv", "Spectral/", Yr, M, D);
    sprintf(nomefile, "S_%02d%02d%02d.csv", Yr, M, D);
    //    Serial.print(nomefile); Serial.println();
    File blah = SD.open(nomefile, FILE_WRITE);
    String dataString = "";
    dataString += String(D); dataString += String("/"); dataString += String(M); dataString += String("/20"); dataString += String(Yr); dataString += String(" ");
    dataString += String(h); dataString += String(":"); dataString += String(m); dataString += String(":00"); dataString += String(",");
    dataString += String(m_oktas * 8 / 768); dataString += String(","); dataString += String(m_TA90); dataString += String(","); dataString += String(m_TO90);
    dataString += String(",");
    for (int x = 0 ; x < 18 ; x++)
    {
      dataString += String(m_AS[x]);
      dataString += String(",");
    }
    blah.println(dataString);
    blah.close();
    //Serial.println(dataString);
    delay(100);
    sprintf(nomefile, "T_%02d%02d%02d.csv", Yr, M, D);
    //Serial.print(nomefile); Serial.println();
    blah = SD.open(nomefile, FILE_WRITE);
    dataString = "";
    dataString += String(D); dataString += String("/"); dataString += String(M); dataString += String("/20"); dataString += String(Yr); dataString += String(" ");
    dataString += String(h); dataString += String(":"); dataString += String(m); dataString += String(":00"); dataString += String(",");
    for (int x = 0 ; x < 767 ; x++)
    {
      dataString += String(m_mlx90640To[x]);
      dataString += String(",");
    }
    dataString += String(m_mlx90640To[767]);
    blah.println(dataString);
    blah.close();
    //**************************************************//
  } else {
    n = n + 1;
    //Serial.println("sommo nuovi dati = "); Serial.println(n);
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    enableMuxPort(0); //TRIAD
    ASsensor.takeMeasurements(); //This is a hard wait while all 18 channels are measured
    AS[0] = ASsensor.getCalibratedA();
    AS[1] = ASsensor.getCalibratedB();
    AS[2] = ASsensor.getCalibratedC();
    AS[3] = ASsensor.getCalibratedD();
    AS[4] = ASsensor.getCalibratedE();
    AS[5] = ASsensor.getCalibratedF();
    AS[6] = ASsensor.getCalibratedG();
    AS[7] = ASsensor.getCalibratedH();
    AS[8] = ASsensor.getCalibratedI();
    AS[9] = ASsensor.getCalibratedJ();
    AS[10] = ASsensor.getCalibratedK();
    AS[11] = ASsensor.getCalibratedL();
    AS[12] = ASsensor.getCalibratedR();
    AS[13] = ASsensor.getCalibratedS();
    AS[14] = ASsensor.getCalibratedT();
    AS[15] = ASsensor.getCalibratedU();
    AS[16] = ASsensor.getCalibratedV();
    AS[17] = ASsensor.getCalibratedW();
    disableMuxPort(0); //TRIAD
    for (int x = 0 ; x < 18 ; x++)
    {
      sum_AS[x] = sum_AS[x] + AS[x];
    }

    enableMuxPort(6);
    TA90 = mlx_fov90.readAmbientTempC();
    TO90 = mlx_fov90.readObjectTempC();
    sum_TA90 = sum_TA90 + TA90;
    sum_TO90 = sum_TO90 + TO90;
    disableMuxPort(6);

    enableMuxPort(7);
    //      Serial.println("MUX 7 MLX90640");
    for (byte x = 0 ; x < 2 ; x++)
    {
      uint16_t mlx90640Frame[834];
      int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame);

      digitalWrite(calcStart, HIGH);
      float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640);
      float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640);

      float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature
      float emissivity = 0.95;

      MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To);
      digitalWrite(calcStart, LOW);
    }
    disableMuxPort(7);

    for (int x = 0 ; x < 768 ; x++)
    {
      if (mlx90640To[x] >= TO90) {
        oktas = oktas + 1;
      }
      sum_mlx90640To[x] = sum_mlx90640To[x] + mlx90640To[x];
    }

    sum_oktas = sum_oktas + oktas;
    oktas = 0;
    if (D != D_old) {
      D_old = D;
    }
  }
}

//Returns true if the MLX90640 is detected on the I2C bus
boolean isConnected()
{
  Wire.beginTransmission((uint8_t)MLX90640_address);
  if (Wire.endTransmission() != 0)
    return (false); //Sensor did not ACK
  return (true);
}
 
I would first put some delay (500ms) after wire.begin, as T4.1 is far too fast to any I2C connected device to start up.
 
Hi WMXZ unfortunately I cannot work on it because the experiment is in my office and I'm at home until monday.
Sorry
 
Back
Top