Powering External LED Lights

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Powering External LED Lights

Post by mkmra2 »

Hi,

I'm wanting to power some external, simple white LED lights with the CPBluefruit. This seems easy enough, just connect them to 5V and GND. But what if I want to control them programmatically? That is turn them on and off using Circuit Python. These aren't NeoPixels. They are just cheap white LEDs. Is there a way to hook them up where I can control them with code?

Thanks!
Mark

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: Powering External LED Lights

Post by dastels »

This https://www.adafruit.com/product/4886 should do the job. It will connect to the CPB via I2C and drive 16 LEDs.

Dave

User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Re: Powering External LED Lights

Post by mkmra2 »

I see. So short of buying some extra hardware there's no control possible? I know, for example with an Arduino, you can have a pin that went high or low and could function like an on/off switch. Or you could use PWM and control the brightness. Nothing like that is possible with the CPB by itself?

Thanks!
Mark

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: Powering External LED Lights

Post by dastels »

You can certainly do that, but you're very limited in the number of connections: at most 8 if you don't need them for anything else. And they are all PWMable. Keep in mind that the CPB is a 3.3v device so you should be using 3.3v for the LEDs unless you use a transistor on each on as a switch. Each pin can handle up to 20mA which should be good, but there's usually a maximum combined current but I haven't found that yet.

Dave

User avatar
liiilmpumpik
 
Posts: 1
Joined: Wed Jan 26, 2022 9:37 am

Re: Powering External LED Lights

Post by liiilmpumpik »

I love this kind of discussion

User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Re: Powering External LED Lights

Post by mkmra2 »

Thanks, Dave. I got it to work by wiring the LED between GND and A2 and using some simple PWM code. Works nicely!

Mark

Code: Select all

import time
import board
import pwmio

# Connect the LED to A2 and to GND (You can use A2, A3, A4, A5, A6)
pin = board.A2
led = pwmio.PWMOut(pin, frequency=5000, duty_cycle=0)

# Maximum brightness (0 to 1)
maxBright = 0.5

# Make this value smaller to pulse slower (range 10 to 5000)
step = 1000

dutyMax = int(65000*maxBright)
dutyInc = int(step*maxBright)
while True:
    # PWM LED up and down
    for i in range(0, dutyMax, dutyInc):
        led.duty_cycle = i
        time.sleep(0.01)
    for i in range(dutyMax, 0, -dutyInc):
        led.duty_cycle = i
        time.sleep(0.01)

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: Powering External LED Lights

Post by dastels »

Cool. Since you're using white LEDs you can likely get away without a series resistor since their forward voltage is often 3v or a bit more.

Dave

User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Re: Powering External LED Lights

Post by mkmra2 »

I've been experimenting with this new LED. It's a 3V, 1W white LED. If I connect it to GND and 3.3V on the CPB it's REALLY bright.

