<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Little Off &#187; resistors</title>
	<atom:link href="http://www.bemasher.net/archives/tag/resistors/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bemasher.net</link>
	<description>Code, Computers, Photography and Guns</description>
	<lastBuildDate>Mon, 21 Nov 2011 06:38:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Resistor Calculator</title>
		<link>http://www.bemasher.net/archives/190?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resistor-calculator</link>
		<comments>http://www.bemasher.net/archives/190#comments</comments>
		<pubDate>Wed, 25 Feb 2009 03:06:31 +0000</pubDate>
		<dc:creator>bemasher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[calculate]]></category>
		<category><![CDATA[equivalent resistance]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[resistor pairs]]></category>
		<category><![CDATA[resistors]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.bemasher.net/?p=190</guid>
		<description><![CDATA[Recently I had need of a simple way to create a circuit of equivalent resistance to a calculated value using only parts I had available from the ECE stockroom. Lucky for me this was made easy since there is a posted parts list spreadsheet including a page of all resistor values in stock. The simplest [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had need of a simple way to create a circuit of equivalent resistance to a calculated value using only parts I had available from the ECE stockroom. Lucky for me this was made easy since there is a posted parts list spreadsheet including a page of all resistor values in stock.</p>
<p>The simplest way I could think of for doing this was just to use two resistors in parallel and find a pair of resistor values in the parts list that is equivalent to the value i'm looking for. Two resistors in parallel have an equivalent resistance of:<br />
 $R_{eq} = \frac{R_1 \times R_2}{R_1 + R_2}$</p>
<p>A little bit of coding and I wound up at this:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span><br />
<br />
res_val <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">19872</span><br />
approx_list <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
parallel <span style="color: #66cc66;">=</span> <span style="color: #ff7700;font-weight:bold;">lambda</span> r1<span style="color: #66cc66;">,</span> r2: <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>r1 * r2<span style="color: black;">&#41;</span> / <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>r1 + r2<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> sig <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>approx_list<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> r1 <span style="color: #ff7700;font-weight:bold;">in</span> stockroom<span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>stockroom<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> r2 <span style="color: #ff7700;font-weight:bold;">in</span> stockroom:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">math</span>.<span style="color: black;">trunc</span><span style="color: black;">&#40;</span>parallel<span style="color: black;">&#40;</span>r1<span style="color: #66cc66;">,</span> r2<span style="color: black;">&#41;</span> / <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>**sig<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #dc143c;">math</span>.<span style="color: black;">trunc</span><span style="color: black;">&#40;</span><span style="color: #008000;">float</span><span style="color: black;">&#40;</span>res_val<span style="color: black;">&#41;</span> / <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>**sig<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>r2<span style="color: #66cc66;">,</span> r1<span style="color: #66cc66;">,</span> parallel<span style="color: black;">&#40;</span>r1<span style="color: #66cc66;">,</span> r2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> approx_list:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; approx_list.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>r1<span style="color: #66cc66;">,</span> r2<span style="color: #66cc66;">,</span> parallel<span style="color: black;">&#40;</span>r1<span style="color: #66cc66;">,</span> r2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> r1<span style="color: #66cc66;">,</span> r2<span style="color: #66cc66;">,</span> req <span style="color: #ff7700;font-weight:bold;">in</span> approx_list:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>r1<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>r2<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>req<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>I left out the stockroom parts list because it's a bit long, there's about 130 resistor values included. Now mind you this program assumes a few things:</p>
<ul>
<li>You've already checked the parts list for the resistor value you're looking for.</li>
<li>You've already checked the parts list for a resistor twice the value of the part you're looking for since in parallel they would equal resistance of the part you're looking for.</li>
</ul>
<p>The basic logic of the program is like so:</p>
<ol>
<li>For 0 to 4 (skips digits when / 10 ** index of this for loop).</li>
<li>If there aren't any resistor pairs in the approximation list then we haven't found a pair yet.</li>
<li>For the first half of the stockroom list (we only need to compare once or we'll end up with swapped duplicates).</li>
<li>Compare every item in the stockroom list to the current resistor value.</li>
<li>If the equivalent resistance of the current pair of resistors is equal to the value you're looking for.</li>
<li>Add it to the list.</li>
</ol>
<p>Once we've compared all of them we go back and do the same process again but only if we found no resistor pairs matching the desired value. The next time through the list we'll ignore the last digit on both the equivalent resistance of the current resistor pair and the desired resistance value so we can at least get some sort of approximation even if it isn't optimal.</p>
<p>Update: I've been thinking a lot in my spare time about this and i'm sure there's a simple recursive way of developing a circuit with only resistors you have on hand provided you had a lot of the same ones. Probably just for nested parallel branches and series of them. If I have enough time to write the code for this I'll be sure to post it here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bemasher.net/archives/190/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

