One MCU port pin state(low, high, Hi-Z), 3 LEDs Design Challenge - Page 1
Forum section: electronics / projects

author: eblc1388
author_rank: Frequent Contributor
timestamp: May 06, 2020, 08:54:15 am
content: Because of convid-19, I'm bored staying at home. So I set myself a challenge. Do you like a challenge?Challenge: using common components, lights 3 separate LED according to MCU port Pin state logic high, logic low and Hi-Z(input)Rules:1. +5V supply for MCU and LEDs2. MCU does not have to service the LED states once port pin state changed3. Only resistor, capacitor, diode(all types), NPN/PNP, N-ch & P-ch MOSFET, JFET allowed4. All LEDs same type, drive current around 2mA5. Assume Component cost(units):a. resistor : 1b. capacitor, diode : 2c. LED, npn/pnp transistor, MOSFET : 3 (First 3 LEDs does not count, only for extra LEDs)d. JFET : 46. the least cost of BOM wins.The best I can do costs 14 units, if a short glitch between pin state changes is allowed, then its comes out to 12 units only.The simulation result is attached.I have attached my design in a zip file. Can you do any better? Do not peek at the solution unless you've given up.I used the following circuit(see image) to simulate the MCU port pin, going from logic 0 to logic 1 and then Hi-Z in the space of three seconds. The .asc file of this template is also attached.Please post the BOM cost, LED current results and your design .asc file if possible.Good luck.

author: Someone
author_rank: Super Contributor
timestamp: May 06, 2020, 12:06:58 pm
content: You can drop the diode if there is scope to carefully select the Vf of the LEDs

author: eblc1388
author_rank: Frequent Contributor
timestamp: May 06, 2020, 12:47:25 pm
content: Quote from: Someone on May 06, 2020, 12:06:58 pmThanks for taken up the challenge.Current in the third LED is only half that of the other two and there are still 0.2~0.5mA flowing which will light up the LED, with or without the diode.

author: MK14
author_rank: Super Contributor
timestamp: May 06, 2020, 05:46:49 pm
content: 3 LEDs, 2 resistors (current limit) + a bjt (2N3904).So, an extra BOM of ZERO! (my way of counting, which would be 3 Leds and 3 current limit resistors = 6 components, so my design swaps a transistor for one of the resistors , hence 0 extra components).But, your way of scoring, would be 0 (Leds) + 2 (resistors) + 3 (points for the NPN transistor) = a BOM of 5.Assumes you either use the same (White) LEDs you used in your answer (Vf around 3.6V to 4V), or 3.3V supply if lower Vf LEDs used, or extra components.Current NOT perfectly balanced between which of the 3 Leds are lit, but could optimise if worried/pedantic about it, by suitable adjustment of the resistor values.No diagram shown (to avoid spoiling it for others who want to try and solve this puzzle, and makes this post easier for me to prepare), but you can see the diagram, if you want, via the attachment, below.

author: Ian.M
author_rank: Super Contributor
timestamp: May 06, 2020, 08:15:54 pm
content: Off state LED current is a concern. Add a SPICE directive to your sim and paste this:Code:.measure L_ID1 find I(D1) at .5.measure L_ID2 find I(D2) at .5.measure L_ID3 find I(D3) at .5.measure H_ID1 find I(D1) at 1.5.measure H_ID2 find I(D2) at 1.5.measure H_ID3 find I(D3) at 1.5.measure T_ID1 find I(D1) at 2.5.measure T_ID2 find I(D2) at 2.5.measure T_ID3 find I(D3) at 2.5.error ; force SPICE errorlog displayinto it to get your first three diodes' If (in Amps) logged in each state. The leading letter of the measurement name is the pin state.My best so far is 1.6uA through the third LED when its off, with 1.8mA through each LED when on for a 10 unit 'cost'.Edit: I should be able to tweak that to 2mA on and <1uA off if I put a bit of time into it, without adding any parts.Code:l_id1: i(d1)=-1.68827e-008 at 0.5l_id2: i(d2)=0.0017939 at 0.5l_id3: i(d3)=1.60261e-006 at 0.5h_id1: i(d1)=0.00178581 at 1.5h_id2: i(d2)=-1.6924e-008 at 1.5h_id3: i(d3)=1.5699e-006 at 1.5t_id1: i(d1)=7.11125e-010 at 2.5t_id2: i(d2)=-6.81232e-010 at 2.5t_id3: i(d3)=0.00182746 at 2.5I'm using a somewhat improved MCU I/O pin model (30R on resistance at logic '1' and 20R when logic '0') that's also much easier to program as a single PWL source controls it (1 is High, -1 is Low and 0 is Tristate), and has the .measure directives so you may prefer my test template to eblc1388's one.

