ESP32 Can't make an accurate Frequency Counter ???
Posted: 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
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
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
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
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