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

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

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

Postby plusorc » Mon Sep 07, 2020 1:25 pm

Thanks for all the people who tried to help
and special thanks to @chegewara for a previous help in a different topic .


The problem was fixed by inverting the Signal , not by any software changes .

It looks like if you hold an INPUT PIN high , you get that behaviour
but if you invert the signal "which I did" , it works with no problems
Have no idea why is that , but the circuit is here and also the code ,so any one interested ..you can try it .

below is the complete circuit for anyone interested

FC_Fixed.png
FC_Fixed.png (52.09 KiB) Viewed 3617 times

the 2 10K resistors are connected to the 3.3v supply , sorry if that is not very clear (I'm a software guy)
Power for PIC817 and 2N222 is NOT from ESP
2n222 was used to invert the signal and that solved the problem .


as for PCNT , LEDC .. , Those are very good for HIGH Frequencies .. not for 50Hz , it's an overkill for such low frequency
besides I got double that frequency accurately with 2 ESPs

and again the Sketch to read is simple while loops to measure the wavelength
I did put it in a Task , but that is not even necessary , it can be in the main loop and it will give the same results

Code: Select all


int FC_PIN = 34 ;


void Get_Cycle_Length(void* arrow)
{
  for(;;)
  {
    char buf[8] = "";
    volatile int64_t start_time, end_time, cycle_duration ;

    noInterrupts() ;
    while(digitalRead(FC_PIN) != LOW)
    // while(analogRead(FC_PIN) > 0)
      {
        // If we get here while the phase is high .. wait till the phase is LOW
      }
      
    
    while(digitalRead(FC_PIN) == LOW)
    // while(analogRead(FC_PIN) == 0)
      {
        // Wait till the LOW Phase ends so we start the timer
      }

     
    start_time = esp_timer_get_time(); // timer in microSeconds
    
    while(digitalRead(FC_PIN) == HIGH)
    // while(analogRead(FC_PIN) > 0)
      {
      // another wait till the  HIGH phase ends 
      }

    while(digitalRead(FC_PIN) == LOW)
    // while(analogRead(FC_PIN) == 0)
    {
      // another wait till we reach the high phase again
    }
    
    end_time = esp_timer_get_time() ;
    
    cycle_duration  =  end_time - start_time ;

    interrupts();
    Serial.printf("Start_Time : %jd \t End_Time : %jd \t", start_time, end_time);
    Serial.printf("Duration for 1 Cycle is : %.3f mSec\n", cycle_duration/1000.0);

    //start_time = end_time = cycle_duration = 0 ;
    
    delay(950); // read wavelength every 950mSec 

    
  }
  
  
  }

void setup() 
{
  Serial.begin(115200);
  pinMode(FC_PIN,INPUT);
  noInterrupts() ;
  delay(1000);

  xTaskCreate(Get_Cycle_Length, "Cycle_Length", 8192, NULL,4,NULL);
}

void loop() 
{
    

  
}
Performance can be watched here , with NO PCNT or ledC or RMT , this is just 50Hz !

https://streamable.com/me5bo8

Ahmet58
Posts: 21
Joined: Sat Jan 22, 2022 8:38 pm

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

Postby Ahmet58 » Wed Nov 30, 2022 10:25 pm

Hello, I've looked at the code a bit and I'm trying to understand it. Didn't you write any code inside the loop? Also, I watched the video in the link. Normally, you were saying that you are trying to count 50hz. I don't understand why it wasn't counted.

Macckone
Posts: 1
Joined: Sun Aug 06, 2023 4:36 pm

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

Postby Macckone » Sun Aug 06, 2023 4:43 pm

The likely cause of issues is a slow rising edge. The usual solution is to implement a schmitt trigger. Which the second transistor will act like.

svedblen
Posts: 3
Joined: Wed Sep 11, 2024 6:31 am

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

Postby svedblen » Wed Nov 06, 2024 6:08 am

@plusorc

Did you ever find an explanation? Any solution?

Cheers!

Who is online

Users browsing this forum: Google [Bot] and 76 guests