ESP32 Can't make an accurate Frequency Counter ???

plusorc
Posts: 41
Joined: Sat Nov 09, 2019 6:27 am

ESP32 Can't make an accurate Frequency Counter ???

Postby plusorc » Thu Sep 03, 2020 6:06 am

After so many issues with esp32 as a frequency counter , I decided to put the issue here ..hopefully I'm wrong .

So , during making this project ..we needed the addition of frequency counter ..and we started noticing erratic behaviour from esp32
I'm the software guy , so I thought something is not right with the custom board
The hardware guys are convinced that it's all about the ESP32 itself , and it's timers .
so in my last attempt I decided to make a separate circuit to test this FC thing in isolation of everything else .

to see the circuit , go to the following link
http://falstad.com/circuit/circuitjs.html

and from File -> Import from text and copy the following and paste it there

$ 1 0.000005 9.78399845368213 59 5 43
v -64 176 -64 240 0 1 50 9 0 0 0.5
d -32 176 80 176 2 1N4004
w -64 176 -32 176 0
r 96 176 224 176 0 1000
w 80 176 96 176 0
w 224 400 -64 400 0
w -64 240 -64 400 0
g 320 368 320 400 0
162 256 352 256 384 2 default-led 0 1 0 0.002
407 224 224 336 224 1
w 320 256 320 288 0
w 224 256 224 400 0
R 320 160 288 160 0 0 40 3.3 0 0 0.5
w 320 176 320 160 0
r 320 176 320 224 0 10000
w 432 288 432 224 0
403 496 176 736 288 0 0_64_0_21259_20_0.0125_0_2_0_3
w 320 224 432 224 2
403 496 320 736 432 0 17_64_0_21003_5_0.00009765625_0_2_17_3
w 320 288 320 368 0
x 259 436 355 439 6 12 Ground\sOf\sESP32
x 405 312 462 315 6 16 GPIO34
p 80 208 80 288 1 0 0
w 80 288 192 288 0
w 192 288 192 256 0
w 192 256 224 256 0
w 80 208 192 208 0
w 192 208 192 224 0
w 192 224 224 224 0
w 224 176 224 224 0
circuit.jpg
circuit.jpg (51.61 KiB) Viewed 13362 times

A/C is a Transformer from 220v to 9v
The Opto Coupler is PC817 or EL817
Diode is 1N4004 but on my circuit i'm using 1N4007


again I'm the software guy , but this circuit works perfectly with arduino uno , the only difference is we pull high to 5V instead of 3.3v
with ESP32 , it gives very un stable readings .

I tried everything from Hardware Timers to even State Machine code !
-Even with State machine Sketch , Arduino uno is more stable
-ESP32 sometimes reaches 57 and 60 Hz ! , needles to say .. my mains run @ 50Hz

The code of just the State Machine sketch is below

Code: Select all


int count = 0;
unsigned long lastEntry;
int stat = 0;


#define INPUTPIN A0


void setup() 
{

  Serial.begin(115200);
  pinMode(INPUTPIN, INPUT);
  
  //Serial.printf("ESP TIMER : %jd \n", esp_timer_get_time());
}

void loop() 
{

  count = 0;
  lastEntry = millis();
  while (millis() < lastEntry + 1000) 
  {

    bool inputSignal = (digitalRead(INPUTPIN));
    switch (stat) 
    {
      case 0:
        if (inputSignal) 
        {
          count++;
          stat = 1;
        }
        break;

      case 1:
        if (!inputSignal) 
          stat = 0;
        break;

      default: 
        break;
    }
  }
  Serial.print("Frequency : ");
  Serial.println(count);

  
}

I would love to be wrong about that , but in reality .. we don't just need an FC that outputs 50Hz , we're required to measure the wave length , and if we can't even have a stable Count from ESP32 , we're wasting time trying to measure the wave length .

Thank You

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby ESP_Sprite » Thu Sep 03, 2020 7:18 am

Could be that the issue is that you're naively assuming that you're the only thing running on an ESP32... while in reality, you also have the OS, WiFi stack, BT stack, NVM, whatever that all can interrupt your task and make you detect the edge too late. Suggest you use a hardware peripheral (e.g. RMT) to do it instead.

plusorc
Posts: 41
Joined: Sat Nov 09, 2019 6:27 am

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby plusorc » Thu Sep 03, 2020 8:05 am

ESP_Sprite wrote:
Thu Sep 03, 2020 7:18 am
Could be that the issue is that you're naively assuming that you're the only thing running on an ESP32... while in reality, you also have the OS, WiFi stack, BT stack, NVM, whatever that all can interrupt your task and make you detect the edge too late. Suggest you use a hardware peripheral (e.g. RMT) to do it instead.
Thanks for taking time to look into this , however , It's not that simple
We tried the noInterrupts() before trying to detect the edges with Interrupt Or pulseIn() but got the same results .
I my self tried a different thing

I got One ESP32 to produce a Square wave and send that through 1 wire to another ESP32 that reads the pulses ,
everything worked perfectly and it was so stable , but once we connect mains power .. we get those erratic readings .

There was also some pattern into this , we got 19.xxx mSec several times and then we get almost half the value and then
almost Zero ..

Original Code "which i"m not free to share is measuring the wave length in a RTOS Task and it disables interrupts before reading"

I included this State Machine Sketch just to bring everything to minimum , so the behaviour i'm mentioning is very clear .

Thanks for your help , but I highly doubt it's just a late detection of an Edge because of other tasks running on the cpu .

