some painful lesson about battery voltage fluctuations

I randomly get freezes on my 2-wheel self-balancing car based on arduino UNO.
it appears more when I give the motor a higher speed. took me a long while to figure out (had to comment out code line by line )

finally found it’s due to battery surge:

it was a bit confusing because when I comment out the gyro read too, the problem would go away. maybe because gyro also takes a lot of current ?? — but certainly not as much as motors

teddyyyy123:
maybe because gyro also takes a lot of current ??

Or because the gyro tells the motors to take a lot of current ?

...R

No, i actually let the whole thing sit still, and manually gave the same values given by gyro to motor, after i disabled gyro
And that works

How much current do the motors take and what sort of battery are you using ? Can you power the motors and Arduino separately ?