Arduino code works when connected to laptop, not when connected to battery or wall socket

So I have an Arduino nano, and I am using two SG90 servos, and 1 BMP280, and 1 MPU6050. My code works fine when I plug my nano into my computer, but when I connect the battery to my nano, it doesn't run. Also it doesn't run when I plug the usb into a wall socket. The servo moves to 90 degrees, but it seems that it is not detecting the sensor data and not moving the servo. I commented out all calls to Serial. Here is my code:

/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BMP280 Breakout
  ----> http://www.adafruit.com/products/2651

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

// MPU
#include <MPU6050.h>

// Servo 
#include <Servo.h>

Servo servoY;
Servo servoX;

MPU6050 mpu;

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;


#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void pitch_roll_yaw(){
  // Serial.print("Pitch: ");
  // Serial.println(pitch);

  // Serial.print("Roll: ");
  // Serial.println(roll);

  // Serial.print("Yaw: ");
  // Serial.println(yaw);
  int thsjsojf = 1;
}


void setup() {
  // Servo
  servoY.attach(8);
  servoY.write(90);

  servoX.attach(9);
  servoX.write(90);


  // Serial.begin(115200);
  // while ( !Serial ) delay(100);   // wait for native usb
  // Serial.println(F("BMP280 test"));
  unsigned status;
  //status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  status = bmp.begin();
  if (!status) {
    // Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
    //                   "try a different address!"));
    // Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
    // Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    // Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    // Serial.print("        ID of 0x60 represents a BME 280.\n");
    // Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */


  // MPU
  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    // Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }
  
  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibrate, comment this line.
  mpu.calibrateGyro();

  // Set threshold sensivty. Default 3.
  // If you don't want use threshold, comment this line or set 0.
  mpu.setThreshold(3);

}

int incrementPitch = 0;
int curAngle = 90;
int incrementPitchDown = 0;

void loop() {
    timer = millis();
    // Read normalized values
    Vector norm = mpu.readNormalizeGyro();

    // Calculate Pitch, Roll and Yaw
    pitch = pitch + norm.YAxis * timeStep;
    roll = roll + norm.XAxis * timeStep;
    yaw = yaw + norm.ZAxis * timeStep;

    float normalY = norm.YAxis;


    // Serial.print(F("Temperature = "));
    // Serial.print(bmp.readTemperature());
    // Serial.println(" *C");

    // Serial.print(F("Pressure = "));
    // Serial.print(bmp.readPressure());
    // Serial.println(" Pa");

    // Serial.print(F("Approx altitude = "));
    // Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
    // Serial.println(" m");

    // pitch_roll_yaw();


    //TODO: This works, but if pitch > 1 and the servo arm is already at max, it doesn't move. Fix. 
    // TODO: If pitch < 1. move the servo in the opposite way by 5 degrees. Need to track current angle.
    if(pitch>1){
      // Serial.println("PITCH IS GREATER++++++THAN 1, MOVING Y SERVO");
      servoY.write(curAngle+incrementPitch);
      curAngle = curAngle+incrementPitch;
      // Serial.print("Pitch: ");Serial.println(pitch);
      if(curAngle>180){
        // This is just so we don't print the current angle if curAngle > 180. This can be antything.
        int something = 1;
      } else {
        // Serial.print("Current Angle of Y servo: ");Serial.println(curAngle);
      }
      incrementPitch+=6;
    } else if(pitch<-1){
      // TODO: If pitch is < -1, then we move the servo in 5 degree increments starting at the current angle. Right now it jumps some degrees, check the if(pitch>1) for errors? MUST FIX.
      // Serial.println("Pitch is LESS---------THAN 1, MOVING Y SERVO");
      servoY.write(curAngle-incrementPitchDown);
      curAngle = curAngle-incrementPitchDown;
      incrementPitchDown+=6;
    }



    // Serial.println();
    delay(200);
}

ALso, none of the other threads solve my problems.

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

  1. I am new, so I don't have a schematic. I can make one?

  2. Give me one second I will post

3.Atmega328 Arduino nano

Heres a picture:

You are powering the servos from the MCU?

Each one of those plastic geared servos draw up to 800mA.

Servo stuff

Power externally.

power distribution blocks.


How much Amperes?

Well, If i just plug it into the wall the servo moves to its 90 degree angle, i think its something with the sensors.

Check that sensors get correct voltage.. If too hi/low sensors may fail.
(external power to Vin.pin. Not 5V-pin)

How would I be able to check?

What battery ?

Ok. Good luck.

You have been trying for about 21 days, try doing what was asked and post an annotated schematic with links to technical information on the hardware. Frizzy pictures do not count. I expect once a proper schematic is posted you will be running in a day.

Check voltage when connected to external power. I've had similar .. 'dead' chip (1117 5v) My board got 7.1V !
All components survived, but my circuit misbehaved.
....
replace voltage regulator ?