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:
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)