Frequency Measurement on digital input

cicciocb
Posts: 4
Joined: Mon Sep 10, 2018 2:08 pm

Re: Frequency Measurement on digital input

Postby cicciocb » Fri Jan 24, 2020 9:40 pm

TinkerBearNZ wrote:
Mon Jul 15, 2019 5:57 am
Wouldn't you get more accuracy by using a hardware signal to gate the counter?

Like... using a 5Hz signal from the LEDC, fed to a GPIO that's also used as the counter gate. Then it's just a matter of adding an interrupt routine on the negative edge of the gate signal to grab the count.

I've had to poke around in the GPIO matrix to avoid using a second GPIO pin, but this does actually work.

My one remaining problem is that sometimes the low-speed LEDC pwm channel I'm using as a gate signal doesn't start, depending on what other LEDC channels are running. I don't have a reliable workaround yet.
Hi TinkerBearNZ ,
could you share what you did on the gpio Matrix to avoid using a second GPIO ?

Thanks

cicciocb

cicciocb
Posts: 4
Joined: Mon Sep 10, 2018 2:08 pm

Re: Frequency Measurement on digital input

Postby cicciocb » Sat Jan 25, 2020 6:49 pm

TinkerBearNZ,
finally I found the solution avoiding to use 2 extra GPIO.

Thanks for the tip

heinzv
Posts: 7
Joined: Fri Sep 20, 2019 7:22 pm

Re: Frequency Measurement on digital input

Postby heinzv » Wed Mar 11, 2020 5:43 am

I'm also looking for a precise frequency measurement of a digital IO for up to 10kHz with an accuracy below 1Hz. Could you share your final solution? BTW I need also to calculate the standard deviation of the pulses. Not sure if a simple HW pulse counter is sufficient then or if I anyhow need to measure each puls length and do the math calculation afterwards.

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Frequency Measurement on digital input

Postby jgustavoam » Thu Jun 18, 2020 2:38 am

Hi,
I made some changes to improve accuracy and stability of the readings.
Unfortunally, the interrupt handler function delays the count. So, the maximum accurate frequency reading is about 98 KHz.
But bellow that frequency, the accuracy is very satisfactory. Minimum frequency is about 8 Hz.
Enjoy it! Thanks Rodmcm!
OBS: connect input pin only after reset, or ESP32 will enter in a loop...

Code: Select all

/*
  Measures the frequency of a square wave appliet to a digital input
  Uses a free running timer/counter for the period measurement
  by: rodmcm
  date: 25/7/2018
  Modified by Gustavo Murta
  date : 17/jun/2020
  https://www.esp32.com/viewtopic.php?f=19&t=6533
*/

const byte        interruptPin = 23;              // Assign the interrupt pin
volatile uint64_t StartValue = 0;                 // First interrupt value
volatile uint64_t PeriodCount;                    // period in counts 
float             Freq;                           // frequency

hw_timer_t * timer = NULL;                        // pointer to a variable of type hw_timer_t

//=======================================
void IRAM_ATTR handleInterrupt()
{
  uint64_t TempVal = timerRead(timer);            // value of timer at interrupt
  PeriodCount = TempVal - StartValue;             // period count between rising edges
  StartValue = TempVal;                           // puts latest reading as start for next calculation
}

//======================================
void setup()
{
  Serial.begin(115200);
  pinMode(interruptPin, INPUT);                                       // sets pin as input
  
  attachInterrupt(interruptPin, handleInterrupt, FALLING);            // attaches pin to interrupt on Falling Edge
  timer = timerBegin(0, 2, true);                                     // configure timer 
  // 0 = first timer
  // 2 is prescaler so 80 MHZ divided by 2 = 40 MHZ signal
  // true - counts up
  timerStart(timer);                                                  // starts the timer
}

void loop()
{
  Freq = 40000000.00 / PeriodCount;                                  // calculate frequency 
  Serial.print("Frequency   "); Serial.println(Freq, 0);
  delay(250);
}
Retired IBM Brasil
Electronic hobbyist since 1976.

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Frequency Measurement on digital input

Postby jgustavoam » Fri Aug 21, 2020 1:53 am

Best Frequency Meter ever made with ESP32 - awesome!

https://www.esp32.com/viewtopic.php?f=19&t=17018

Enjoy it!
Retired IBM Brasil
Electronic hobbyist since 1976.

rodmcm
Posts: 65
Joined: Sat Sep 02, 2017 3:31 am

Re: Frequency Measurement on digital input

Postby rodmcm » Fri Aug 21, 2020 9:23 pm

Thanks for that
I tried your code against my original and didn't really see any difference in accuracy or stability. Can you explain what you saw with your code please.

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Frequency Measurement on digital input

Postby jgustavoam » Sat Aug 22, 2020 12:45 am

Hi Rodmcm,
As already mentioned:
Unfortunally, the interrupt handler function delays the count. So, the maximum accurate frequency reading is about 98 KHz.
But bellow that frequency, the accuracy is very satisfactory. Minimum frequency is about 8 Hz.

If you did not find it pertinent to quote the topic about the new frequency meter, no problem. I can delete it. My intention is to contribute, not compare.
Retired IBM Brasil
Electronic hobbyist since 1976.

rodmcm
Posts: 65
Joined: Sat Sep 02, 2017 3:31 am

Re: Frequency Measurement on digital input

Postby rodmcm » Sat Aug 22, 2020 10:14 pm

II was really trying to understand what part of the interrupt caused the count delay but I didn't get to the high frequency input limitation.
Except for the port call there is not much difference between your and my code...

As to your frequency meter I don't have an LCD and was going to adjust your code to just get print out but haven't got around to it yet.. Impressive frequency range...

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Frequency Measurement on digital input

Postby jgustavoam » Sun Aug 23, 2020 4:03 pm

Hi rodmcm,
The project does not need an LCD Display. This is optional.
You can see the frequency on the Arduino IDE console. And more, you can adjust the frequency of the oscillator to test it.
But you must connect GIPO34 to GPIO33 as indicated.
The ESP32 is incredible!
Retired IBM Brasil
Electronic hobbyist since 1976.

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 38 guests