Frequency measurement on ESP32 (3.0.x)

There have been various changes since the new core 3.0.x release. May I ask what library you are using now for frequency measurement? I haven't found anything that works. Thank you very much.

would help to know what frequency range and what is the source

1 Like

Anemometer. There is a range of approximately 0 to 600Hz.

See if this project helps:

Ref:
Definitions:

GPIO_34 = Freq Meter Input
GPIO_33 = Oscillator output - to test Frequency Meter
To test freq Meter with internal oscillator, make connection between GIPO_34 and GPIO_33 (optional).
Change frequency, inputting value at Arduino console (1 Hz to 40 MHz)

1 Like

if it is pulse timing you could simply attachInterrupts to check for rising and falling edges and use micros() to measure times, e.g.

// ESP32 - ESP32 measure pulse width and rising edge-to-edge time using micros()

#define RISE 18  // pin to input pulses
#define FALL 19

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.printf("\n\nESP32 measure pulse width and rising edge-to-edge time\n");
  attachInterrupt(digitalPinToGPIONumber(RISE), rising, RISING);    // detect rising edge on pin 16
  attachInterrupt(digitalPinToGPIONumber(FALL), falling, FALLING);  // detect falling edge on pin 17
}

volatile unsigned long pulseWidth = 0, pulseEdge = 0, pulseStart = 0, counter = 0;
void rising() {
  pulseEdge = pulseEdge + (micros() - pulseStart);
  pulseStart = micros();
}
void falling() {
  // falling edge calculate pulse HIGH width
  pulseWidth = pulseWidth + (micros() - pulseStart);
  counter = counter + 1;
}

void loop() {
  static unsigned long timer = millis();
  if (millis() - timer >= 1000) {
    timer = millis();
    //Serial.println(counter);
    unsigned long pw = (unsigned long)(pulseWidth / counter), pe = (unsigned long)(pulseEdge / counter);
    Serial.printf("pulse HIGH width %luuSec %.3fmSec\n", (unsigned long)pw, pw / 1000.0);
    Serial.printf("rising edge-to-edge time %luuSec %.3fmSec frequency %.3fHz\n", pe,
                  pe / 1000.0, 1.0e6 / pe);
    pulseWidth = pulseEdge = counter = (volatile unsigned long)0;
  }
}

some results

1KHz
pulse HIGH width 499uSec 0.499mSec
rising edge-to-edge time 998uSec 0.998mSec frequency 1002.004Hz
pulse HIGH width 499uSec 0.499mSec
rising edge-to-edge time 998uSec 0.998mSec frequency 1002.004Hz
pulse HIGH width 499uSec 0.499mSec
rising edge-to-edge time 999uSec 0.999mSec frequency 1001.001Hz
pulse HIGH width 499uSec 0.499mSec
rising edge-to-edge time 999uSec 0.999mSec frequency 1001.001Hz


10KHz
pulse HIGH width 49uSec 0.049mSec
rising edge-to-edge time 99uSec 0.099mSec frequency 10101.010Hz
pulse HIGH width 49uSec 0.049mSec
rising edge-to-edge time 99uSec 0.099mSec frequency 10101.010Hz
pulse HIGH width 49uSec 0.049mSec
rising edge-to-edge time 99uSec 0.099mSec frequency 10101.010Hz
pulse HIGH width 49uSec 0.049mSec
rising edge-to-edge time 99uSec 0.099mSec frequency 10101.010Hz

100Hz
pulse HIGH width 4999uSec 4.999mSec
rising edge-to-edge time 9999uSec 9.999mSec frequency 100.010Hz
pulse HIGH width 4999uSec 4.999mSec
rising edge-to-edge time 9999uSec 9.999mSec frequency 100.010Hz
pulse HIGH width 4999uSec 4.999mSec
rising edge-to-edge time 9999uSec 9.999mSec frequency 100.010Hz
pulse HIGH width 4999uSec 4.999mSec
rising edge-to-edge time 9999uSec 9.999mSec frequency 100.010Hz

1Hz
pulse HIGH width 500003uSec 500.003mSec
rising edge-to-edge time 1000008uSec 1000.008mSec frequency 1.000Hz
pulse HIGH width 500006uSec 500.006mSec
rising edge-to-edge time 1000011uSec 1000.011mSec frequency 1.000Hz
pulse HIGH width 500005uSec 500.005mSec
rising edge-to-edge time 1000011uSec 1000.011mSec frequency 1.000Hz
pulse HIGH width 500005uSec 500.005mSec
rising edge-to-edge time 1000011uSec 1000.011mSec frequency 1.000Hz

alternatively use esp_timer_get_time() from ESP Timer (High Resolution Timer)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.