analogWrite with RX or TX on the Photon?

The Photon docs claim that RX and TX can be used as analogWrite pins. (http://docs.particle.io/photon/firmware/#i-o-analogwrite)

To test this, I wired up an LED (330ohm resistor in series, connected to the 3.3V rail). Here’s the sketch I ran:

int led_pin = RX;

void setup() {
    pinMode(led_pin, OUTPUT);
    pinMode(D7, OUTPUT);
}

void loop() {
    analogWrite(led_pin, 255);
    digitalWrite(D7, LOW);
    delay(1000);
    analogWrite(led_pin, 0); 
    digitalWrite(D7, HIGH);
    delay(1000);
}

Rather than blinking on and off in sync with the user led as expected, it seems to randomly switch between working “correctly” and only blinking on briefly (maybe 100-150ms, don’t have a scope to check) on the transitions between high and low. And sometimes it stays on and blinks off briefly on the transitions.

I’ve tried changing the delay from 1000 to 1300, 1500 and 2000. No change.

I tried TX instead of RX. No change.

I tried setting both TX and RX to OUTPUTs in setup() instead of just the pin I was analogWriting to. No change.

I’ve tried it both on direct USB power and a 3.7V lipo cell. No change.

I tried using digitalWrite instead of analog write (with HIGH and LOW) and it worked as expected.

Anybody seeing the same behavior or know what’s going on?

@paulmand3l, I think you missed this in the documentation:

NOTE: pinMode(pin, OUTPUT); is required before calling analogWrite(pin, value);

:smile:

It’s in the setup function. Or do I need to do it every time just before I call analogWrite?

@paulmand3l, DOH! I got misled by the led_pin thing :flushed:

I’ll have to run the code and scope the output to see what’s happening. What version of the system firmware are you running on your Photon?

Good question. How do I check?

@paulmand3l, I can confirm the problem with my scope. I set the intervals to 200ms and got this:

The spikes are there and it seems that the analogWrite() dosen’t “kick in” correctly. The spikes are exactly 2ms long and exactly the time for one 500Hz period of the PWM clock!

Perhaps @BDub can comment on this?

Bump? Anybody?

edit - Nevermind. Just saw the commit: https://github.com/spark/firmware/commit/b541fe402c7342f21e6b5f7357e9c4ef10872d59