cannot get FreqMeasure Library to work on Teensy 4.0

Knas

Member
Hello. I'm trying to run the example program given on the FreqMeasure Librarys page

Code:
#include <FreqMeasure.h>

void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}

I've got a 0-2.6Vpp 600Hz square wave coming out of a Wavetek connected to pin 22 on the Teensy and ... nothing! I dont even get a incorrect signal.

It seems FreqMeasure.available is never true despite seeing the signal on my scope from the Teensy pin directly - i also tried it with a brand new Teensy 4.0 to be sure.

Im running Linux and ive got a whole slew of other Teensy stuff working just dandy, but this simply wont do it for some reason. Any ideas of what could be going on here?? It smells of me having done something fundamentally wrong..

Thanks

Karl
 
Do you have common ground between teensy and generator.
Try this test with PWM (jumper pin 12 to pin 22)
Code:
#include <FreqMeasure.h>

void setup() {
  Serial.begin(57600);
  analogWriteFrequency(12,600);   //jumper pin 12 to pin 22
  analogWrite(12,128);
  FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}
 
Last edited:
Do you have any input conditioning, filtering, squaring circuitry such as this or is the signal generator connected directly to the pin?

i didn't since its a super clean output from a high quality signal generator.

Do you have common ground between teensy and generator.
Try this test with PWM (jumper pin 23 to pin 22)
Code:
#include <FreqMeasure.h>

void setup() {
  Serial.begin(57600);
  analogWriteFrequency(23,600);   //jumper pin 23 to pin 22
  analogWrite(23,128);
  FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}

So this did work but to my surprise the output of pin 23 is 4.4Vpp despite the Teensy being 3.3V so i did a test and put an attenuator in between pin 23 and 22 and already at ~3.2V and lower i started getting freaky unstable results. This is a bit confusing to me as i thought it would work with normal logic levels?
 