When I use PWM it's much dimmer. And I'm puzzled how I can get it to maximum brightness. I thought if I used a 100% duty cycle it would be the max. (I'm using PWM so I can vary it).

Below is my simple test code. I was hoping this would make it as bright as directly connecting it to 3.3 and GND. Any thoughts anyone?

Thanks!
Mark

Code: Select all

import board
import pwmio

pin = board.A2
led = pwmio.PWMOut(pin, frequency=500, duty_cycle=2**16-1)

while True:
    pass

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: Powering External LED Lights

Post by dastels »

You have to consider current. a 1W 3v LED wants 1w/3v Amps = ~300mA. Each output can supply (as I mentioned) 20mA, so the LED will only get to about 16% brightness. Granted LED brightness isn't always linear.

You will need to drive it with a transistor. You will also need an external supply as you can only expect about 300mA from the 3.3v regulator on the CPB, which means 1 LED. If you want more LEDs you'll need more current. See https://learn.adafruit.com/transistors-101 for more about transistors including an example for driving LEDs (a 12v strip in this case, but the only difference for you is the voltage).

Dave

User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Re: Powering External LED Lights

Post by mkmra2 »

Thanks for the info and the link, Dave! I'll look into it this evening.

Mark

User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Re: Powering External LED Lights

Post by mkmra2 »

Hi, I read your transistor article and looked over the LED example. Very useful, thanks! I wired up my circuit the same way. I'm also using the TIP120 transistor (since I had one). I'm using a variable power supply to test with. I have it set to 3.3V (the minimum). I'm using the same 3V, 1W white LED. I have it hooked up like the photo below.
MMCircuit.jpg
MMCircuit.jpg (128.55 KiB) Viewed 193 times
The resistor is 10K. I wasn't quite sure how to compute the correct value but I tried to model it after your example. R=V/I so 3V/(0.3A / 1000) = 10K. The /1000 comes from the gain from the transistor. Not sure I have that right.

When I try it, it's still pretty dim (maybe the resistor is not the right one). But I can see the gentle pulses from the PWM. I can crank the voltage up and it gets very bright. At 5V it's blinding. But I don't see any pulse. Which seems weird to me - I thought it would continue to cycle, but just be brighter.

My questions are:
Is there a formula I can use based on the number of LEDs and the voltage and power they take, to compute the right resistance?
Is there an explanation for why the light intensity doesn't seem to pulse when it gets brighter?

Thanks for your help!
Mark

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: Powering External LED Lights

Post by dastels »

From the photos it looks like it has a luminescent material which I believe is excited by the light from the LED and glows. If so then at high LED brightness it might get bright enough that it doesn't dim significantly before the LED is bright again. Try increasing the length of the sleeps in the ramp up/down loops to see if that changes things.

Dave

User avatar
mkmra2
 
Posts: 36
Joined: Fri Jul 28, 2017 4:06 pm

Re: Powering External LED Lights

Post by mkmra2 »

To conclude this thread I'll post my findings...

I wanted to control some external LEDs without buying any additional hardware. But, having tried that, and having tried what Dave suggested at the beginning, purchasing the LED Driver Breakout was the best move! LOL

It's just so much easier.
https://www.adafruit.com/product/4886

Here's some sample code:

Code: Select all

"""
Demonstrates using the AW9523 GPIO Expander for controlling LEDs
https://www.adafruit.com/product/4886

This lets you attach up to 16 LEDs to your microcontroller. 

The code does a few simple ramping lighting effects. 
Mark Meier, 02/2022
"""
import time
import busio
import board
import adafruit_aw9523

i2c = busio.I2C(board.SCL, board.SDA)
aw = adafruit_aw9523.AW9523(i2c)
print("Found AW9523")

# Set all pins to outputs and LED (const current) mode
aw.LED_modes = 0xFFFF
aw.directions = 0xFFFF

# Specify the maximum current to use. This determines the brightness. 
max_current = 50 # 0 to 255, 0 is off, 255 is max

# The list of pins the LEDs are plugged into. This doesn't have to be sequential. 
PINS = [1, 2, 3, 4]

# Set all to the specified current (0 is off)
def set_all(current):
    for pin in range(len(PINS)):
        aw.set_constant_current(PINS[pin], current)

# Ramp all the LEDs up to max and down again, at the same time
# The value delay is the amount to wait after each step up in current 
def ramp_up_down(delay):
    for current in range(0, max_current):
        for pin in range(len(PINS)):
            aw.set_constant_current(PINS[pin], current)
        time.sleep(delay)
    for current in range(max_current, 0, -1):
        for pin in range(len(PINS)):
            aw.set_constant_current(PINS[pin], current)
        time.sleep(delay)

# Ramp each LED up to max, one after another, leaveing previous ones on
def ramp_each(delay): 
    for pin in range(len(PINS)): 
        for current in range(max_current): 
            aw.set_constant_current(PINS[pin], current)
            time.sleep(delay)

# Ramp each LED up to max, turning the previous one off 
# before moving to the next LED
def ramp_each_alone(delay, pause_after_on): 
    for pin in range(len(PINS)): 
        for current in range(max_current): 
            aw.set_constant_current(PINS[pin], current)
            time.sleep(delay)
        time.sleep(pause_after_on)
        aw.set_constant_current(PINS[pin], 0) # Turn off the last one

# How much to pause after every change in brightness (in seconds)
current_ramp_delay = 0.01    

# Keep looping
while True:
    ramp_up_down(0.05)
    set_all(0) # Reset all pins to off

    ramp_each(current_ramp_delay)
    set_all(0) # Reset all pins to off

    ramp_each_alone(current_ramp_delay, 0.5)
    set_all(0) # Reset all pins to off
Thanks for the great support.

Mark

User avatar
sipan1313
 
Posts: 18
Joined: Sun Jan 30, 2022 4:37 pm

Re: Powering External LED Lights

Post by sipan1313 »

Properly powering a light emitting diode (LED) or group of LEDs is different than powering most electronics. While most electronics require a
constant voltage source, LEDs require a constant current source. Most power supplies will provide a set voltage, such as 5V, and have a maximum current rating. The power supply will maintain this voltage and provide enough current to meet the demand of the circuit it is
powering. This means a dedicated power supply would need to be implemented to drive any LEDs in a circuit.

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”