Calculating RC values for 555 timers

When setting up a 555 timer for Astable operation, you have 3 components to adjust to determine the output. Read about this here : http://www.uoguelph.ca/~antoon/gadgets/555/555.html . The given formula to calculate frequency is :

f = 1/(.693 x C x (R1 + 2 x R2))

So, if you are trying to obtain a certain frequency from the 555 timer, you have to calculate the appropriate values for the capacitor and the 2 resistors. Solving for these 3 variables is complicated by the fact that there are only certain values available, not counting variable resistors. Anyway, instead of doing math, I wrote this python script to find the closest values. It seems to work alright.

#!/usr/bin/env python
#
## author: Benjamin Eckel
## 03/17/2009
## Calculates best values for C, R1, and R2 to achieve a desired frequency
import sys

DESIRED_FREQ = 14 #Hz

freqfunc = lambda (c, r1, r2): 1/(.693 * c * (r1 + 2*r2))

common_r_values = [1.0, 2.2, 3.3, 4.7]
for exponent in range(2, 5):
	common_r_values += [val*(10**exponent) for val in common_r_values]

common_c_values = [val*0.000001 for val in [0.0015, 0.0022, 0.047, 0.47, 0.01, 0.1, 1.0]] #convert to microF

min_dif = ((), sys.maxint, 1.0)

for c in common_c_values:
	for r1 in common_r_values:
		for r2 in common_r_values:
			freq = freqfunc((c, r1, r2))
			diff = abs(DESIRED_FREQ  - freq)
			#print diff
			if (diff < min_dif[1]):
				min_dif = ((c, r1, r2), diff, freq)

print "R1 = "+"%e" % min_dif[0][2]
print "R2 = "+"%e" % min_dif[0][1]
print "C  = "+"%e" % min_dif[0][0]
print "Frequency was %f" % min_dif[2]
print "Delta was %e" % min_dif[1]

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment