Resistor Color Codes Script

Published by Arun Isaac on

Tags: electronics, python, software, project

I just decided to write a Python script to convert resistor color codes to resistance values. Later, I also wrote a script to do the reverse - namely to find the color code of a given resistor.

I was feeling particularly jobless and decided to learn Python. I had fair experience with a number of other scripting languages and Python wasn't all that different or difficult to pick up. And, as a little exercise, I just decided to write a Python script to convert resistor color codes to resistance values. Later, I also wrote a script to do the reverse - namely to find the color code of a given resistor. And, obviously, I decided to share them. That's why we're here.

The first script, "col2res.py" converts given color code to the appropriate resistance value. The second script, "res2col.py" takes a resistance value as input from the user, finds the closest available standard resistor, and displays the corresponding color code. The script is written to have a fairly user-friendly command-line interface. The full details of the syntax will be display on executing the scripts with a "-h" option, that is, as shown below:

$ ./col2res.py -h
$ ./res2col.py -h

Example Usage

To find the resistance for color code [orange,orange,red]

$ ./col2res.py orange,orange,red

To find the resistance for color code [red,red,brown] with a gold tolerance ring

$ ./col2res.py red,red,brown -t gold

Mentioning the tolerance ring is optional. If mentioned at the command line, the tolerance percentage of the resistor is displayed. Else, it is as if no one has to say anything about tolerance.

To find the closest standard resistor for 13.9 kohms

$ ./res2col.py 13.9k

To find the closest standard resistor to 235 ohms with tolerance 1%

$ ./res2col.py 235 -t 1

Mentioning the tolerance is again optional. If tolerance is not mentioned, it is assumed you are looking for a 10% tolerance resistor. And, as the first example might have indicated, prefixes k, M, G are allowed for 10^3, 10^6, 10^9 respectively.

I have tested the scripts with a fair number of test cases. Still, the script may yet have bugs. In the end, we are all human anyway. So, if you happen to notice any inconsistency with the behaviour of the script or any buggy behaviour, I would greatly appreciate it if you could post it here as a comment to this article. Feedback is also most welcome, of course.

Downloads