TFT LED control using D3 for PWM issue [solved]

I have been using WKP as the pin to drive the LEDs on an attached TFT. In order to use the WKP pin for waking the photon from deep sleep I decided to swap the pin use around and use D3 instead to drive the LED. The PWM is used to soft lite and dim the screen rather than just turning it on and off and to provide brightness control. However, since I have made this change I have noticed that the screen (LED) flickers. Is this some issue with D3 PWM or some other clash in the use of D3 as PWM output with something else - code or pin use? I am using 0.8.0-RC.4 on a photon.

That's difficult to answer without actually knowing how you use the pins.

These are the pin assignments and uses.

I understood A4 and D3 are paired for PWM timer use - I thought A4 is an input (MISO). So should be OK.

const uint16_t currPin = A0; analog input
const uint16_t pwrPin = A1; digital output
const uint16_t tftcs = A2; SPI
// A4 MISO SPI Input
const uint16_t dcrs = A6; SPI
const uint16_t tftLite = D3; PWM Output
const uint16_t sdcs = D4; SPI
const uint16_t button1 = A7; Digital Input
const uint16_t i2cSDA = D0; I2C
const uint16_t i2cSCL = D1; I2C
const uint16_t rst = D2; SPI
const uint16_t button2 = D5; Digital Input
const uint16_t button3 = D6; Digital Input
const uint16_t button4 = D7; Digital Input

I haven’t yet tried but will - setting the PWM frequency to something other than the default 500 HZ.

Under what circumstances does it flicker?
Does it flicker even with a constant duty cycle?
Does it flicker most at any particular duty cycle?
Does it also flicker when you use digitalWrite(D3, HIGH)?

I was noticing a lot of flicker (blicking) when I was testing the board with a Ammeter in line with the 5V supply to the PCB. I have just tried it again without the Ammeter and I am struggling to see any flicker - which is good news if that was the root cause. I hadn’t see this before with a previous revision of board. More testing required! Maybe there is a poor connection in the test harness to the ammeter?

To answer your questions; It was flickering, screen background when black in constant duty cycle - typically 120/255 is what I drive it at. I didn’t notice when dimming and lighting i.e. when going down to and up from 0/255. I have not tried digitalWrite(pin, HIGH);

Are you reapplying the same value or only change the ratio on demand?

In old versions there was an issue with the timer being changed immediately when you set the new value, which caused some flicker.
But this was fixed a long time ago, so that new values only get applied when the timer acutally expires.

The dimming is done like this. The aim is for a linear reduction with a curved end as it approaches zero. screenBrightness is a global variable to know whether the screen light is off or not. Param.bright is the parameter setting for screen brightness (H, M, L, Off). I guess from what you are saying I would be better off to increase the frequency from the default 500Hz to 2000Hz because the smallest change step delay I have is 1mS?

void softDim()
    if (screenBrightness > 0)
        int j = 4;
        for (int i = param.bright; i >= 0; i--)
            screenBrightness = i;
            analogWrite(tftLite, (byte) i);
            i < 20 ? j+=1 : j = 4;
            if (i < 10) j+=3; 
        screenBrightness = 0;

I use D3 on a Photon to control a Dimmer pin on LCD panel without any issues or flickering.

I assume it’s in your function code.

@RWB Thanks for commenting - the problem with flickering was down to the test harness power supply and not the photon pin, system or application firmware.