lfo pmw out

Status
Not open for further replies.

lokki

Well-known member
hi,

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)
 
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.
 
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.
 
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.
 

Attachments

  • Screenshot from 2020-06-27 12-46-02.png
    Screenshot from 2020-06-27 12-46-02.png
    11.1 KB · Views: 85
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?

Code:
float phase = 0.0;
float twopi = 3.14159265358 * 2;
elapsedMicros usec = 0;
volatile float phaseinc = 0.02;
IntervalTimer lfo;
void setup() {
  pinMode(2, OUTPUT);
  analogWriteResolution(12);
  analogWriteFrequency(2,36621.09);
  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;
}
 
Status
Not open for further replies.
Back
Top