I am using a ESP32-D0WD-V3 v3.0 with Tasmota on it, and I am generally quite pleased with both!
But with respect to counters I find some quirky results. I generate a PWM=1000 Hz signal on GPIO04, and feed it into GPIO17, configured as Counter, on the same ESP. Then I determine the counts over a period of 1 sec.
Well, 1000 Hz over 1 sec should give 1000 counts. But I do get significantly lower counts, namely around 975. And while the PWM rate seems to be very stable, the measured count rate fluctuates heavily between 975 and 979.
My expectation is that the counter is a hardware feature, and as such is NOT influenced by the things going on in the ESP, in particular WiFi and I2C communication? Is that not the case? Is the counting influenced by ESP32 activity, and type of activity?
How can I get stable counting?
ESP32 and its Counters
-
lbernstone
- Posts: 1132
- Joined: Mon Jul 22, 2019 3:20 pm
Re: ESP32 and its Counters
What commands are you setting in Tasmota to set the pwm frequency?
The default appears to be the 977 that you are getting.
The available frequencies are a function of the bit resolution used to define the timer, so the higher the bit resolution, the less accurate your control of the frequency. At the resolutions Tasmota is using (8-10), this should not really be a problem to get 1kHz.
The default appears to be the 977 that you are getting.
The available frequencies are a function of the bit resolution used to define the timer, so the higher the bit resolution, the less accurate your control of the frequency. At the resolutions Tasmota is using (8-10), this should not really be a problem to get 1kHz.
Re: ESP32 and its Counters
Tasmota command is: Looking at an osci I see a stable signal of "Freq=1.00kHz", "Width<10.00µs", "Period=1000ms" (Data measured by Osci). A 5 digit DMM measures "0.9998 kHz". I also used a pulse generator, which claims "1.000 000 kHz" (50%Duty cycle) and it gave the same missing-counts problem. I decided on the PWM on the grounds that times and pulses would be based on the same clock-crystal, and so remove one uncertainty.
No doubt, it is 1000 Hz. What features of the ESP could impact the counting?
Code: Select all
pwmfrequency 1000No doubt, it is 1000 Hz. What features of the ESP could impact the counting?
Re: ESP32 and its Counters
I now have learned that Tasmota may be doing more things in counting, which may affect the outcome. So I rest my questions here.
But one more question on the type of Counters in the ESP32, what type are the counters? Is it:
A) a pure hardware device, which upon receiving a pulse increments a register outside of the RAM. It can be read and zeroed by the CPU
B) a software device, which upon receiving a pulse triggers an interrupt, which lets the CPU increment a variable inside its RAM. And so CPU can also read and zero this counting value.
But one more question on the type of Counters in the ESP32, what type are the counters? Is it:
A) a pure hardware device, which upon receiving a pulse increments a register outside of the RAM. It can be read and zeroed by the CPU
B) a software device, which upon receiving a pulse triggers an interrupt, which lets the CPU increment a variable inside its RAM. And so CPU can also read and zero this counting value.
-
MicroController
- Posts: 2667
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: ESP32 and its Counters
The ESP32 does have the pulse counter peripheral (PCNT), but Tasmota appears to not be using it instead employing the GPIO interrupt approach, including debouncing in software.
Re: ESP32 and its Counters
That is what it looks like, unfortunately.
Another of my stupid questions: On ESP there is a millis and a micros timer. Do those solely depend on the system-clock, or are they being affected by CPU activity, in particular things like i2c serving, any WiFi, any Bluetooth?
Another of my stupid questions: On ESP there is a millis and a micros timer. Do those solely depend on the system-clock, or are they being affected by CPU activity, in particular things like i2c serving, any WiFi, any Bluetooth?
-
lbernstone
- Posts: 1132
- Joined: Mon Jul 22, 2019 3:20 pm
Re: ESP32 and its Counters
Millis and micros are calculated from the number of ticks passing, so they are affected in minute ways by changing the system frequency. I wouldn't recommend you change the system frequency while you are doing microsecond timing, but other than that, you shouldn't see any difference (ie, they are managed by hardware).
Who is online
Users browsing this forum: No registered users and 4 guests