Teensy 4.0 and leds

Matuso2

Member
Hi, it is probably stupid question, but it is first time i am using teensy 4.0 so I don't want to fry it.

In my project i will be using teensy 4.0 as microcontroller and I need to control 2 regular 5mm leds (red, blue). My question is, is there some way to control them directly from teensy, without external components like transistros or 74hc595 chip ? The teensy will be powered by 5V from external source.
 
Hi, it is probably stupid question, but it is first time i am using teensy 4.0 so I don't want to fry it.

In my project i will be using teensy 4.0 as microcontroller and I need to control 2 regular 5mm leds (red, blue). My question is, is there some way to control them directly from teensy, without external components like transistros or 74hc595 chip ? The teensy will be powered by 5V from external source.

It depends on the LEDs. If the LED can be driven by 3.3v power and it includes a resistor to regulate the power to the LED, then you can hook up the LED directly (LED plus to a data pin, LED minus to ground).

If they can be powered by 3.3v but don't include a resistor, then you likely need to include a resistor. To figure out the resistor needed, you need to go into the datasheet, and pick out the numbers that can be fed into one of the various calculators for the resistor needed. That's why I like using LEDs with built-in resistors.

If the LED can't be powered by 3.3v or it uses too much milli-amps, then you likely need a transistor control the LED and use 5 volt power.

I like to use the Adafruit Sequins because they do include a resistor, and I can fit them in a prototype board:

Sparkfun has a bag of 20 5mm LEDs with 4 different colors (blue, red, green, yellow -- 5x LEDs per color) that could also be used:

Alternatively, you could use SK6812 based LEDs. These LEDs are addressable, and each LED can be set to the 3 colors each at different power levels to make different colors. As long as you don't set the power level too high and you use the SK6812 variant instead of the older WS2812B variant, you can power a few pixels with 3.3v. Adafruit calls these 'neopixels' (the newer Adafruit neopixels now all use SK6812 LEDs). They do have a pack of 5 5mm LEDs, along with many other form factors (rings, strips, matrixes, etc.). Unfortunately the 5mm neopixels are sold out, but the 8mm neopixels are available:
 
Thanks, much better answer then I expected.

I'll probably look into SK6812 based LEDs, power them with separate source and use teensy just for control signal.
 
Thanks, much better answer then I expected.

I'll probably look into SK6812 based LEDs, power them with separate source and use teensy just for control signal.

Note, if you do that you may/may not have some issues. The Adafruit Neopixel Uberguide Best Practices covers a lot of cases. The guide is perhaps getting a little old, but it pulls together some of the issues. In general, the SK6812's seem to be more forgiving than the WS2812B's. Some of the things you may or may not need to do:

  • Be sure to connect the ground between the Teensy and your external power source. For just a few neopixels, I would think using the Teensy VIN pin for power is simpler than an external power supply. If you are doing hundreds of LEDs, then yes you likely need external power supplies.
  • Put a 300-500 Ohm resistor between the data pin and the first neopixel to prevent ringing in the neopixels;
  • Put a 500–1000 µF at 6.3V or higher Electrolytic capacitor near the first neopixel to even out the power supply;
  • Minimize the distance between the Teensy pin and the first neopixel;
  • If powering the pixels with a separate supply, apply power to the pixels before applying power to the microcontroller;
  • If you are using a 5 volt power supply, you may need a logic level voltage shifter such as a 74AHCT125 or 74HCT245. Note, many other voltage shifters are not fast enough for the neopixel protocol (which has a data pin at a fixed timing window and no clock signal). If you are using a battery which provides 4.2 volts maximum, you likely don't need the voltage shifter. And SK6812's tend to be more resistant than WS2812B's. That being said, it helps to test the actual LEDs that you have since there is sample variation;
  • Note if you are mixing different brands or makes of LEDs, the color variation can be apparent between the two. Even if you buy from the same maker (like Adafruit), over time, there may be changes (such as going from WS2812Bs to SK6812s). With the global supply chain issues, it likely will get worse over time. As I recall, the WS2812B pulse their lights at a different speed than SK6812s. IIRC, the SK6812's were friendlier to people sensitive to flashing lights (like people with epilepsy), but I don't recall the differences;
  • Also pay attention to the wiring differences. Adafruit sells lots of neopixels where you have to solder connections to the neopixel. But they also sell strips and such with plugs built-in. They have 2 different plugs they use and in one case, they used the same plug as other strips but you attached input to the male end instead of the female end. And the order of the 3 wires can be different (i.e. ground, power, data or ground, data, power);
  • I mostly use neopixels in cosplay setups. There due to vibrations during use, I frequently have to resolder connections each time I take out the prop. At one point I was buying neopixel rings with the plug already attached on the assumption their solder would hold better than what I do, but I've had to resolder those as well.

That being said, a lot of times, I don't follow the best practices for powering 32 or so neopixels. It mostly works, but i have seen times where it doesn't. I just built a rig that I can use to see if I need to use the extra steps. Within the rig, I can:

  • Select different power supplies;
  • Use two different voltage shifters, or don't use either shifter;
  • Add 2 different capacitors to the power (one Electrolytic, one a 0.1upf ceramic capacitor);
  • Add a 330 Ohm resistor.

While you indicated you were only doing 2 LEDs, if you want to jump into the flashy world of more LEDs, the Teensy has a board that allows you to setup 8 parallel streams of LEDs. This board has all of the necessary level shifters, resistors, etc. IIRC, on the Teensy 4.x, you can actually use more than 8 streams in parallel and use two separate boards:


Alternatively for just a few streams in parallel, you can use the WS2812Seriall library to run the neopixels via DMA, which is friendlier if you are playing music or running servos because it does not turn off interrupts while updating the neopixel chain.

Finally, if your processing requirements are much smaller, the Teensy LC has one pin (17/A3) that is logic level shifted to VIN on a different output pin in the board. That way, all you might need is a resistor and a capacitor.
 
Last edited:
Ok, thanks i'll take those into account, I was just wondering, if it's big deal if I power controller and LEDs at the same time, bacause as I said I'm planning to use just 2 LEDs and they will be powered from the same 5V source as teensy.
 
Ok, thanks i'll take those into account, I was just wondering, if it's big deal if I power controller and LEDs at the same time, bacause as I said I'm planning to use just 2 LEDs and they will be powered from the same 5V source as teensy.

It really depends on how powerful the LEDs are. For just 2 LEDs that don't have to be excessively bright, you are likely over-thinking things. The simplest thing is to get two LEDs with resistors attached, and hook them up to be powered off of 3.3v.

Or if you want to buy a bunch of LEDs without the resistors, just buy a bag of different resistors, and figure out the resistor to use for each LED. I hate doing that, which is why I tend to go for the LEDs with resistors built-in. But if you want to go old school, you can.

As things get more complicated (i.e. you use way more than 2 LEDs, you want to use more than a few neopixels, or the LEDs use a lot of power) then you will need more elaborate setups.

Be aware of feature creep. It can cause for endless cycles of I want to add just this one more thing....

So lets start with the basics. What are you trying to do? It is complicated to answer your question, since I have no idea how much you need to do, and things seem to shift. In general, LEDs can be fairly simple, but there are cases where they aren't. Yes, you can setup a 1,000 neopixels driven from a Teensy, and that takes some amount of engineering. But if you just want two LEDs to indicate the program is at a particular state, you don't need the amount of engineering you might need for 1,000 neopixels.
 
Back
Top