Bogus PWM output, doesn't look consistent

I’m using analogWrite on my P1 to generate PWM on two different lines, A4 and A7 (WKP). The output signal on A7 looks normal and as expected, but the output on A4 has a very weird and inconsistent period (see pic). This pic shows a PWM of 50/255. Why is this not consistent? Any thoughts? Thanks!

What system version are you using?
How often do you set the PWM rate (show code please)?
Are you using D3 in any way during that test?
Can you provide a graph showing both channels parallel?

1 Like

ScruffR, thanks for the reply, much appreciated! I’m actually out of the office and can’t send any scope shots right now, but it’s literally the above PWM signal at one time and then a completely normal PWM signal at another time. The code is below and is a very simple routine which is a Cloud.function and is only executed once in a while when the user presses a button on the associated smart phone app. It just goes between one of three states: both sides driven high (off state), right side PWM on, left side PWM on. That’s it, nothing complicated.

It’s running the latest 0.4.7 system code. No, not using D3 at all, it’s just disconnected. Any ideas? I’m using it to control a ceramic heater element, and with the current PWM output, it doesn’t have a consistent period, which means that it’s not going to allow me to accurately predict the analog average output, as is. Thanks!

int setHeat(String str)
{
    static int side = 0;    // 0 is neither on, 1 is right, 2 is left
    Serial1.println(str);

    switch (side){
        case 0:
            digitalWrite(RIGHT, 1); // turn OFF (with a 1) right side
            digitalWrite(LEFT, 1); // turn OFF (with a 1) left side
            break;
        case 1:
            digitalWrite(LEFT, 1); // turn OFF (with a 1) left side
            analogWrite(RIGHT, 50);    // 50% duty cycle
            break;
        case 2:
            digitalWrite(RIGHT, 1); // turn OFF (with a 1) right side
            analogWrite(LEFT, 200);    // 50% duty cycle
            break;
        default:
            break;
    }
    
    side++;
    if (side == 3)
        side = 0;
}

I’ll have a try with your code, but one thing you should always do.

Since your function is declared int setHeat() you have to return an int.
Just add return side; as last statement in your function.


I’ve just flashed a sketch incorporating your fn with my additions and it works just as expected.
Could you show your complete sketch please.

BTW: Neither 50 nor 200 denote 50% duty cycle (as you suggest in your comments)