<?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>Interactive Matter &#187; Sensors</title>
	<atom:link href="http://interactive-matter.eu/category/sensors/feed/" rel="self" type="application/rss+xml" />
	<link>http://interactive-matter.eu</link>
	<description>Tinkering with electronics &#38; ambient interaction</description>
	<lastBuildDate>Thu, 08 Dec 2011 15:57:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
			<title>Interactive Matter</title>
			<url>http://interactive-matter.org/wp-content/uploads/2010/03/im-feed.png</url>
			<link>http://interactive-matter.eu</link>
			<width></width>
			<height></height>
			<description>Tinkering with electronics &amp; ambient interaction</description>
		</image>		<item>
		<title>Arduino &amp; Barometric Pressure Sensor BMP085</title>
		<link>http://interactive-matter.eu/2009/12/arduino-barometric-pressure-sensor-bmp085/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=arduino-barometric-pressure-sensor-bmp085</link>
		<comments>http://interactive-matter.eu/2009/12/arduino-barometric-pressure-sensor-bmp085/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 21:05:54 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Sensors]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Barometer]]></category>
		<category><![CDATA[BMP085]]></category>
		<category><![CDATA[I2C]]></category>
		<category><![CDATA[Pressure Sensor]]></category>
		<category><![CDATA[SMT]]></category>

		<guid isPermaLink="false">http://interactive-matter.org/?p=451</guid>
		<description><![CDATA[In lack of any new projects (which are currently all in an very intermediate state) Interactive Matter presents yet another &#8216;how to connect a cool I2C sensor to Arduino&#8217; post. This time it is all about pressure. The BMP085 pressure sensor combines a absolute barometric pressure sensor (aka barometer) with an temperature sensor. It is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In lack of any new projects (which are currently all in an very intermediate state) Interactive Matter presents yet another &#8216;how to connect a cool <a href="http://en.wikipedia.org/wiki/I2c">I2C</a> sensor to Arduino&#8217; post.</p>
<p>This time it is all about pressure. The <a href="http://www.bosch-sensortec.com/content/language1/html/3477.htm">BMP085</a> pressure sensor combines a absolute barometric pressure sensor (aka <a href="http://en.wikipedia.org/wiki/Barometer">barometer</a>) with an temperature sensor. It is build by <a href="http://www.bosch-sensortec.com/content/language1/html/index.htm">Bosch Sensortec</a> and intended to help <a href="http://en.wikipedia.org/wiki/Gps">GPS navigation units</a> to detect their height above sea level. It combines the advantage of being quite cheap (~5$) and precise.</p>
<p><a title="Arduino &amp; Barometric Pressure Sensor BMP085" href="http://farm3.static.flickr.com/2485/4159864537_6e11c303e3_b.jpg"><img class="slickr-post colorbox-451" src="http://farm3.static.flickr.com/2485/4159864537_6e11c303e3.jpg" alt="Arduino &amp; Barometric Pressure Sensor BMP085" width="500" height="375" /></a></p>
<p>But of course there are myriads of other applications as well, like using it in a digital weather station, detecting the force of slammed doors or …</p>
<p><span id="more-451"></span></p>
<p>The information about this sensor is very sparse and Bosch is not very keen to give any information to makers. But fortunately <a href="http://news.jeelabs.org/">Jeelabs</a> offers it as an <a href="http://shop.jeelabs.com/products/pressure-plug">extension module</a> for their <a href="http://shop.jeelabs.com/products/jeenode-usb">Jeenode</a> and <a href="http://news.jeelabs.org/2009/02/19/hooking-up-a-bmp085-sensor/">published some very helpful code</a> for it (which helped a lot – check out they are doing really amazing stuff).</p>
<h3>Hardware</h3>
<p><a title="Arduino &amp; Barometric Pressure Sensor BMP085" href="http://farm3.static.flickr.com/2619/4159862811_a851fe280c_b.jpg"><img class="slickr-post colorbox-451" src="http://farm3.static.flickr.com/2619/4159862811_a851fe280c.jpg" alt="Arduino &amp; Barometric Pressure Sensor BMP085" width="500" height="375" /></a></p>
<p>Hooking up the BMP085 to the Arduino works just like any other I2C part: Connect VCC to VCC and GND to GND, SCL goes to analogue pin 5, SDA to analogue pin4. Adding some pull up resistors (1K to 20K, most often something like 4.7K) between SDA, SCL and VCC finishes the setup (this was included in my breakout board).</p>
<p>The BMP08 accepts 1.8 to 3.6 Volts – so no chance to connect it directly to 5 Volts. The BMP085 has an additional EOC (end of conversion) pin indicating the successful data capture. This was connected to analogue pin 2 – but not used in the software implementation.</p>
<h3>Software</h3>
<p>Fortunately the code by Jeenode contained all the functionality, you need, taken directly from <a href="http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf">the datasheet</a>. The only thing I added was the ability to use all oversampling modes (the BMP085 offers 4 oversampling mode, each on taking longer than the other and using more energy, but delivering more precise results).</p>
<p>First we need the ability to read 16 bit values from the BMP085 registers. Nearly all registers of the BMP085 are 16 bit wide:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> r<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> msb<span style="color: #339933;">,</span> lsb<span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// register to read</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read a byte</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  msb <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  lsb <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>msb<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>lsb<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Second you need a function to write a 8 bit register:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span> read_register<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> r<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> v<span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// register to read</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read a byte</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  v <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> v<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we can define some global variables to read the Eeprom calibration data:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//just taken from the BMP085 datasheet</span>
<span style="color: #993333;">int</span> ac1<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> ac2<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> ac3<span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ac4<span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ac5<span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ac6<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> b1<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> b2<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> mb<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> mc<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> md<span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span>  bmp085_get_cal_data<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Reading Calibration Data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ac1 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xAA</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AC1: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>ac1<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ac2 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xAC</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AC2: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>ac2<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ac3 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xAE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AC3: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>ac3<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ac4 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xB0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AC4: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>ac4<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ac5 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xB2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AC5: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>ac5<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ac6 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xB4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AC6: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>ac6<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  b1 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xB6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;B1: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>b1<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  b2 <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xB8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;B2: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>b1<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  mb <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xBA</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MB: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mb<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  mc <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xBC</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MC: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mc<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  md <span style="color: #339933;">=</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xBE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MD: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>md<span style="color: #339933;">,</span>DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Eeprom readout can be done more efficiently by reading all the values with just one register write. But it is more comprehensible like this and in the initialization phase we have good amount of time.<br />
The raw temperature (<code>ut</code>) and pressure (<code>up</code>) data can be readout as 16 bit and 24 bit value:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> bmp085_read_ut<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  write_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xf4</span><span style="color: #339933;">,</span><span style="color: #208080;">0x2e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//longer than 4.5 ms</span>
  <span style="color: #b1b100;">return</span> read_int_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xf6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">long</span> bmp085_read_up<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  write_register<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xf4</span><span style="color: #339933;">,</span><span style="color: #208080;">0x34</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>oversampling_setting<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>pressure_waittime<span style="color: #009900;">&#91;</span>oversampling_setting<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> msb<span style="color: #339933;">,</span> lsb<span style="color: #339933;">,</span> xlsb<span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span><span style="color: #208080;">0xf6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// register to read</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read a byte</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  msb <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  lsb <span style="color: #339933;">|=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  xlsb <span style="color: #339933;">|=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>msb<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>lsb<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>xlsb<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">8</span><span style="color: #339933;">-</span>oversampling_setting<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The conversion between the raw data and the real temperature data in 0.1 <a href="http://en.wikipedia.org/wiki/Degree_Celsius">°C</a> an <a href="http://en.wikipedia.org/wiki/Pascal_(unit)">Pascal</a> is taken from the <a href="http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf">datasheet</a> (combined with <a href="http://news.jeelabs.org/2009/02/19/hooking-up-a-bmp085-sensor/">the corrections</a> of Jeenodes):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> bmp085_read_temperature_and_pressure<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #339933;">*</span> temperature<span style="color: #339933;">,</span> <span style="color: #993333;">long</span><span style="color: #339933;">*</span> pressure<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span>  ut<span style="color: #339933;">=</span> bmp085_read_ut<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">long</span> up <span style="color: #339933;">=</span> bmp085_read_up<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">long</span> x1<span style="color: #339933;">,</span> x2<span style="color: #339933;">,</span> x3<span style="color: #339933;">,</span> b3<span style="color: #339933;">,</span> b5<span style="color: #339933;">,</span> b6<span style="color: #339933;">,</span> p<span style="color: #339933;">;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> b4<span style="color: #339933;">,</span> b7<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//calculate the temperature</span>
  x1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>ut <span style="color: #339933;">-</span> ac6<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> ac5 <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">15</span><span style="color: #339933;">;</span>
  x2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span> mc <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #0000dd;">11</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span>x1 <span style="color: #339933;">+</span> md<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  b5 <span style="color: #339933;">=</span> x1 <span style="color: #339933;">+</span> x2<span style="color: #339933;">;</span>
  <span style="color: #339933;">*</span>temperature <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>b5 <span style="color: #339933;">+</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//calculate the pressure</span>
  b6 <span style="color: #339933;">=</span> b5 <span style="color: #339933;">-</span> <span style="color: #0000dd;">4000</span><span style="color: #339933;">;</span>
  x1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>b6 <span style="color: #339933;">*</span> b6 <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">11</span><span style="color: #339933;">;</span>
  x2 <span style="color: #339933;">=</span> ac2 <span style="color: #339933;">*</span> b6 <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">11</span><span style="color: #339933;">;</span>
  x3 <span style="color: #339933;">=</span> x1 <span style="color: #339933;">+</span> x2<span style="color: #339933;">;</span>
  b3 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int32_t</span><span style="color: #009900;">&#41;</span> ac1 <span style="color: #339933;">*</span> <span style="color: #0000dd;">4</span> <span style="color: #339933;">+</span> x3<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
  x1 <span style="color: #339933;">=</span> ac3 <span style="color: #339933;">*</span> b6 <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">13</span><span style="color: #339933;">;</span>
  x2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>b6 <span style="color: #339933;">*</span> b6 <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">16</span><span style="color: #339933;">;</span>
  x3 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x1 <span style="color: #339933;">+</span> x2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
  b4 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ac4 <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">uint32_t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>x3 <span style="color: #339933;">+</span> <span style="color: #0000dd;">32768</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">15</span><span style="color: #339933;">;</span>
  b7 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">uint32_t</span><span style="color: #009900;">&#41;</span> up <span style="color: #339933;">-</span> b3<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">50000</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> oversampling_setting<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  p <span style="color: #339933;">=</span> b7 <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #208080;">0x80000000</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>b7 <span style="color: #339933;">*</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> b4 <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>b7 <span style="color: #339933;">/</span> b4<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
&nbsp;
  x1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  x1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>x1 <span style="color: #339933;">*</span> <span style="color: #0000dd;">3038</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">16</span><span style="color: #339933;">;</span>
  x2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">7357</span> <span style="color: #339933;">*</span> p<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">16</span><span style="color: #339933;">;</span>
  <span style="color: #339933;">*</span>pressure <span style="color: #339933;">=</span> p <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x1 <span style="color: #339933;">+</span> x2 <span style="color: #339933;">+</span> <span style="color: #0000dd;">3791</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Unfortunately temperature and pressure had to be computed at the same time (the temperature value is used for the pressure calculation). <code>int*</code> means a pointer to an integer value, which is written like <code>*temperature = (b5 + 8 ) &gt;&gt; 4;</code>. the function is called like</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  <span style="color: #993333;">int</span>  temperature <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">long</span> pressure <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  bmp085_read_temperature_and_pressure<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>temperature<span style="color: #339933;">,&amp;</span>amp<span style="color: #339933;">;</span>pressure<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is very simple and gives very good results.<br />
If you want to try it yourself get the <a href="http://interactive-matter.org/wp-content/uploads/2009/12/BMP085.pde_.zip">BMP085 Arduino Sketch</a>. If there is any interest in the breakout board (assembled or unassembled) just <a href="http://interactive-matter.org/contact/">drop me a note.</a></p>
<p><em>And big thanks to <a href="http://news.jeelabs.org/">Jeelabs</a>. Their code helped me a lot they real awesome stuff!</em></p>
<p>Related posts:<ul>
<li><a href='http://interactive-matter.eu/2010/02/bmp085-barometric-pressure-sensor-breakout-boards-arrived/' rel='bookmark' title='BMP085 Barometric Pressure Sensor Breakout Boards arrived!'>BMP085 Barometric Pressure Sensor Breakout Boards arrived!</a></li>
<li><a href='http://interactive-matter.eu/2009/02/arduino-lis302dl/' rel='bookmark' title='Arduino &amp; LIS302DL'>Arduino &#038; LIS302DL</a></li>
<li><a href='http://interactive-matter.eu/2009/07/arduino-ad7746/' rel='bookmark' title='Arduino &amp; AD7746'>Arduino &#038; AD7746</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://interactive-matter.eu/2009/12/arduino-barometric-pressure-sensor-bmp085/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Arduino &amp; AD7746</title>
		<link>http://interactive-matter.eu/2009/07/arduino-ad7746/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=arduino-ad7746</link>
		<comments>http://interactive-matter.eu/2009/07/arduino-ad7746/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 16:14:00 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Sensors]]></category>
		<category><![CDATA[Tinkering]]></category>
		<category><![CDATA[AD7746]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Capacity Sensor]]></category>
		<category><![CDATA[I2C]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Sparkfun]]></category>
		<category><![CDATA[Touch Sensor]]></category>

		<guid isPermaLink="false">http://interactive-matter.org/?p=274</guid>
		<description><![CDATA[In my ongoing series about cool sensors Interactive Matter presents the AD7746 capacity sensor: What is a capacity sensor good for? You can of course make a device to identify small capacitance (up to 4pF) but that&#8217;s boring. Much more interesting is how it reacts to touching. Read on how to connect it to the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my ongoing series about cool sensors <em>Interactive Matter</em> presents the <a href="http://www.analog.com/en/analog-to-digital-converters/capacitance-to-digital-converters/AD7746/products/product.html">AD7746 capacity sensor</a>:</p>
<p><a title="Arduino &amp; AD7746" href="http://farm3.static.flickr.com/2458/3600509491_56845934a9_b.jpg"><img class="slickr-post colorbox-274" src="http://farm3.static.flickr.com/2458/3600509491_56845934a9.jpg" alt="Arduino &amp; AD7746" width="500" height="375" /></a></p>
<p>What is a capacity sensor good for? You can of course make a device to identify small capacitance (up to 4pF) but that&#8217;s boring. Much more interesting is how it reacts to touching.</p>
<p>Read on how to connect it to the Arduino and how to use it to sense touching</p>
<p><span id="more-274"></span></p>
<p>Fortunately there is a basic sketch how to use an <a href="http://www.interaccess.org/blog/?p=1886&amp;cpage=1">AD7746 with Arduino</a>. Unfortunately this sketch has one or two awkward structures and inconsistencies. Much more unfortunately it somehow works. So lets clean this thing a bit up.</p>
<h3>I2C Basics</h3>
<p>The <a href="http://www.arduino.cc/playground/Learning/I2C">I2C on Arduino</a> uses analog pin 4 as SDA and analog pin 5 as SCL . With the <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=7918">Sparkfun AD7746 breakout board</a> you need some pull up resistors for the I2C lines – So I added some 4.7k resistors to VCC (forgetting the pull up resistors is the most common source for an I2C connection not working – at least for me).</p>
<h3>Connecting AD7746 to Arduino</h3>
<p>The Sparkfun Ad7746 breakout board comes with two long lines to direct the sensing away from the logic and some &#8216;touch plate&#8217;. As you can see in the above photo you just connect the I2C pins, GND and VCC. Despite the marking &#8216;<em>3.3V</em>&#8216; on the breakout board the AD7746 accepts up to 5Volt as input voltage. So no special 3.3V voltage supply is needed.</p>
<p>The software part was a little more tricky. I used the <a href="http://www.interaccess.org/blog/?p=1886&amp;cpage=1">AD7746 Arduino sketch</a> I found and refactored it a bit. First of all I used my convenience extensions to read and write I2C registers conveniently (download the whole sketch – it is far too long to list here). Next we define our ADD7746 registers for convenience reasons:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &amp;lt;Wire.h&amp;gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//AD7746 definitions</span>
<span style="color: #339933;">#define I2C_ADDRESS  0x48 //0x90 shift one to the right</span>
&nbsp;
<span style="color: #339933;">#define REGISTER_STATUS 0x00</span>
<span style="color: #339933;">#define REGISTER_CAP_DATA 0x01</span>
<span style="color: #339933;">#define REGISTER_VT_DATA 0x04</span>
<span style="color: #339933;">#define REGISTER_CAP_SETUP 0x07</span>
<span style="color: #339933;">#define REGISTER_VT_SETUP 0x08</span>
<span style="color: #339933;">#define REGISTER_EXC_SETUP 0x09</span>
<span style="color: #339933;">#define REGISTER_CONFIGURATION 0x0A</span>
<span style="color: #339933;">#define REGISTER_CAP_DAC_A 0x0B</span>
<span style="color: #339933;">#define REGISTER_CAP_OFFSET 0x0D</span>
<span style="color: #339933;">#define REGISTER_CAP_GAIN 0x0F</span>
<span style="color: #339933;">#define REGISTER_VOLTAGE_GAIN 0x11</span>
&nbsp;
<span style="color: #339933;">#define RESET_ADDRESS 0xBF</span>
&nbsp;
<span style="color: #339933;">#define CAP_ZERO 0x800000L</span></pre></div></div>

<p>Now we can write a little startup routine to initialize the AD7746. It performs a reset (you never know), enables uses channel 1 for measuring capacitance and tries to calibrate the AD7746:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#91;</span>…<span style="color: #009900;">&#93;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Initializing&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// start i2c cycle</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>RESET_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// reset the device</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// ends i2c cycle</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//wait a tad for reboot</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  writeRegister<span style="color: #009900;">&#40;</span>REGISTER_EXC_SETUP<span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// EXC source A</span>
&nbsp;
  writeRegister<span style="color: #009900;">&#40;</span>REGISTER_CAP_SETUP<span style="color: #339933;">,</span>_BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// cap setup reg - cap enabled</span>
&nbsp;
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Getting offset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>readInteger<span style="color: #009900;">&#40;</span>REGISTER_CAP_OFFSET<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factory offset: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>offset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  writeRegister<span style="color: #009900;">&#40;</span><span style="color: #208080;">0x0A</span><span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// set configuration to calib. mode, slow sample</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//wait for calibration</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  displayStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Calibrated offset: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>readInteger<span style="color: #009900;">&#40;</span>REGISTER_CAP_OFFSET<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>offset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#91;</span>…<span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The calibration is a bit more tricky, later more on that. First we have to get an readout of the sensor:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">long</span> readValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #993333;">long</span> ret <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
 <span style="color: #993333;">uint8_t</span> data<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #993333;">char</span> status <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">//wait until a conversion is done</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>status <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>_BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 status<span style="color: #339933;">=</span> readRegister<span style="color: #009900;">&#40;</span>REGISTER_STATUS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> value <span style="color: #339933;">=</span>  readLong<span style="color: #009900;">&#40;</span>REGISTER_CAP_DATA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 value <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;=</span><span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">//we have read one byte to much, now we have to get rid of it</span>
 ret <span style="color: #339933;">=</span>  value<span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">return</span> ret<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It waits until an measurement has finished an reads the data value from the data register. The read routine returns a 32 bit long value. We just need the first three bytes, so we shift it one byte to the left. The datasheet specifies 0&#215;800000 as null point, so we should subtract this value from readout. This was omitted here, since the datasheet also say that the value in REGISTER_CAP_OFFSET contains the real zero point after an calibration. So we just give back the value of REGISTER_CAP_OFFSET as offset. The content of REGISTER_CAP_OFFSET can be used in the external program logic (later more on that).</p>
<p>The AD7746 is not very nice to read from. The datasheet says, that you can write an address and then read as many bytes from the chip as you like. But not if you sent a STOP after you have written the address. But this is exactly what the Wire library does. So the readout routine (e.g. for Long values) looks a bit nasty since the address pointer is set back to 0:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> readLong<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">union</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> value<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  byteMappedLong<span style="color: #339933;">;</span>
&nbsp;
  byteMappedLong.<span style="color: #202020;">value</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0L</span><span style="color: #339933;">;</span>
&nbsp;
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// begin read cycle</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//pointer to first data register</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// end cycle</span>
  <span style="color: #666666; font-style: italic;">//the data pointer is reset anyway - so read from 0 on</span>
&nbsp;
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #339933;">,</span>r<span style="color: #339933;">+</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// reads 2 bytes plus all bytes before the register</span>
&nbsp;
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>r<span style="color: #339933;">+</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//wait</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span>r<span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">uint8_t</span> c <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      byteMappedLong.<span style="color: #202020;">data</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> c<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> byteMappedLong.<span style="color: #202020;">value</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that we are getting some real values we can try to calibrate the chip. It comes factory calibrated, which is reset after each reset. First of all we try to perform an built in calibration:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> calibrate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  calibration <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Calibrating CapDAC A&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #993333;">long</span> value <span style="color: #339933;">=</span> readValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>value<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>VALUE_UPPER_BOUND <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> calibration <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #0000dd;">128</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    calibration<span style="color: #339933;">++;</span>
    writeRegister<span style="color: #009900;">&#40;</span>REGISTER_CAP_DAC_A<span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> calibration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    value <span style="color: #339933;">=</span> readValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;done&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After this i done we just switch over to continuous read mode, read out a value, print it with all calibration settings to the serial port:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  writeRegister<span style="color: #009900;">&#40;</span>REGISTER_CAP_SETUP<span style="color: #339933;">,</span>_BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// cap setup reg - cap enabled</span>
&nbsp;
  writeRegister<span style="color: #009900;">&#40;</span>REGISTER_EXC_SETUP<span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// EXC source A</span>
&nbsp;
  writeRegister<span style="color: #009900;">&#40;</span>REGISTER_CONFIGURATION<span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// continuous mode</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// main program begins</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #009900;">&#91;</span>…<span style="color: #009900;">&#93;</span>
&nbsp;
  <span style="color: #993333;">long</span> value <span style="color: #339933;">=</span> readValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>offset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>calibration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>valueVALUE_UPPER_BOUND<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    outOfRangeCount<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>outOfRangeCount<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>MAX_OUT_OF_RANGE_COUNT<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> VALUE_LOWER_BOUND<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      calibrate<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>CALIBRATION_INCREASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      calibrate<span style="color: #009900;">&#40;</span>CALIBRATION_INCREASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    outOfRangeCount<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The whole sketch contains a bit more code and can be much better understood if you got all the code – but that would be too much code for this post. So download the whole <a href="http://interactive-matter.org/wp-content/uploads/2009/07/AD7746.zip">Arduino AD7746 Sketch</a> and try it yourself.</p>
<h3>Displaying the results</h3>
<p>The results are displayed with an simple processing sketch, mimicking some kind of cheapo oscilloscope. It reads the values from the serial port and displays them. Anybody interested in the details of the sketch can download the whole <a href="http://interactive-matter.org/wp-content/uploads/2009/07/adjd7746-processing.zip">AD7746 processing sketch</a>.</p>
<p>The formula used to calculate the real capacitance is derrived by th datasheet:</p>
<pre>capacitance = (value-calibration) * 2.44e-07-capdac*0.164;</pre>
<p>I do not know if it completely perfect, but it gives some useful values, interestingly always smaller than 0 – despite the fact that the sensor should be calibrated to 0. For getting absolute capacitance readouts I suggest quadruple checking this to the datasheet and trying to optimize the adjustment of the sensor.</p>
<p>Leaving the sensor alone leads to some graphics like this:</p>
<p><a title="AD7746 Screenshot 1" href="http://farm4.static.flickr.com/3423/3732535736_c329e32923_o.png"><img class="slickr-post colorbox-274" src="http://farm4.static.flickr.com/3423/3732535736_41a5454e04.jpg" alt="AD7746 Screenshot 1" width="500" height="250" /></a></p>
<p>As you can see we there is some noise present, we could filter out, but for now we live with it. It is not very strong. Especially if you compare it with the next graph.</p>
<p>If you tap on the sensing wires it gets a very strong readout &#8211; just by touching the two fine lines on the board:</p>
<p><a title="AD7746 Screenshot 4" href="http://farm4.static.flickr.com/3419/3731737831_001af444b9_o.png"><img class="slickr-post colorbox-274" src="http://farm4.static.flickr.com/3419/3731737831_fa7e23c952.jpg" alt="AD7746 Screenshot 4" width="500" height="250" /></a></p>
<p>This gives a much stronger readout (you can still see the noise &#8211; but that is very easy to distinct from the &#8216;signal&#8217;). This should be very easy to detect. If you look closer you will see that the readout does not really get back to the values just before touching the wires &#8211; It will go down slowly. So for detecting a signal it should be fairly easy to just compare the difference between two readouts and if it is bigger than some threshold it will interpreted as a &#8216;touch&#8217;.</p>
<p>If you use the touch pad, which comes with the AD7746 breakout board and touch the bare metal you get readout which is extremely strong &#8211; but that seems impractical because you would probably never you would never expose such big pads on your design, without some kind of coating.</p>
<p><a title="AD7746 Screenshot 5" href="http://farm4.static.flickr.com/3418/3731737927_240a8d61be_o.png"><img class="slickr-post colorbox-274" src="http://farm4.static.flickr.com/3418/3731737927_130f770797.jpg" alt="AD7746 Screenshot 5" width="500" height="250" /></a></p>
<p>But if you lay a cover of paper over the sensor you get still an extremely sharp signal:</p>
<p><a title="AD7746 Screenshot 6" href="http://farm4.static.flickr.com/3117/3732574394_7d58a07190_o.png"><img class="slickr-post colorbox-274" src="http://farm4.static.flickr.com/3117/3732574394_f7acb08325.jpg" alt="AD7746 Screenshot 6" width="500" height="250" /></a></p>
<h2>The Bottom Line</h2>
<p>In the end you see that the AD7746 is very interesting for creating some kind of hidden input elements. But it is some dificult part. The read routines are not really compatible with the wire library. Combing all the different possibilities to adjust the sensor and the complexity of calculationg the real value (the datasheet is a bit hard to understand on that topic) makes it very difficutl to tune the sensor optimally. But I hope that this material gives enough hints to use this sensor for a project. But nevertheless there are perhaps easier capacitive input sensors out there.</p>
<p>You can find the <a href="https://github.com/interactive-matter/Arduino---AD7746">full source code over at github</a> or directly <a href="https://github.com/interactive-matter/Arduino---AD7746/zipball/master">download it from github</a>.</p>
<p>Related posts:<ul>
<li><a href='http://interactive-matter.eu/2009/02/arduino-lis302dl/' rel='bookmark' title='Arduino &amp; LIS302DL'>Arduino &#038; LIS302DL</a></li>
<li><a href='http://interactive-matter.eu/2009/12/arduino-barometric-pressure-sensor-bmp085/' rel='bookmark' title='Arduino &amp; Barometric Pressure Sensor BMP085'>Arduino &#038; Barometric Pressure Sensor BMP085</a></li>
<li><a href='http://interactive-matter.eu/2008/08/tinkering-with-adjd-s371-q999/' rel='bookmark' title='Tinkering with ADJD-S371-Q999'>Tinkering with ADJD-S371-Q999</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://interactive-matter.eu/2009/07/arduino-ad7746/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>SFH 3310 &amp; SFH 5711 Light Sensors</title>
		<link>http://interactive-matter.eu/2009/05/let-me-see-the-light/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=let-me-see-the-light</link>
		<comments>http://interactive-matter.eu/2009/05/let-me-see-the-light/#comments</comments>
		<pubDate>Mon, 18 May 2009 12:00:47 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Sensors]]></category>
		<category><![CDATA[Tinkering]]></category>
		<category><![CDATA[Breakout]]></category>
		<category><![CDATA[Light]]></category>
		<category><![CDATA[Photo Diode]]></category>
		<category><![CDATA[Photo Sensor]]></category>
		<category><![CDATA[Photo Transistor]]></category>
		<category><![CDATA[SFH 3310]]></category>
		<category><![CDATA[SFH 3710]]></category>
		<category><![CDATA[SFH 5711]]></category>

		<guid isPermaLink="false">http://interactive-matter.org/?p=190</guid>
		<description><![CDATA[After I successfully shed some light its time to see some light. For this you normally use photo diodes, photo transistors or photo resistors. But there are two nice little detectors which I came across, special enough to post them here: The Osram SFH 3710 and SFH 5711. Both are photo diodes respectively transistors adapted [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>After I successfully <a href="http://interactive-matter.org/2009/05/shed-some-light/">shed some light</a> its time to see some light. For this you normally use photo diodes, photo transistors or photo resistors. But there are two nice little detectors which I came across, special enough to post them here:</p>
<p><a href="http://farm4.static.flickr.com/3658/3515014951_3ed4a3d4a8_o.jpg"><img class="slickr-post colorbox-190" src="http://farm4.static.flickr.com/3658/3515014951_5ee118025a.jpg" alt="" width="500" height="375" /></a></p>
<p>The Osram <a href="http://catalog.osram-os.com/catalogue/catalogue.do?favOid=0000000200022c8e001e0023&amp;act=showBookmark">SFH 3710</a> and <a href="http://catalog.osram-os.com/catalogue/catalogue.do?favOid=0000000300037a3100ad0023&amp;act=showBookmark">SFH 5711</a>. Both are photo diodes respectively transistors adapted to the human eye sensitivity (V?). They are very insensitive in the infrared spectrum (typical for humans, untypical for photo detectors) and have a detection curve close to the human eye. But more details after the click.<br />
<span id="more-190"></span></p>
<h3>SFH 3710</h3>
<p>The SFH 3710 is more or less a very normal photo diode. Nothing special about it. It outputs something between 4 µA and 12 mA depending on the amount of light shining on the sensor. Unfortunately it has current output. So you need an sensing resistor to ground to get some voltage (you remember the law of<a href="http://en.wikipedia.org/wiki/Ohms_law"> Mr. Ohm</a>).</p>
<p>But it hast two very special advantages:</p>
<ol>
<li>It got a very nice 0805-like tiny SMT package. So it consumes very little board space.</li>
<li>But much more important it has a similar detection curve as the human eye.</li>
</ol>
<p>The last point is very important (so I used it for may &#8216;<a href="http://interactive-matter.org/2008/10/how-was-your-day-darling/">How was your day darling</a>&#8216; project). Normal photo detectors are very sensitive in the infrared spectrum, the humane eye is not. What is the effect? Artificial light (especially from normal light bulbs) have an high amount of infrared light and a low amount of visible light (One of the reasons for the inefficiency of light bulbs). For normal photo sensor the light of a light bulb seems to be as bright as daylight (figuratively spoken).</p>
<p>So if you want to detect light in the same way as it is perceived by an human the SFH 3710 is your friend. BTW: The <a href="http://catalog.osram-os.com/catalogue/catalogue.do?favOid=000000020001eab3001d0023&amp;act=showBookmark">SFH 3310</a> is the same detector in a more or less &#8216;normal&#8217; T1 (3mm) LED package.</p>
<h3>SFH 5711</h3>
<p>The SFH 5711 is quite a different beast. It is an photo transistor. Nothing special about this. It also got an V? curve. Fine. But it is logarithmic. To understand the advantages of an logarithmic light sensor you have to understand how the human eye works:</p>
<p>The human eye interprets lights in logarithmic levels. If you see an light level X than 2*X will not appear twice as bright, X*X appears twice as bright. This enables the human eye to see things in a dark cave and bright sun light. To understand how light something appears to a human you have to apply the logarithmic scale.</p>
<p>This is exactly the work done by the SFH 5711. It outputs an current from 5 to 50 µA &#8211; depending on the light level. But it puts out about 10 µA per decade (of brightness). The details are a bit complex &#8211; but the result is quite easy: If the readout is quite as big the light will be interpreted as twice as bright by the human eye. So you get an more or less constant resolution over the whole spectrum of brightness &#8211; as perceived by the human eye. Very useful to adapt the brightness of a display according to the ambient light levels.</p>
<p>The disadvantage: The SFH 5711 contains an amplifier to get an readout &#8211; so you have to suplly VCC (from 2.3V to 5.5V) and GND. The sensor is bigger and needs a special footprint. In the end still very practical.</p>
<p>Related posts:<ul>
<li><a href='http://interactive-matter.eu/2008/08/how-was-your-day-the-first-prototype/' rel='bookmark' title='How was your day: The first prototype'>How was your day: The first prototype</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://interactive-matter.eu/2009/05/let-me-see-the-light/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino &amp; LIS302DL</title>
		<link>http://interactive-matter.eu/2009/02/arduino-lis302dl/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=arduino-lis302dl</link>
		<comments>http://interactive-matter.eu/2009/02/arduino-lis302dl/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 20:56:31 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Sensors]]></category>
		<category><![CDATA[Tinkering]]></category>
		<category><![CDATA[Accelerometer]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[I2C]]></category>
		<category><![CDATA[LIS302DL]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Sparkfun]]></category>
		<category><![CDATA[TWI]]></category>

		<guid isPermaLink="false">http://interactive-matter.org/?p=127</guid>
		<description><![CDATA[Ages ago I bought an LIS302DL from Sparkfun. One of the old (little bit) messed up ones, but still 3 axis accelerometer with I2C output. No external parts. No pain. Fine! Before I go in any further investigation. I need to get it up &#38; running with my arduino (actually an boarduino &#8211; but who [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Ages ago I bought an <a href="http://www.st.com/internet/analog/product/152913.jsp">LIS302DL</a> from <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8658">Sparkfun</a>. One of the old (little bit) messed up ones, but still 3 axis accelerometer with I2C output. No external parts. No pain. Fine!<br />
<a href="http://farm4.static.flickr.com/3551/3405309412_e74a3883d3_o.png"><img class="slickr-post colorbox-127" src="http://farm4.static.flickr.com/3551/3405309412_a32d7d8b68.jpg" alt="" width="500" height="375" /></a><br />
Before I go in any further investigation. I need to get it up &amp; running with my arduino (actually an <a href="http://www.ladyada.net/make/boarduino/">boarduino</a> &#8211; but who cares?). Read all the nifty details after the break.<br />
<span id="more-127"></span><br />
The hookup is very easy (but a little bit off standard) &#8211; remember we use I2C:<br />
<a title="P1000773_2.png" href="http://farm4.static.flickr.com/3513/3276642377_0be6f31663_o.png"></a><a href="http://farm4.static.flickr.com/3663/3404501877_0b3a857ec3_o.jpg"><img class="slickr-post colorbox-127" src="http://farm4.static.flickr.com/3663/3404501877_0c6ac4e4c9.jpg" alt="" width="500" height="375" /></a></p>
<ul>
<li><strong>VCC</strong> is connected to <strong>pin 13</strong> &#8211; so you can power this from the arduino. But you should also be able to power it directly from VCC. Remember it was 3.3V</li>
<li><strong>GND</strong> is connected to <strong>GND</strong> &#8211; this was a bit obvious.</li>
<li><strong>SCL</strong> is connected to analog <strong>pin 5</strong> (arduino&#8217;s I2C SCL)</li>
<li><strong>SCA</strong> is connect to analog <strong>pin 4</strong> (arduino&#8217;s I2C SCA)</li>
<li><strong>MISO</strong> was tied to <strong>GND</strong> &#8211; in order to select the I2C address <em>28</em> &#8211; tie this to VCC and you got address <em>29</em> &#8211; if you want to use two LIS302DL &#8211; there is perhaps some reason for this &#8211; who knows?</li>
<li><strong>INT2</strong> is connected to <strong>pin 2</strong> &#8211; in order to be able to use the interrupt. I did not &#8211; who knows? Perhaps I will do it later</li>
</ul>
<p>The basic setup was the same as with my <a href="http://interactive-matter.org/2008/08/tinkering-with-adjd-s371-q999/">ADJD</a> experiments. A 3.3V breadboard and the same I2C-routines for reading and setting registers:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> writeRegister<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> r<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> v<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> readRegister<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> r<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> v<span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// register to read</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>I2C_ADDRESS<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read a byte</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// waiting</span>
  <span style="color: #009900;">&#125;</span>
  v <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> v<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First of all we power up the LIS302DL and initialize it:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> setupLIS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Setting up LIS302DL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  pinMode<span style="color: #009900;">&#40;</span>LIS302_POWER_PIN<span style="color: #339933;">,</span> OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//the power pin</span>
  pinMode<span style="color: #009900;">&#40;</span>LIS302_INT2_PIN<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//the int2 pin</span>
&nbsp;
  digitalWrite<span style="color: #009900;">&#40;</span>LIS302_POWER_PIN<span style="color: #339933;">,</span>LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//switch off the LIS302</span>
&nbsp;
  Wire.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// join i2c bus (address optional for master)</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> startLIS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Powering up LIS302DL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>LIS302_POWER_PIN<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//switch off the LIS302</span>
&nbsp;
  delay<span style="color: #009900;">&#40;</span>POWER_UP_DELAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  writeRegister<span style="color: #009900;">&#40;</span>CTRL_REG1 <span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span>CTRL_PD<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span>CTRL_XEN<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span>CTRL_YEN<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span>CTRL_ZEN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//read who am i</span>
  <span style="color: #993333;">char</span> myself <span style="color: #339933;">=</span> readRegister<span style="color: #009900;">&#40;</span>REGISTER_WHO_AM_I<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>myself <span style="color: #339933;">==</span> WHO_AM_I_RESULT<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;yippie got an lis302DL!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;found an &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>myself<span style="color: #339933;">,</span> HEX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;! very strange!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The routine <code>setupLIS()</code> does the basic initialization while startLIS() does everything to power it up (I put it in two routines since I thought I would write an power down routine &#8211; but this did not happen until now).<br />
Most of the magic is in the routine</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  writeRegister<span style="color: #009900;">&#40;</span>CTRL_REG1 <span style="color: #339933;">,</span> _BV<span style="color: #009900;">&#40;</span>CTRL_PD<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span>CTRL_XEN<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span>CTRL_YEN<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> _BV<span style="color: #009900;">&#40;</span>CTRL_ZEN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It stops internal power down (CTRL_PD), and enables all three axis for detection. After this is done I check if the LIS302DL does correctly report to &#8216;who am I&#8217; &#8211; register <em>0x0f</em>. This is more or less an sanity check (as you can see by the diagnostic output).<br />
After you have down the right initialization magic the readout is an charm:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  <span style="color: #993333;">char</span> x <span style="color: #339933;">=</span> readRegister<span style="color: #009900;">&#40;</span>X_OUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span> y <span style="color: #339933;">=</span> readRegister<span style="color: #009900;">&#40;</span>Y_OUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span> z <span style="color: #339933;">=</span> readRegister<span style="color: #009900;">&#40;</span>Z_OUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Right now I just read it out every 100ms &#8211; you could of courser do it in a more sophisticated way by waiting until new data is ready by watching the status registers or using interrupts. But there is always something better to do. Perhaps I will add it later.</p>
<p>And before you ask how to do it yourself: <a href="http://interactive-matter.org/wp-content/uploads/2009/02/lis302dl-090213a.zip">Check the source code</a>!</p>
<p>Related posts:<ul>
<li><a href='http://interactive-matter.eu/2009/12/arduino-barometric-pressure-sensor-bmp085/' rel='bookmark' title='Arduino &amp; Barometric Pressure Sensor BMP085'>Arduino &#038; Barometric Pressure Sensor BMP085</a></li>
<li><a href='http://interactive-matter.eu/2009/07/arduino-ad7746/' rel='bookmark' title='Arduino &amp; AD7746'>Arduino &#038; AD7746</a></li>
<li><a href='http://interactive-matter.eu/2009/12/decoupling-lis302dl-%e2%80%93-there-i-fixed-it/' rel='bookmark' title='Decoupling LIS302DL – There I fixed it!'>Decoupling LIS302DL – There I fixed it!</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://interactive-matter.eu/2009/02/arduino-lis302dl/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Tinkering with ADJD-S371-Q999</title>
		<link>http://interactive-matter.eu/2008/08/tinkering-with-adjd-s371-q999/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tinkering-with-adjd-s371-q999</link>
		<comments>http://interactive-matter.eu/2008/08/tinkering-with-adjd-s371-q999/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 16:23:05 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Sensors]]></category>
		<category><![CDATA[Tinkering]]></category>
		<category><![CDATA[ADJD-S371-Q999]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Color]]></category>
		<category><![CDATA[I2C]]></category>
		<category><![CDATA[Light]]></category>
		<category><![CDATA[Sparkfun]]></category>

		<guid isPermaLink="false">http://interactive-matter.org/?p=19</guid>
		<description><![CDATA[The initial idea of &#8220;How was your day, darling&#8221; was to record the colors for a day. You know a morning is golden, evening is reddish, thunderstorm is greenish and so on. Measuring this with a simple RGB LED is a brave approach &#8211; but hard and cumbersome. So I ordered an ADJD-S371-Q999 color sensor [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="wp-caption alignnone" style="width: 500px">
	<a href="http://www.flickr.com/photos/29486873@N08/2769956227/"><img class="colorbox-19"  title="ADJD-S371-Q999" src="http://farm4.static.flickr.com/3046/2769956227_6568e49e2e.jpg" alt="The ADJD breakou board by Sparkfun" width="500" height="357" /></a>
	<p class="wp-caption-text">The ADJD breakout board by Sparkfun</p>
</div>
<p>The initial idea of &#8220;How was your day, darling&#8221; was to record the colors for a day. You know a morning is golden, evening is reddish, thunderstorm is greenish and so on. Measuring this with a simple RGB LED is a brave approach &#8211; but hard and cumbersome. So I ordered an <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8663">ADJD-S371-Q999 color sensor breakout board</a> from Sparkfun and played around with it.</p>
<p>Finding resource on this chip is very hard and getting it really working is much harder. Dealing with this complex topic is much more difficult than I thought. So I prepared some arduino code to get you started (after the break).</p>
<p><span id="more-19"></span></p>
<p>First of all: How to feed it? It takes voltages up to 3.6 volt. An USB Buarduino loves 5 volt. First I tried to take the 3V from the <a href="http://www.ftdichip.com/Products/FT232R.htm">FTDI232R</a> on the <a href="http://www.adafruit.com/index.php?main_page=product_info&amp;cPath=19&amp;products_id=91">Boardino</a>. Don&#8217;t try this at home. The 3.3V is go for 50mA but neither enough to power the whole Boardino, nor to power the color sensor or a RGB LED. The <a href="http://www.ftdichip.com/Products/FT232R.htm">FTD232R</a> got so hot that I thought I fried it &#8211; the Boardino and USB port acted quite weird. But it was just overheated.</p>
<div class="wp-caption alignnone" style="width: 500px">
	<img class="colorbox-19"  title="ADJD Breadboard" src="http://farm4.static.flickr.com/3203/2770805898_96382831c6.jpg" alt="The ADJD with Buardino on the breadboard" width="500" height="357" />
	<p class="wp-caption-text">The ADJD with Buardino on the breadboard</p>
</div>
<p>So I grabbed my 3.3V breadboard (see above, just a breadboard with some batteries, regulated down to 3.3V), disabled the Boardnio power supply, hooked up the <a href="http://en.wikipedia.org/wiki/I2c">I2C connection</a>, enabled TWI in my sketch &#8211; and here we go:</p>
<p>The TWI/I2C uses the following ports on the Arduino:</p>
<ul>
<li>SCL &#8211; the serial clock &#8211; is connected to a5 (analog input 5)</li>
<li>SDA &#8211; the serial data line &#8211; is connected to a5 (analog input 4)</li>
<li>SLEEP was tied to GND</li>
<li>LED was connected to pin 2</li>
<li>XCLK &#8211; the external clock was left floating.</li>
</ul>
<p>TWI can be enabled quite simple, just by including the header &#8220;wire.h&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include</span></pre></div></div>

<p>Grab the data sheet, find some application note, read it and pretend to understand what you are doing. Fine. But how do I actually talk to that thing? Google! At the end I came up with a sketch in a <a href="http://forum.sparkfun.com/viewtopic.php?p=46516&amp;sid=ef45536bbf0ff6baa6b96b1ff490cbcc">thread on the Sparkfun Forums</a>. It got everything to write. But reading was more or less random. Until I found out that the reading from the ADJD was like writing the adress of the register and waiting for the answer. Somewhere in the Internet I found a piece of code to use (if you know where, please leave a note in the comments &#8211; I would really like to give the correct credit. But I did not find it again.):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;Wire.h&gt;</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> serialSpeed<span style="color: #339933;">=</span><span style="color: #0000dd;">9600</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> ledPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// the light</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//ADJD Settings</span>
<span style="color: #339933;">#define ADJD 0x74</span>
&nbsp;
<span style="color: #339933;">#define CAP_RED 0x06</span>
<span style="color: #339933;">#define CAP_GREEN 0x07</span>
<span style="color: #339933;">#define CAP_BLUE 0x08</span>
<span style="color: #339933;">#define CAP_CLEAR 0x09</span>
&nbsp;
<span style="color: #339933;">#define INT_RED_LO 0x0A</span>
<span style="color: #339933;">#define INT_RED_HI 0x0B</span>
<span style="color: #339933;">#define INT_GREEN_LO 0x0C</span>
<span style="color: #339933;">#define INT_GREEN_HI 0x0D</span>
<span style="color: #339933;">#define INT_BLUE_LO 0x0E</span>
<span style="color: #339933;">#define INT_BLUE_HI 0x0F</span>
<span style="color: #339933;">#define INT_CLEAR_LO 0x10</span>
<span style="color: #339933;">#define INT_CLEAR_HI 0x11</span>
&nbsp;
<span style="color: #339933;">#define DATA_RED_LO 0x40</span>
<span style="color: #339933;">#define DATA_RED_HI 0x41</span>
<span style="color: #339933;">#define DATA_GREEN_LO 0x42</span>
<span style="color: #339933;">#define DATA_GREEN_HI 0x43</span>
<span style="color: #339933;">#define DATA_BLUE_LO 0x44</span>
<span style="color: #339933;">#define DATA_BLUE_HI 0x45</span>
<span style="color: #339933;">#define DATA_CLEAR_LO 0x46</span>
<span style="color: #339933;">#define DATA_CLEAR_HI 0x47</span>
&nbsp;
<span style="color: #339933;">#define OFFSET_RED 0x48</span>
<span style="color: #339933;">#define OFFSET_GREEN 0x49</span>
<span style="color: #339933;">#define OFFSET_BLUE 0x4A</span>
<span style="color: #339933;">#define OFFSET_CLEAR 0x4B</span>
&nbsp;
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  pinMode<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// declare the ledPin as an OUTPUT</span>
  Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span>serialSpeed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// set up Serial library at 9600 bps</span>
  Wire.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Sending Calibration data ... &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  adjd_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Calibration data sent. &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  read_register<span style="color: #009900;">&#40;</span>DATA_RED_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span>LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">4000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">void</span> adjd_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  write_register<span style="color: #009900;">&#40;</span>CAP_RED<span style="color: #339933;">,</span> <span style="color: #208080;">0x05</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>CAP_GREEN<span style="color: #339933;">,</span> <span style="color: #208080;">0x05</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>CAP_BLUE<span style="color: #339933;">,</span> <span style="color: #208080;">0x05</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>CAP_CLEAR<span style="color: #339933;">,</span> <span style="color: #208080;">0x05</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  write_register<span style="color: #009900;">&#40;</span>INT_RED_LO<span style="color: #339933;">,</span> <span style="color: #208080;">0xC4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_RED_HI<span style="color: #339933;">,</span> <span style="color: #208080;">0x09</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_GREEN_LO<span style="color: #339933;">,</span> <span style="color: #208080;">0xC4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_GREEN_HI<span style="color: #339933;">,</span> <span style="color: #208080;">0x09</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_BLUE_LO<span style="color: #339933;">,</span> <span style="color: #208080;">0xC4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_BLUE_HI<span style="color: #339933;">,</span> <span style="color: #208080;">0x09</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_CLEAR_LO<span style="color: #339933;">,</span> <span style="color: #208080;">0xC4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  write_register<span style="color: #009900;">&#40;</span>INT_CLEAR_HI<span style="color: #339933;">,</span> <span style="color: #208080;">0x09</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #993333;">void</span> read_adjd_offset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">void</span> write_register<span style="color: #009900;">&#40;</span><span style="color: #993333;">uint8_t</span> register_name<span style="color: #339933;">,</span> <span style="color: #993333;">uint8_t</span> register_value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>ADJD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>register_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>register_value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">uint8_t</span> read_register<span style="color: #009900;">&#40;</span><span style="color: #993333;">uint8_t</span> register_name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>ADJD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>register_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>ADJD<span style="color: #339933;">,</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;read:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #993333;">int</span> result <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The routine for reading the register did the trick:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">uint8_t</span> read_register<span style="color: #009900;">&#40;</span><span style="color: #993333;">uint8_t</span> register_name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span>ADJD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>register_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span>ADJD<span style="color: #339933;">,</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;read:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #993333;">int</span> result <span style="color: #339933;">=</span> Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That was a promising beginning! But the sketch did miss one big feature of the ADJD: It did not adapt to different light levels. I had to come up with a very clever idea how to calibrate it. The calibration was done adapting the integration time of the sensors to the light level. First for the clear channel (it is allays a bit more sensitive than the color levels):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> getClearGain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> gainFound <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> upperBox<span style="color: #339933;">=</span><span style="color: #0000dd;">4096</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> lowerBox <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> half<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>gainFound<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    half <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>upperBox<span style="color: #339933;">-</span>lowerBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>lowerBox<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//no further halfing possbile</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>half<span style="color: #339933;">==</span>lowerBox<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      gainFound<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      set_gain<span style="color: #009900;">&#40;</span>REG_INT_CLEAR_LO<span style="color: #339933;">,</span>half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      performMeasurement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span> halfValue <span style="color: #339933;">=</span> get_readout<span style="color: #009900;">&#40;</span>REG_DATA_CLEAR_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        upperBox<span style="color: #339933;">=</span>half<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        lowerBox<span style="color: #339933;">=</span>half<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        gainFound<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> half<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After this worked flawlessly the same trick was used to get the perfect color detection gain. According to the data sheet in both cases 1000 was used as target value (for the clear readout or the one of the colors).</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> getColorGain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> gainFound <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> upperBox<span style="color: #339933;">=</span><span style="color: #0000dd;">4096</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> lowerBox <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> half<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>gainFound<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    half <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>upperBox<span style="color: #339933;">-</span>lowerBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>lowerBox<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//no further halfing possbile</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>half<span style="color: #339933;">==</span>lowerBox<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      gainFound<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      set_gain<span style="color: #009900;">&#40;</span>REG_INT_RED_LO<span style="color: #339933;">,</span>half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      set_gain<span style="color: #009900;">&#40;</span>REG_INT_GREEN_LO<span style="color: #339933;">,</span>half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      set_gain<span style="color: #009900;">&#40;</span>REG_INT_BLUE_LO<span style="color: #339933;">,</span>half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      performMeasurement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span> halfValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
      halfValue<span style="color: #339933;">=</span>max<span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">,</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_RED_LO<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      halfValue<span style="color: #339933;">=</span>max<span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">,</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_GREEN_LO<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      halfValue<span style="color: #339933;">=</span>max<span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">,</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_BLUE_LO<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        upperBox<span style="color: #339933;">=</span>half<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>halfValue<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        lowerBox<span style="color: #339933;">=</span>half<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        gainFound<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> half<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Two small helper method were used to read or write the 16bit registers (and yes they could have been named better. The gain setting routine is this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> set_gain<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> gainRegister<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> gain<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>gain <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4096</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">uint8_t</span> hi <span style="color: #339933;">=</span> gain <span style="color: #339933;">&gt;&gt;</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">uint8_t</span> lo <span style="color: #339933;">=</span> gain<span style="color: #339933;">;</span>
&nbsp;
    set_register<span style="color: #009900;">&#40;</span>gainRegister<span style="color: #339933;">,</span> lo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    set_register<span style="color: #009900;">&#40;</span>gainRegister<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> hi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and the read routine is this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> get_readout<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> readRegister<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> read_register<span style="color: #009900;">&#40;</span>readRegister<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>read_register<span style="color: #009900;">&#40;</span>readRegister<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now I was able to get perfect readouts from the chip. The next, quite complicated task was to calibrate the ADJD. The whole setup was again covered by a half table tennis ball. But somehow the internal LED of the ADJD was far to weak to illuminate table tennis ball. So I added a second (white) LED to get a bit more effect. The calibration was done adding capacitors from the measurement (anyone who read the datasheet knows what I mean): You can set the number of capacitors used during the measurement. Unfortunately the data sheet only tells that the more capacitor you use the lower readout you get. How the capacitors work or what they are remains unclear. Now matter. The sketch was just implemented that way:</p>
<ul>
<li>The white LED is lighted</li>
<li>Each color is read and the capacitors are trimmed to get more or less the same readout from each color channel, this is repeated until the gap between the readouts do not get smaller (I know &#8211; I will come up with a better routine for this).</li>
<li>The capacitor values are stored as defaults.</li>
</ul>
<p>The sketch tries to keep the values as low as possible &#8211; since the drop between the clear and the color channels is high enough &#8211; this gives at least a comparable readout (for example if you use the same integration time for the clear and color values:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> calibrateChip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  calibrationRed <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  calibrationBlue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  calibrationGreen <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  byte calibrated <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//neede to store detect better calibration</span>
  <span style="color: #993333;">int</span> oldDiff <span style="color: #339933;">=</span> <span style="color: #0000dd;">5000</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>calibrated<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//enabled the white light</span>
    digitalWrite<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// calibrate the sensor</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// sensor gain setting (Avago app note 5330)</span>
    <span style="color: #666666; font-style: italic;">// CAPs are 4bit (higher value will result in lower output)</span>
    set_register<span style="color: #009900;">&#40;</span>REG_CAP_RED<span style="color: #339933;">,</span> calibrationRed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    set_register<span style="color: #009900;">&#40;</span>REG_CAP_GREEN<span style="color: #339933;">,</span> calibrationGreen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    set_register<span style="color: #009900;">&#40;</span>REG_CAP_BLUE<span style="color: #339933;">,</span> calibrationBlue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">int</span> colorGain <span style="color: #339933;">=</span> getColorGain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    set_gain<span style="color: #009900;">&#40;</span>REG_INT_RED_LO<span style="color: #339933;">,</span>colorGain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    set_gain<span style="color: #009900;">&#40;</span>REG_INT_GREEN_LO<span style="color: #339933;">,</span>colorGain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    set_gain<span style="color: #009900;">&#40;</span>REG_INT_BLUE_LO<span style="color: #339933;">,</span>colorGain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">int</span> maxRead <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> minRead <span style="color: #339933;">=</span> <span style="color: #0000dd;">4096</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> red<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> green<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> blue<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">4</span> <span style="color: #339933;">;</span>i <span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      performMeasurement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      red <span style="color: #339933;">+=</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_RED_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      green <span style="color: #339933;">+=</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_GREEN_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      blue <span style="color: #339933;">+=</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_BLUE_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    red <span style="color: #339933;">/=</span><span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
    green <span style="color: #339933;">/=</span><span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
    blue <span style="color: #339933;">/=</span><span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
&nbsp;
    maxRead <span style="color: #339933;">=</span> max <span style="color: #009900;">&#40;</span>maxRead<span style="color: #339933;">,</span> red<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    maxRead <span style="color: #339933;">=</span> max <span style="color: #009900;">&#40;</span>maxRead<span style="color: #339933;">,</span> green<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    maxRead <span style="color: #339933;">=</span> max <span style="color: #009900;">&#40;</span>maxRead<span style="color: #339933;">,</span> blue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    minRead <span style="color: #339933;">=</span> min<span style="color: #009900;">&#40;</span>minRead<span style="color: #339933;">,</span> red<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    minRead <span style="color: #339933;">=</span> min<span style="color: #009900;">&#40;</span>minRead<span style="color: #339933;">,</span> green<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    minRead <span style="color: #339933;">=</span> min<span style="color: #009900;">&#40;</span>minRead<span style="color: #339933;">,</span> blue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">int</span> diff <span style="color: #339933;">=</span> maxRead <span style="color: #339933;">-</span> minRead<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>oldDiff <span style="color: #339933;">!=</span> diff<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>maxRead<span style="color: #339933;">==</span>red<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;;</span> <span style="color: #009900;">&#40;</span>calibrationRed<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calibrationRed<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>maxRead <span style="color: #339933;">==</span> green<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>calibrationGreen<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calibrationGreen<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>maxRead <span style="color: #339933;">==</span> blue<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>calibrationBlue<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calibrationBlue<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      calibrated <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    oldDiff<span style="color: #339933;">=</span>diff<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">int</span> rCal <span style="color: #339933;">=</span> calibrationRed<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> gCal <span style="color: #339933;">=</span> calibrationGreen<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> bCal <span style="color: #339933;">=</span> calibrationBlue<span style="color: #339933;">;</span>
&nbsp;
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;calibration :&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>diff<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; r=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>rCal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>red<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;, g=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>gCal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>green<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; b=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>bCal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>blue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  digitalWrite<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The next step was to calibrate the RGB LED against the ADJD. If you send the same courrent to each color of the RGB LED you get some red result. Since the red LED normally is the most efficient. The least efficient LED is normally the blue one. So I developed a way to calibrate the different LED colors:</p>
<p>These routines use floating point variables, tis might not be the most efficient way &#8211; but the math was complex enough. In the end the calibration is calculated the following way:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">float</span> redFactor<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #993333;">float</span> blueFactor<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #993333;">float</span> greenFactor<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> calibrateRGB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>redPin<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>bluePin<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>greenPin<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> ledGain <span style="color: #339933;">=</span> getColorGain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  set_gain<span style="color: #009900;">&#40;</span>REG_INT_RED_LO<span style="color: #339933;">,</span>ledGain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  set_gain<span style="color: #009900;">&#40;</span>REG_INT_GREEN_LO<span style="color: #339933;">,</span>ledGain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  set_gain<span style="color: #009900;">&#40;</span>REG_INT_BLUE_LO<span style="color: #339933;">,</span>ledGain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  performMeasurement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> red<span style="color: #339933;">=</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_RED_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> green<span style="color: #339933;">=</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_GREEN_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> blue<span style="color: #339933;">=</span>get_readout<span style="color: #009900;">&#40;</span>REG_DATA_BLUE_LO<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>redPin<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>bluePin<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>greenPin<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> m<span style="color: #339933;">=</span><span style="color: #0000dd;">2000</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//bigger anyway</span>
  m<span style="color: #339933;">=</span>min<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span>red<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  m<span style="color: #339933;">=</span>min<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span>green<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  m<span style="color: #339933;">=</span>min<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span>blue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  redFactor<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>m<span style="color: #339933;">*</span><span style="color:#800080;">255.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>red<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  greenFactor<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>m<span style="color: #339933;">*</span><span style="color:#800080;">255.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>green<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  blueFactor<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>m<span style="color: #339933;">*</span><span style="color:#800080;">255.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>blue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the replay of the 10bit readouts goes like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  <span style="color: #993333;">float</span> rv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>red<span style="color: #339933;">*</span>redFactor<span style="color: #339933;">;</span>
  <span style="color: #993333;">float</span> gv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>green<span style="color: #339933;">*</span>greenFactor<span style="color: #339933;">;</span>
  <span style="color: #993333;">float</span> bv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>blue<span style="color: #339933;">*</span>blueFactor<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> r <span style="color: #339933;">=</span> rv<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> g <span style="color: #339933;">=</span> gv<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> b <span style="color: #339933;">=</span> bv<span style="color: #339933;">;</span>
  analogWrite<span style="color: #009900;">&#40;</span>redPin<span style="color: #339933;">,</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  analogWrite<span style="color: #009900;">&#40;</span>bluePin<span style="color: #339933;">,</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  analogWrite<span style="color: #009900;">&#40;</span>greenPin<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the end it gives quite usefull results.</p>
<p>I tinkered around with it a bit further and got a complete working program: <a href="http://interactive-matter.org/wp-content/uploads/2008/08/colormeter.zip">ADJD-S371</a>. It contains a lot of rubbish and the organization of the code is less than perfect. But at least it gives a good overview of how it can be done.</p>
<p>Related posts:<ul>
<li><a href='http://interactive-matter.eu/2009/02/arduino-lis302dl/' rel='bookmark' title='Arduino &amp; LIS302DL'>Arduino &#038; LIS302DL</a></li>
<li><a href='http://interactive-matter.eu/2009/12/arduino-barometric-pressure-sensor-bmp085/' rel='bookmark' title='Arduino &amp; Barometric Pressure Sensor BMP085'>Arduino &#038; Barometric Pressure Sensor BMP085</a></li>
<li><a href='http://interactive-matter.eu/2009/07/arduino-ad7746/' rel='bookmark' title='Arduino &amp; AD7746'>Arduino &#038; AD7746</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://interactive-matter.eu/2008/08/tinkering-with-adjd-s371-q999/feed/</wfw:commentRss>
		<slash:comments>75</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc (User agent is rejected)
Object Caching 1285/1366 objects using apc

Served from: interactive-matter.eu @ 2012-02-04 15:35:15 -->