author: Ian.M
author_rank: Super Contributor
timestamp: May 06, 2020, 09:26:46 pm
content: MK14's results:Code:l_id1: i(d1)=-5.56678e-009 at 0.5l_id2: i(d2)=4.80122e-005 at 0.5l_id3: i(d3)=0.00111349 at 0.5h_id1: i(d1)=0.0010563 at 1.5h_id2: i(d2)=6.44745e-005 at 1.5h_id3: i(d3)=-4.80829e-011 at 1.5t_id1: i(d1)=1.67117e-005 at 2.5t_id2: i(d2)=0.000742218 at 2.5t_id3: i(d3)=1.66234e-005 at 2.5Low I(D2) is 48uA - not too great for the off state, and its on state Tristate I(D2) is only 0.74mA vs approx 1.1mA for the others.ebcl1388's results:Code:l_iled1: i(led1)=0.00206615 at 0.5l_iled2: i(led2)=2.52873e-009 at 0.5l_iled3: i(led3)=5.90605e-012 at 0.5h_iled1: i(led1)=-1.68777e-008 at 1.5h_iled2: i(led2)=0.00205739 at 1.5h_iled3: i(led3)=-1.03487e-011 at 1.5t_iled1: i(led1)=4.14614e-006 at 2.5t_iled2: i(led2)=4.39791e-009 at 2.5t_iled3: i(led3)=0.00189952 at 2.5Worst off-state current is Tristate I(LED1) of 4.1uAModified .measure script for LED1-LED3 rather than D1-D3:Code:.measure L_ILED1 find I(LED1) at .5.measure L_ILED2 find I(LED2) at .5.measure L_ILED3 find I(LED3) at .5.measure H_ILED1 find I(LED1) at 1.5.measure H_ILED2 find I(LED2) at 1.5.measure H_ILED3 find I(LED3) at 1.5.measure T_ILED1 find I(LED1) at 2.5.measure T_ILED2 find I(LED2) at 2.5.measure T_ILED3 find I(LED3) at 2.5.error ; force SPICE errorlog displayOf course the sane solution is a string of Neopixel LEDs for an 'eblc cost' of zero. You also get pretty RGB colours, which may make 1 Neopixed LED sufficient for all the indicators at an 'ebcl cost' of -3!