actually, T4 pwm test should use pin 12 not pin 23
(see https://www.pjrc.com/teensy/td_libs_FreqMeasure.html )

? if AC add blocking capacitor?

Where do you get this from? It says in the list to use pin 22 as input pin for Teensy 4.0 when using FreqMeasure.

Btw i added a comparator to my signal and overdrove it to a completely clipped square @ 0 - 3.8Vpp in order to be similar to when pin 23 is driving pin 22 directly but no dice.

I am very confused by this, it should be so easy...
 
Where do you get this from? It says in the list to use pin 22 as input pin for Teensy 4.0 when using FreqMeasure.

In the 2nd column, it says when using T4 pin 22 as input, then you can't use pin 23 for analogWrite(). So choose another PWM pin, e.g. pin 12, for the PWM test example.

Teensy 4 pins are NOT 5 volt tolerant, so you shouldn't drive pins over 3.3v.
 
In the 2nd column, it says when using T4 pin 22 as input, then you can't use pin 23 for analogWrite(). So choose another PWM pin, e.g. pin 12, for the PWM test example.

Teensy 4 pins are NOT 5 volt tolerant, so you shouldn't drive pins over 3.3v.

Well i tested it again and tho you are correct that it says pin 23 can't be used it clearly works for me which is odd. I also tested putting the signal on pin 18 which yielded the exact same results. To further add to the confusion, both the PWM on pin 23 and pin 18 are around 4V.

Idk why this would have anything with the frequency measuring on pin 22 not working, but it sure as hell makes me feel like shit is awry
 
If signal generator was driving pin 22 at more than 3.3v, then maybe you fried pin 22 and other pins. You should be seeing only 3.3v on PWM output pins. :confused:

you could try some pin tests, digitalRead() digitalWrite(), and measure voltage with meter on digitalWrite()

do you have soldered headers on your T4? maybe attach photo of your Teensy
 
Last edited:
If signal generator was driving pin 22 at more than 3.3v, then maybe you fried pin 22 and other pins. You should be seeing only 3.3v on PWM output pins. :confused:

you could try some pin tests, digitalRead() digitalWrite(), and measure voltage with meter on digitalWrite()

do you have soldered headers on your T4? maybe attach photo of your Teensy

It's weird but i wouldn't be too worried. I read the same on both of the *brand new* Teensys i have - and, they work excellently in all regards except i cannot measure frequency with either of them.
 
but to my surprise the output of pin 23 is 4.4Vpp

Can you show us the measurement? If your scope doesn't have screen capture, just a quick cell phone photo of the screen should do.

I tried the code from msg #4. Amazingly it does measure 600 Hz even when using pin 23 to as PWM. This is the waveform my scope sees.

file1.png

In this screenshot, you can see the scope's automatic measurement thinks the peak to peak voltage is 3.62V. But if you look at the waveform and grid on the scope, you can pretty clearly see the logic low level is 0 volts and logic high is somewhere just over 3 volts, and the waveform has some slight overshoot and undershoot right after each rising or falling edge. The scope's peak-to-peak measurement is detecting that overshoot and undershoot.

This sort of overshoot is pretty common with quick scope measurements. I just clipping my scope's probe to a piece of wire, and used its normal ground clip. Those are known to give overshoot and undershoot. You can also see in the screenshot that I configured channel 1 to bandwidth limit.

If I turn off the bandwidth limit, this is the result for exactly the same measurement.

file2.png

You can see the overshoot and undershoot are pretty extreme with this quick & dirty (but so very convenient) probe connection when the scope's full bandwidth is used (spec on my scope is 200 MHz, probes are 700 MHz... but lots of measurements look like Agilent may have actually put the 350 MHz hardware in my scope). The scope now thinks the peak to peak is 6.16 volts!!!

The reality of high bandwidth scope measurements (on digital signals with high bandwidth edges) you get huge overshoot and undershoot if using a convenient ground clip and especially if a piece of extra wire is between the probe and circuit.

Obviously the real signal isn't really 6.16 volts peak-to-peak. But what the scope sees is, because I didn't use a good high bandwidth connection. I just casually clipped the probe's ground lead to the circuit and also used a piece of wire a few inches length between the pin and probe.

Whether this is really the problem you're seeing, I don't know, since I can't see your scope. But I can tell you Teensy's digital pin doesn't output 4.4 volts when the power supply is only 3.3V, so this sort of very common artifact of oscilloscope measurement is very likely the cause.
 
Can you show us the measurement? If your scope doesn't have screen capture, just a quick cell phone photo of the screen should do.

I tried the code from msg #4. Amazingly it does measure 600 Hz even when using pin 23 to as PWM. This is the waveform my scope sees.

Yeah i was surprised by this, im not sure what the reason it woudn't work is tho?

The reality of high bandwidth scope measurements (on digital signals with high bandwidth edges) you get huge overshoot and undershoot if using a convenient ground clip and especially if a piece of extra wire is between the probe and circuit.

Obviously the real signal isn't really 6.16 volts peak-to-peak. But what the scope sees is, because I didn't use a good high bandwidth connection. I just casually clipped the probe's ground lead to the circuit and also used a piece of wire a few inches length between the pin and probe.

Whether this is really the problem you're seeing, I don't know, since I can't see your scope. But I can tell you Teensy's digital pin doesn't output 4.4 volts when the power supply is only 3.3V, so this sort of very common artifact of oscilloscope measurement is very likely the cause.

Oooohhh okay that makes sense, cool. Well i still never got the input measurements to work but ill have to get back to that in a minute because i somehow just killed one of my teensys i think in a mysterious way (i am apparently only trouble) Ill write another post about that rn and get back to this later.
 
So okay i did another test and here's a view of my scope. The cyan waveform is the output from pin 12 of the teensy and the magenta one is the output from my (a little wonky) wavetek with a -4V offset so both waveforms can be viewed

scopestyle.jpg

Connecting pin 22 to pin 12 i get a steady "1672.02" output in the serial view. When connecting pin 22 to my wavetek (which is of course grounded with the teensy) i get this kinda stuff
Code:
2071.51
2355.56
2029.63
2111.72
2355.12
2722.03
2467.71
2201.48
2300.83
2071.85
2462.85
2353.72
2156.94
2721.14

Now my wavetek is totally out of calibration and a old guy that i got for free BUT the scope doesn't lie (its *pretty much* stable) and the only reason i tried this is because the circuit i'm *actually* trying to measure didn't work. odd?
 
Sure enough, I can see the overshoot and undershoot on the cyan waveform.

Looks like your scope is triggering on channel 2 (cyan). Can you change it to trigger on channel 3 (magenta), so we can get a good look at the wavetek's output?
 
Sure enough, I can see the overshoot and undershoot on the cyan waveform.

Looks like your scope is triggering on channel 2 (cyan). Can you change it to trigger on channel 3 (magenta), so we can get a good look at the wavetek's output?

Here ya go. You can tell im having big issues getting the signals to have the same frequency because the wavetek is so out of calibration and has a stupid 80s membrane key interface that barely works lol

scopeagain.jpg
 
Back
Top