problems with seeeduino xiao when using uart for communication

hello,
I have a problem where im trying to make a motor spin for 360 degrees and then back and simultaneously serial printing the voltages I'm getting from a 360 degrees potentiometer for angle detection.
every time i run the program the motor starts lagging like its getting disconnected and it doesnt let me open the serial monitor. so to test connectivity issues i opened the device manager while running the system and i noticed the page is getting refreshed very often and it seems like the xiao keeps disconnecting which is troubling the whole system, i would be glad for some help if possible. the code is down below. thank you very much

int voltage=0;
int dir=9;
int ena=10;
boolean dirr=LOW;
long time_now=0;
long time_prev=0;
int counter=0;
void setup() 
{
  analogReadResolution(12);
  Serial.begin(2000000);
  pinMode(ena,OUTPUT);
  pinMode(dir,OUTPUT);
  digitalWrite(ena,HIGH);
  digitalWrite(dir,HIGH);
  dirr=1;


}

void loop() 
{
voltage=analogRead(8); 
Serial.print("voltage now  ");
Serial.println(voltage);
time_now=millis();
if(abs(time_prev-time_now)>=680)
{
  if(counter==0)
  {
   digitalWrite(9,HIGH);
  }
   
  else if (counter==1)
  {
   digitalWrite(9,LOW);
  }
  counter++;
   if(counter==2)
   {
   counter=0;
   }
 time_prev=time_now;
 Serial.print("time now  ");
 Serial.println(time_prev);
 Serial.print("counter now ");
 Serial.println(counter);

}




}

What's a seeeduino xiao? (looks like way too many vowels there!)

@shadyganaem

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Looks like you're using a motor shield which makes it important to know how everything is connected (including power). Please provide a wiring diagram.

Does your problem go away if you disconnect the motor?

//Edit
Please provide links to datasheets of the motor, shield and whatever else you think can be relevant.

@
CrossRoads

here is a link to the microchip :

@sterretje
motor driver data sheet : Pololu - DRV8838 Single Brushed DC Motor Driver Carrier
seeeduino xiao wiki : Getting Started with Seeed Studio XIAO SAMD21 - Seeed Wiki
rotary pot : https://www.bourns.com/docs/product-datasheets/3382.pdf?sfvrsn=8e3b8ff1_9/

diagram : diagram hosted at ImgBB — ImgBB
motor specs: specs-for-motor hosted at ImgBB — ImgBB
motor pic : mototr-pic hosted at ImgBB — ImgBB

You did not answer the question if the problem goes away when you disconnect the motor?

I do not see how the complete setup is powered? From USB? From external 3.3.V power supply?

OP's schematic

OP's motor spec

I uploaded your code to a xiao without connection the poti and motor and it works like expected

The motor is pretty small but will still pull more current than the XIAO onboard voltage-supply can deliver.
I estimate this small motor pulls 10 to 30 mA.

This makes the board reset.

I made some minor modifications to your code:

IO-pin-number should be defined as const which means constant.
If you accidently try to change a variable defined as constant the compilers complaints

You should consequently use variables
not hardcoded numbers like here

   digitalWrite(9,HIGH);

or her

voltage=analogRead(8);

if you define variables used with millis() as unsigend longs rollovers of millis() are automatically handled the right way and you don't need abs()
so the timing if-conditions simplifies to

  if (time_now - time_prev >= 680) {

You put the baudrate to 2.000.000 this is no problem but you should put the printing of the voltage inside the timed if-condition
here is the modified code

int voltage = 0;

const int poti =  8;
const int dir  =  9;
const int ena  = 10;
boolean dirr = false;
unsigned long time_now = 0;
unsigned long time_prev = 0;
int counter = 0;

void setup() {
  analogReadResolution(12);
  Serial.begin(2000000);
  Serial.println("Setup-STart");
  pinMode(ena, OUTPUT);
  pinMode(dir, OUTPUT);
  digitalWrite(ena, HIGH);
  digitalWrite(dir, HIGH);
  dirr = true;
}

void loop() {
  voltage = analogRead(poti);
  time_now = millis();

  if (time_now - time_prev >= 680) {
    // you should actualise time_prev as the first thing
    // to get constant intervalls regardless of what the rest of the code iso doing
    time_prev = time_now; 
    Serial.print("voltage now  ");
    Serial.print(voltage);

    Serial.print(" time now  ");
    Serial.print(time_prev);
    Serial.print(" counter now ");
    Serial.println(counter);

    if (counter == 0) {
      digitalWrite(dir , HIGH);
    }

    else if (counter == 1) {
      digitalWrite(dir , LOW);
    }
    
    counter++;
    if (counter == 2) {
      counter = 0;
    }
  }
}

I'm pretty sure your repeated dis/enabling of the comport is caused by resets of the XIAO through current-overload
best regards Stefan

@
sterretje
sorry for not answering all your questions, yes currently the xiao takes power from the pc using the cable, the problem doesn't occur when the motor is not connected, in fact weirdly if i connect the motor and let it run on constant speed without reversing the direction and just serial print the voltages it works fine, im guessing reversing the motor brings voltages spikes to the system and making the xiao reset ?
what do you think i should do ? should i power the motor from the from the 5 volts pin of xiao since its directly connected to the vcc of the input from the computer ? i.e current will flow from pc and not the xiao
thank you very ,uch for your time

@StefanL38
first of all im very grateful for your help and time. thank you for fixing my code.
the xiao takes power directly from pc using the type c cable, the motor essentialy takes power from the driver and the driver takes power from the 3v pin of the xiao ( like the diagram i posted ) do you think the driver should power from the 5v pin so the current comes through the pc and not directly from the xiao using the 3volts pin ?
thank you

Get another power supply for your motor; connect to the Vin pin of the driver as that powers the motor.

I googled for XIAO maximum current. Google show some hits that asnwer the question how much current a single IO-pin can drive.

But I found this

The 1st answer mentions

If you scroll down on that page you link to you can see the power circuit. It uses an XC6206 voltage regulator which has a maximum input voltage of 7V and a dropout voltage that could be as high as 680mV. And has a link to the datasheet of the voltage-regulator

The datasheet says the XC6206 can do a maximum output-current of 200 mA.

So from this side it should work. The specs of th emotor say at 3V 23 mA At 3,7V 26 mA at 5V 35 mA
The code you posted above does just print-out the analogRead-value
the H-Bridge Motordriver is set to some mode.

Whenever you start a new project. Write small testprograms that do just one thing.
If you are not yet familair with the hardware a malfunction can have various reasons and testing in a bigger program means more possabilities where the bug could be. (by the way in 90% of all cases the "bug" has its hand on the keyboard and is staring at the screen :-)))))

So reducing the possible reasons causing non-functioning will it make easier to find the real reason.

And as a pre-pre-Test. You should just connect the motor to the driverboard using an extra powersupply and just connect the control-pins of the motordriver-board EN and PHASE to the Vcc or ground of the powersupply as a first basic simple test if you udnerstand how the board shall work.

Do you have a digital multimeter? If no this is the right time to buy one.
I recomend this one

ver good price/perfomance ratio. Can measure capacity, temperature, duty-cycle, frequency and has a bluetooth app for slow-speed recording measurings
Tinkering with microcontrollers and connecting harware to it cries for a digital multimeter and a 8 channel logic analyser
like this one for just $10

I bought one recently. It works with the opensource-software pulseview which can be downloaded here
https://sigrok.org/wiki/Windows

pulseView can even analyse the bitbanging of Serial I2C. SPI amd CAN-Bus to show you the bytevalues that have been transmitted

best regards Stefan