Weird ESP32 timing issue

alan-bc
Posts: 4
Joined: Mon Jun 23, 2025 10:58 pm

Weird ESP32 timing issue

Postby alan-bc » Mon Jun 23, 2025 11:24 pm

I have a servo motor .. DC motor with quadrature encoder .. connected to a servo controller. The controller gets fed a short pulse and applies enough power to the motor to move it one quadrature count. Give it a steady stream of pulses and the motor rotates as if it were a conventional motor.

I want to create these pulses using a microcontroller. (The end game is to control the speed using a potentiometer.)

I started with an Arduino Nano, but it couldn't generate pulses fast enough. So I moved to an ESP32 S2 because they're fast and because I have some.

It works .. kinda. What happens is that when I feed the pulses, the motor spins beautifully for almost exactly two seconds, then goes "thump" and continues to spin, then two seconds later, "thump" .. and so on.

Spin spin spin thump spin spin spin thump.

My GUESS is that the stream of pulses from the ESP32 are being interrupted by .. something inside the ESP32 .. and so the servo controller rightly tries to stop the motor. But the pulses resume almost immediately and so rotation continues, just with a thump in between.

The "thumps" are extremely regular. And they're independent of speed. No matter how fast or slow I feed the pulses, the thumps appears just about exactly 2 seconds apart.

Any idea what could be causing this?

Here's the test code .. pretty darn simple.

Code: Select all

const byte stepPin = 13;

void setup()
{
   pinMode(stepPin, OUTPUT); 
   digitalWrite(stepPin, LOW);  
}

void loop()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(250);
}

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: Weird ESP32 timing issue

Postby lbernstone » Tue Jun 24, 2025 5:34 am

ESP32 has a hardware peripheral specifically for motor control. The ESP32-S2 does not have this peripheral, but it does have a PWM generator, which will provide a consistent pulse rate (that should be perfectly adequate for a single motor). If you try to bitbang, you are going to get delays from the system processes.

MicroController
Posts: 2668
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Weird ESP32 timing issue

Postby MicroController » Tue Jun 24, 2025 8:38 am

Any idea what could be causing this?
The Arduino main task repeatedly calls yieldIfNecessary(), which for some reason does a delay every 2 seconds.

As Ibernstone said, use hardware for consistent timing.

alan-bc
Posts: 4
Joined: Mon Jun 23, 2025 10:58 pm

Re: Weird ESP32 timing issue

Postby alan-bc » Tue Jun 24, 2025 3:28 pm

Thanks to all for the PWM suggestion!

I clearly didn't didn't research the capabilities of this chip thoroughly enough. I started with an Arduino and found that an 8MHz processor was not quite fast enough to bit-bang and the PWM was not nearly fast enough.

So I looked for the fastest processor in the CPU drawer and found the S2. Quick calculations showed that bit-banging the S2 would certainly work and I didn't look past that. And bit-banging DID work .. except for this 2 second thump.


In my own defense, I'm new to this (seductive) Arduino stuff and indeed, C/C++ programming. I've spent 40 years or more programming microcontrollers in assembler. If something unexpected happened, it was either a bug in the chip (rare but not unheard of) or much much more frequently, my mistake.

So I would stare and stare at my code and eventually figure it out.

Because EVERYTHING that happened was a result of my typing. There was no one else's code in there .. no explicitly called libraries, no unknown automatic added libraries, just my code. So mysterious 2 second thumps never happened unless I put one there.

Old timer musing here .. I once wrote some PIC code, just as an exercise, to dial a modem, bit banging the serial, form a PPP connection, upload a one byte file to an FTP server that represented the state of an input pin, download another one byte file that described the state of an output pin, set the pin accordingly and hang up. It all fit in 1k of memory, and most of that was text strings. But it took DAYS and DAYS to write. With this Arduino business, it's little more troublesome than typing the name of the library correctly. As I say, seductive, but with consequences.

Anyway, thanks again! Now back to the S2, with it's annoying upload procedure.

horace99
Posts: 35
Joined: Mon Oct 14, 2024 10:38 am

Re: Weird ESP32 timing issue

Postby horace99 » Wed Jul 09, 2025 11:26 am

could you use the ESP32S2 RMT (Remote Control Transceiver) https://docs.espressif.com/projects/esp ... s/rmt.html to generate the pulses with variable frequency and duty cycle?

alan-bc
Posts: 4
Joined: Mon Jun 23, 2025 10:58 pm

Re: Weird ESP32 timing issue

Postby alan-bc » Fri Jul 11, 2025 12:10 am

could you use the ESP32S2 RMT (Remote Control Transceiver)
Thanks for this!

I've actually gone a different route, as my brilliant idea hit a real world (near) showstopper.

Annoyingly, the non-workable plan used a motor and controller I already had. The new plan had me buy a new motor and a new controller.

The good news is that the new controller runs off a low speed PWM signal, so the stock ESP32 PWM module will work just fine.

HOWEVER, I have another project in mind .. an LED remote control, more or less .. and I had NO IDEA the little chip had this built in. What a lot of stuff in that tiny little square!

Thanks for the heads up.

Alan

noweare
Posts: 96
Joined: Tue Jul 02, 2019 11:35 am

Re: Weird ESP32 timing issue

Postby noweare » Mon Jul 21, 2025 4:43 pm

Ahh, I remember the simple world of programming in assembly. Every bit was under your control. No outside interference with calling libraries or background things going on. No operating system or API behind the scene.

Welcome to the modern world. Software has gotten so complex since the 80's. Working with a 8 bit processors in C and internal registers is refreshing.

alan-bc
Posts: 4
Joined: Mon Jun 23, 2025 10:58 pm

Re: Weird ESP32 timing issue

Postby alan-bc » Mon Jul 21, 2025 4:56 pm

So you think coding in assembler is .. refreshing. Yes, well, maybe it will be again for me. Increasingly though, I find it tedious. Guess I'm turning into a crotchety old man.

(In the background, I can hear my wife calling out "Turning? TURNED honey, already turned.)

But I still have a wealth of old 8 bit PICs around, and even more 8051s in multiple variations. Maybe I should haul one of those out for my next project ...

Who is online

Users browsing this forum: Bing [Bot], ChatGPT-User and 1 guest