Inconsistent values when switching from usb to external power

Hello,
I have a weight sensor with an external power. I used an arduino pro micro. When i print values on the serial monitor and SD card, everything is good but when i disconnect the usb and continue to print the data on the SD card, there is some issues. I get strange values. Did you know whats happening ?
My code :

#include "Arduino.h"
#include <HX711.h>
#include <SPI.h>
#include <SD.h> 
#include <LowPower.h>

//void calibrate();
 
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 7;
 
 File myFile;

HX711 scale;
 
void setup() {
  Serial.begin(57600);
  while (!Serial);
  Serial.println("HX711 Demo");
 
  Serial.println("Initializing the scale");
  SD.begin(4);
  myFile = SD.open("res.txt", FILE_WRITE);
  if (myFile) {
    Serial.println(F("File opened ok"));
    myFile.println("weight");
    myFile.close();
    //
  } else {
    // if the file didn't open, print an error:
    Serial.println(F("error opening sd file"));
  }
 
  // Initialize library with data output pin, clock input pin and gain factor.
  // Channel selection is made by passing the appropriate gain:
  // - With a gain factor of 64 or 128, channel A is selected
  // - With a gain factor of 32, channel B is selected
  // By omitting the gain factor parameter, the library
  // default "128" (Channel A) is used here.
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_gain(128);
 
  calibrate();
 
  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC
 
  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC
 
  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()
 
  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
  // by the SCALE parameter set with set_scale
 
  Serial.println("Readings:");
}
 
void loop() {
  Serial.print("one reading:\t");
  Serial.print(scale.get_units(), 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(10), 1);
       myFile = SD.open("res.txt", FILE_WRITE);
      if (myFile) {
        myFile.print(scale.get_units(10),1);
        myFile.println(",");
      }
      myFile.close();
    
  scale.power_down(); 
   lowPowerSleep(1);
// put the ADC in sleep mode
  //delay(2000);
  //delay(60000);
  scale.power_up();
}
 
void calibrate() {
  // Remove any calibration values and clear the scale
  scale.set_scale();
  scale.tare();
 
  // Prompt the user
  Serial.println("Add your known weight to the scale, enter the weight and press <Enter>");
  int userInput = -123;
  String inputString = "";
  // Loop until we receive an input (which will change the value of userInput
  while (userInput == -123) {
    // Read serial input:
    while (Serial.available() > 0) {
      int inputChar = Serial.read();
      Serial.print("inputChar:");

      Serial.println(inputChar);
      if (isDigit(inputChar)) {
        // convert the incoming byte to a char and add it to the string:
        inputString += (char)inputChar;
        Serial.print("inputString:");
        Serial.println(inputString);
        //userInput = inputString.toInt();
        userInput = inputString.toInt();
        Serial.print("userInput");
        Serial.println(userInput);
      }
      // if you get a newline, print the string, then the string's value:
//      if (inputChar == '\n') {
//        userInput = inputString.toInt();
//        Serial.print("userInput");
//        Serial.println(userInput);
//      }
    }
  }
 
  // Now get the reading from the scale
  float calReading = scale.get_units(10);
 
  Serial.print("Setting the cal to ");
  Serial.println(calReading / userInput);
 
  scale.set_scale(calReading / userInput);
}
void lowPowerSleep(int minutes)
{
  int seconds = minutes * 60;
  int sleeps = seconds / 8;
  for (int i = 0 ; i < sleeps ; i++) {
   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  }
}

The SD card values :

2544.3,
2531.3,
2528.9,
2514.9,
 disconect usb , 
476.4,186.7,
-270.0,
225.4,
-418.8,169.4,
-1071.8,108.1,
-1097.4,108.9,

Can you provide a schematic of your setup please

The global schematic of my project is below.


I connected my external battery on VCC.

You have a lot of devices tied to the 5v rail. The Arduino isn't a power supply and you're likely exceeding its capacity. When you're plugged into the PC, the USB is sharing the load.

I note you have 3 3-pin headers, with VCC, GND and PWM pin connections. Suspicion, are you intending to drive servo motors, or other high power PWM driven devices? If so, be aware that the 5V from the ProMicro is in no way suitable for those loads.

Thank you for your answers.

Yes but i connected my external battery on this 5V pin.

No, all of my sensors are on the shematic. I made a mistake when I made the PCB, I did not connect anything on the raw pin. I used only the +5V and GND.

You've verified the voltage while running on the battery?

No, I don't have a voltmeter. However I have a 12v 9Ah battery with a voltage converter to get 5v. Normally that should be enough for the sensors I use ?

Linear, buck, etc. Is it a converter that has a potentiometer adjustment? I think you should verify the output voltage before trying to do anything else.

Agreed, particularly if it's an 'adaptable' buck converter. For example, if it has jumpers to set the voltage to various values. When I originally set the ones I use to 5V, the actual output was 4.2V, which caused me grief until I got out the trusty multimeter. I had to resort to the adjustable pot setting to get a good 5V output, which leaves me very uncomfortable, as vibration, inadvertent motion, etc. could all mess with the setting. So yes, check your voltage. If you'll be in this hobby long, just go buy a cheap one to start. Once you have some confidence, you'll probably want another one for the bench. They're incredibly useful for answering questions.

Can you show a schematic or block diagram of how you have everything connected? Including both power sources. Do you turn the whole project off BEFORE disconnecting?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.