-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to make accurate Mains Frequency Counter with ESP32 #4325
Comments
This forum is for issues with the repository codebase. Please ask general questions at https://esp32.com |
I actually think this is a hardware problem in the chip . |
So post it somewhere the engineers might see it. This repository is for the arduino-esp32 codebase, which is an API shim. What I will tell you is the same as others- the esp32 is not good for bitbanging. It is a multi-tasking OS. Use the pcnt peripheral, like in https://esp32.com/viewtopic.php?f=19&t=17018 |
Highly unlikely. You are trying to do timing sensitive operations in a way that is guaranteed to fail due to the RTOS nature of the ESP32. There WILL be other tasks running on the system that WILL interrupt your task. You have a few options to work with the RTOS nature of the ESP32 and you have already mentioned them. As @lbernstone has mentioned, this should have been posted on the esp32 forums. |
@Ibernstone OK , I was under the impression that any thing related to Arduino should be posted here
I think it's very likely this behaviour is hardware related . but , Thanks anyway |
@MG-Tawfeek even in a FreeRTOS task it will be preempted by other tasks with higher priority. The only way to avoid that would be using an isr and track the timing inside the isr with the Arduino side (or FreeRTOS task) poll the frequency as reported inside the isr. Keep in mind that the setup() and loop() methods run in a FreeRTOS task with priority 1 (just above idle) and all other tasks in the system are at much higher priorities. |
I closed the issue but Just to clarify why I keep saying this is a hardware issue : One of the tests I made was to connect 2 ESPs , One to Generate a square wave and the other to receive it The result ? .. the Kind of accuracy that you would expect from a 240Mhz MCU reading 50Hz , not a single faulty reading When you connect an ESP32 to an External Source and have to shared Power or GND , this issue appears Thanks for your time |
That would mostly rule out a hardware issue as the hardware (esp32) is able to more or less accurately read the signal you are feeding it.
Your test circuit is nearly identical to the circuit used in almost every model railroad DCC interface which converts the AC (8kHz roughly) like signal to an MCU friendly high/low pulse pattern. In the case of these it uses |
This could be an interface issue. I built a system that measures the speed of a 3phase wind generator and it works great up to over 1KHZ so 50hz is nothing for ESP32. Verify you are using Pins that support pullups or add external pull ups to be sure. I have used an opto isolator in the past to go from HV to logic voltage, this solves any ground reference issues. Finally note if you are using an ISR (my system does) that on some pins the RISING/FALLING mode does not work properly. There is a good write up on that here: #1111 (comment) In my implementation, I use a FRTOS queue to write timer values from inside the ISR to the Queue and then unpack those in the foreground. Here is some code that might work for you. Create the RPM Task in Free RTOS or just run it in the main loop. NOTE that Pin 34 is not a good ISR Pin but pin 17 works great, the discussion above goes into all the detail you will need. Hope this is helpful. // =============================================== // ===============================================
} // ===============================================
} // RPM_ISR
} |
Thank you all for your help Thread at esp32.com |
This is the circuit I'm using to measure Frequency of mains , it's as simple as it can be
but with ESP32 , for some reason .. I'm getting inconsistent data .
This same circuit works perfectly with great accuracy with Arduino Uno , You just have to connect the OptoCoupler to arduino
instead of ESP32
The Code I'm using to test this is a simple while loops to detect high/low cycles , and again I'm getting great accuracy from
Arduino .
When using ESP32 (devKit) , :
1-If I connect the Power and GND of the PC817/EL817 from ESP32 -> lots of wrong values
2-If I power the PC817 from an External Power source and share a common ground with esp32,
it gets better , but occasionally I get Zero or half the value
This is a sample of the value I get
and those are the Zeros and Half Values I get
I use 2 sketches to debug this
-first one is to see the stability of reading per minute , just a state machine sketch
``
int count = 0;
unsigned long lastEntry;
int stat = 0;
#define INPUTPIN 34
void setup()
{
Serial.begin(115200);
pinMode(INPUTPIN, INPUT);
//Serial.printf("ESP TIMER : %jd \n", esp_timer_get_time());
}
void loop()
{
noInterrupts();
count = 0;
lastEntry = millis();
while (millis() < lastEntry + 1000)
{
}
Serial.print("Frequency : ");
Serial.println(count);
interrupts();
delay(500);
}
The 2nd Sketch is to measure the Cycle length
``int FC_PIN = 34 ;
void setup()
{
Serial.begin(115200);
pinMode(FC_PIN,INPUT);
noInterrupts() ;
delay(1000);
}
void loop()
{
char buf[8] = "";
volatile int64_t start_time, end_time, cycle_duration ;
// int64_t end_time ;
// int64_t cycle_duration ;
}
hopefully I'm missing something here
I've seen lots of people telling me to go for Ledc , PCNT , RMT .. and so on
All of those are fine If I'm getting a constant data but more accuracy is needed , but now it's just inconsistent data
and why I'm getting either the zero (very low value) or nearly half the reading ?
Thanks
The text was updated successfully, but these errors were encountered: