How to achieve constant current for LED when voltage is variable in MCU
Yes, this is a problem. I have had to deal with it before with blue LED's on single cell lithium battery systems. In my case, I did have a regulated 3.3V available, but I wanted to drive the blue LED directly from VBATT without varying brightness. Anyway, here is how to fix (or improve) it.







simulate this circuit &ndash; Schematic created using CircuitLab

Voltage at base will be relatively fixed because of the two diodes. So the voltage at R3 will be relatively fixed. So the collector current will be relatively insensitive to VBATT. You can adjust R3 to adjust the current in the diode. R3 will have approximately 0.6V across it. So the current through D3 will be approximately 0.6/R3.

Note that Q1 is not operating as a saturated switch. This is a linear, analog circuit. D1 and D2 could be in a single SOT23 package to save space.