Code will run On usb connected to IDE, but not on a battery

Ok so basically I put together this code, Using serial comm the code executes and the hardware functions well with the software, however when I hook up a Battery (in this case a 9V battery to 5V buck converter, connected to 5V and GND on a teensy 4.1) the code does not execute and Servos dont go off / data isnt saved. not to sure what im doing wrong here. any help appreciated

#include "Wire.h"
#include <MPU6050_light.h>
#include "HCPCA9685.h"
#include <PID_v1.h>
#include <SD.h>
#include <SPI.h>
#define  I2CAdd 0x40

// PID Controller definitions
double Setpoint ; // will be the desired servo output value
double Input; // GYRO
double Output ; //servo actual output
// select sd card on board
const int chipSelect = BUILTIN_SDCARD;
//PID parameters
double Kp = 4, Ki =.1 , Kd = .1;
unsigned long clocktime;

//create PID instance 
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

MPU6050 mpu(Wire);
unsigned long timer = 0;

HCPCA9685 HCPCA9685(I2CAdd);

void setup() {
   Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect.
  }


  Serial.print("Initializing SD card...");//SD card check
   if (!SD.begin(chipSelect)) {
    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.");
  
  
  //setup for mpu
  Serial.begin(115200);
  Wire.begin();
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while (status != 0) { } // stop everything if could not connect to MPU6050
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(); // gyro and accelero
  Serial.println("Done!\n");

  /* Initialise the library and set it to 'servo mode' */
  HCPCA9685.Init(SERVO_MODE);
  /* Wake the device up */
  HCPCA9685.Sleep(false);

  Serial.begin(115200);   
  //Hardcode the gyro value
  Setpoint = 0;
  //Turn the PID on
  myPID.SetMode(AUTOMATIC);
  //Adjust PID values
  myPID.SetTunings(Kp, Ki, Kd);
}
  
void loop() {
 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 += ",";
      
  mpu.update();
  char result[4]; // Buffer big enough for 7-character float 
  
  if ((millis() - timer) > 10) { // print data every 10ms
    Serial.write("X : ");
    Serial.write (dtostrf(mpu.getAngleX(), 6, 2, result));
    Serial.write("\tY : ");
    Serial.write(dtostrf(mpu.getAngleY(), 6, 2, result));
    Serial.write("\tZ : ");
    Serial.write(dtostrf(mpu.getAngleZ(), 6, 2, result));
    Serial.println();
    timer = millis();
  }
  
  //Read the value from the GYRO
  Input =(mpu.getAngleZ());
  //PID calculation
  myPID.Compute();
  
  //Write the output as calculated by the PID function   
     HCPCA9685.Servo(0, (Output*.2)+250);
     HCPCA9685.Servo(1, (Output*.2)+250);
     
    Serial.print(Input);
  Serial.print(",");
  Serial.print(Output);
  Serial.print(",");  
  Serial.print(Setpoint);
  Serial.print(((Output*.2)));  


    }
  // open the file.
  File dataFile = SD.open("datalog.txt", FILE_WRITE); 
 // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(mpu.getAngleZ()); 
     dataFile.print(",");
    dataFile.print(mpu.getAngleX());
      dataFile.print(",");
    dataFile.print(mpu.getAngleY());
      dataFile.print(",");
     dataFile.println(millis());   
    dataFile.close();
    
  } else {
    // if the file isn't open, pop up an error:
    Serial.println("error opening datalog.txt");
  }
delay(5); 



  
  }}

What does the 5 volt output of the buck converter measure on your DVM when the arduino is hooked up to it?

Your battery is too weak and short-lived. Stop wasting time and money with 9V batteries.

It sounds like the issue is using one of these:

Which don't work very well.

while (!Serial)

wait until Serial port established. But it never is on battery!

2 Likes

i thought this was the issue but i wasnt able to get rid of this and have the code be functional. if this is the issue how can i still have like this step in the process just to make sure my connection is established but to the hardware, because i still need it to record the data. Thanks.

It is a Li- ion 600 mAh 9v but let me check if it works from the power supply and if it does ill make a new bank out of some scrap

Does not function with the power supply either sensors receive 5 v and 3.3v. only draws 10 mA this battery should be able to hold. pretty sure it has something to do with the serial wait.

We get a lot of cases like this and it is never the software, how could it be because software in totally unaware of the power source?

So you have a hardware problem, therefore you need to post an accurate and complete schematic , not a frutzing crap physical layout diagram.

Because the hardware is functional via the USB port, only Difference is the buck converter is connected to the ground bus connected to GND on the teensy, and the 5v output to the 5v bus connected to the 5V pin on the teensy.

Cannot get code to run by using a PS either through the buck converter it has something to do with waiting for the serial.

What part of we need to SEE a schematic are you having trouble with?

How does the code know it is running on USB power or other power?

Post some images of your setup.

You using a 9V battery?

6

And you are going to insist running your project off a 9V battery?

sorry for poor quality

No i am willing to build a new pack, but i have used these in other projects, even running with a DC power supply it will not run properly, i think it has something to do with adressing the serial.

Good luck.

Please provide more details about the problem you had when you removed that line:

I don't understand this statement.

I would assume that the whole point of the battery is to allow you to use your device without being connected to your computer. But if it is not connected to the computer then you don't have any need to read the data that is printed to Serial.

If you want to sometimes connect it to your computer and read from Serial, then you can enable that line then. But for now if your goal is to get the thing running on battery and you think that line is the problem, then comment it out and tell us whether that causes it to start working.

1 Like