Arduino nano only runs the code when it is connected to the computer

I am making an ironman helmet and I have two servos connected to pins 9/10, GRD, and to the 9v battery trough a switch. Then the battery is in GRD and VIN. An led to pin 4 and GRD. And a button on pin 2 and GRD. Everything works fine as long as the arduino is connected to a computer and the battery is on it works but as soon as I unplug it nothing works. Can anyone help?

Screenshot 2023-03-29 8.31.54 AM
This is the wiring guide with out the potentiometer and a 9v battery.
Do i have to declare it in the code somewhere

Can you post your code and the rated voltage of the servos? It's possible 9v is not enough juice to power your servos + Arduino together.

Also, when how is the power wired when connected to the Arduino?

When you say nothing works, exactly what happens?

That battery holder is 4v x 1.5 = 6v... which is connected to D13? I see... image is reversed. Vin.

Does the power LED (marked ON) on the Nano stay ON when you disconnect the USB cable?

#include <Servo.h>

Servo servo1;
Servo servo2;
int buttonState = 0;
int lastButtonState = 0;
int servoPos = 0;
int ledPin=4;

void setup() {
  servo1.attach(9);
  servo2.attach(10);
  pinMode(2, INPUT_PULLUP);
  pinMode(4, OUTPUT);
}

void loop() {
  buttonState = digitalRead(2);
  
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      // Button was just pressed
      if (servoPos == 0) {
        // Turn servos to 90 degrees
        servo1.write(90);
        servo2.write(90);
        servoPos = 1;
        digitalWrite(LOW);
      } else {
        // Turn servos back to 0 degrees
        servo1.write(0);
        servo2.write(0);
        servoPos = 0;
        digitalWrite(HIGH);
      }
    }
  }
  
  lastButtonState = buttonState;
}

This is the code and the servos are 4.3v-6v

its actually 9v battery connected to VIN

???

If the battery is still switched on it stays on yes!!

Kindly check the following to get the issue resolved
Battery Voltage as per the board Requirement
mAH of the battery
Is the inbuilt led glows brightly or not when connected to battery
Try Changing the battery
Check 5V GND Correctly

Did you try pushing the RESET button?

Yes I did

I will check that thank you

OP answered that question before you asked it.

If you are using a plain alkaline type 9V battery then it cannot supply enough current to run the servos, nano and LEDs at the same time especially if it is not brand new. You should use a lithium 9V or 6 AA batteries

1 Like

Would i still connect it to VIN

Yes

ok thank you very much

@vincguar15 - Six of the AAA (LR03) in your drawing might not have the current capacity, so be sure to use AA (R6) as noted by @jim-p

Is R6 just a different kind of battery