Created
April 7, 2014 13:23
-
-
Save onyxfish/10020212 to your computer and use it in GitHub Desktop.
Dead simple script to find optimal resistor and capacitor values for a 555 timer circuit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import itertools | |
TIME_HIGH_GOAL = int(sys.argv[1]) | |
TIME_LOW_GOAL = int(sys.argv[2]) | |
print 'Seeking optimal R1, R2 and C1 values for a %i pulse every %i milliseconds...' % (TIME_HIGH_GOAL, TIME_LOW_GOAL) | |
# Common resistor values | |
RESISTORS = [0, 1.5, 4.7, 10, 47, 100, 220, 330, 470, 680, 1000, 2200, 3300, 4700, 10000, 22000, 47000, 100000, 330000, 1000000] | |
# Common capacitor values | |
CAPACITORS_MICRO = [1000, 470, 100, 47, 22, 10, 4.7, 2.2, 1] | |
CAPACITORS_NANO = [470, 200, 100, 22, 10, 4.7, 2.2, 1] | |
CAPACITORS_PICO = [47, 22, 10, 470, 220, 100] | |
CAPACITORS = [float(c) / 1000 for c in CAPACITORS_MICRO] | |
CAPACITORS.extend([float(c) / 1000000 for c in CAPACITORS_NANO]) | |
CAPACITORS.extend([float(c) / 1000000000 for c in CAPACITORS_PICO]) | |
guesses = itertools.product(RESISTORS, RESISTORS, CAPACITORS) | |
best_guess = (0, 0, 0) | |
best_off_by = 999999999 | |
for r_one, r_two, c_one in guesses: | |
# R1 minimum is 1K ohms | |
if r_one < 1000: | |
continue | |
# C1 inimum is 0.0005 microfarads | |
if c_one < (0.0005 / 1000): | |
continue | |
time_high = 0.693 * (r_one + r_two) * c_one | |
time_low = 0.693 * r_two * c_one | |
off_by = abs(TIME_HIGH_GOAL - time_high) + abs(TIME_LOW_GOAL - time_low) | |
if off_by < best_off_by: | |
best_guess = (r_one, r_two, c_one) | |
best_off_by = off_by | |
r_one, r_two, c_one = best_guess | |
print 'Best guess:' | |
print 'R1:', r_one | |
print 'R2:', r_two | |
print 'C1:', c_one | |
print 'Time high:', 0.693 * (r_one + r_two) * c_one | |
print 'Time low:', 0.693 * r_two * c_one |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment