Advanced LED series resistor value & power disspation calculation using Python

The following script uses UliEngineering (version 0.4.20+ required) to compute the series resistor value and power dissipation for a series resistor and LED circuit.

It will also select the next standard resistor for you and repeat the calculation with that resistor value.

Furthermore, it takes into account the resistor’s tolerance for both the LED current and the resistor’s power dissipation and the LED forward voltage range, so you will never exceed a safe power dissipation rating even with component variation.

led_series_calc.py
# Configuration parameters
from UliEngineering.Electronics.LED import *
from UliEngineering.Electronics.Resistors import *
from UliEngineering.EngineerIO import *

### BEGIN OF CONFIGURATION ###
desired_led_current = "8mA"
vsupply = "3.3V"
vforward_min = "1.8V"
vforward_max = "2.4V"
# Typical forward voltage is often the average of min and max
# (or you can enter a specific typical value if you have one)
vforward_min_val = normalize_numeric(vforward_min)
vforward_max_val = normalize_numeric(vforward_max)
vforward_typ_val = (vforward_min_val + vforward_max_val) / 2
vforward_typ = f"{vforward_typ_val}V"
tolerance = "1%" # typical 1% or 5%
# Choose whether to select the next higher or lower standard resistor value
# ("higher" yields less current, "lower" yields more current)
higher_or_lower_resistor = next_higher_resistor
### END OF CONFIGURATION ###

print(f"LED Series Resistor calculation for a LED with Vf min..typ..max: {vforward_min}..{vforward_typ}..{vforward_max}")
print()

# Helper: list of forward voltages (min, typ, max)
vforward_vals = [vforward_min_val, vforward_typ_val, vforward_max_val]

# Compute ideal series resistors for min, typ and max forward voltages
series_resistors = [led_series_resistor(vsupply, desired_led_current, vf) for vf in vforward_vals]
# Pre-format resistor values for concise printing
series_resistors_fmt = [format_value(r, 'Ω') for r in series_resistors]
print(f"Ideal LED series resistor for {vsupply} and {desired_led_current}: {series_resistors_fmt[0]}..{series_resistors_fmt[1]}..{series_resistors_fmt[2]}")

# Power dissipated by the ideal resistor for each forward voltage
powers_ideal = [led_series_resistor_power(vsupply, desired_led_current, vf) for vf in vforward_vals]
# Pre-format power values for concise printing
powers_ideal_fmt = [format_value(p, 'W') for p in powers_ideal]
print(f"Power dissipated by the ideal series resistor: {powers_ideal_fmt[0]}..{powers_ideal_fmt[1]}..{powers_ideal_fmt[2]}")

# Get the next higher standard E24 resistor value
print()
# Choose standard resistor based on the TYPICAL (middle) ideal value
standard_series_resistor = higher_or_lower_resistor(series_resistors[1], sequence=e24)
standard_series_resistor_fmt = format_value(standard_series_resistor, 'Ω')
print(f"Next standard resistor value: {standard_series_resistor_fmt}")

# Calculate the actual current through the LED & series resistor for min..typ..max when selecting the next standard resistor
standard_currents = [led_series_resistor_current(vsupply, standard_series_resistor, vf) for vf in vforward_vals]
# Pre-format currents for concise printing
standard_currents_fmt = [format_value(I, 'A') for I in standard_currents]
print(f"Current through the LED & series resistor: {standard_currents_fmt[0]}..{standard_currents_fmt[1]}..{standard_currents_fmt[2]}")

# Calculate the actual power dissipation in the selected standard resistor for each forward voltage
standard_powers = [power_dissipated_in_resistor_by_current(standard_series_resistor, I) for I in standard_currents]
# Pre-format powers for concise printing
standard_powers_fmt = [format_value(p, 'W') for p in standard_powers]
print(f"Power dissipated by the {standard_series_resistor_fmt} series resistor: {standard_powers_fmt[0]}..{standard_powers_fmt[1]}..{standard_powers_fmt[2]}")

# Calculate how much maximum current would be permissible for the series resistor
max_current = led_series_resistor_maximum_current(resistance=standard_series_resistor, power_rating="250mW")
print(f"Maximum permissible current for the {standard_series_resistor_fmt} series resistor: {format_value(max_current, 'A')}")

