forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resistor_color_code.py
374 lines (311 loc) · 12.3 KB
/
resistor_color_code.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Title : Calculating the resistance of a n band resistor using the color codes
Description :
Resistors resist the flow of electrical current.Each one has a value that tells how
strongly it resists current flow.This value's unit is the ohm, often noted with the
Greek letter omega: Ω.
The colored bands on a resistor can tell you everything you need to know about its
value and tolerance, as long as you understand how to read them. The order in which
the colors are arranged is very important, and each value of resistor has its own
unique combination.
The color coding for resistors is an international standard that is defined in IEC
60062.
The number of bands present in a resistor varies from three to six. These represent
significant figures, multiplier, tolerance, reliability, and temperature coefficient
Each color used for a type of band has a value assigned to it. It is read from left
to right.
All resistors will have significant figures and multiplier bands. In a three band
resistor first two bands from the left represent significant figures and the third
represents the multiplier band.
Significant figures - The number of significant figures band in a resistor can vary
from two to three.
Colors and values associated with significant figure bands -
(Black = 0, Brown = 1, Red = 2, Orange = 3, Yellow = 4, Green = 5, Blue = 6,
Violet = 7, Grey = 8, White = 9)
Multiplier - There will be one multiplier band in a resistor. It is multiplied with
the significant figures obtained from previous bands.
Colors and values associated with multiplier band -
(Black = 100, Brown = 10^1, Red = 10^2, Orange = 10^3, Yellow = 10^4, Green = 10^5,
Blue = 10^6, Violet = 10^7, Grey = 10^8, White = 10^9, Gold = 10^-1, Silver = 10^-2)
Note that multiplier bands use Gold and Silver which are not used for significant
figure bands.
Tolerance - The tolerance band is not always present. It can be seen in four band
resistors and above. This is a percentage by which the resistor value can vary.
Colors and values associated with tolerance band -
(Brown = 1%, Red = 2%, Orange = 0.05%, Yellow = 0.02%, Green = 0.5%,Blue = 0.25%,
Violet = 0.1%, Grey = 0.01%, Gold = 5%, Silver = 10%)
If no color is mentioned then by default tolerance is 20%
Note that tolerance band does not use Black and White colors.
Temperature Coeffecient - Indicates the change in resistance of the component as
a function of ambient temperature in terms of ppm/K.
It is present in six band resistors.
Colors and values associated with Temperature coeffecient -
(Black = 250 ppm/K, Brown = 100 ppm/K, Red = 50 ppm/K, Orange = 15 ppm/K,
Yellow = 25 ppm/K, Green = 20 ppm/K, Blue = 10 ppm/K, Violet = 5 ppm/K,
Grey = 1 ppm/K)
Note that temperature coeffecient band does not use White, Gold, Silver colors.
Sources :
https://www.calculator.net/resistor-calculator.html
https://learn.parallax.com/support/reference/resistor-color-codes
https://byjus.com/physics/resistor-colour-codes/
"""
valid_colors: list = [
"Black",
"Brown",
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"Violet",
"Grey",
"White",
"Gold",
"Silver",
]
significant_figures_color_values: dict[str, int] = {
"Black": 0,
"Brown": 1,
"Red": 2,
"Orange": 3,
"Yellow": 4,
"Green": 5,
"Blue": 6,
"Violet": 7,
"Grey": 8,
"White": 9,
}
multiplier_color_values: dict[str, float] = {
"Black": 10**0,
"Brown": 10**1,
"Red": 10**2,
"Orange": 10**3,
"Yellow": 10**4,
"Green": 10**5,
"Blue": 10**6,
"Violet": 10**7,
"Grey": 10**8,
"White": 10**9,
"Gold": 10**-1,
"Silver": 10**-2,
}
tolerance_color_values: dict[str, float] = {
"Brown": 1,
"Red": 2,
"Orange": 0.05,
"Yellow": 0.02,
"Green": 0.5,
"Blue": 0.25,
"Violet": 0.1,
"Grey": 0.01,
"Gold": 5,
"Silver": 10,
}
temperature_coeffecient_color_values: dict[str, int] = {
"Black": 250,
"Brown": 100,
"Red": 50,
"Orange": 15,
"Yellow": 25,
"Green": 20,
"Blue": 10,
"Violet": 5,
"Grey": 1,
}
band_types: dict[int, dict[str, int]] = {
3: {"significant": 2, "multiplier": 1},
4: {"significant": 2, "multiplier": 1, "tolerance": 1},
5: {"significant": 3, "multiplier": 1, "tolerance": 1},
6: {"significant": 3, "multiplier": 1, "tolerance": 1, "temp_coeffecient": 1},
}
def get_significant_digits(colors: list) -> str:
"""
Function returns the digit associated with the color. Function takes a
list containing colors as input and returns digits as string
>>> get_significant_digits(['Black','Blue'])
'06'
>>> get_significant_digits(['Aqua','Blue'])
Traceback (most recent call last):
...
ValueError: Aqua is not a valid color for significant figure bands
"""
digit = ""
for color in colors:
if color not in significant_figures_color_values:
msg = f"{color} is not a valid color for significant figure bands"
raise ValueError(msg)
digit = digit + str(significant_figures_color_values[color])
return str(digit)
def get_multiplier(color: str) -> float:
"""
Function returns the multiplier value associated with the color.
Function takes color as input and returns multiplier value
>>> get_multiplier('Gold')
0.1
>>> get_multiplier('Ivory')
Traceback (most recent call last):
...
ValueError: Ivory is not a valid color for multiplier band
"""
if color not in multiplier_color_values:
msg = f"{color} is not a valid color for multiplier band"
raise ValueError(msg)
return multiplier_color_values[color]
def get_tolerance(color: str) -> float:
"""
Function returns the tolerance value associated with the color.
Function takes color as input and returns tolerance value.
>>> get_tolerance('Green')
0.5
>>> get_tolerance('Indigo')
Traceback (most recent call last):
...
ValueError: Indigo is not a valid color for tolerance band
"""
if color not in tolerance_color_values:
msg = f"{color} is not a valid color for tolerance band"
raise ValueError(msg)
return tolerance_color_values[color]
def get_temperature_coeffecient(color: str) -> int:
"""
Function returns the temperature coeffecient value associated with the color.
Function takes color as input and returns temperature coeffecient value.
>>> get_temperature_coeffecient('Yellow')
25
>>> get_temperature_coeffecient('Cyan')
Traceback (most recent call last):
...
ValueError: Cyan is not a valid color for temperature coeffecient band
"""
if color not in temperature_coeffecient_color_values:
msg = f"{color} is not a valid color for temperature coeffecient band"
raise ValueError(msg)
return temperature_coeffecient_color_values[color]
def get_band_type_count(total_number_of_bands: int, type_of_band: str) -> int:
"""
Function returns the number of bands of a given type in a resistor with n bands
Function takes total_number_of_bands and type_of_band as input and returns
number of bands belonging to that type in the given resistor
>>> get_band_type_count(3,'significant')
2
>>> get_band_type_count(2,'significant')
Traceback (most recent call last):
...
ValueError: 2 is not a valid number of bands
>>> get_band_type_count(3,'sign')
Traceback (most recent call last):
...
ValueError: sign is not valid for a 3 band resistor
>>> get_band_type_count(3,'tolerance')
Traceback (most recent call last):
...
ValueError: tolerance is not valid for a 3 band resistor
>>> get_band_type_count(5,'temp_coeffecient')
Traceback (most recent call last):
...
ValueError: temp_coeffecient is not valid for a 5 band resistor
"""
if total_number_of_bands not in band_types:
msg = f"{total_number_of_bands} is not a valid number of bands"
raise ValueError(msg)
if type_of_band not in band_types[total_number_of_bands]:
msg = f"{type_of_band} is not valid for a {total_number_of_bands} band resistor"
raise ValueError(msg)
return band_types[total_number_of_bands][type_of_band]
def check_validity(number_of_bands: int, colors: list) -> bool:
"""
Function checks if the input provided is valid or not.
Function takes number_of_bands and colors as input and returns
True if it is valid
>>> check_validity(3, ["Black","Blue","Orange"])
True
>>> check_validity(4, ["Black","Blue","Orange"])
Traceback (most recent call last):
...
ValueError: Expecting 4 colors, provided 3 colors
>>> check_validity(3, ["Cyan","Red","Yellow"])
Traceback (most recent call last):
...
ValueError: Cyan is not a valid color
"""
if number_of_bands >= 3 and number_of_bands <= 6:
if number_of_bands == len(colors):
for color in colors:
if color not in valid_colors:
msg = f"{color} is not a valid color"
raise ValueError(msg)
return True
else:
msg = f"Expecting {number_of_bands} colors, provided {len(colors)} colors"
raise ValueError(msg)
else:
msg = "Invalid number of bands. Resistor bands must be 3 to 6"
raise ValueError(msg)
def calculate_resistance(number_of_bands: int, color_code_list: list) -> dict:
"""
Function calculates the total resistance of the resistor using the color codes.
Function takes number_of_bands, color_code_list as input and returns
resistance
>>> calculate_resistance(3, ["Black","Blue","Orange"])
{'resistance': '6000Ω ±20% '}
>>> calculate_resistance(4, ["Orange","Green","Blue","Gold"])
{'resistance': '35000000Ω ±5% '}
>>> calculate_resistance(5, ["Violet","Brown","Grey","Silver","Green"])
{'resistance': '7.18Ω ±0.5% '}
>>> calculate_resistance(6, ["Red","Green","Blue","Yellow","Orange","Grey"])
{'resistance': '2560000Ω ±0.05% 1 ppm/K'}
>>> calculate_resistance(0, ["Violet","Brown","Grey","Silver","Green"])
Traceback (most recent call last):
...
ValueError: Invalid number of bands. Resistor bands must be 3 to 6
>>> calculate_resistance(4, ["Violet","Brown","Grey","Silver","Green"])
Traceback (most recent call last):
...
ValueError: Expecting 4 colors, provided 5 colors
>>> calculate_resistance(4, ["Violet","Silver","Brown","Grey"])
Traceback (most recent call last):
...
ValueError: Silver is not a valid color for significant figure bands
>>> calculate_resistance(4, ["Violet","Blue","Lime","Grey"])
Traceback (most recent call last):
...
ValueError: Lime is not a valid color
"""
is_valid = check_validity(number_of_bands, color_code_list)
if is_valid:
number_of_significant_bands = get_band_type_count(
number_of_bands, "significant"
)
significant_colors = color_code_list[:number_of_significant_bands]
significant_digits = int(get_significant_digits(significant_colors))
multiplier_color = color_code_list[number_of_significant_bands]
multiplier = get_multiplier(multiplier_color)
if number_of_bands == 3:
tolerance_color = None
else:
tolerance_color = color_code_list[number_of_significant_bands + 1]
tolerance = (
20 if tolerance_color is None else get_tolerance(str(tolerance_color))
)
if number_of_bands != 6:
temperature_coeffecient_color = None
else:
temperature_coeffecient_color = color_code_list[
number_of_significant_bands + 2
]
temperature_coeffecient = (
0
if temperature_coeffecient_color is None
else get_temperature_coeffecient(str(temperature_coeffecient_color))
)
resisitance = significant_digits * multiplier
if temperature_coeffecient == 0:
answer = f"{resisitance}Ω ±{tolerance}% "
else:
answer = f"{resisitance}Ω ±{tolerance}% {temperature_coeffecient} ppm/K"
return {"resistance": answer}
else:
raise ValueError("Input is invalid")
if __name__ == "__main__":
import doctest
doctest.testmod()