author: MK14
author_rank: Super Contributor
timestamp: May 06, 2020, 10:26:11 pm
content: Quote from: Ian.M on May 06, 2020, 09:26:46 pmYou are right, but..When I came up with the circuit design (tonight), it was intended for there to be red LEDs (or similar, somewhat sharp voltage cut-off LEDs).The datasheet, seems to show that the current (and hence brightness), falls sharply, from around a 0.5 volt drop (from around 2V full brightness 20 ma, down to less than 100 microamps (and less), at around 1.5V to 1.6V. According to the (typical) graphs. See page 2..https://www.vishay.com/docs/83171/tlur640.pdfMCUs, tend to be 3.3V (or lower, at least internally), rather than the previous 5V standard, these days. Which would be needed, to avoid the lower voltage red LEDS, from turning on (D1 & D3), because of the voltage across them in my circuit.tl;drI haven't tried it. But, Red (i.e. around 1.8V to 2V Vf LEDs, but Green or Yellow, should work as well, as long as NOT the later, higher voltage types) and 3.3V supply, should allow the diode drop of the transistor B/E junction, to NOT turn on led D2, too much to be annoyingly visible, when it is suppose to be off.At some lower led drive current, the led will/should appear to be 'off'. The datasheet doesn't seem to mention what current/voltage that is.Modern ones, tend to light up, even with tiny currents. But the (considerably, now obsolete) older ones, tended to (at least in some cases, especially with really cheap ones) turn/appear off, at lower currents. If I remember correctly.

author: Ian.M
author_rank: Super Contributor
timestamp: May 06, 2020, 10:40:37 pm
content: Your memory isn't far wrong. Somewhere I've still got a couple of very old red LEDs, that needed to be run at about 50mA, and were dimmer than a modern LED (not a high efficiency one) running at 0.5mA. Heck at 30mA you could still clearly see the shape of their die!You could try your sim with my usual generic LED models:Code:*Typ IR LED from optocoupler: Vf=1.2V @10mA.model LED0 D (IS=1p N=1.999644 RS=0 BV=6 IBV=10u+ CJO=10p EG=1.424 TT=500n)*Typ RED GaAs LED: Vf=1.7V Vr=4V If=40mA trr=3uS.MODEL LED1 D (IS=93.2P RS=42M N=3.73 BV=4 IBV=10U+ CJO=2.97P VJ=.75 M=.333 TT=4.32U)*Typ RED,GREEN,YELLOW,AMBER GaAs LED: Vf=2.1V Vr=4V If=40mA trr=3uS.MODEL LED2 D (IS=93.1P RS=42M N=4.61 BV=4 IBV=10U+ CJO=2.97P VJ=.75 M=.333 TT=4.32U)*Typ BLUE SiC LED: Vf=3.4V Vr=5V If=40mA trr=3uS.MODEL LED3 D (IS=93.1P RS=42M N=7.47 BV=5 IBV=30U+ CJO=2.97P VJ=.75 M=.333 TT=4.32U)and post the resulting currents.P.S. My template is a bit easier to change the supply voltage in than the original as it actually emulates a complimentary output stage powered from VCC. However you may want to tweak the switch on resistances to be more realistic for a 3.3V MCU.

author: MK14
author_rank: Super Contributor
timestamp: May 07, 2020, 12:23:18 am
content: Starting with your Red Led model.I seem to get about 1.3ma (on one Led), and around 1.7ma on the others.I'm happy with this (and want to minimise the component count), as I expect it would look ok, for indicator use leds.But, if anyone insists on it being closer to the OP's stated puzzle requirements.Then the resistors (gently lower the values) could be adjusted to be closer to 2ma's, and another resistor could be used/added, to make the third LED (unfortunately, the voltage drop across the transistor, messes up it being almost identical to the other Leds currents, but only by a modest amount), also closer to 2ma's.I'm surprised the Leds didn't use more (off state) current, but I guess it is down to the model (and exact Led specifications), as to why it worked out ok.Even at 5V (so I left it, and didn't drop it to 3.3V), the currents (off) seem to peak at around 20 microamps. Which I hope would either be visibly off or dim enough to be acceptable. Alternatively (as already discussed), use (e.g. old Leds) worse Leds, which no longer light at such low currents.Files/models used (only used the Red Led one (IanModelIRedLed.txt) so far):

author: eblc1388
author_rank: Frequent Contributor
timestamp: May 07, 2020, 01:13:20 am
content: Thanks to the inputs by Ian.M and MK14.I have tabulated the results of their design in the following table. As of present, both of them offer a better solution with respect to cost or performance. Ian.M's design performs better with lower cost of 10 units and lower residual current. MK14's design offers a surprisingly good performance using old type Red LED model, with as little as three components, excluding the LEDs.If I use 2N3904 or 2N2222 in MK14's design instead of the generic NPN, the simulation result is even better. The off state current decreases down to 12uA.My hat off to both.

author: Ian.M
author_rank: Super Contributor
timestamp: May 07, 2020, 04:14:13 am
content: As I said earlier my solution is tweakable:Code:l_id1: i(d1)=-1.68828e-008 at 0.5l_id2: i(d2)=0.00210069 at 0.5l_id3: i(d3)=9.20373e-007 at 0.5h_id1: i(d1)=0.00208944 at 1.5h_id2: i(d2)=-1.68424e-008 at 1.5h_id3: i(d3)=9.00493e-007 at 1.5t_id1: i(d1)=8.98131e-010 at 2.5t_id2: i(d2)=-8.52647e-010 at 2.5t_id3: i(d3)=0.00210052 at 2.5so that's an almost exact 2.1mA for all the LEDs will all off state currents under 1uA, just by fine tuning the (E24) resistor values, still at 10 'ebcl cost' units. Also due to my chosen topology, lower voltage LEDs (all same type) can be used without risk of 'shootthrough' increasing the off state current, simply retune the resistors to get back to the current performance.Interesting 'torture tests' for any design to check its real world practicality, are to check the effect of supply voltage variation e.g. +/-5% or +/-10% or to inject a +/-1uA leakage current when the I/O is tristate, or to override the LED models and inject a +/-0.1V Vf variation between LEDs, and see how badly our carefuly polished results degrade. Unfortunately setting up LTspice to Monte-Carlo passive tolerances isquite complicated, but I'm sure that would also be educational.

author: Someone
author_rank: Super Contributor
timestamp: May 07, 2020, 05:47:47 am
content: Quote from: eblc1388 on May 06, 2020, 12:47:25 pmDepends on the LED and, the diode, and if your models match the real world (they don't).In that configuration, with identical Vf LEDs, you end up with 1uA or less flowing in the bypassed junction(s). Compare your theoretical result with the actually measured 20nA. Even in a dark room its not visible unless you cover over/hide the other LEDs. Selecting a suitable Vf makes all the difference when you're specifying a fixed voltage supply.

author: Someone
author_rank: Super Contributor
timestamp: May 07, 2020, 05:56:03 am
content: Quote from: MK14 on May 07, 2020, 12:23:18 amStated requirements are a suitably vague "All LEDs same type, drive current around 2mA" I would have thought that less than a factor of 2 would be well within "around". Hence a proposed 1.3mA/2.8mA solution.Indicator LEDs are really non-critical for any more precision, just look at the binning ranges of typical LEDs used.

author: Ian.M
author_rank: Super Contributor
timestamp: May 07, 2020, 06:31:20 am
content: Quote from: Someone on May 07, 2020, 05:47:47 amLight leakage within the case from an adjacent LED that's on is far more likely to be objectionable, unless there's an opaque separator between them gasketed to the board. All-in-one multiple moulded light-pipes *SUCK* in this respect!QuoteYes. Vf vs Vcc can be rather critical - which is why I suggested sweeping the supply voltage, as most real-life 5V supplies wont be better than +/-5% and many at the cheap end of the market are significantly worse once you've factored in variation with load, and unit to unit.To do the Vcc sweep, mod my sim (or sims based on my template) to make the value of source Vsupply{VCC}and add the command.step param Vcc 4.5 5.5 0.02, (for 51 runs, enough to get reasonably smooth curves over a +/-10% Vcc range) then in the SPICE error log right-click and select 'plot .stepped .meas data'. You then have to add all the traces you want, one at a time from the right-click add trace dialog. You'll need multiple plot panes, one for On state, one for low leakage Off state, and probably another for Off state compromises, as its impossible to see the variations between traces three orders of magnitude down on the ones setting the plot scale. As its a PITA to set up, don't forget to save the plot settings of the resultingSimName.log.raw plot window so it remembers the selected traces and their panes next time you run the sim and re-select 'plot .stepped .meas data'.As you can see, my tweaked TriLED sim is reasonably good over a +/-5% Vcc range, but certainly wouldn't tolerate +/-10% variation. That's mostly down to my choice of MOSFETs as I initially selected ones with relatively high threshold voltages for no good reason so LED D3 is current starved at low Vcc s the MOSFETs only have Vcc/2 gate voltage. If I pick different MOSFETs, (e.g. PMOS Si1013, NMOS BSS123, chosen for low Vto in their SPICE models), it can tolerate over +/-10% Vcc variation, with a drastic improvement at the low end.

author: MK14
author_rank: Super Contributor
timestamp: May 07, 2020, 12:13:41 pm
content: Quote from: Someone on May 06, 2020, 12:06:58 pmI like your circuit. But, can we do better ?(Strictly speaking, it is against the 'all leds must be the same' rule).Just have 3 Leds and 2 resistors, ONLY. In your circuit.Make the third led a high(er) Vf one, such as white (and some/all blues). E.g. Vf around 3.6V.Leave out (short) the signal diode.Then use lower Vf leds (e.g. Vf 1.9V Red) for the other 2. Such as Red, Green, Yellow etc.EDIT:CORRECTION. You had already said similar/same in your description. I had mainly concentrated on the circuit diagram.