# Compute the power dissipation if the resistor is at the lower or upper end of its tolerance
print()
# Get the tolerance bounds
tolerance_bounds = resistor_tolerance(resistance=standard_series_resistor, tolerance=tolerance)
lowest_resistor_value = tolerance_bounds.lower
highest_resistor_value = tolerance_bounds.upper
lowest_resistor_value_fmt = format_value(lowest_resistor_value, 'Ω')
highest_resistor_value_fmt = format_value(highest_resistor_value, 'Ω')

# Lower-end (smaller resistance -> higher current)
print(f"If the {standard_series_resistor_fmt} resistor is at the lower end of its {tolerance} tolerance ({lowest_resistor_value_fmt}):")
lowest_resistor_currents = [led_series_resistor_current(vsupply, lowest_resistor_value, vf) for vf in vforward_vals]
lowest_resistor_currents_fmt = [format_value(I, 'A') for I in lowest_resistor_currents]
print(f"Current through the LED & {lowest_resistor_value_fmt} series resistor: {lowest_resistor_currents_fmt[0]}..{lowest_resistor_currents_fmt[1]}..{lowest_resistor_currents_fmt[2]}")
lowest_resistor_powers = [power_dissipated_in_resistor_by_current(lowest_resistor_value, I) for I in lowest_resistor_currents]
lowest_resistor_powers_fmt = [format_value(p, 'W') for p in lowest_resistor_powers]
print(f"Power dissipated by the {lowest_resistor_value_fmt} series resistor: {lowest_resistor_powers_fmt[0]}..{lowest_resistor_powers_fmt[1]}..{lowest_resistor_powers_fmt[2]}")

# Upper-end (larger resistance -> lower current)
print()
print(f"If the {standard_series_resistor_fmt} resistor is at the upper end of its {tolerance} tolerance ({highest_resistor_value_fmt}):")
highest_resistor_currents = [led_series_resistor_current(vsupply, highest_resistor_value, vf) for vf in vforward_vals]
highest_resistor_currents_fmt = [format_value(I, 'A') for I in highest_resistor_currents]
print(f"Current through the LED & {highest_resistor_value_fmt} series resistor: {highest_resistor_currents_fmt[0]}..{highest_resistor_currents_fmt[1]}..{highest_resistor_currents_fmt[2]}")
highest_resistor_powers = [power_dissipated_in_resistor_by_current(highest_resistor_value, I) for I in highest_resistor_currents]
highest_resistor_powers_fmt = [format_value(p, 'W') for p in highest_resistor_powers]
print(f"Power dissipated by the {highest_resistor_value_fmt} series resistor: {highest_resistor_powers_fmt[0]}..{highest_resistor_powers_fmt[1]}..{highest_resistor_powers_fmt[2]}")

Example output

example.txt
LED Series Resistor calculation for a LED with Vf min..typ..max: 1.8V..2.1V..2.4V

Ideal LED series resistor for 3.3V and 8mA: 187 Ω..150 Ω..112 Ω
Power dissipated by the ideal series resistor: 12.0 mW..9.60 mW..7.20 mW

Next standard resistor value: 150 Ω
Current through the LED & series resistor: 10.00 mA..8.00 mA..6.00 mA
Power dissipated by the 150 Ω series resistor: 15.0 mW..9.60 mW..5.40 mW
Maximum permissible current for the 150 Ω series resistor: 40.8 mA

If the 150 Ω resistor is at the lower end of its 1% tolerance (148 Ω):
Current through the LED & 148 Ω series resistor: 10.1 mA..8.08 mA..6.06 mA
Power dissipated by the 148 Ω series resistor: 15.2 mW..9.70 mW..5.45 mW

If the 150 Ω resistor is at the upper end of its 1% tolerance (152 Ω):
Current through the LED & 152 Ω series resistor: 9.90 mA..7.92 mA..5.94 mA
Power dissipated by the 152 Ω series resistor: 14.9 mW..9.50 mW..5.35 mW

Check out similar posts by category: Electronics, Python