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.

# Configuration parameters
desired_led_current = "4mA"
vsupply = "54V"
vforward = LEDForwardVoltages.Red
tolerance = "5%" # typical 1% or 5%
higher_or_lower_resistor = next_higher_resistor

series_resistor = led_series_resistor(vsupply, desired_led_current, vforward)
print(f"Ideal LED series resistor for {vsupply} and {desired_led_current}: {format_value(series_resistor, 'Ω')}")

power = led_series_resistor_power(vsupply, desired_led_current, vforward)
print(f"Power dissipated by the ideal {format_value(series_resistor, 'Ω')} series resistor: {format_value(power, 'W')}")

# Get the next higher standard E24 resistor value
print()
standard_series_resistor = higher_or_lower_resistor(series_resistor, sequence=e24)
print(f"Next higher standard resistor value: {format_value(standard_series_resistor, 'Ω')}")

# Calculate the actual current through the LED & series resistor when selecting the next higher resistor
standard_current = led_series_resistor_current(vsupply, standard_series_resistor, vforward)
print(f"Current through the LED & series resistor: {format_value(standard_current, 'A')}")

# Calculate the actual power disspation with the "next highest" resistor
standard_power = power_dissipated_in_resistor_by_current(standard_series_resistor, desired_led_current)
print(f"Power dissipated by the {format_value(standard_series_resistor, 'Ω')} series resistor: {format_value(standard_power, 'W')}")

# 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 {format_value(standard_series_resistor, 'Ω')} series resistor: {format_value(max_current, 'A')}")

# Compute the power dissipation if the resistor is at the lower end (which will increase the current)
print()
lowest_resistor_value = resistor_tolerance(resistance=standard_series_resistor, tolerance=tolerance).lower
print(f"If the {format_value(standard_series_resistor, 'Ω')} resistor is at the lower end of its {tolerance} tolerance ({format_value(lowest_resistor_value, 'Ω')}):")
lowest_resistor_current = led_series_resistor_current(vsupply, lowest_resistor_value, vforward)

print(f"Current through the LED & series resistor: {format_value(lowest_resistor_current, 'A')}")
lowest_resistor_power = power_dissipated_in_resistor_by_current(lowest_resistor_value, desired_led_current)

print(f"Power dissipated by the {format_value(lowest_resistor_value, 'Ω')} series resistor: {format_value(lowest_resistor_power, 'W')}")

Example output

Ideal LED series resistor for 54V and 4mA: 13.1 kΩ
Power dissipated by the ideal 13.1 kΩ series resistor: 210 mW

Next higher standard resistor value: 15.0 kΩ
Current through the LED & series resistor: 3.49 mA
Power dissipated by the 15.0 kΩ series resistor: 240 mW
Maximum permissible current for the 15.0 kΩ series resistor: 4.08 mA

If the 15.0 kΩ resistor is at the lower end of its 5% tolerance (14.2 kΩ):
Current through the LED & series resistor: 3.68 mA
Power dissipated by the 14.2 kΩ series resistor: 228 mW