Karl Rixon

Javascript Resistor Calculation

with one comment

I recently wrote the following javascript class which can be used to calculate information about electronic resistors. There are various online apps which already do this sort of thing, but I thought I would have a go at creating something as it meant I would learn a bit about resistors in the process, and it may even help someone who is interested in looking at how these calculations work as well as getting the results.

The idea is, you create a Resistor object and provide some known information such as the colour bands, or the value, or the SMT code. The object will then calculate any missing information based on what you have provided. For instance, if you only have the resistor in front of you, you can set the object’s colour bands and it will calculate the resistor’s value, tolerance, temperature coefficient and SMT code.

Here is the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
function Resistor() {
   
    this.smtCode = null;
    this.value = 0;
    this.scaledUnit = null;
    this.scaledValue = 0;
    this.tolerance = 0;
    this.tempCoefficient = 0;
    this.bands = new Array();
   
    var that = this;
    var colorMap = {
        black:  {color: "black",  value: 0,    multiplier: 1,      tolerance: null, tempCoefficient: null},
        brown:  {color: "brown",  value: 1,    multiplier: 1e1,    tolerance: 1,    tempCoefficient: 100},
        red:    {color: "red",    value: 2,    multiplier: 1e2,    tolerance: 2,    tempCoefficient: 50},
        orange: {color: "orange", value: 3,    multiplier: 1e3,    tolerance: null, tempCoefficient: 15},
        yellow: {color: "yellow", value: 4,    multiplier: 1e4,    tolerance: null, tempCoefficient: 25},
        green:  {color: "green",  value: 5,    multiplier: 1e5,    tolerance: 0.5,  tempCoefficient: null},
        blue:   {color: "blue",   value: 6,    multiplier: 1e6,    tolerance: 0.25, tempCoefficient: null},
        violet: {color: "violet", value: 7,    multiplier: 1e7,    tolerance: 0.1,  tempCoefficient: null},
        gray:   {color: "gray",   value: 8,    multiplier: 1e8,    tolerance: 0.05, tempCoefficient: null},
        white:  {color: "white",  value: 9,    multiplier: 1e9,    tolerance: null, tempCoefficient: null},
        gold:   {color: "gold",   value: null, multiplier: 1e-1,   tolerance: 5,    tempCoefficient: null},
        silver: {color: "silver", value: null, multiplier: 1e-2,   tolerance: 10,   tempCoefficient: null},
        none:   {color: "none",   value: null, multiplier: 1,      tolerance: 20,   tempCoefficient: null}
    };
    var unitMap = new Array("pΩ", "nΩ", "µΩ", "mΩ", "Ω", "kΩ", "MΩ", "GΩ", "TΩ");

    this.setColorBands = function(colors) {
        for (index in colors) {
            if (typeof colorMap[colors[index]] != "undefined") {
                this.bands.push(colorMap[colors[index]]);
            }
        }
    }
   
    this.setSmtCode = function(code) {
        this.smtCode = String(code);
    }
   
    this.setValue = function(value) {
        this.value = Number(value);
        this.scaledUnit = calculateScaledUnit(this.value);
        this.scaledValue = calculateScaledValue(this.value);
    }
   
    this.calculate = function() {
        if (!this.value > 0) {
            this.setValue(calculateValue());
        }
        if (this.smtCode == null) {
            this.setSmtCode(calculateSmtCode(this.value));
        }
        if (!this.bands.length > 0) {
            this.setColorBands(calculateBands(this.value));
        }
        if (this.bands.length > 4) {
            this.tolerance = this.bands[4].tolerance;
            if (this.bands.length == 6) {
                this.tempCoefficient = this.bands[5].tempCoefficient;
            }
        }
    }
   
    this.toString = function() {
        var string = "Value: " + this.scaledValue + this.scaledUnit + " - ";
        string += "Colours: ";
        for (i in this.bands) {
            string += this.bands[i].color + ", ";
        }
        string = string.slice(0, -2) + " - ";
        string += "SMT Code: " + this.smtCode + " - ";
        string += "Tolerance: " + (this.tolerance == 0 ? "Unknown" : this.tolerance + "%") + " - ";
        string += "Temperature Coefficient: " + (this.tempCoefficient == 0 ? "Unknown" : this.tempCoefficient + "ppm");
        return string;
    }
   
    var calculateValue = function() {
        if (that.bands.length > 3) {
            return calculateValueViaBands(that.bands);
        }
        if (that.smtCode != null) {
            return calculateValueViaSmtCode(that.smtCode);
        }
        return false;
    }
   
    var calculateValueViaBands = function(bands) {
        var value = String(bands[0].value) + String(bands[1].value);
        var multiplier = bands[2].multiplier;
        if (bands.length > 4) {
            value += String(bands[2].value);
            multiplier = bands[3].multiplier;
        }
        return Number(value) * multiplier;
    }
   
    var calculateValueViaSmtCode = function(smtCode) {
        var value;
        if (smtCode.indexOf("R") > -1) {
            value = smtCode.replace(/R/i, ".");
        } else {
            var multiplier = 1;
            switch (smtCode.length) {
                case 2:
                    value = smtCode;
                    break;
                case 3:
                    value = smtCode.substring(0, 2);
                    multiplier = smtCode.substring(2);
                    break;
                case 4:
                    value = smtCode.substring(0, 3);
                    multiplier = smtCode.substring(3);
                    break
                default:
                    return false;
            }
            value = Number(value) * Math.pow(10, Number(multiplier));
        }
        return value;
    }
     
    var calculateBands = function(value) {
        var multiplier = Math.pow(10, String(value).length - 2);
        var values = String(value / multiplier).split("");
        var result = new Array();
        for (i in colorMap) {
            if (colorMap[i].multiplier == multiplier) {
                multiplier = i;
                break;
            }
        }
        for (i in values) {
            for (j in colorMap) {
                if (colorMap[j].value == Number(values[i])) {
                    result.push(j);
                }
            }
        }
        result.push(multiplier);
        return result;
    }
   
    var calculateSmtCode = function(value) {
        var indeces = calculateBands(value);
        var multiplier = indeces.pop();
        var lastDigit = (Math.log(colorMap[multiplier].multiplier) / Math.LN10);
        if (lastDigit > 9) {
            return false;
        }
        var code = "";
        for (i in indeces) {
            code += colorMap[indeces[i]].value;
        }
        return code + lastDigit;
    }
   
    var calculateScaledValue = function(value) {
        return value/Math.pow(1000, getExponent(value));
    }
   
    var calculateScaledUnit = function(value) {
        if (value == 0) {
            return "Ω";
        }
        var unit = unitMap[getExponent(value) + 4];
        if (typeof unit == "undefined") {
            return false;
        }
        return unit;
    }
   
    var getExponent = function(value) {
        return Math.floor(Math.log(value) / Math.log(1000));
    }
   
}

