Dual LFO with variable duty cycle?


I wonder what the “simplest” way of getting a Photon to generate two independent LF square waves with variable duty cycle and frequency?

I think i would like something like 50Hz - 5kHz with a 5-80% duty cycle.

I can’t really wrap my head around what the Photon can and cannot do with its PWM outputs etc.

You might like to read this one :wink:


heh That seem pretty spot on… yes. But, if you do not mind me asking… i cannot find any pointer how to actually USE that contribution?

If you feel like it, you can clone the develop branch and build locally, or you wait a bit (hopefully not too long ;-)) till 0.4.8 gets released.

This will hopefully have this on board already.

Welllll given that I have been a Particle owner for about two days… I think “wait” is a wiser choise… :smile:


@janjoh, the low level timer/pwm control stuff is begging for a library which I really want to get to when I clear my plate. You may want to look at the SoftPWM library in the IDE. If you just want to manage two pins then you can reduce the max number of channels and play with the 66us (15KHz) timer interval to get your desired frequencies. One note of caution - don’t go below 10us for the timer interrupt intervals. :wink:

Thanks for SoftPWM-tip. But it does not appear to let me set Duty Cycle? Or am i missing tsomthing obvious?

@jan, I believe the SoftPWMSetPercent() function sets the duty cycle.

Gah, sorry, i mean “It does not appear to give me variable frequency” :slight_smile:

@jan, I believe it could but not necessarily as-is. I would look at the _isr_softcount variable in the ISR. It sets the PWM frequency to 15KHz (66us interrupts) / 255 = 60Hz. So, it is possible to lower the count to increase the frequency to 5KHz (count to 3 instead of 255). To get 50-5000hz, you could set the base timer period to 80us. That coupled with a _isr_softcount set for 250 would give you 50Hz. A count of 2 would give you a 6250Hz.

For better accuracy, you can make _isr_softcount a uint16_t instead of uint8_t to give you more granularity on the count. With that and a timer period of 40us (25KHz), a count of 500 would give you 50Hz and a count of 5 would give you 5000Hz.

As it stands, there is no way to change the frequency but it would be easy to add a function to do that with. Test the above to see if the results are as expected and I can the the function after that. :smile: