Constant Current Circuit for Flexible Filament LEDs Using Cytron Maker Nano RP2040 With PWM Brightness Control
by kevinjwalters in Circuits > LEDs
4370 Views, 57 Favorites, 0 Comments
Constant Current Circuit for Flexible Filament LEDs Using Cytron Maker Nano RP2040 With PWM Brightness Control
This article shows how to power flexible "filament" LEDs and vary their brightness using a microcontroller. A simple transistor-based circuit is used to provide a constant current to drive each LED with a microcontroller providing pulse-width modulated control to vary the brightness. A six LED driver circuit is presented. This also includes a light-dependent resistor to measure the ambient light level.
A Cytron Maker Nano RP2040 was selected for this project as it has some useful extras and it's CircuitPython compatible. The CircuitPython test program gradually varies the brightness of the LEDs using the button to change the animation mode and the RGB pixels to indicate low supply voltage and extreme temperatures.
This article explores several simple approaches to LED current limiting and the use of pulse-width modulation (PWM) to vary the apparent brightness.
The quality and accuracy of a datasheet is one factor in the evaluation of a component. There appears to be no datasheets for this type of filament LED. The article looks at how to measure the forward voltage of the LEDs which is essential for some approaches to limiting the current.
This project uses stripboard and involves soldering. An LED driver breakout board is a simpler alternative solution and requires less soldering.
The flexible filament LEDs can be seen in use in Instructables: Making an Illuminated Wooden Name Plate Using Flexible Filament LEDs, a Microcontroller and a Constant Current Circuit.
Supplies
- Soldering iron and solder. Lead-based solder is easier to use but modern lead-free solder is okay too.
- Cytron Maker Nano RP2040: DigiKey | Cytron | PiHut
- 6x 3V ~300mm flexible filament LEDs: Adafruit nOOds | Pimoroni LED Noodles | DFRobot Flexible LED Filament
- Stripboard (Veroboard) circa 37x24 holes (a bit larger will allow easier mounting).
- Something to cut/drill away stripboard tracks.
- 2x 15 row female headers to act as socket for the microcontroller.
- A 10uF electrolytic capacitor.
- 12x NPN transistors. BC337 transistors were used for this project. Almost any will do as the current and power requirements per transistor are not particularly high with a 5V power supply.
- 6x 1k resistors.
- 3x 10k resistors.
- 6x "sense" resistors to set the current - the value needs to be calculated, between 6 and 14 ohms is likely. Low tolerance resistors are typically used in circuits like this for accuracy but this is less important here.
- A phototransistor like the LiteOn LTR-4206 or a light-depdendent resistor (LDR).
- Some wire (and wirestrippers) for the circuit and to join the LEDs.
- Heatshrink could be useful to reinforce the end parts of the filament LED near its terminals. This may also be useful in a design which needs some small sections of the filament LED to be dark.
Selecting a Controller Board
The requirements for this project are:
- Essential
- 6 PWM-capable GPIO pins.
- A button (or two) to offer the user some control over the software.
- Easy to connect to some stripboard (or maybe easy to connect to a decent size protoboard).
- CircuitPython/MicroPython compatible.
- Can easily supply around 300mA at around 5V to drive 6 filament LEDs simultaneously at 50mA each.
- Nice to have
- An analogue pin to measure ambient light.
- Some more analogue pins to check voltages on the circuit to monitor supply voltage and verify LED current.
- A popular connector to allow extra sensors to be added.
- A standard pinout allowing the microcontroller board to be replaced/upgraded in the future.
- A reset button in case the software misbehaves - easier than pulling the plug.
Some of the usual suspects (there will be many more):
- Arduino UNO Rev 3 - widely used, lots of good (and bad) clone boards available, but not CircuitPython compatible due to its basic microcontroller.
- Pi Pico WH - very good value for money especially for a Wi-Fi capable board, but no user buttons or connectors on the board, not even a reset button!
- Adafruit and third party Feather boards - standard pinout, lots of boards, not as cheap as Pi Pico W. Need to check PWM outputs per microcontroller.
- Micro:bit V2 - good value for money, lots of sensors, two buttons, can use more pins if display is disabled but one issue is the edge connector only supplies 3.3V, however there is no direct 5V from USB power.
- Cytron Maker Nano RP2040 - follows pinout of the 3.3V Arduino Nano, good balance of price vs features, includes a user button, a reset button, small speaker, two Maker ports (Qwiic/STEMMA QT compatible connectors), two RGB pixels and GPIO LEDs.
The Cytron Maker Nano RP2040 was selected for this project based on its size, price, 5V current capability, button and novelty for the author of the article. Its Maker ports are attractive for allowing the easy addition of extra sensors. The power limitations for this board and the others are explored in detail in Instructables: Powering Peripherals From a Microcontroller - Arduino UNO R3, Pi Pico W, Adafruit Feather NRF52840, BBC Micro:bit and Cytron Maker Nano RP2040.
LED Forward Voltage - Some Measurements
There is a universal, disappointing absence of datasheets for flexible filament LEDs sold in the hobbyist marketplace. Here's some vendor descriptions for red and green "3V" 260mm-300mm (~10-12") in length.
- Adafruit nOOds
- red and green:
- product page: "As the LEDs are in parallel, if you were to power them directly from 3V they can draw up to 200mA - however, like other LEDs it's not recommended to drive them in 'voltage' mode. We recommend current limiting with a resistor to let max 50mA through, so think of them as a If=50mA, Vf=3V LED and calculate the resistor that way."
- learn guide: "An inline current-limiting resistor is recommended. For 3V nOOds, try around 50 Ohms if the supply voltage is close to 3V, and 220Ω for a 5-ish Volt supply."
- Pimoroni LED Noodles
- red and green: "We've tested them up to 200mA but current limiting them to 50-100mA or so is sensible if you want to extend their lifespan."
- DFRobot Flexible LED Filament
- red: "supply voltage: 3V, operating current: <0.2A"
- green: "supply voltage: 3V, operating current:80mA"
An Adafruit Industries video notes the red ones have a different appearance with individual LEDs being visible when illuminated. The blue ones from this range are also different in having a colourless (clear) shroud. This could indicate that most of the filament LEDs are using phosphors with coloured shrouds to produce the desire colour (spectrum) and the others produce the desired colour directly. The forward voltage (Vf) could be different in this case and this would affect simple resistor calculations for current limiting.
Big Clive states the red ones are using blue LEDs and phosphor to produce the colour. The individual LEDs on the red filament are probably visible just because this combination is inefficient and they are dimmer than the rest.
Low current testing
The following are measurements of the forward voltage from an AstroAI DM6000AR multimeter, a Zoyi ZT-703S and a no-brand T7 Multi-function tester. None of these is calibrated and none states the current for the Vf measurement in diode mode but these do provide a useful relative comparison. The same connectors and filament LED orientation (LED polarity) were used for all of the T7 tests. All are 300mm (~12in) in total length with 285mm of glowing LEDs.
- Red 1: 2.370V, 2.369V, 2.44V.
- Red 2: 2.372V, 2.373V, 2.44V.
- Pink: 2.384V, 2.385V, 2.45V.
- Yellow: 2.404V, 2.405V, 2.47V.
- Lime Green 1: 2.379V, 2.380V, 2.45V.
- Lime Green 2: 2.384V, 2.386V, 2.45V.
- Blue 1: 2.379V, 2.380V, 2.45V.
- Blue 2: 2.382V, 2.383V, 2.45V.
- Warm white: 2.392V, 2.393V, 2.46V.
The results are all very similar which is reassuring and means there's no need for special-case current calculations for different colour filament LEDs. The T7 pulsed the LEDs on very briefly and appeared to be brighter suggesting the current level is higher for the forward voltage measurement offering an explanation for the slightly higher values.
The T7 tester turned out to be very useful for its other measurements. It showed the capacitance for the filament LEDs ranges between 3.81nF and 8.02nF with a fairly uniform distribution. This is far higher (~100x) than the tens of pF that's typical for a single 5mm LED and has implications for high frequency pulse-width modulation - this is discussed on subsequent pages.
The similar forward voltages suggest that all of the LEDs could be the same colour. Given the blue one appears clear this could mean all of them are blue LEDs and phosphor is used for non-blue colours.
High current testing
The motor driver for a USB-powered EDU PICO was enlisted as a voltage supply for some higher current testing with a pair of precision series resistors. These currents are too high being above the 50mA maximum cited for normal use. There were only applied for a few seconds to get the readings, the values below are for Red 1 filament LED using the ZT-703S at a room temperature of 23 degrees Celsius.
- R=10.04, Vs=4.508, Vr=1.8941. Calculated values: Vf=2.6139V at 188.7mA.
- R=19.94, Vs=4.605, Vr=2.0351. Calculates values: Vf=2.5699V at 102.1mA.
- R=29.98, Vs=4.649, Vr=2.1004. Calculated values: Vf=2.5486V at 70.06mA.
Vf=2.5V
For 50mA the forward voltage appears to be around 2.5V, for this 4.7V power source a (4.7 - 2.5) / 0.050 = 44.0 ohm resistor would be appropriate, 47 is the nearest E24 series resistor value. Nearest here would be determined by rounding up to avoid exceeding the maximum current.
Some guesswork is required without a datasheet for a safe value for continuous long-term use, perhaps 35mA? This would need a (4.7 - 2.5) / 0.035 = 62.8 ohm resistor, a low tolerance 62 ohm resistor would be very close or a 68 ohm could be used.
The Vf measurements here are also useful in confirming that a 3.3V supply is viable to drive these LEDs.
BC337 Transistor Gain (hFE)
The T7 Multi-function tester can also be used to check the health and gain of the pile of BC337-25 transistors that were used for this project. The datasheet has values for three bins - after manufacturing the transistors must be sorted by gain and placed into bin 16, 25 or 40. The sample tested is marked as bin 25 giving it an hFE (DC gain) between 100-400 for Vce=1V and Ic=100mA. The humble T7 is not testing at this current and isn't necessarily going to be very accurate with its measurement of 431 for this transistor. This is still a useful test as it's easy to fall into a habit of using a rule of thumb of 100 for gain and here the gain is around 400, 4x higher!
LED Current Limiting Circuits
There are many ways to limit the current to an LED. Several techniques are described below for a supply voltage of Vs and a desired (maximum) current of If. Five simple ones are shown above in Falstad Circuit Simulator marked A to E with four of them featuring on/off control. Signals from 3.3V GPIOs on a microcontroller could be used to control them.
The Vf (at 50mA) above in the simulator (shown above) is actually 2.2V rather than the intended 2.5V but this has minimal effect on the currents.
The circuit above represents the GPIO as 3.3V CLK with a 99 ohm output impedance.
All of the approaches below need to take into account the current. Up to around 50mA common components can be used, for higher currents more thought is required on power dissipation and thermal effects. The ambient and component temperature can affect the circuits - this may problematic, hazardous or occasionally useful.
For a high current LED module (say 500mA) with a supply voltage that is considerably higher than the LED's forward voltage (e.g. 12V and Vf=3V), a DC-DC converter with constant current output will be more efficient and may be a better solution.
A - Series Resistor
A resistor value is chosen using (Vs - Vf) / If and placed in series with the LED.
The simplicity is very attractive. This does depend on having a fixed, stable Vs to maintain an accurate If.
B - Transistor Switching with Series Resistor
This is very similar to the prior option but a bipolar transistor is added to turn the LED on and off. This is a NPN transistor with a high side load which in this case is the LED with its series resistor. There is a small voltage drop across the collector-emitter of the transistor which means the resistor will have a slightly lower value of (Vs - Vf - Vce) / If.
This option is shown because it enables pulse-width modulation of the LED which can be used to vary the apparent brightness. NPN transistor are often used because they are suitable for the 3.3V output of a microcontroller with a higher voltage for the switched load.
This still depends on having a fixed, stable Vs.
C - Transistor Current Control
This is similar to the prior option but the base resistance is now set to control the base current and this in turn controls the collector current limiting the current through the LED without a series resistor. A resistor plus variable resistor (trim potentiometer) is shown as one way to set this value.
The resistance value needs to take into account the DC gain (hFE) of the transistor which unfortunately varies considerably between transistors of the same type. This means each circuit driving an LED needs to be calibrated manually and the resistance then fixed. The base current is also dependent on Vbe voltage (of its p-n junction) which varies with (junction) temperature, approximately -2mV per degree Celsius. The junction temperature will be affected by ambient temperature and self-heating from power dissipation. In this circuit only the transistor is in series with the LED and there will be a large voltage across the transistor, hence greater power dissipation than with a resistor. As the transistor gets hotter more current will pass through it causing more heating. The is an unfortunate case of undesirable positive feedback.
D - Transistor Current Control with Emitter Resistor
A combination of a resistor placed at the emitter and a fixed voltage at the base of the transistor will give good current control at approximately (Vb - Vbe) / Re. This is less affected by hFE than the previous option. It still depends on the Vbe of the transistor passing the load current.
E - Vbe referenced Two Transistor Current Control
This is similar to the prior circuit but adds a second "feedback" transistor to steal base current from the first if the voltage across the emitter resistor exceeds the Vbe voltage. This negative feedback has the effect of limiting the current to Vbe / Rs where Vbe is for the second feedback transistor. This removes the need to deal with hFE variance and caters for varying Vbe in the first high-current LED-driving transistor. The circuit is still sensitive to ambient temperature because this will affect the Vbe of the second feedback transistor. It this case it's potentially useful as it will reduce the current slightly to the LED as the temperature increases.
Constant-current Diode
The constant-current diode (or regulator) is actually an n-channel JFET with a sense resistor.
Voltage Regulator as Current Source
Voltage regulators can easily be used as constant current sources. This is attractive because it uses a readily-available component carefully designed for the purpose and is still a simple circuit.
Transistor Current Control via Op-amp Feedback
This is similar to option E but an op-amp provides the feedback comparing the voltage across the sense resistor with a reference voltage. This can make a very precise current source with a stable reference voltage.
Use a Constant Current LED driver chip/board
There are many multi-channel chips available to drive LEDs. These often have open collector/drain outputs which allow a different, typically higher voltage rail to power the LEDs. A higher voltage rail for the LEDs allows serial chains of LEDs per output.
LED Current Limiting Circuits and Effect of Varying Supply Voltage
This is the same circuit as the previous page but shows the supply voltage ramping between 3.0 and 7.0V. This range covers
- 3.3V which is used for many microcontrollers,
- 3.3V to 4.2V which is the approximate useful range for a LiPo battery and
- 5.0V for 5V microcontrollers and USB.
This is useful to see how the current does or does not vary for each circuit.
The C and E circuits are very good at keeping a constant current regardless of the supply voltage to LEDs, the D circuit is good but works less well at low voltages.
This simulator is not modelling thermal effects like self-heating from power dissipation or ambient temperature variation.
Controlling the Filament LEDs With a Microcontroller
It might be tempting to try and drive the filament LEDs directly from the GPIO. A typical microcontroller will supply a few mA well and tend to struggle beyond 10mA. The Arduino UNO R3 is a little unusual with its ATmega328P having an absolute maximum rating of 40mA per pin. This is an absolute maximum and a lower value is going to be more reliable and stress the microcontroller less - this probably explains the Arduino UNO R3 recommendation of 20mA per pin. There are compatibility issues in the UNO range now with the R4 only having an 8mA maximum per pin. The RP2040 has a programmable per-pin drive strength up to 12mA.
Three of the previous circuits shown for current control of the LEDs included an input which could be driven from a 3.3V microcontroller to turn the LEDs fully on and off. The Vbe referenced Two Transistor Current Control (E) option was chosen to explore this circuit and due to owning a large pile of BC337 transistors.
All of the GPIO on the RP2040 microcontroller are capable of high resolution, high frequency pulse-width modulation (PWM) at a frequency where no flicker will be visible. This can be used to vary the brightness of the filament LEDs.
The RP2040 has four analogue inputs which could be used to check the voltage across the sense resistors. This isn't essential but if they aren't being used for anything else and have no potential future use then it's an interesting way to calculate and check the emitter current which will be close to the current flowing into the collector from the LED.
It would also be useful to measure the light falling near the project to be able to set the brightness of filament LEDs to a level suitable for the ambient light. A phototransistor or light-dependent resistor in a potential divider with a resistor is an easy way to do this. This would require one analogue input.
The power supply voltage is worth monitoring as high currents could cause a voltage drop. The 5V value is too high for the 0-3.3V range of the analogue to digital converter (ADC) but this can be lowered with a potential divider.
Measuring Vbe on Breadboard Prototype
The default bipolar NPN transistor in the Falstad Circuit Simulator in the circuit has a Vbe of 604mV. The T7 tester measured two BC337-25 as 647mV and 648mV (at 23 degrees Celsius). This value is important as it sets the current. The image above shows the voltage being measured as 622mV on a breadboard implementation of the circuit. This equates to a 6.224/10.04 = 62.0mA current, 12mA above the recommended maximum for Adafruit nOOds. The LEDs were only left fully on for a few seconds for this test.
The multimeter was also used to check the voltage at the Maker Nano RP2040's pin (not shown) and it was 4.70V. This is going to be due to a schottky diode between the 5V USB power and the pin.
PWM Vs Filament LED Capacitance in Simulator
The circuit above has the same constant current drivers replicated and driven by three PWM signals from GPIO, from bottom to top and left to right these are:
- 100Hz,
- 1907Hz (medium, this value is the 125MHz RP2040 clock speed divided by 65536) and
- 30517Hz (high).
For comparison, old CRT-based televisions would flicker at 50-60Hz, computer CRT-based monitors between 50-120Hz and fluorescent tubes used in lighting and early flat panel LCD (TFT) monitors at 100 or 120Hz.
The duty cycle has been set to 98% where the GPIO output voltage spends most of the time at 3.3V to verify that the brief dips to 0V are not affected by the high capacitance of the filament LEDs - the capacitance could power the LED and it may not turn off for high duty cycle percentages affecting the accuracy of the duty cycle for LED on/off time. This is modelled here by the addition of some 8nF capacitors representing the worst case from previous measurements.
The voltage line (in green) looks concerning as it ideally would rapidly fall to 0V but the measurements (on the left in small white text) indicate the medium frequency one is only falling to 1.482V and high frequency to 1.782V. The yellow lines shows the current which is far more representative of the light output and this fortunately does fall rapidly.
The screenshot on the right is a closer look at the medium frequency PWM. The current fall is down to about 10% of the maximum current within one minor division which is 200ns. This indicates the duty cycle will closely match the LED on/off time at this frequency.
PWM Vs Filament LED Capacitance on Breadboard
The oscilloscope is showing the voltage across the filament LED. A bench oscilloscope would not be able to measure this with a single basic probe as the probe's ground is connected to the transistor's collector/LED's cathode which is not at ground voltage. The oscilloscope is battery powered with no USB connection, hence there's no common (shared) ground between it and the circuit allowing it to measure this voltage.
For a 98% duty cycle (photograph on the left) the voltage fall from around 2.4V looks a tad under 200mV which is a much smaller fall than the simulator value. The second photograph shows a 20% duty cycle using a different time scale and the fall is only 330mV not much more than the 98% one. The simulator would be more useful with an accurate model for this particular LED.
The next step to investigate this would be to look at the light output either with a fast photodetector or one that can reliably integrate the light. For most applications this is unlikely to be important.
Circuit Implementation on Stripboard
A stripboard layout for the Cytron Maker Nano RP2040 is shown above together with the completed stripboard. The diagram is a top view with the copper strips shown as if the board was transparent.
There are two extra (solid core) wires on the underside of the board not shown in the diagram. These were added to monitor two of the transistors with the remaining two analogue inputs.
In general, it's worth checking the microcontrollers pins have the desired functionality with a small test program before doing the design and layout. The rules for GPIO capabilities can be complicated and vary wildly between different series of microcontrollers. The RP2040's very flexible PIO offers extra functionality for the GPIO.
In this case the RP2040 GP6 (D6) and GP7 (D7) have been avoided because they share PWM resources with GP22 which is connected to a small speaker on this board. It would be useful to allow that to be used for (PWM-driver) audio.
Some notes on the circuit and stripboard implementation follow.
- There are cuts below the microcontroller board in the tracks for all 15 pin rows.
- A 10uF electrolytic capacitor hides underneath the board to improve the stability of the 5V supply a tiny bit in the face of all of the LEDs turning on and off rapidly due to PWM.
- The top three rows are for a phototransistor sensor (a light-dependent resistor in the photograph) to sense ambient light. This is likely to need some sort of partial shroud to stop it picking up LED illumination.
- A potential divider using two 10k resistors allows the 5V pin to be monitored within the 0-3.3V range of the ADC inputs.
- There are six LED driver circuits. Each one has a 1k transistor limiting the current to the transistor base, a pair of NPN transistors and a 10 ohm and 27 ohm resistor (1% tolerance) in parallel to make a 7.30 ohm resistor. Here the resistor pair is based on component availability. Multiple (ideally equal) resistors can be used to lower the power per resistor for higher currents.
- The remaining two analogue inputs are connected on the underside to the base of two of the nearby transistors to allow the Vbe to be checked when the LED is on.
- The components that might get hot are placed at the right end of the board and spaced out. The board orientation is likely to have this end at the top which will prevent convection air currents heating the other components (in a ventilated deployment).
- Intentional solder bridges will join the three rows used for 5V power to the LEDs.
At a Vbe of 620mV the 7.3ohm resistance provides a current of 84.9mA per LED and 510mA for all six LEDs. This may not be suitable for long-term, continuous use but PWM mostly at 50% or less should make this more reasonable. A 13 ohm resistor would provide 47.7mA per LED, a pair of 27 ohm resistors in parallel would provide 45.9mA per LED.
If you want to put standoffs in the corners to mount the stripboard then it needs to be a little larger than the one used here.
Checking the Stripboard Soldering
Stripboard is particularly vulnerable to two implementation faults:
- soldering across the tracks and
- failing to cut a track completely.
The photograph above shows a difficult-to-see, unintentional small solder bridge between tracks. This tiny solder blob causes a resistance of around 100 milliohms between the two tracks. This sort of error could be very problematic and needs to be detected and corrected before use! There is also a piece of dust or a very thin copper fragment near the top right of the image - that needs to be swept away with an (anti-static) brush and then checked again.
The risk of solder bridges will be lower if component leads are perpendicular to the board.
Visual Inspection
For soldering by hand it's a good idea to periodically check the solder joints during the manufacturing process.
- A visual inspection with a magnifying glass with a separate bright light is useful. Changing the orientation of the board and/or light source can help check the soldering is good and has not gone beyond the intended area.
- A smartphone with a magnifier using the camera is an alternative option.
- A good quality photograph viewed on a large screen can also be useful.
Continuity / Resistance Measurement
A multimeter's resistance or continuity** mode is very useful to ensure
- all track cuts have infinite resistance,
- tracks connected with wires have very low resistance and
- parallel unconnected tracks really are still unconnected if soldering has happened nearby.
As components are added it becomes a little more complicated as some of these will conduct. A multimeter does apply small voltages to the circuit so this testing is best done without the microcontroller and any components where incorrect polarity voltages in awkward places may damage them.
** The continuity mode provides a very clear indication of a low resistance issue with a beep but on some multimeters for a resistance in the kiloohm or megaohm range there will be no beep and no indication - the AstroAI DM6000AR only beeps below 30 ohms.
Stripboard From Unknown Vendors
The stripboard came from a pack purchased from the cheapest seller on Ebay many years ago. It was used as a gift for practising soldering and only intended for that.
The long headers were unusually snug fitting in the holes suggesting something was amiss. The image of the underside of the board annotated with regularly-spaced white lines marking the top, bottom and centre of the copper tracks helps to show the problem. There is a tiny bit of geometric distortion from the camera mainly at the edges but the annotations help to show that the track spacing is good but the holes are vertically spaced too far apart and this gradually has the effect of making the holes go off-centre towards the top.
Ebay (and Amazon and other market places) may not be the best place to purchase good/reasonable quality electronic components from unknown sellers.
PWM Optimisation for Combined LED Current
For PWM with multiple LEDs below 100% duty cycle there is a question of when the on periods occur as this can affect the peak current draw. The example above, borrowed from Instructables: RGB LED Current Measurement With Nordic Power Profiler Kit II, shows two ways independent PWM channels driving three LEDs at 25% duty cycle could combine. The left image shows all three 15mA LEDs illuminated at the same time with a peak current of 45mA, the right image shows them neatly interlaced via phase offsets with a much lower peak current of 15mA.
The excellent RP2040 datasheet probably has details of how PWM are generated but if we assume the problematic case where they are all synchronised then changing the frequency slightly will keep the on periods unaligned most of the time.
BASE_FREQUENCY = 1907 # rounded 125MHz / 2**16
pwms = (pwmio.PWMOut(board.GP9, frequency=BASE_FREQUENCY), # pwm0
pwmio.PWMOut(board.GP8, frequency=BASE_FREQUENCY), # pwm1
pwmio.PWMOut(board.GP5, frequency=BASE_FREQUENCY + 79), # pwm2
pwmio.PWMOut(board.GP4, frequency=BASE_FREQUENCY + 79), # pwm3
pwmio.PWMOut(board.GP1, frequency=BASE_FREQUENCY + 127), # pwm4
pwmio.PWMOut(board.GP0, frequency=BASE_FREQUENCY + 127)) # pwm5
The reason the frequency is only changed every pair of pins is due to an RP2040 restriction. If you are only powering a few LEDs then it's best to use the following combinations.
- 1 LED: pwm2 (GP5)
- 2 LEDs: pwm2, pwm4 (GP5, GP1)
- 3 LEDs: pwm0, pwm2, pwm4 (GP9, GP5, GP1)
- More LEDs: same as previous option but use some of the remaining three.
The preference for pwm2 and pwm4 is due to the Vbe monitoring using analogue inputs.
Installing CircuitPython and Program on Cytron Maker Nano RP2040
If you are not familiar with CircuitPython then it's worth reading the Welcome to CircuitPython guide first.
- Install the latest version of CircuitPython (9.0.5 on July 2024) from https://circuitpython.org/ - this process is described for a similar RP2040-based board in Introducing Adafruit Feather RP2040: Installing CircuitPython.
- Verify the new installation by connecting to the serial console over USB. The REPL prompt shows the version number. The version can also be checked by inspecting the boot_out.txt file on the CIRCUITPY drive.
- Download the example program to CIRCUITPY by clicking Save link as... on filament-led-cycling.py.
- Rename or delete any existing code.py file on CIRCUITPY, then rename the filament-led-cycling.py to code.py. This file is run when the CircuitPython interpreter starts or reloads.
The versions used for this article were:
- CircuitPython: 9.0.5
Single Filament LED Test
This is an early test of the circuit when only one pair of transistors had been added. A red crocodile (alligator) clip is used to connect the filament LED's anode (marked with a tiny hole in the connector) to the 5V, a white one is used to connect the LED's cathode to the far transistor's collector lead.
Cheap crocodile cables often have a fairly high resistance of a few ohms and/or poor wire crimps which can cause a small voltage drop. In this case it's not too important as the constant current circuit makes this irrelevant unless the resistance is large and this is only a test to ensure the circuit functions.
It's harmless to get the filament LED the wrong way around - if it doesn't illuminate then reverse the LED before further troubleshooting.
Six Filament LED Test
This is a test showing the completed circuit with all six filament LEDs. The colours from left to right are red, green, yellow, warm white, blue, pink. The video goes through the five modes, mode changes are accomplished with a press of the Maker Nano RP2040's button.
- 00:05 Random fluctuations
- 00:31 Rotating bright one (left to right)
- 00:52 All pulsing with beeping
- 01:22 Simple Larson scanner
- 01:49 All are gradually turned on at full brightness
The video was shot in a room with daylight. The camera's colour balance was set to daylight.
A small inline meter plugged into the power bank (top left) shows the voltage at the power bank and current. This peaks at 0.54A when all six filament LEDs are on and approximately 0.09A is added if the speaker is used continuously. This appears accurate as peak total LED current would be 6 * 85 = 510mA and (RP2040 + other components/losses on the board) could be approximately 30mA.
The video shows red crocodile clips connecting to the stripboard tracks with 5V power. These need to be carefully positioned and not moved to avoid them going anywhere near the nearby ground (G) pin.
NPN Transistor As a Temperature Sensor
The Fairchild BC-337 datasheet doesn't include the temperature coefficient for Vbe (and Vce) but the one from the On Semiconductors is shown above. The -2mV per degree Celsius is commonly quoted for Vbe but the chart shows it does vary with collector current.
- At a collector current of 1.6mA (the feedback transistor) the temperature coefficient is -2.35mV per degree Celsius.
- At a collector current of 85mA (the LED driving transistor) the temperature coefficient is -1.85mV per degree Celsius.
This temperature sensitivity of the p-n junction is typically an inconvenience but it can be useful to measure temperature. The circuit includes two analogue inputs measuring Vbe on the two of the feedback transistors. When the LED is on these readings can be turned into an approximate indication of temperature. The feedback transistor is attractive for this measurement because the low current passing through it (1-2mA) will cause negligible self-heating and the temperature will closely match the ambient (room) temperature.
Going Further
Some ideas for areas to explore:
- Make some LED artwork with up to six 3V filament LEDs using this circuit.
- Enhance the program to (optionally) lower the brightness level in dark conditions.
- Check the supply voltage drop as the current increases on six LEDs.
- Enhance the program to scale the brightness for different colours to achieve a constant brightness across all of them. The red one appears to be the darkest one and maybe the green or warm white are the brightest.
- Explore different ways to map brightness level to PWM duty cycle and see how they look.
- Make a temperature gauge from the LEDs using the temperature derived from Vbe measurements.
- Explore varying PWM brightness to compensate for the small changes in Vbe and hence current with ambient temperature changes.
- Calculate the current/power limits for relevant transistor, resistor and supply voltage combinations.
- Check temperature of the controller board and LED driver stripboard with all LEDs on. A multimeter with a thermocouple or a thermal camera might be useful for this or a cautious finger.
- Power profile the LEDs to check the current/power usage and see how well the PWM distributes the on part of the duty cycles across time. Instructables: RGB LED Current Measurement With Nordic Power Profiler Kit II shows a similar investigation.
LED driver products:
- Adafruit 12-Channel 16-bit PWM LED Driver - SPI Interface - TLC59711 - the Texas Instrument TLC59711 is a versatile LED driver up to 60mA per channel.
- SparkFun LED Driver Breakout - TLC5940 (16 Channel) - this does not look well-designed for LEDs as it has 2.2k pull-up resistors on each output to make it suitable for use with servos. These surface mount resistors would have to be desoldered and removed to drive LEDs efficiently or to drive LEDs at more than a milliamp.
Similar projects:
- PWM
- mbedded.ninja blog: Controlling LED Brightness Using PWM - detailed discussion on the use of a (logic-level) n-channel MOSFET to drive an LED with PWM from a Nordic nRF52 series microcontroller.
- Constant current
- Instructables: High Power LED Driver Circuits - shows many constant current circuits, very similar to this article but using a logic-level MOSFET transistor to support high currents, note author uses a less-common symbol for an n-channel MOSFET. One of the circuits is built without a circuit board and encapsulated in Instructables: Power LED's - Simplest Light With Constant-current Circuit (the MOSFET will heat the feedback transistor in this implementation).
- Instructables: Battery Capacity Measurement Using Kitronik Inventor's Kit and Adafruit CLUE - this shows how to make a constant current circuit using feedback from a microcontroller using its ADC. The capacitor in the circuit does limit the ability to make rapid changes to the current level. Replacing the bipolar transistor with a (logic-level) MOSFET would allow a much smaller capacitor to be used to enable rapid modulation of the current level.
- Instructables: DIY Laser Diode Driver || Constant Current Source - a good example of (MOSFET) transistor current control via op-amp feedback with a mention of a second solution using a voltage regulator as a constant current source.
- Flexible "filament" LEDs:
- BrickNerd: DIY LEGO Lighting: Creating Custom Light Effects
- Adafruit Industries: SHOW and TELL 8/14/2024 - David VK4DN presents some artwork featuring PCBs and Adafruit n00ds.
- Random fun with LEDs:
- spidermaf: Turning a UFO garage kit into the Avrocar VZ-9AV fighter bomber with WEMOS D1 wled Neopixel leds (YouTube)
Further reading:
- DigiKey
- DigiKey: LED Basics - Electronics with Becky Stern | Digi-Key Electronics (YouTube)
- How To Pick The Best Microcontroller For Your Project by Becky Stern
- Fundamentals of Current Measurement
- Part 1 – Current Sense Resistors
- Part 2 – Current Sense Amplifiers
- Part 3 – Funnel Amplifiers
- Texas Instruments: Common LED Functions and LED Driver Design Considerations (pdf)
- SDG Electronics: SDG #020 Linear LED Constant Current Driver Circuits - a thorough and practical look at different ways to drive LEDs. Covers series resistor, LM317 voltage regulator used as constant current source, bipolar transistor pair (Vbe referenced), transistor with negative feedback from op-amp. Each circuit is shown (with neat handwriting!) and then built on a breadboard to observe and verify the behaviour.
- Robert Loos: Constant Current Sources - a detailed discussion of many bipolar transistor circuits with simulation results.
- Instructables: Practical Circuit Construction With Strip Board - useful, detailed guide on how to use stripboard.
- Onsemi: Advantages of Constant Current Regulators (CCR) in Driving LEDs (YouTube)
- bigclivedotcom: How long LED filaments are made (YouTube) - a look at the construction of flexible "filament" LEDs including the use of phosphors where red is produced by blue LEDs and how some sellers are selling partially-defective seconds.
- mikeselectricstuff: LED Spaghetti - flexible LED filaments (YouTube) - a look at the construction of long flexible "filament" LEDs.
- Adam Savage’s Tested: How To Use Flexible LED Filament Wire for Lighting Projects! (YouTube) - a good overview with some ideas on how to use them and design with them but very poor advice/demonstrations on powering, i.e. not current limiting and using LEDs in parallel and running ~300mm lengths at ~200mA.
- Wilkins, Arnold & Veitch, Jennifer & Lehman, Brad. (2010): LED Lighting Flicker and Potential Health Concerns: IEEE Standard PAR1789 Update (pdf)
- Element 14 Community Blog: Diodes and Bipolar Junction Transistors (BJTs) - Measuring the Temperature Coefficient.