Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Issue using PWM output on Teensy 4

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    7

    Issue using PWM output on Teensy 4

    I am attempting to control the brightness (via the LITE pin) on a 3.5" LCD TFT (HX8357 based) display with a PWM output from a Teensy 4. Previously, I was able to do this on a SparkFun Pro Micro using:

    #define TFT_CS 10
    #define TFT_DC 9
    #define TFT_RST 8
    //MOSI in on Teensy 4 Pin 11
    #define TFT_LITE 0

    TFT.begin();
    pinMode(TFT_LITE,OUTPUT);
    analogWrite(TFT_LITE,255);

    ...my TFT graphics code follows here.


    Attempting to do the same on the Teensy 4 seems to give me unpredictable results--occasional the screen partially redraws. If I remove anything involving the TFT_LITE (pin 0), then the code displays graphics as expected. If I use pinMode() and analogWrite() as shown, the graphics partially draw or don't draw at all or draw strangely.

    I suspect that this is because I am using a pin (0, in my case) for PWM output that is sharing a timer or other Teensy resource that is also being used by one of my other pins (8,9,10 or MOSI), but I was unable to find any documentation stating this....does anyone have another suggestion as to what might be causing this?

  2. #2
    Senior Member
    Join Date
    Feb 2016
    Location
    Australia
    Posts
    248
    Is it because teensy 4.0 is 3.3v?

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,327
    How much current does that LITE pin expect to work?

    Does this work?
    Code:
    pinMode(TFT_LITE,OUTPUT);
    digitalWrite(TFT_LITE,HIGH);
    And pinMode(,OUTPUT) isn't needed for analogWrite()

  4. #4
    Junior Member
    Join Date
    Jan 2020
    Posts
    7
    Gibbedy:
    According to the LCD docs, it should be fine with a supply from 3-5volts so I expect is would not be because the teensy 4 is 3.3v.

    defragster,
    I am not certain of the expected current of the LITE pin, but it does seem to work fine if it is held at 3.3v (full brightness) with no issues. It is only when attempting to use it with a PWM output from the Teensy that the problem appears. I tried your suggestion of digitalWrite(,HIGH) and it works fine. I've also tried it on several different PWM pins, all with similiar results.

    Here is another oddity....If I perform the PWM change of the LITE pin for the LCD *before* I draw my graphics such as:

    TFT.begin();
    analogWrite(TFT_LITE,62);
    DrawGraphics();

    ...the brightness change takes effect, but the graphics are draw with odd colors (if at all) as the problem persists. However, If I perform the PWM hange of the LITE pin for the LCD *after* I draw my graphics with.....

    TFT.begin();
    DrawGraphics();
    analogWrite(TFT_LITE,62);

    ...the problem doesn't appear--and the graphics appear normal.

    So I tried this:

    TFT.begin();
    analogWrite(TFT_LITE,62);
    DrawGraphics();
    analogWrite(TFT_LITE,62);

    This give the same problem. However, this

    TFT.begin();
    analogWrite(TFT_LITE,255);
    DrawGraphics();
    analogWrite(TFT_LITE,62);

    ...works fine.

    It appears that any attempt to alter the LITE with PWM using a value other thatn 0 or 255 somehow causes the LCD to incorrectly display its graphics, even if the value is reset afterwords. However, if I set the PWM to 0 or 255 *first*, then draw, then adjust the PWM afterwards it works.

    I may have to borrow a scope that can record all this so I can see if the LITE, CLK or MOSI pins are doing something strange that I can't detect on my tools.

  5. #5
    Junior Member
    Join Date
    Jan 2020
    Posts
    7
    Gibbedy:
    According to the LCD docs, it should be fine with a supply from 3-5volts so I expect is would not be because the teensy 4 is 3.3v.

    defragster,
    I am not certain of the expected current of the LITE pin, but it does seem to work fine if it is held at 3.3v (full brightness) with no issues. It is only when attempting to use it with a PWM output from the Teensy that the problem appears. I tried your suggestion of digitalWrite(,HIGH) and it works fine. I've also tried it on several different PWM pins, all with similiar results.

    Here is another oddity....If I perform the PWM change of the LITE pin for the LCD *before* I draw my graphics such as:

    TFT.begin();
    analogWrite(TFT_LITE,62);
    DrawGraphics();

    ...the brightness change takes effect, but the graphics are draw with odd colors (if at all) as the problem persists. However, If I perform the PWM hange of the LITE pin for the LCD *after* I draw my graphics with.....

    TFT.begin();
    DrawGraphics();
    analogWrite(TFT_LITE,62);

    ...the problem doesn't appear--and the graphics appear normal.

    So I tried this:

    TFT.begin();
    analogWrite(TFT_LITE,62);
    DrawGraphics();
    analogWrite(TFT_LITE,62);

    This give the same problem. However, this

    TFT.begin();
    analogWrite(TFT_LITE,255);
    DrawGraphics();
    analogWrite(TFT_LITE,62);

    ...works fine.

    It appears that any attempt to alter the LITE with PWM using a value other thatn 0 or 255 somehow causes the LCD to incorrectly display its graphics, even if the value is reset afterwords. However, if I set the PWM to 0 or 255 *first*, then draw, then adjust the PWM afterwards it works.

    I may have to borrow a scope that can record all this so I can see if the LITE, CLK or MOSI pins are doing something strange that I can't detect on my tools.

  6. #6
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,934
    In cases like this, it would be good if you could give a link to the display that you are using...

    I believe some displays have some built in support for the backlight, where they input pin to this then feeds an internal transistor or the like, as to not put much drain on the IO pin. Others do not... I know on some boards I played with I followed FrankB's lead and added transistors on the board for this... But again don't know if your display has an on display driver or not...

    If it were me and I would experiment some with the code, but again not sure what all is going on...
    If I do a quick and dirty program like:
    Code:
    void setup() {
      pinMode(0, OUTPUT);
      pinMode(13, OUTPUT);
    }
    uint16_t pwm_levels[] = {0, 64, 128, 196, 255};
    uint8_t pwm_level_index = 0; 
    void loop() {
    
      digitalWriteFast(13, !digitalReadFast(13));
      analogWrite(0, pwm_levels[pwm_level_index]);
      delay(100);
      pwm_level_index++;
      if (pwm_level_index == (sizeof(pwm_levels)/sizeof(pwm_levels[0]))) pwm_level_index = 0;
    }
    And look at output under logic Analyzer... I see that the PWM pin is going at duty of about 0, 25, 50, 75,99.6% with a frequency of about: 4.48khz...

    So PWM looks OK here. You might experiment with setting the analogWriteFrequency or resolution...

    But then I simply again wondering about the wiring and the like.
    For example is reasonable power being passed to the VCC pin of the display? Likewise decent ground. Might help to see picture of hook up. I would for sure double check and do things like measure the voltage going into the display. What are you using to power the display? One of the 3.3v pins? How much current does this display need?

    Again I ask questions like this, as for example with RA8875 like displays I did not have luck powering them with 3.3v, so instead I purchased them (or modified) to get their 5v version which have their own voltage regulator and as such not tax the VR on the Teensy.

  7. #7
    Junior Member
    Join Date
    Jan 2020
    Posts
    7
    [QUOTE=KurtE;226861]In cases like this, it would be good if you could give a link to the display that you are using...


    Good point, KurtE: The display I am using is this
    https://www.adafruit.com/product/2050


    I will experiment with some of the things you mentioned in your reply, thank you for the suggestions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •