|
| 1 | +""" |
| 2 | +Title : Calculating the resistance of a n band resistor using the color codes |
| 3 | +
|
| 4 | +Description : |
| 5 | + Resistors resist the flow of electrical current.Each one has a value that tells how |
| 6 | + strongly it resists current flow.This value's unit is the ohm, often noted with the |
| 7 | + Greek letter omega: Ω. |
| 8 | +
|
| 9 | + The colored bands on a resistor can tell you everything you need to know about its |
| 10 | + value and tolerance, as long as you understand how to read them. The order in which |
| 11 | + the colors are arranged is very important, and each value of resistor has its own |
| 12 | + unique combination. |
| 13 | +
|
| 14 | + The color coding for resistors is an international standard that is defined in IEC |
| 15 | + 60062. |
| 16 | +
|
| 17 | + The number of bands present in a resistor varies from three to six. These represent |
| 18 | + significant figures, multiplier, tolerance, reliability, and temperature coefficient |
| 19 | + Each color used for a type of band has a value assigned to it. It is read from left |
| 20 | + to right. |
| 21 | + All resistors will have significant figures and multiplier bands. In a three band |
| 22 | + resistor first two bands from the left represent significant figures and the third |
| 23 | + represents the multiplier band. |
| 24 | +
|
| 25 | + Significant figures - The number of significant figures band in a resistor can vary |
| 26 | + from two to three. |
| 27 | + Colors and values associated with significant figure bands - |
| 28 | + (Black = 0, Brown = 1, Red = 2, Orange = 3, Yellow = 4, Green = 5, Blue = 6, |
| 29 | + Violet = 7, Grey = 8, White = 9) |
| 30 | +
|
| 31 | + Multiplier - There will be one multiplier band in a resistor. It is multiplied with |
| 32 | + the significant figures obtained from previous bands. |
| 33 | + Colors and values associated with multiplier band - |
| 34 | + (Black = 100, Brown = 10^1, Red = 10^2, Orange = 10^3, Yellow = 10^4, Green = 10^5, |
| 35 | + Blue = 10^6, Violet = 10^7, Grey = 10^8, White = 10^9, Gold = 10^-1, Silver = 10^-2) |
| 36 | + Note that multiplier bands use Gold and Silver which are not used for significant |
| 37 | + figure bands. |
| 38 | +
|
| 39 | + Tolerance - The tolerance band is not always present. It can be seen in four band |
| 40 | + resistors and above. This is a percentage by which the resistor value can vary. |
| 41 | + Colors and values associated with tolerance band - |
| 42 | + (Brown = 1%, Red = 2%, Orange = 0.05%, Yellow = 0.02%, Green = 0.5%,Blue = 0.25%, |
| 43 | + Violet = 0.1%, Grey = 0.01%, Gold = 5%, Silver = 10%) |
| 44 | + If no color is mentioned then by default tolerance is 20% |
| 45 | + Note that tolerance band does not use Black and White colors. |
| 46 | +
|
| 47 | + Temperature Coeffecient - Indicates the change in resistance of the component as |
| 48 | + a function of ambient temperature in terms of ppm/K. |
| 49 | + It is present in six band resistors. |
| 50 | + Colors and values associated with Temperature coeffecient - |
| 51 | + (Black = 250 ppm/K, Brown = 100 ppm/K, Red = 50 ppm/K, Orange = 15 ppm/K, |
| 52 | + Yellow = 25 ppm/K, Green = 20 ppm/K, Blue = 10 ppm/K, Violet = 5 ppm/K, |
| 53 | + Grey = 1 ppm/K) |
| 54 | + Note that temperature coeffecient band does not use White, Gold, Silver colors. |
| 55 | +
|
| 56 | +Sources : |
| 57 | + https://www.calculator.net/resistor-calculator.html |
| 58 | + https://learn.parallax.com/support/reference/resistor-color-codes |
| 59 | + https://byjus.com/physics/resistor-colour-codes/ |
| 60 | +""" |
| 61 | +valid_colors: list = [ |
| 62 | + "Black", |
| 63 | + "Brown", |
| 64 | + "Red", |
| 65 | + "Orange", |
| 66 | + "Yellow", |
| 67 | + "Green", |
| 68 | + "Blue", |
| 69 | + "Violet", |
| 70 | + "Grey", |
| 71 | + "White", |
| 72 | + "Gold", |
| 73 | + "Silver", |
| 74 | +] |
| 75 | + |
| 76 | +significant_figures_color_values: dict[str, int] = { |
| 77 | + "Black": 0, |
| 78 | + "Brown": 1, |
| 79 | + "Red": 2, |
| 80 | + "Orange": 3, |
| 81 | + "Yellow": 4, |
| 82 | + "Green": 5, |
| 83 | + "Blue": 6, |
| 84 | + "Violet": 7, |
| 85 | + "Grey": 8, |
| 86 | + "White": 9, |
| 87 | +} |
| 88 | + |
| 89 | +multiplier_color_values: dict[str, float] = { |
| 90 | + "Black": 10**0, |
| 91 | + "Brown": 10**1, |
| 92 | + "Red": 10**2, |
| 93 | + "Orange": 10**3, |
| 94 | + "Yellow": 10**4, |
| 95 | + "Green": 10**5, |
| 96 | + "Blue": 10**6, |
| 97 | + "Violet": 10**7, |
| 98 | + "Grey": 10**8, |
| 99 | + "White": 10**9, |
| 100 | + "Gold": 10**-1, |
| 101 | + "Silver": 10**-2, |
| 102 | +} |
| 103 | + |
| 104 | +tolerance_color_values: dict[str, float] = { |
| 105 | + "Brown": 1, |
| 106 | + "Red": 2, |
| 107 | + "Orange": 0.05, |
| 108 | + "Yellow": 0.02, |
| 109 | + "Green": 0.5, |
| 110 | + "Blue": 0.25, |
| 111 | + "Violet": 0.1, |
| 112 | + "Grey": 0.01, |
| 113 | + "Gold": 5, |
| 114 | + "Silver": 10, |
| 115 | +} |
| 116 | + |
| 117 | +temperature_coeffecient_color_values: dict[str, int] = { |
| 118 | + "Black": 250, |
| 119 | + "Brown": 100, |
| 120 | + "Red": 50, |
| 121 | + "Orange": 15, |
| 122 | + "Yellow": 25, |
| 123 | + "Green": 20, |
| 124 | + "Blue": 10, |
| 125 | + "Violet": 5, |
| 126 | + "Grey": 1, |
| 127 | +} |
| 128 | + |
| 129 | +band_types: dict[int, dict[str, int]] = { |
| 130 | + 3: {"significant": 2, "multiplier": 1}, |
| 131 | + 4: {"significant": 2, "multiplier": 1, "tolerance": 1}, |
| 132 | + 5: {"significant": 3, "multiplier": 1, "tolerance": 1}, |
| 133 | + 6: {"significant": 3, "multiplier": 1, "tolerance": 1, "temp_coeffecient": 1}, |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +def get_significant_digits(colors: list) -> str: |
| 138 | + """ |
| 139 | + Function returns the digit associated with the color. Function takes a |
| 140 | + list containing colors as input and returns digits as string |
| 141 | +
|
| 142 | + >>> get_significant_digits(['Black','Blue']) |
| 143 | + '06' |
| 144 | +
|
| 145 | + >>> get_significant_digits(['Aqua','Blue']) |
| 146 | + Traceback (most recent call last): |
| 147 | + ... |
| 148 | + ValueError: Aqua is not a valid color for significant figure bands |
| 149 | +
|
| 150 | + """ |
| 151 | + digit = "" |
| 152 | + for color in colors: |
| 153 | + if color not in significant_figures_color_values: |
| 154 | + msg = f"{color} is not a valid color for significant figure bands" |
| 155 | + raise ValueError(msg) |
| 156 | + digit = digit + str(significant_figures_color_values[color]) |
| 157 | + return str(digit) |
| 158 | + |
| 159 | + |
| 160 | +def get_multiplier(color: str) -> float: |
| 161 | + """ |
| 162 | + Function returns the multiplier value associated with the color. |
| 163 | + Function takes color as input and returns multiplier value |
| 164 | +
|
| 165 | + >>> get_multiplier('Gold') |
| 166 | + 0.1 |
| 167 | +
|
| 168 | + >>> get_multiplier('Ivory') |
| 169 | + Traceback (most recent call last): |
| 170 | + ... |
| 171 | + ValueError: Ivory is not a valid color for multiplier band |
| 172 | +
|
| 173 | + """ |
| 174 | + if color not in multiplier_color_values: |
| 175 | + msg = f"{color} is not a valid color for multiplier band" |
| 176 | + raise ValueError(msg) |
| 177 | + return multiplier_color_values[color] |
| 178 | + |
| 179 | + |
| 180 | +def get_tolerance(color: str) -> float: |
| 181 | + """ |
| 182 | + Function returns the tolerance value associated with the color. |
| 183 | + Function takes color as input and returns tolerance value. |
| 184 | +
|
| 185 | + >>> get_tolerance('Green') |
| 186 | + 0.5 |
| 187 | +
|
| 188 | + >>> get_tolerance('Indigo') |
| 189 | + Traceback (most recent call last): |
| 190 | + ... |
| 191 | + ValueError: Indigo is not a valid color for tolerance band |
| 192 | +
|
| 193 | + """ |
| 194 | + if color not in tolerance_color_values: |
| 195 | + msg = f"{color} is not a valid color for tolerance band" |
| 196 | + raise ValueError(msg) |
| 197 | + return tolerance_color_values[color] |
| 198 | + |
| 199 | + |
| 200 | +def get_temperature_coeffecient(color: str) -> int: |
| 201 | + """ |
| 202 | + Function returns the temperature coeffecient value associated with the color. |
| 203 | + Function takes color as input and returns temperature coeffecient value. |
| 204 | +
|
| 205 | + >>> get_temperature_coeffecient('Yellow') |
| 206 | + 25 |
| 207 | +
|
| 208 | + >>> get_temperature_coeffecient('Cyan') |
| 209 | + Traceback (most recent call last): |
| 210 | + ... |
| 211 | + ValueError: Cyan is not a valid color for temperature coeffecient band |
| 212 | +
|
| 213 | + """ |
| 214 | + if color not in temperature_coeffecient_color_values: |
| 215 | + msg = f"{color} is not a valid color for temperature coeffecient band" |
| 216 | + raise ValueError(msg) |
| 217 | + return temperature_coeffecient_color_values[color] |
| 218 | + |
| 219 | + |
| 220 | +def get_band_type_count(total_number_of_bands: int, type_of_band: str) -> int: |
| 221 | + """ |
| 222 | + Function returns the number of bands of a given type in a resistor with n bands |
| 223 | + Function takes total_number_of_bands and type_of_band as input and returns |
| 224 | + number of bands belonging to that type in the given resistor |
| 225 | +
|
| 226 | + >>> get_band_type_count(3,'significant') |
| 227 | + 2 |
| 228 | +
|
| 229 | + >>> get_band_type_count(2,'significant') |
| 230 | + Traceback (most recent call last): |
| 231 | + ... |
| 232 | + ValueError: 2 is not a valid number of bands |
| 233 | +
|
| 234 | + >>> get_band_type_count(3,'sign') |
| 235 | + Traceback (most recent call last): |
| 236 | + ... |
| 237 | + ValueError: sign is not valid for a 3 band resistor |
| 238 | +
|
| 239 | + >>> get_band_type_count(3,'tolerance') |
| 240 | + Traceback (most recent call last): |
| 241 | + ... |
| 242 | + ValueError: tolerance is not valid for a 3 band resistor |
| 243 | +
|
| 244 | + >>> get_band_type_count(5,'temp_coeffecient') |
| 245 | + Traceback (most recent call last): |
| 246 | + ... |
| 247 | + ValueError: temp_coeffecient is not valid for a 5 band resistor |
| 248 | +
|
| 249 | + """ |
| 250 | + if total_number_of_bands not in band_types: |
| 251 | + msg = f"{total_number_of_bands} is not a valid number of bands" |
| 252 | + raise ValueError(msg) |
| 253 | + if type_of_band not in band_types[total_number_of_bands]: |
| 254 | + msg = f"{type_of_band} is not valid for a {total_number_of_bands} band resistor" |
| 255 | + raise ValueError(msg) |
| 256 | + return band_types[total_number_of_bands][type_of_band] |
| 257 | + |
| 258 | + |
| 259 | +def check_validity(number_of_bands: int, colors: list) -> bool: |
| 260 | + """ |
| 261 | + Function checks if the input provided is valid or not. |
| 262 | + Function takes number_of_bands and colors as input and returns |
| 263 | + True if it is valid |
| 264 | +
|
| 265 | + >>> check_validity(3, ["Black","Blue","Orange"]) |
| 266 | + True |
| 267 | +
|
| 268 | + >>> check_validity(4, ["Black","Blue","Orange"]) |
| 269 | + Traceback (most recent call last): |
| 270 | + ... |
| 271 | + ValueError: Expecting 4 colors, provided 3 colors |
| 272 | +
|
| 273 | + >>> check_validity(3, ["Cyan","Red","Yellow"]) |
| 274 | + Traceback (most recent call last): |
| 275 | + ... |
| 276 | + ValueError: Cyan is not a valid color |
| 277 | +
|
| 278 | + """ |
| 279 | + if number_of_bands >= 3 and number_of_bands <= 6: |
| 280 | + if number_of_bands == len(colors): |
| 281 | + for color in colors: |
| 282 | + if color not in valid_colors: |
| 283 | + msg = f"{color} is not a valid color" |
| 284 | + raise ValueError(msg) |
| 285 | + return True |
| 286 | + else: |
| 287 | + msg = f"Expecting {number_of_bands} colors, provided {len(colors)} colors" |
| 288 | + raise ValueError(msg) |
| 289 | + else: |
| 290 | + msg = "Invalid number of bands. Resistor bands must be 3 to 6" |
| 291 | + raise ValueError(msg) |
| 292 | + |
| 293 | + |
| 294 | +def calculate_resistance(number_of_bands: int, color_code_list: list) -> dict: |
| 295 | + """ |
| 296 | + Function calculates the total resistance of the resistor using the color codes. |
| 297 | + Function takes number_of_bands, color_code_list as input and returns |
| 298 | + resistance |
| 299 | +
|
| 300 | + >>> calculate_resistance(3, ["Black","Blue","Orange"]) |
| 301 | + {'resistance': '6000Ω ±20% '} |
| 302 | +
|
| 303 | + >>> calculate_resistance(4, ["Orange","Green","Blue","Gold"]) |
| 304 | + {'resistance': '35000000Ω ±5% '} |
| 305 | +
|
| 306 | + >>> calculate_resistance(5, ["Violet","Brown","Grey","Silver","Green"]) |
| 307 | + {'resistance': '7.18Ω ±0.5% '} |
| 308 | +
|
| 309 | + >>> calculate_resistance(6, ["Red","Green","Blue","Yellow","Orange","Grey"]) |
| 310 | + {'resistance': '2560000Ω ±0.05% 1 ppm/K'} |
| 311 | +
|
| 312 | + >>> calculate_resistance(0, ["Violet","Brown","Grey","Silver","Green"]) |
| 313 | + Traceback (most recent call last): |
| 314 | + ... |
| 315 | + ValueError: Invalid number of bands. Resistor bands must be 3 to 6 |
| 316 | +
|
| 317 | + >>> calculate_resistance(4, ["Violet","Brown","Grey","Silver","Green"]) |
| 318 | + Traceback (most recent call last): |
| 319 | + ... |
| 320 | + ValueError: Expecting 4 colors, provided 5 colors |
| 321 | +
|
| 322 | + >>> calculate_resistance(4, ["Violet","Silver","Brown","Grey"]) |
| 323 | + Traceback (most recent call last): |
| 324 | + ... |
| 325 | + ValueError: Silver is not a valid color for significant figure bands |
| 326 | +
|
| 327 | + >>> calculate_resistance(4, ["Violet","Blue","Lime","Grey"]) |
| 328 | + Traceback (most recent call last): |
| 329 | + ... |
| 330 | + ValueError: Lime is not a valid color |
| 331 | +
|
| 332 | + """ |
| 333 | + is_valid = check_validity(number_of_bands, color_code_list) |
| 334 | + if is_valid: |
| 335 | + number_of_significant_bands = get_band_type_count( |
| 336 | + number_of_bands, "significant" |
| 337 | + ) |
| 338 | + significant_colors = color_code_list[:number_of_significant_bands] |
| 339 | + significant_digits = int(get_significant_digits(significant_colors)) |
| 340 | + multiplier_color = color_code_list[number_of_significant_bands] |
| 341 | + multiplier = get_multiplier(multiplier_color) |
| 342 | + if number_of_bands == 3: |
| 343 | + tolerance_color = None |
| 344 | + else: |
| 345 | + tolerance_color = color_code_list[number_of_significant_bands + 1] |
| 346 | + tolerance = ( |
| 347 | + 20 if tolerance_color is None else get_tolerance(str(tolerance_color)) |
| 348 | + ) |
| 349 | + if number_of_bands != 6: |
| 350 | + temperature_coeffecient_color = None |
| 351 | + else: |
| 352 | + temperature_coeffecient_color = color_code_list[ |
| 353 | + number_of_significant_bands + 2 |
| 354 | + ] |
| 355 | + temperature_coeffecient = ( |
| 356 | + 0 |
| 357 | + if temperature_coeffecient_color is None |
| 358 | + else get_temperature_coeffecient(str(temperature_coeffecient_color)) |
| 359 | + ) |
| 360 | + resisitance = significant_digits * multiplier |
| 361 | + if temperature_coeffecient == 0: |
| 362 | + answer = f"{resisitance}Ω ±{tolerance}% " |
| 363 | + else: |
| 364 | + answer = f"{resisitance}Ω ±{tolerance}% {temperature_coeffecient} ppm/K" |
| 365 | + return {"resistance": answer} |
| 366 | + else: |
| 367 | + raise ValueError("Input is invalid") |
| 368 | + |
| 369 | + |
| 370 | +if __name__ == "__main__": |
| 371 | + import doctest |
| 372 | + |
| 373 | + doctest.testmod() |
0 commit comments