D2 PWM Frequency

Tried to use D2 pin as DAC output.

analogWriteMaxFrequency(D2) returns 234375. But find that PWM frequency only have 15Hz no matter setting in analogWrite(D2, 255, 500) function.

How can I set higher frequency? Is there timer conflict?

Thks

The only real DAC pins on the Photon (assuming that's the device you are using as you haven't stated it in your post) are A3/A6 (aka DAC :wink: ) - PWM and DAC are different things.

Are you using any of the other pins in the PWM group (e.g. A5) for analogWrite() at the same time?

BTW, 500Hz is the default anyway and with an analog value of 255 you won't get a lot of PWMing either.
Try 128 (~50% duty cycle) and check with an oscilloscope.

1 Like

Thks for prompt reply.

We are using Photon. Suppose D2/D3/A4/A5 are in the same group. We use D2 as PWM output while all other pins are I/O. Would A5 acting as SPI_CLK also affect D2 PWM performance?

Thks

I'd not think so.

But why cannot set higher pwm frequency? It always stay in 15Hz.

Thks

I’ll have to check myself.
What system version are you running?

We are using System version 0.7.0

Thks

May I have any test result in PWM frequency?

Thks a lot

How are you measuring this 15Hz? With 100% duty cycle (255), unless you changed the bit resolution, the output is flat 3.3V and identical to a digitalWrite(D2, HIGH).

As @ScruffR said, try with 50% duty cycle (128) and check again.

This is the output of the default 500Hz


1000Hz

10000Hz

10kHz (same as above other scale)

max frequency 65.535kHz (new scale again)

This was the code to test this with

void setup() {
    Particle.function("setPWM", setPWM);
    pinMode(D2, OUTPUT);
}

void loop() { }

int setPWM(String arg) {
    int pwm = constrain(arg.toInt(), 1, 65535);
    analogWrite(D2, 128, pwm);
    return pwm;
}
3 Likes