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

Thread: First try - LED and PWM

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    10

    First try - LED and PWM

    This is my first time using the Teensy controller, so I thought I start simple by making two LEDs blink. Switching them on and off by alternating digital out to True and False worked fine - the LEDs shine brightly. Next step was to use PWM, and then I barely get any light from the LED, even when the pins are set to 100% duty cycle. The LEDs are directly connected between ground and pin 1 (and ground to pin 23 for the second) on my Teensy 4.1 board. Shouldn't brightness be the same when duty cycle is at 100% with PWM as when setting a digital pin high?

    I use Circuitpython to experiment with this, and code is included below:

    Code:
    import digitalio
    import board
    import time
    from ulab import numpy as np
    import pwmio
    
    def cycle(vs):
        v = vs[-1]
        vs[1:] = vs[:-1]
        vs[0] = v
        return vs
    
    pinlist = [board.D1, board.D23]
    m = len(pinlist)
    v = np.zeros((m), dtype=np.uint16)
    pwm = True
    if pwm:
        v[0] = 20
        v[1] = 255
    else:
        v[0] = 1
    
    def light_leds(pins, v):
        for k in range(m):
            if pwm:
                pins[k].duty_cycle = 2**16-1  # v[k]*v[k]
            else:
                pins[k].value = v[k] > 0
        return pins
    
    pins = []
    for k in range(m):
        if pwm:
            pin = pwmio.PWMOut(pinlist[k], frequency=1000, duty_cycle=0)
        else:
            pin = digitalio.DigitalInOut(pinlist[k])
            pin.direction = digitalio.Direction.OUTPUT
        pins.append(pin)
    
    while True:
        pins = light_leds(pins, v)
        v = cycle(v)
        time.sleep(0.5)

  2. #2
    Junior Member
    Join Date
    Oct 2021
    Posts
    10
    No ideas about the difference between 100% pwm and digital output?

  3. #3
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,663
    maybe start with simpler PWM test, just pwmio.PWMOut(board.D23, frequency=1000, duty_cycle=2**15)
    That works for me (verified with scope or logic analyzer). I don't believe D1 is a PWM pin on teensy 4, see pin out card
    https://www.pjrc.com/teensy/pinout.html
    EDIT ??: D1 is PWM on T4 but circuitpython doesn't seem to handle it correctly?

    You will damage teensy if you don't have a resistor (1K ?) in line with your LED. I see dim LED with
    pwmio.PWMOut(board.D3, frequency=1000, duty_cycle=6000)
    and bright LED with pwmio.PWMOut(board.D22, frequency=1000, duty_cycle=65000)

    Code:
    import board
    import digitalio
    import pwmio
    
    led = digitalio.DigitalInOut(board.D1)
    led.direction = digitalio.Direction.OUTPUT
    led.value = True
    
    pwmio.PWMOut(board.D22, frequency=1000, duty_cycle=65000)
    pwmio.PWMOut(board.D23, frequency=1000, duty_cycle=2**15)
    pwmio.PWMOut(board.D3, frequency=1000, duty_cycle=6000)
    Last edited by manitou; 10-06-2021 at 02:31 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2021
    Posts
    10
    Thanks for having a look at this!
    Quote Originally Posted by manitou View Post
    maybe start with simpler PWM test, just pwmio.PWMOut(board.D23, frequency=1000, duty_cycle=2**15)
    That works for me (verified with scope or logic analyzer). I don't believe D1 is a PWM pin on teensy 4, see pin out card
    https://www.pjrc.com/teensy/pinout.html
    EDIT ??: D1 is PWM on T4 but circuitpython doesn't seem to handle it correctly?
    I don't have a logic analyser at hand, so I thought I could use the LED to check that I get pulsed output. In the end I want to connect it to something else.

    You will damage teensy if you don't have a resistor (1K ?) in line with your LED. I see dim LED with
    pwmio.PWMOut(board.D3, frequency=1000, duty_cycle=6000)
    and bright LED with pwmio.PWMOut(board.D22, frequency=1000, duty_cycle=65000)
    I've tried both with a 220 Ohm resistor and without (since I thought the outputs were current limited). Both ways give bright light from my LED when using digital out, but nothing with PWM output. How do I know if I've damaged the Teensy? Is the behaviour I see an effect of damaged hardware? Is the digital out using different hardware in the chip that is current limited, and thus works when just connecting an LED without a resistor, but PWM is not, so it gets damaged?

    It was a good idea to try with a more simplified program. With your program I get the LED to shine with digital out, but not with PWMOut. Same as before for me.

  5. #5
    Senior Member
    Join Date
    Jul 2020
    Posts
    1,362
    The outputs are not designed to be continuously shorted even in current limited modes, so always use a resistor with an LED.
    The configuration of output currents is about reducing EMI, not driving low-impedance loads.

  6. #6
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,663
    As i recall, the teensy 4 data sheet says max GPIO current is 4 ma. The T4 schematic shows the onboard LED (pin 13) with a 470 ohm resistor.

  7. #7
    Junior Member
    Join Date
    Oct 2021
    Posts
    10
    Latest update: I get the PWM to work when using C and the Teensyduino IDE to program it. So, I didn't break anything on the board by not using a resistor (which is a relief, but I'll make sure I use a resistor in the future). Second conclusion is that I must be doing something wrong with Circuitpython if I don't get it to work there, but manitou does. What version of circuitpython did you use? I loaded 7.0.0.

  8. #8
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,663
    Quote Originally Posted by Erst View Post
    What version of circuitpython did you use? I loaded 7.0.0.
    i'm using v7, CircuitPython 7.0.0 on 2021-09-20; Teensy 4.1 with IMXRT1062DVJ6A
    i tested my simple sketch in post #3 on both T4.0 and T4.1. tested with scope and with LED and 470 ohm resistor

    As noted, I don't think circuitpython PWM works on Teensy 4 D0 or D1. Such issues might best be discussed on thread
    https://forum.pjrc.com/threads/59040...on-on-Teensy-4!

  9. #9
    Junior Member
    Join Date
    Oct 2021
    Posts
    10
    Quote Originally Posted by manitou View Post
    i'm using v7, CircuitPython 7.0.0 on 2021-09-20; Teensy 4.1 with IMXRT1062DVJ6A
    i tested my simple sketch in post #3 on both T4.0 and T4.1. tested with scope and with LED and 470 ohm resistor

    As noted, I don't think circuitpython PWM works on Teensy 4 D0 or D1. Such issues might best be discussed on thread
    https://forum.pjrc.com/threads/59040...on-on-Teensy-4!
    PWM with Circuitpython doesn't work for pins 10 to 19 (which apparently uses some other type of timer). But at least then it complains about not being able to use that pin for PWM.

    For pins 1, 2, 23 etc, it doesn't complain, but it also doesn't output anything for me. I'll ask in the thread you pointed to also.

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,216
    Here's the PWM documentation.

    https://www.pjrc.com/teensy/td_pulse.html

    Scroll down to "PWM Frequency" for a list showing which pins are controlled by which timers.

    As far as I know, Circuit Python only supports the FlexPWM timers.

  11. #11
    Junior Member
    Join Date
    Oct 2021
    Posts
    10
    Quote Originally Posted by PaulStoffregen View Post
    Here's the PWM documentation.

    https://www.pjrc.com/teensy/td_pulse.html

    Scroll down to "PWM Frequency" for a list showing which pins are controlled by which timers.

    As far as I know, Circuit Python only supports the FlexPWM timers.
    Thanks. Yes, I actually found that list already and the problem is still there when using FlexPWM pins. And since it works with C code trough the teensyduino ide, I think this is mostly a circuit python issue. Would be nice to get it working though…

Posting Permissions

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