# Calculating the NCP380 Ilim resistor using Python

#### Problem

You want to calculate the correct value for the Ilim resistor for the NCP380 current limiter IC with a custom current limit. #### Solution

This script not only uses the formula from the datasheet to compute the theoretical resistor value but also performs range checks (the NCP380 only supports currents from 0.1 to 2.1 amperes) and computes the nearest E96 resistor value.

In order to run the script, you need to place `Resistors.py` in the same directory (see this previous post for details).

```#!/usr/bin/env python3
"""
A script that computes the NCP380 limit resistor ValueError

Based on Resistors.py, see
http://techoverflow.net/blog/2015/05/19/finding-the-nearest-e96-resistor-value-in-python/
"""
from UliEngineering.Electronics.Resistors import *
from UliEngineering.EngineerIO import format_value

__author__ = "Uli Koehler"
__version__ = "1.0"

"""Compute the exact value for the NCP380 adjust resistor,
given a Ilim value in amperes"""
#Check limits
if ilim < 0.100: print("Warning: NCP380 does not support currents below 100 Milliamperes")
elif ilim > 2.1: print("Warning: NCP380 does not support currents above 2.1 Amperes")
#Compute resistor according to equation 5 from the datasheet
rlim = -5.2959 * ilim**5 + 45.256 * ilim**4 - 155.25 * ilim**3 + 274.39 * ilim**2 - 267.6 * ilim + 134.21
return rlim * 1000.0 #equation gives us kiloohms

# Usage example: Compute E96 Rlim for Ilim=0.23A
if __name__ == "__main__":
# Compute theoretical value