First try - LED and PWM

Status
Not open for further replies.

Erst

Member
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)
 
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:
Thanks for having a look at this!
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.
 
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.
 
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.
 
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.
 
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-CircuitPython-on-Teensy-4!
 
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-CircuitPython-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.
 
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…
 
Status
Not open for further replies.
Back
Top