<?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>10Dogs.net &#187; Sensors</title>
	<atom:link href="http://www.10dogs.net/tag/sensors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.10dogs.net</link>
	<description>Talk techie to me...</description>
	<lastBuildDate>Tue, 30 Aug 2011 10:54:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Quadrature Encoder</title>
		<link>http://www.10dogs.net/2010/02/02/quadrature-encoder/</link>
		<comments>http://www.10dogs.net/2010/02/02/quadrature-encoder/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:26:23 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[embeddedSPARK]]></category>
		<category><![CDATA[Sensors]]></category>

		<guid isPermaLink="false">http://www.10dogs.net/?p=8</guid>
		<description><![CDATA[Physical Setup The hardware to build a quadrature encoder is pretty simple. For the sensors, you need a couple of photo-interrupters and something to do some very lightweight logic and timing calculations (in my case the Arduino). The encoder is a slotted disk, with equal length and spacing of &#8220;light&#8221; and &#8220;dark&#8221;, which rotates through [...]]]></description>
			<content:encoded><![CDATA[<h3>Physical Setup</h3>
<p>The hardware to build a quadrature encoder is pretty simple. For the sensors, you need a couple of photo-interrupters and something to do some very lightweight logic and timing calculations (in my case the Arduino). The encoder is a slotted disk, with equal length and spacing of &#8220;light&#8221; and &#8220;dark&#8221;, which rotates through the slot of the photo-interrupters (I will post the circuit that I used in a follow-up). The photo-interrupters need to be spaced at a quarter of a wavelength (1 wavelength = the total length of one light and dark section of the slotted disk.)</p>
<p>For my proof of concept, I bread-boarded the components and used a piece of card, cut to mimic the slotted disk.</p>
<div class="wp-caption aligncenter" style="width: 250px"><a title="Quadrature Encoder Setup" href="http://www.flickr.com/photos/41078582@N06/4321746335/"><img title="High-Tech Encoder Simulator" src="http://farm5.static.flickr.com/4032/4321746335_39ace990de_m.jpg" alt="Quadrature Encoder Setup" width="240" height="160" /></a><p class="wp-caption-text">High-Tech Encoder Simulator</p></div>
<div class="wp-caption aligncenter" style="width: 250px"><a title="Circuit and Arduino" href="http://www.flickr.com/photos/41078582@N06/4322480488/"><img title="Spaghetti" src="http://farm3.static.flickr.com/2724/4322480488_51b81a1f59_m.jpg" alt="Circuit and Arduino" width="240" height="160" /></a><p class="wp-caption-text">Spaghetti</p></div>
<h3>The Logic</h3>
<p>Below is a table and sequential images showing the sensor outputs. Note that the reference sensor is the left one. (The phase number printed on the card when aligned with this sensor corresponds to the phase in the table.)</p>
<table border="1">
<tbody>
<tr style="text-align: center;">
<th>Phase</th>
<th>Sensor A</th>
<th>Sensor B</th>
<th>Image</th>
</tr>
<tr>
<th>1</th>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td><a title="Quadrature Encoder by sishea, on Flickr" href="http://www.flickr.com/photos/41078582@N06/4322480858/"><img src="http://farm3.static.flickr.com/2746/4322480858_db5fdab502_t.jpg" alt="Quadrature Encoder" width="100" height="67" /></a></td>
</tr>
<tr>
<th>2</th>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td><a title="Quadrature Encoder by sishea, on Flickr" href="http://www.flickr.com/photos/41078582@N06/4321747713/"><img src="http://farm5.static.flickr.com/4014/4321747713_55e90a92f8_t.jpg" alt="Quadrature Encoder" width="100" height="67" /></a></td>
</tr>
<tr>
<th>3</th>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1</td>
<td><a title="Quadrature Encoder by sishea, on Flickr" href="http://www.flickr.com/photos/41078582@N06/4322481594/"><img src="http://farm3.static.flickr.com/2766/4322481594_7a7a30a620_t.jpg" alt="Quadrature Encoder" width="100" height="67" /></a></td>
</tr>
<tr style="text-align: center;">
<th>4</th>
<td>1</td>
<td>0</td>
<td><a title="Quadrature Encoder by sishea, on Flickr" href="http://www.flickr.com/photos/41078582@N06/4322481984/"><img src="http://farm5.static.flickr.com/4053/4322481984_d504ee957c_t.jpg" alt="Quadrature Encoder" width="100" height="67" /></a></td>
</tr>
<tr>
<th>1</th>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td><a title="Quadrature Encoder by sishea, on Flickr" href="http://www.flickr.com/photos/41078582@N06/4322480858/"><img src="http://farm3.static.flickr.com/2746/4322480858_db5fdab502_t.jpg" alt="Quadrature Encoder" width="100" height="67" /></a></td>
</tr>
<tr>
<th>2</th>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td><a title="Quadrature Encoder by sishea, on Flickr" href="http://www.flickr.com/photos/41078582@N06/4321747713/"><img src="http://farm5.static.flickr.com/4014/4321747713_55e90a92f8_t.jpg" alt="Quadrature Encoder" width="100" height="67" /></a></td>
</tr>
<tr>
<th>etc&#8230;</th>
<td style="text-align: center;">etc&#8230;</td>
<td style="text-align: center;">etc&#8230;</td>
<td>*<a href="http://en.wikipedia.org/wiki/Mongkut">Mongkut</a></td>
</tr>
</tbody>
</table>
<p>By only looking at the outputs each time <em>Sensor A</em> changes state, we can tell which direction the encoder is travelling.</p>
<p>If we step through the table rows above, when the state of <em>Sensor A</em> changes from 0 to 1 (phase 2 &#8211;&gt; 3), the sensor values are both 1. When <em>Sensor A</em> changes from 1 to 0 (phase 4 &#8211;&gt; 1), the values are 0 and 0.</p>
<h4>Forward Direction</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://www.youtube.com/v/3gMTMDdnC0Y&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=en_US&amp;feature=player_embedded&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/3gMTMDdnC0Y&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=en_US&amp;feature=player_embedded&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>If we now go from bottom to top. When the state of <em>Sensor A</em> changes from 0 to 1 (phase 1 &#8211;&gt; 4), the sensor values are 1 and 0. When the state of <em>Sensor A</em> changes from 1 to 0 (phase 3 &#8211;&gt; 2), the sensor values are 0 and 1.</p>
<h4>Reverse Direction</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/LJx89bxv9YE&amp;hl=en_GB&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/LJx89bxv9YE&amp;hl=en_GB&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>In simpler terms: when <em>Sensor A</em> is triggered, if both of the sensor outputs are the same, our encoder is travelling forward; if the sensor outputs are different, the encoder is travelling in the reverse direction.</p>
<p>Give or take a <em>negation</em>, this is the exact behaviour of the <em>exclusive disjunction</em> (or <em>exclusive or</em> ; &#8220;XOR&#8221;) logical connective.</p>
<table border="1" cellpadding="10">
<tbody>
<tr>
<th colspan="2" rowspan="2">P XOR Q</th>
<th colspan="2">Q</th>
</tr>
<tr>
<th>T</th>
<th>F</th>
</tr>
<tr>
<th rowspan="2">P</th>
<th>T</th>
<td>F</td>
<td>T</td>
</tr>
<tr>
<th>F</th>
<td>T</td>
<td>F</td>
</tr>
</tbody>
</table>
<p>Restructured to look more similar to my sensor output table:</p>
<table border="1" cellpadding="10">
<tbody>
<tr>
<th>P</th>
<th>Q</th>
<th>P XOR Q</th>
</tr>
<tr>
<td style="text-align: center;">T</td>
<td style="text-align: center;">T</td>
<td style="text-align: center;">F</td>
</tr>
<tr>
<td style="text-align: center;">T</td>
<td style="text-align: center;">F</td>
<td style="text-align: center;">T</td>
</tr>
<tr>
<td style="text-align: center;">F</td>
<td style="text-align: center;">T</td>
<td style="text-align: center;">T</td>
</tr>
<tr>
<td style="text-align: center;">F</td>
<td style="text-align: center;">F</td>
<td style="text-align: center;">F</td>
</tr>
</tbody>
</table>
<p>So, this very long-winded look at the sensor outputs and comparison to the &#8220;XOR&#8221; logic table gives us the following pseudo-code:</p>
<blockquote><p>When <em>Sensor A</em> changes</p>
<p style="padding-left: 30px;">if <em>Sensor A </em>XOR <em>Sensor B </em>then</p>
<p style="padding-left: 60px;"><em>direction </em>= reverse</p>
<p style="padding-left: 30px;">else</p>
<p style="padding-left: 60px;"><em>direction </em>= forward</p>
</blockquote>
<p>All that&#8217;s left to do is to time how long between interrupts on <em>Sensor A</em>; count the number of notches on your disk and calculate RPM to give you the shaft speed. Easy!</p>
<p>I will do another post shortly with the Arduino specifics and code. (TODO: insert link here)</p>
<p><strong>NB: </strong>The <em>forward</em> and <em>reverse</em> directions of the encoder, the reference sensor and the <em>logical states</em> represented in each phase are entirely arbitrary. I am sure many examples will have made different assumptions around these conditions and will therefore show variations of the output tables etc. The concepts remain the same, it&#8217;s just the detail that changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.10dogs.net/2010/02/02/quadrature-encoder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

