Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Generating a 2 MHz clock signal from a Teensy 4.0

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    3

    Generating a 2 MHz clock signal from a Teensy 4.0

    I need a 2 MHz clock pulse, which I tried to do with PWM using:

    analogWriteFrequency(14, 2000000);
    analogWrite(14, 128);

    This doesn't work. It looks like the maximum frequency I'm able to generate this way is 100 kHz on a Teensy 4.0.

    So my question is: what is the best way to generate a 2 MHz clock pulse on a Teensy 4.0?

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,020
    See Table: PWM Resolution (Teensy LC, 3.0 - 3.6, 4.0, 4.1)

    Try appropriate values like analogWriteResolution( 6 );
    > 6 bits or lower allows higher frequency

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thank you Defragster. Unfortunately my project still doesn't work and my cheap oscilloscope can't measure such high frequencies, so I can't check if the clock signal is correct or it is another issue. I think my next step will be to program another microcontroller I've laying around (a Raspberry Pi Pico) to work as a frequency counter to check the pulse.

  4. #4
    Member
    Join Date
    Feb 2020
    Location
    Dublin, Ireland
    Posts
    81
    Hi,
    If you can connect your PWM output to pin 9 on your Teensy 4.x, you can use the FreqCount library to check the frequency,
    All the best,
    Alan

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,398
    I'm running it on a Teensy 4.0 right now. Here's what my scope sees on pin 14.

    Click image for larger version. 

Name:	file.png 
Views:	14 
Size:	25.6 KB 
ID:	28360

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,020
    Quote Originally Posted by PaulStoffregen View Post
    I'm running it on a Teensy 4.0 right now. Here's what my scope sees on pin 14.

    ...
    Paul, posting complete code in use would be cool - though it is just expected to be the res(6) line before the p#1 two lines.

    with a T_4.1: Today's new DVM does Freq Hz counting and gives a ballpark number of 1.999 for the 2 MHz - so it wasn't accurate enough to rush and report success, a loop() {toggle} gave expected results ... and I got distracted seeing if I could drop resolution and get higher Hz counts - only seemed to get write of 37M/2(18.75MHz) on pin at 2 bit res, and measured 0 on the full 37.5M pin. The 0 Hz pins show ~'half voltage' - book doesn't quote Max usable Hz.

    I played with a T_4.1 since I got a new DVM today - a $24 unit that seems better than others on hand - except all black case. But has NonContact AC detection too. And lights the Probe connect ports for the selected function!

    It is '6000 Counts' manual ranging which means I get better range and values it seems not having to use 2V or 20V of the old Sparkfun or other - so should be better for measuring low current.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,398
    Quote Originally Posted by defragster View Post
    Paul, posting complete code in use would be cool
    I just copied those 2 lines from msg #1 into an otherwise empty Arduino template program.

    Code:
    void setup() {
      analogWriteFrequency(14, 2000000);
      analogWrite(14, 128);
    }
    
    void loop() {
    }

  8. #8
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,707
    Works for me too.

    Here is complete example using FreqCount to measure frequency on pin 14. (For Teensy 4, gateinterval is in microseconds ! Who knew. web page documentation needs updating.) Jumper pin 9 to pin 14.
    Code:
    #include <FreqCount.h>
    
    void setup() {
      Serial.begin(57600);
      while (!Serial);
      analogWriteFrequency(14, 2 * 1000000); // test jumper 9 to 14
      analogWrite(14, 128);
    
      FreqCount.begin(1000000);  // 1 second gateinterval for T4
    }
    
    void loop() {
      if (FreqCount.available()) {
        unsigned long count = FreqCount.read();
        Serial.println(count);
      }
    }
    Sketch reports 2000000 every second. No real need to mess with analogWriteResolution(), as the teensy core adjusts duty value based on frequency (hardware/teensy/avr/cores/teensy4/pwm.c)

  9. #9
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thank you all for your feedback. I tested it and it works and then I tested it running through a TXS0108E level converter and it still worked and then I reconnected my set-up including a feed-back to pin 9 for testing and all of a sudden everything works

    No idea what I've changed. Or perhaps something on the breadboards didn't connect properly. Anyway: it work.

    Just to tell what I'm doing: I'm reworking an old Teensy 3.x project called YM2159Synth (https://github.com/trash80/Ym2149Synth) to drive an AY-3-8910 music chip from a Teensy 4.0 (through the aforementioned level converter and an MCP23017 i2c expander. My idea is to feed the audio output back into the Teensy using the audio board to add effects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •