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

Thread: lfo pmw out

  1. #1

    lfo pmw out


    what is the best option to output a variable waveform for an lfo (i.e. square with pwm, sine, triangle) via an analog out (pwm), the frequency would be given by a tap-tempo.

    on arduino i used to setup a timer and an interrupt to generate waveforms at exact frequencies, setting up a counter (sawtooth wave ) and taking a lookup-table approach. but i am not sure how i would go about it on the teensy 4.

    the desired frequency range will be from 0.1hz to about 20-30hz. since i will also output a clock signal that is sent via a digital pin to other synths, the lfo will need to reset its phase to zero after some cycles in order to stay in sync with the tap-tempo (since frequency will never be exactly right, and hence there will be some drift)

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    If your existing code just uses a timer to run an interrupt function at precise intervals, maybe you could get that running on Teensy by using IntervalTimer to replace the hardware timer code?

    Another option may be to use the Audio library. PWM output is not yet supported, but maybe MQS would work? You could use the design tool to connect the Waveform object to a MQS output, then just write a small amount of code to configure the waveform you want.

  3. #3
    thanks for your answer. i will try the IntervalTimer route. as the timing with IntervalTimer seems very flexible, i can see two ways to accomplish a given frequency:
    -adjust the phase accumulation amount
    -changing the IntervalTimer timing while the accumulation amount stays the same.

  4. #4
    Senior Member
    Join Date
    Apr 2020
    If you;re looking for a pulse in synch with your waveform and your wave will have very low frequencies, you can use MQS and get great S/N at <100Hz with the right lowpass filter on the appropriate MQS pin.

    It seems like maybe you're doing this the hard way? If you need an LFO programmed through tap tempo, here's a pedal with CV input (that can be modulated by audio) for freq and waveform. Combine it with a tap tempo input from a footswitch and set the osc freq from that. The amp is set to a very high gain of 1000 or more so it will quickly clip, giving you essentially a square wave on the second output that is in phase with the analog output wave.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Screenshot from 2020-06-27 12-46-02.png 
Views:	9 
Size:	11.1 KB 
ID:	20766  

  5. #5
    ok, i have this code now (i know a sine not a square), which works (at audio rate for now, not lfo) but i get a lot of "digital" (aliasing like) noise (more then i am used to from pwm). i guess that is because i have a pwm rate of 36621.09 and a interrupt that runs at 50000 hz. is there a way to run the intervalTimer at the exact pwm rate? would that even help?

    float phase = 0.0;
    float twopi = 3.14159265358 * 2;
    elapsedMicros usec = 0;
    volatile float phaseinc = 0.02;
    IntervalTimer lfo;
    void setup() {
      pinMode(2, OUTPUT);
      lfo.begin(lfo_update, 20);
    void loop() {
      //set frequency to taptempo e.g. 500ms= 500000usec
    phaseinc = 1 / (4545.4545 / 100.0);
    void lfo_update() {
        float val = sin(phase) * 2000.0 + 2050.0;
      analogWrite(2, int(val));
    //analogWrite(2, 1000);
        phase = phase + phaseinc;
       if (phase >= twopi) phase = 0;

Posting Permissions

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