XIAO ESP32C2 ~5 ms pause/freeze every 2 seconds

Hello,

i have a problem with the XIAO ESP32c3. In my setup, I use a AS5600 absolute angle sensor that is connected to the ESP32 via I2C. I use this sensor to measure the angle of a motor shaft. The motor is controlled with a DRV8833 motor driver an I use PWM to set the motor speed.

Now to the problem: The senor readings have jumps repeatable jumps. Between every two jumps there is exactly 2000 ms time passing.
To show this behavior, I used PuTTY to log the serial output. In the next figure you see the plot of the angle measurement with the jump in angle value:

(As a new user I’m only allowed to upload one picture per post, so i will continue in the next post)

But not only the angle measurement shows a jump, also if i plot millis() there is a jump every 2 seconds:


In this figure you can see that the millis() on the Y-axis jump about 5 ms between measurement point 2311 and measurement point 2312 whereas normally the difference is only 1 ms.

(As a new user I’m only allowed to upload one picture per post, so i will continue in the next post)

Here you see millis() again on the y-axis over the measuring points but more zoomed out. At every blue circle a jump is happening. By comparing the Y-values, it’s clear that there is always 2000 ms between to jumps:

First, I thought it was a problem with the used AS5600 library and I opened an issue on GitHub (Link). I recommend reading the issue to see the tests I did to solve the problem. But in short I tried:

  • Changing baud rate → no difference
  • Trying another AS5600 board → no difference
  • Using the Seeed AS5600 library → no difference
  • calling yield() in void loop() → no difference
  • calling delay(1) in void loop() → no difference
  • Setting i2c clock to 1000000 → no difference
  • Measuring at motor standstill → no difference

Then i tested the same code on an ESP8266 and the problem was not present anymore.
Is there a function that stops/blocks/pauses/interrupts/etc. the XIAO ESP32 every 2 seconds and how can I solve my problem?

Thank you for your help!

Here is the sketch is use:

#include <Wire.h>
#include "AS5600.h"



AS5600 as5600;

//PWM
//https://www.upesy.com/blogs/tutorials/how-to-use-pwm-on-esp32-with-examples

const int in1 = D1; // Steuer-Pins für den drv8833
const int in2 = D2;
const int frequency = 1000; // 5000 Hz
const int pwmChannel1 = 1;
const int pwmChannel2 = 2;
const int resolution = 8; // 8-bit resolution


int motorSpeed = 200;

int ang = 0;
int raw_ang = 0;
int old_ang = 0;
int revs = 0;
int abs_ang = 0;

int counter = 1;

int t_start = 0;
int t_end = 0;

int t = 0;


void setup() {
Serial.begin(115200);
Serial.println("DRV Test");

Wire.begin();
Wire.setClock(400000);

ledcSetup(pwmChannel1, frequency, resolution);
ledcSetup(pwmChannel2, frequency, resolution);

ledcAttachPin(in1, pwmChannel1);
ledcAttachPin(in2, pwmChannel2);

as5600.begin();
as5600.setDirection(AS5600_CLOCK_WISE);  // default, just be explicit
int b = as5600.isConnected();
Serial.print("Connect: ");
Serial.println(b);

ang = as5600.rawAngle();
ang = ang * AS5600_RAW_TO_DEGREES;
old_ang = ang;

}

void loop() {

  ledcWrite(pwmChannel1, motorSpeed);
  ledcWrite(pwmChannel2, 0);
 
  raw_ang = as5600.rawAngle();

  ang = raw_ang * AS5600_RAW_TO_DEGREES;

  if (old_ang > 340 && ang < 20)
    {
      revs++;
    }
   if (old_ang < 20 && ang > 340)
    {
      revs--;
    }

  abs_ang = revs * 360 + ang;
  if (counter == 3)
    {Serial.print(ang);
    Serial.print("\t");
    Serial.print(raw_ang);
    Serial.print("\t");
    Serial.print(abs_ang);
    Serial.print("\t");
    Serial.print(revs);
    Serial.print("\t");
    t = millis();
    Serial.print(t);
    Serial.print("\t");
    Serial.print(as5600.getAngularSpeed());
    Serial.println();
    counter = 0;
    }
  counter++;
  old_ang = ang;

}

Hi , Very interesting issue , I would like to know Have you tried changing the SPEED of the motor?
How do you use the Interrupt? Only the PWM function.
TIA
GL :slight_smile:

Some more tests i did in the meantime:

  • changing motor speed from 200 to 100
  • testing another program to log the serial data
  • disabling ledcSetup, ledcAttachPin and ledcWrite so that no PWM signal is created and the motor is at standstill
    Nothing changed, the problem is still there.

What do you mean by that? I only use the code posted in post 3

I just tried with another XIAO ESP32C2 board - still the same problem every 2000 milliseconds.

Hey,
I’m facing trouble with the AS5600 and XIAO ESP32C3.
I did the next connections:
SDA - GPIO 6
SCL - GPIO 7
GND - GND
VDD - 5V

No matter what code I’m uploading, nothing work and I cant read any value.
Can someone help me please?