It should be pretty self explanatory, but here are a few simple very examples of usage (these are really just tests and will only work in firefox due to the console.log and String::toString methods):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var resistor = new Resistor();
resistor.setColorBands(["brown", "black", "black", "black", "brown", "yellow"]);
resistor.calculate();
console.log(resistor.toString());
// Value: 100Ω - Colours: brown, black, black, black, brown, yellow - SMT Code: 101 - Tolerance: 1% - Temperature Coefficient: 25ppm
       
var smtResistor = new Resistor();
smtResistor.setSmtCode("101");
smtResistor.calculate();
console.log(smtResistor.toString());
// Value: 100Ω - Colours: brown, black, brown - SMT Code: 101 - Tolerance: Unknown - Temperature Coefficient: Unknown
     
var knownResistor = new Resistor();
knownResistor.setValue(220);
knownResistor.calculate();
console.log(knownResistor.toString());
// Value: 220Ω - Colours: red, red, brown - SMT Code: 221 - Tolerance: Unknown - Temperature Coefficient: Unknown

Written by Karl

October 7th, 2009 at 8:53 pm

Posted in Javascript

One Response to 'Javascript Resistor Calculation'

Subscribe to comments with RSS or TrackBack to 'Javascript Resistor Calculation'.

  1. Its pretty good. Well done!

    Boomg

    27 Oct 09 at 2:13 am

Leave a Reply