Agree007
Posts: 103
Joined: Mon Sep 18, 2017 7:11 pm
Location: Copenhagen

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby Agree007 » Thu Sep 03, 2020 12:42 pm

If this statement:

//
I got One ESP32 to produce a Square wave and send that through 1 wire to another ESP32 that reads the pulses ,
everything worked perfectly and it was so stable , but once we connect mains power .. we get those erratic readings
//

Then your problem is with your power circuit and not with the esp32. How did you powered the esp32 when it worked perfect ?

Make sure you have solid ground, eliminate noice and supply the current required by the esp32.

plusorc
Posts: 41
Joined: Sat Nov 09, 2019 6:27 am

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby plusorc » Thu Sep 03, 2020 2:38 pm

Agree007 wrote:
Thu Sep 03, 2020 12:42 pm
If this statement:

//
I got One ESP32 to produce a Square wave and send that through 1 wire to another ESP32 that reads the pulses ,
everything worked perfectly and it was so stable , but once we connect mains power .. we get those erratic readings
//

Then your problem is with your power circuit and not with the esp32. How did you powered the esp32 when it worked perfect ?

Make sure you have solid ground, eliminate noice and supply the current required by the esp32.

I made 2 experiments for testing

1) An ESP8266 produces a Square wave and an ESP32 to receive it
That wasn't very accurate , but it was working
Power supply was just 2 usb cables from the PC , or a separate adaptor 2A to the Signal Producer Chip ESP8266
and the ESP32 from USB from the PC

2) An ESP32 to Produce the Square Wave and another ESP32 to receive it
That was completely accurate as you would expect from an MCU with 240 MHz counting only 50Hz
Both Methods of supplying Current was extremely accurate and the variation in readings was no more than few
micro seconds .

In both cases , the Function Generator Chip (the chip producing the Square wave) Only supplies 1 wire Only to the receiver chip
No Common Ground

Trying to read Mains Frequency , I have to connect 3.3v and GND from the receiving chip And the Signal Wire of course ,
Seems like this situation is causing the mentioned behaviour

I also noticed that if 1 power supply to any of the chips is not enough , all the readings are compromised ..I mean all of them .not the erratic behaviour that I get from the circuit above .


Thanks for taking time to look into this

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby idahowalker » Thu Sep 03, 2020 9:18 pm

Will the built in pulse counter work for your application?

from the ESP32 PCNT API: The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of an input signal.

https://docs.espressif.com/projects/esp ... /pcnt.html

woodmna69
Posts: 1
Joined: Fri Sep 04, 2020 7:39 am

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby woodmna69 » Fri Sep 04, 2020 7:51 am

I'm glad I found this. I have the exact same problem right now! I'm using a Vishay H11AA1 to detect zero crosses on 60hz AC. This works perfectly on an 328p Arduino at 3.3v, but moving it over to the ESP32, it does not. I'm not including code because the test code is super dumb simple. Attach interrupt to pin, ISR increments an int, in the main loop every 1000 ms report the int and reset to 0. On 328P 120-121 like a rock as it should, but the ESP32 reports 400-500. This is the exact same circuit in place just literally moving wires over from some breadboard holes to others to switch chips. I'm also reducing the ADC resolution to 10 bit to match the Arduino. It's driving me crazy and I'm glad I'm not the only one seeing this. If any advise, I'm all ears! Thanks!

And the pulse counter thing, I need to look into that, but this seemed like a simple task really.

plusorc
Posts: 41
Joined: Sat Nov 09, 2019 6:27 am

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby plusorc » Fri Sep 04, 2020 8:17 am

@idahowalker

I don't think the behaviour I'm getting from the chip is related to the method I use to detecting the edge Rising/Falling
It looks like a Power Issue

I powered the Opto Coupler on the Circuit above from an external source , and now I'm getting much more stable readings ..
I still get those half values and Zeros from time to time , but much more steady than what I get If I powered it from ESP Pins .

Still investigating this

Thanks for taking time to look into this

plusorc
Posts: 41
Joined: Sat Nov 09, 2019 6:27 am

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby plusorc » Sun Sep 06, 2020 4:17 pm

@woodmna69

The complete circuit
OptoCoupler_Circuit.png
OptoCoupler_Circuit.png (43.48 KiB) Viewed 13154 times
The Data analysed
Screenshot from 2020-09-05 10-41-34.png
Screenshot from 2020-09-05 10-41-34.png (123.02 KiB) Viewed 13154 times
The Zeros and Half Values
Screenshot from 2020-09-04 07-07-08.png
Screenshot from 2020-09-04 07-07-08.png (273.69 KiB) Viewed 13154 times

Looks like no one is taking this seriously
I usually code IDF not Arduino core , Only this project needed Arduino because of the many libraries we use

I'm now totally convinced it's a hardware issue
testing with Arduino , I was measuring the wavelength every 13 mSec , Rock Solid ..not a single faulty reading , but with ESP32
I can never eliminate those zeros and half values

Also I don't understand when people say , interrupt .. the ESP stack .. freeRTOS , the Code in reality is freeRTOS and this is the bare minimum to illustrate the issue

btw , The original circuit is an Op Amp circuit .. not the Opto-Coupler like above

Looks like for a FC that is complying with a Government regulations , I should think of adding something like attiny85 , pretty sure that will be accurate enough to meet the requirements

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 Can't make an accurate Frequency Counter ???

Postby chegewara » Mon Sep 07, 2020 12:45 pm


Who is online

Users browsing this forum: No registered users and 133 guests