Shop OBEX P1 Docs P2 Docs Learn Events
Smartpin PWM tutorial — Parallax Forums

Smartpin PWM tutorial

Is there an application note or tutorial on using the PWM modes of the smartpins?


  • rosco_pcrosco_pc Posts: 450
    edited 2024-03-10 08:45

    Propeller 2 Smart Pin Supplementary Documentation by Jon Titus is a commenting-enabled Google Doc with examples and further explanation beyond the Silicon Documentation

  • evanhevanh Posts: 15,187
    edited 2024-03-10 09:31

    I guess you're after the details of what's different between the three smartpin modes. And since we're looking at differences we may as well include the NCO Duty mode too.

    00111_0 P_NCO_DUTY
    01000_0 P_PWM_TRIANGLE
    01001_0 P_PWM_SAWTOOTH
    01010_0 P_PWM_SMPS

    Sawtooth mode is your regular PWM. It produces a simple duty from 0% (flat low) to 100% (flat high). Output is high on leading portion of period.

    Triangle mode is a phase midpoint timed version of the sawtooth. It equally leads and lags any adjustment. Timings are doubled numerically. Compared to sawtooth, the period is doubled and output high is shifted to middle of period. Phase centring isn't usually needed in most applications.

    SwitchModePowerSupply mode has a set of inputs for gating when pulses occur. It's same PWM as sawtooth otherwise. Not well documented at all. I've used it once as a gated clock gen just by setting 50% duty.

    Duty mode is technically Pulse Density Modulation rather than Pulse Width Modulation. Frequency is product of duty, with 50% duty producing highest frequency. PDM has superior DAC performance compared to PWM. Useful to know for such applications. However, in power applications, PDM will destroy the power stage switching transistors. So know when to use it.

    PS: Jon Titus's diagrams of the triangle and sawtooth are excellent. He's got the polarity and phase correct. Thanks for the link Rosco.

  • JonnyMacJonnyMac Posts: 8,924
    edited 2024-03-10 12:43

    There is a section about using smart pin PWM in this article I wrote from Nuts & Volts when the P2 first came out.

    My basic PWM objects are pretty easy to follow and should help you make sense of things.

  • What exactly are you planning to do with the PWM? LEDs or something with higher power (motors)?

  • ke4pjwke4pjw Posts: 1,079

    Thanks everyone for the resources! @JonnyMac even though I am not driving an LED, I used your example from the Nuts and Volts article. I just changed the frequency to match what I am driving. It worked splendidly.

    Again, thanks!


  • JonnyMacJonnyMac Posts: 8,924
    edited 2024-03-12 16:50

    Excellent! As you can see, the PWM object is general-purpose -- I added the DMX-compatible start() method for LEDs because I do so much of that. Glad it the object worked for you!

Sign In or Register to comment.