<?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>Coding Color &#187; Micro Controllers</title>
	<atom:link href="http://www.codingcolor.com/category/microcontrollers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingcolor.com</link>
	<description>is Manuel Gonzalez</description>
	<lastBuildDate>Wed, 25 May 2011 06:53:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Quartz Composer and Arduino</title>
		<link>http://www.codingcolor.com/microcontrollers/quartz-composer-and-arduino/</link>
		<comments>http://www.codingcolor.com/microcontrollers/quartz-composer-and-arduino/#comments</comments>
		<pubDate>Wed, 25 May 2011 06:33:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Micro Controllers]]></category>
		<category><![CDATA[Arduino Pro Mini]]></category>
		<category><![CDATA[MacBook]]></category>
		<category><![CDATA[Potentiometer]]></category>
		<category><![CDATA[Quartz]]></category>
		<category><![CDATA[Quartz Serial input]]></category>

		<guid isPermaLink="false">http://www.codingcolor.com/?p=3725</guid>
		<description><![CDATA[Quartz Composer is a graphics development environment that gives you access to the graphics stack of Mac OS X. Quartz uses a Visual Programming paradigm which enables you to combine the capabilities of Cocoa, Quartz 2D, Core Image, OpenGL, and QuickTime. You can create Image filters, screen savers, data-driven visual effects, live performance animations and [...]]]></description>
			<content:encoded><![CDATA[<p>Quartz Composer is a graphics development environment that gives you access to the graphics stack of Mac OS X. Quartz uses a Visual Programming paradigm which enables you to combine the capabilities of Cocoa, Quartz 2D, Core Image, OpenGL, and QuickTime. You can create Image filters, screen savers, data-driven visual effects,  live performance animations and custom user-interface widgets. </p>
<p>The Visual programing paradigm easily allows you to create prototypes by manipulating graphical API elements on the system rather than by specifying them textually as in traditional programming languages.  These system API elements are referred to as patches and are connected to create a composition. As you work with a composition, you can visually see the results in a viewer window, no compilation required.</p>
<p>The following is a really simple Quartz example utilizing an Arduino Micro-Controller as an input. We will be connecting a pot (potentiometer ) to the Arduino and measuring the amount of voltage resistance as an analog value. The Arduino will convert the value to a number between 0 and 1023 and it will print the number to the serial port . We will set up Quartz to monitor the serial port and to display the incoming value in its viewer.</p>
<p><strong>REQUIRED HARDWARE :</strong></p>
<li>Arduino Board</li>
<li>10-kilohm Potentiometer </li>
<li> Jumper wire</li>
<p><strong>REQUIRED SOFTWARE :</strong></p>
<li>Arduino IDE</li>
<li>Quartz Composer</li>
<li>Kineme Serial IO patch</li>
<p>I&#8217;m going to assume you are familiar with the Arduino board and you have already installed the Arduino Ide on your computer. If not, here&#8217;s a <a href="http://arduino.cc/en/Guide/MacOSX"><u>basic tutorial</u></a> to get you up and running. I will walk you through the necessary steps to connect Quartz Composer to the Arduino. </p>
<p>Let&#8217;s start off by identifying your <a href="http://www.apple.com/support/mobileme/en/os_version.html"><u>operating system version</u></a>, and determine if your Mac&#8217;s graphics card supports Quartz Extreme or Core Image.</p>
<p><em>*NOTE : If you use Mac OS X v10.6, your Mac already meets the requirements for Quartz Extreme and Core Image graphics.</em></p>
<p>The following steps will help you identify your graphics card support for Quartz Extreme or Core Image.</p>
<p>(1) Open System Profiler (in /Applications/Utilities).<br />
(2) Click &#8220;Graphics/Displays&#8221; under Contents.<br />
(3) In the right pane under &#8220;Display,&#8221; look for the Quartz Extreme and Core Image lines; next to each, you will see either<br />
     &#8220;Supported&#8221; or &#8220;Not Supported.&#8221;</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/system_profiler.jpg" alt="" title="system_profiler" width="518" height="481" class="aligncenter size-full wp-image-3736" /></p>
<p>Computers that have a Core Image capable graphics card will sometime display &#8220;Hardware Accelerated&#8221; and those that do not will have software written in system profiler like so: &#8220;Software (Titanium PowerBook G4) &#8221;</p>
<p>As you can see my Macbook Pro has the muscles needed to continue on to the next step of installing Apple&#8217;s Xcode suite of tools. If you haven&#8217;t already registered as an <a href="http://developer.apple.com/"><u>Apple Developer</u></a>, <em>(all the cool kids are doing it)</em> this is the perfect opportunity to do so. The main reason to register is to be able to access the downloads section of the Developer Tools and of course to get support from Apple if ever needed. You don&#8217;t have to join the paid program, choose the FREE option. </p>
<p>Here&#8217;s the direct link to the <a href="http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.3.3.3.1"><u>Apple Developer tools section</u></a><em> (you must be logged in to Apple Support to access the page)</em>. Peruse the list, download and install a compatible version of Xcode for your current operating system. </p>
<p>Once you have installed Xcode, you now have access to the Quartz Composer application. Quartz Composer is located under:</p>
<p><strong>/Developer/Applications </strong><br />
<em>(not in the standard/Applications directory)</em></p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Picture-14.png" alt="Quartz Composer file location" title="Quartz Composer location" width="739" height="435" class="aligncenter size-full wp-image-3782" /><br />
Select the Quartz Composer.app icon and drag it to your dock for easier access. The next thing you need to do is register with <a href="http://kineme.net/"><u>Kineme</u></a> to have access to their media patches. The patch we are most interested in is the <a href="http://kineme.net/release/SerialIO/200912060.2"><u>Kineme Serial IO plugin</u></a> <em>(you need to be registered to access the Quartz patch)</em>. Once you have downloaded the patch, put it in the following folder:</p>
<p><strong> /Users/[you]/Library/Graphics/Quartz Composer Patches/ </strong></p>
<p>If the folder doesn&#8217;t already exist, CREATE IT!.</p>
<p><a href="http://www.codingcolor.com/wp-content/uploads/2011/05/Serail_iO.jpg"><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Serail_iO.jpg" alt="" title="Serail_iO" width="575" height="310" class="aligncenter size-full wp-image-3786" /></a></p>
<p>That pretty much covers the software needs for the Quartz Composer end of the equation. Now, lets focus on setting up your Arduino. Grab the Arduino, jumper wire and potentiometer. Connect a wire from one of the outer pins of the potentiometer to the ground pin on the Arduino. Connect another wire to the other outer pin of the potentiometer to the 5 volts pin on the Arduino . Lastly, connect a wire from the middle pin of the potentiometer to the analog input pin 0 on the Arduino.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/AnalogReadSerial_BB.png" alt="" title="AnalogReadSerial_BB" width="321" height="372" class="aligncenter size-full wp-image-3834" /></p>
<p>Connect your Arduino board via USB to your computer and open up the Arduino IDE.<br />
Go to Tools->Serial Port and choose the entry that reads /dev/tty.usbserial-XXXXXXXX. Take note of the Serial port location, we will need to add it as a variable to the Serial IO patch in the Quartz Composer composition.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Picture-2.png" alt="" title="Picture 2" width="500" height="614" class="aligncenter size-full wp-image-3842" /></p>
<p>Next Go to Tools->Board and select your board.</p>
<p>You can either copy, type or download the source for the following Arduino sketch.</p>
<p><code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
This example shows the output of an analogRead() of a Potentiometer.<br />
By M.Gonzalez<br />
www.codingcolor.com<br />
The example code is in the public domain<br />
*/</span><br />
<span style="color: #993333;">int</span> potentiometerPin <span style="color: #339933;">=</span> A0<span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// select the input pin for the potentiometer</span><br />
<span style="color: #993333;">int</span> potentiometerValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// variable to store the value coming from the potentiometer</span><br />
<br />
<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><br />
<br />
&nbsp; Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>potentiometerPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<br />
<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><br />
&nbsp; <span style="color: #666666; font-style: italic;">// read the value from the potentiometer:</span><br />
&nbsp; potentiometerValue <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>potentiometerPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<br />
&nbsp; <span style="color: #666666; font-style: italic;">// send the value to Quartz Composer with line break</span><br />
&nbsp; Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span> potentiometerValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></span></code></p>
<p>The sketch is relatively simple. All it&#8217;s doing is reading the input from the pot and printing the data. Upload the Sketch to your Arduino and open your Serial Monitor <em>(click on the button directly to the right of the &#8220;Upload&#8221; button)</em>. You should see a steady stream of numbers ranging from 0-1023, correlating to the position of the pot. As you turn your potentiometer, these numbers will respond almost instantly.  If all went well, you can close the Arduino Ide and open up Quartz Composer.</p>
<p>The first thing you will do is create a Blank Composition.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Picture-17.png" alt="" title="QC_blank composition" width="625" height="561" class="aligncenter size-full wp-image-3853" /></p>
<p>Next, select the Patch Creator button, which will spawn the Patch Creator utility window. The window enables you to browse the available Quartz Composer patches and clips. When you select a patch name, you can read its description and pertinent information about the patch .</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/patch_library.jpg" alt="" title="patch_library" width="282" height="343" class="aligncenter size-full wp-image-3856" /></p>
<p>In the search bar enter the text &#8220;clear&#8221;. The results should display the Clear patch. The Clear patch paints the entire rendering destination (Viewer Window) with a constant color and clears the depth buffer. Either double click on the patch or drag it to the composition editor window. If the Viewer Window is open, you will notice that it&#8217;s now rendering a black screen. To change the color, you select the Clear patch in the Editor window and click on the Patch Parameters button on the toolbar at the top of the editor window.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Picture-21.png" alt="" title="Patch Parameters" width="623" height="555" class="aligncenter size-full wp-image-3859" /></p>
<p>The patch parameters pane will be shown to the right of the workspace. The pane provides convenient access for editing input parameters. You will notice that the Clear Color input is set to black, this is where you would change the color if need be.</p>
<p>Next navigate back to the Patch Creator utility window and search for &#8220;Serial input&#8221;. If you followed the Quartz Composer set up, you will have successfully found the Serial input by Kineme. Double click on the Serial Input patch or drag it to the composition editor window. With the Kineme Serial Input patch selected in the editor window, select the Patch Inspector button on the toolbar to spawn the Patch Inspector utility window. Remember when I told you to take note of the Serial port location of your Arduino, well this is the time when you need it. Enter the location of your USB port, enter the Baud rate (by default 9600) and enter &#8216;\n&#8217; into the Break String input field.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Picture-3.png" alt="" title="Picture 3" width="293" height="445" class="aligncenter size-full wp-image-3864" /></p>
<p>This Serial patch is all you need to receive data from your Arduino. Whatever the Arduino is printing out, will be available to the Quartz application. It is up to you to handle the data and transform it into a visual representation. </p>
<p>Navigate back to the Patch Creator utility window and run a search for &#8220;Image With String&#8221;. Double click on the patch or drag it to the composition editor window. Now here&#8217;s your first connection. Select the <strong>Data</strong> output on the Kineme Serial Input patch and connect it to the <strong>String</strong> input of the Image With String patch.</p>
<p><a href="http://www.codingcolor.com/wp-content/uploads/2011/05/1st_connection.jpg"><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/1st_connection.jpg" alt="" title="1st_connection" width="600" height="439" class="aligncenter size-full wp-image-3872" /></a></p>
<p>Navigate back to the Patch Creator utility window and run a search for “Billboard”. Double click on the patch or drag it to the composition editor window. Connect the <strong>Image</strong> output of the Image With String patch to the <strong>Image</strong> input on the Billboard patch. </p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2011/05/Picture-19.png" alt="" title="Final composition" width="697" height="518" class="aligncenter size-full wp-image-3886" /></p>
<p>There you have it, you should now be viewing the value of the Arduino pot in the Viewer Window. Turn the pot and the numbers will update in the viewer. This posting introduces you to the possibilities of interaction you can have between Quartz and the Arduino. If you are wondering if you can use Quartz to send data to Arduino, well the answer is yes, but I&#8217;ll save that for another post. For now get familiar with Quartz it&#8217;s a powerful application, take this example and plug it into a Particle system or make a blur filter. Swap out the pot for a Photocell, get creative!<br />
Enjoy!</p>
<a class="downloadlink" href="http://www.codingcolor.com/downloads/Potentiometer_example.zip" title="Version1.0 downloaded 9 times" >Quartz & Arduino (9)</a>
]]></content:encoded>
			<wfw:commentRss>http://www.codingcolor.com/microcontrollers/quartz-composer-and-arduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I ♥ the Arduino Pro Mini</title>
		<link>http://www.codingcolor.com/microcontrollers/i-the-arduino-pro-mini/</link>
		<comments>http://www.codingcolor.com/microcontrollers/i-the-arduino-pro-mini/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 07:05:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Micro Controllers]]></category>
		<category><![CDATA[Arduino Pro Mini]]></category>
		<category><![CDATA[Arduino Pro Mini 328 - 5V]]></category>
		<category><![CDATA[BreadBoard]]></category>
		<category><![CDATA[microcontroller]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[RAW]]></category>
		<category><![CDATA[RESET]]></category>
		<category><![CDATA[right angle headers]]></category>
		<category><![CDATA[straight headers]]></category>
		<category><![CDATA[VCC]]></category>

		<guid isPermaLink="false">http://www.codingcolor.com/?p=3251</guid>
		<description><![CDATA[I assume you have either made the leap from the Arduino Duemilanove to an Arduino Pro Mini or you are contemplating to do so. In a nutshell the Arduino Pro Mini is basically an Arduino Duemilanove ATMega328 without a USB port and FTDI chip. It&#8217;s a really small board, approximately 0.7&#8243; x 1.3&#8243;. You can [...]]]></description>
			<content:encoded><![CDATA[<p>I assume you have either made the leap from the <a href="http://www.sparkfun.com/products/666" target="blank"><u>Arduino Duemilanove</u></a> to an <a href="http://www.arduino.cc/en/Main/ArduinoBoardProMini" target="blank"><u>Arduino Pro Mini</u></a> or you are contemplating to do so. In a nutshell the Arduino Pro Mini is basically an Arduino Duemilanove ATMega328 without a USB port and FTDI chip. It&#8217;s a really small board, approximately 0.7&#8243; x 1.3&#8243;. You can find Arduino Pro Mini&#8217;s in semi-permanent installations, in objects or exhibitions, where the engineers had limited space or wanted to conceal the micro-controller.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/11/ArduinoProMini-300x168.jpg" alt="" title="ArduinoProMini" width="300" height="168" class="aligncenter size-medium wp-image-3310" /><br />
It&#8217;s pretty much my go to micro-controller when prototyping interactive applications. A side from being tiny, it&#8217;s the least expensive of the Arduino family, at $18 a piece. Since the Arduino Pro Mini does not have a USB port or FTDI chip, you will need a serial connection to program the board. I recommend the <a href="http://www.sparkfun.com/products/9716" target="blank"><u>SparkFuns FTDI Basic board</u></a>.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/11/Picture-2-300x215.png" alt="" title="new basic board" width="300" height="215" class="aligncenter size-medium wp-image-3329" /> You only have to buy one FTDI Basic board, to program as many Arduino Pro Mini(s) you can afford. </p>
<p>The Arduino Pro Mini comes in two flavors, 3.3 volt or 5 volt. I prefer the 5 volt option. I alter the voltage when need be, especially when working with sensors that operate at 3.3V . A few other things you will find a bit different migrating from the Duemilanove are the RAW, VCC and RESET pins.  The RAW pin enables you to supply the board with a raw unregulated voltage (5-12 volts, depending on the model). The VCC pins requires a regulated 3.3 or 5 volt supply (once again, depending on the model) to power the board. I usually power the board via the RAW pin and use the VCC pin to output a regulated 5v power source. The RESET pin is typically used to add a reset button to shields which block the one on the board. </p>
<p>You will notice that the board has analog pins 4(sda) and 5(scl) on the interior of the board. Pins 4 and 5 have a specialized functionality. The pins are used to support I2C (TWI) communications using the <a href="http://www.arduino.cc/en/Reference/Wire" target="blank"><u>Wire library</u></a>. That pretty much sums up the differences between the Duemilanove beginners board and the Arduino Pro Mini. I love the size of the Pro Mini. It compliments most of my prototyping applications and gives me the inputs and outputs I have grown to love from the  Duemilanove. </p>
<p>I recommend you take an Arduino Pro Mini and set it up to be Breadboard friendly. To do this you will need a medium size Breadboard, a set of <a href="http://www.sparkfun.com/products/116" target="blank">straight headers</a> and (optional) a set of <a href="http://www.sparkfun.com/products/553" target="blank">Right Angle</a> breakaway headers. The next steps provide you with info on how to set up your Arduino Pro Mini to be Breadboard friendly.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/11/setUpProMini.jpg" alt="" title="setUpProMini" width="500" height="400" class="aligncenter size-full wp-image-3340" /></p>
<p>Take the break away headers and cut them to fit the Arduino Pro Mini. You will need (2) 12 pin headers, (1) 6 pin header and (1) 2 pin header.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/11/setUpProMini_breakaway.jpg" alt="" title="setUpProMini_breakaway" width="500" height="400" class="aligncenter size-full wp-image-3343" /></p>
<p>Now get your soldering iron out and lets solder the headers onto the board. The first thing I do is solder the 6 pin header to the top of the Arduino board. This is where the Arduino board will connect with the Basic FTDI board. I prefer to use Right Angle headers for the 6 pin. Place the 6 pin header on the Arduino board and flip the board over keeping the header pins in place and solder. Take a pair of wire snipers and snip the excess wire from the bottom of the board to allow the Arduino Mini to set flush up against a Breadboard. You should now have an Arduino board that looks like this (if you used the Right angle header):</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/11/proMiniTopHeader.jpg" alt="" title="proMiniTopHeader" width="174" height="295" class="aligncenter size-full wp-image-3347" /></p>
<p>Next, grab the medium size Breadboard and insert the 2 pin header into the Breadboard (shorter side of the pins up). Then flip the Arduino board over (backside up) and set the analog A4 and A5 holes of the Arduino onto the header pins. Align the Arduino board parallel to the Breadboard and solder. When you are done grab your snipers and cut the excess wire on the bottom of the Arduino board. You should now have 2 pins sticking out of the top of the Arduino board, enabling access to analog pins 4 and 5. </p>
<p>Placing the headers on a Breadboard prior to soldering helps, keep the Arduino Mini in place. In the next step we use this technique to add the 12 pin headers.</p>
<p>Simply place one of the 12 pin headers (short side of the pins up) onto the Breadboard. Place the Arduino Mini (top side up) onto the header. Take a mental note of the placement of the holes in relation to the Breadboard and place the other 12 pin header in its place. You should now have a stable Arduino Mini sitting on top of the 12 pin headers. All you have left to do is solder the headers in place. The end result should look like this:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/11/setUpProMini_final.jpg" alt="" title="setUpProMini_final" width="243" height="376" class="aligncenter size-full wp-image-3375" /></p>
<p>That concludes my instructional. </p>
<p>I have one more cool tip for you. Look into <a href="http://shop.moderndevice.com/products/wired-breadboard" target="blank"><u>Modern Devices wired Breadboard</u></a>. They make a foolproof beginners PCB board  that enables you to transfer your project directly from your solderless breadboard to the PCB, now that&#8217;s cool!  This is just the tip of the iceberg, I will release other topics primarily featuring the Arduino Pro Mini, so subscribe to my <a href="http://www.codingcolor.com/feed/" target="blank"><u>RSS</u></a> feed to get the latest and greatest on my Arduino adventures.<br />
Till next time, Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingcolor.com/microcontrollers/i-the-arduino-pro-mini/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An Arduino Pro Mini ultra-sonic beeping thingamajiggy</title>
		<link>http://www.codingcolor.com/microcontrollers/an-arduino-pro-mini-ultra-sonic-beeping-thingamajiggy/</link>
		<comments>http://www.codingcolor.com/microcontrollers/an-arduino-pro-mini-ultra-sonic-beeping-thingamajiggy/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 04:09:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Micro Controllers]]></category>
		<category><![CDATA[8 ohm mini speaker]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Arduino Pro Mini 328 - 5V]]></category>
		<category><![CDATA[BlinkM RGB Led]]></category>
		<category><![CDATA[FTDI Basic Breakout - 5V]]></category>
		<category><![CDATA[Parallax's PING)))™ ultrasonic sensor]]></category>
		<category><![CDATA[ultrasonic sensor]]></category>

		<guid isPermaLink="false">http://www.codingcolor.com/?p=2740</guid>
		<description><![CDATA[The other day I received an email from an associate, who inquired if I was familiar with the Polaroid sx70 SONAR module. I replied with a simple NO, but I do have experience working with low-cost ultrasonic sensors like the Parallax&#8217;s PING)))™ ultrasonic sensor, Maxbotix ultrasonic rangefinder and the Devantech SRF02. Sonar or ultrasonic sensors [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I received an email from an associate, who inquired if I was familiar with the Polaroid sx70 SONAR <a href="http://www.acroname.com/robotics/parts/R11-6500.html" target="blank"><u>module</u></a>. I replied with a simple NO, but I do have experience working with low-cost ultrasonic sensors like the <a href="http://www.parallax.com/tabid/768/ProductID/92/Default.aspx" target="blank"><u>Parallax&#8217;s PING)))™ ultrasonic sensor</u></a>, <a href="http://www.maxbotix.com/BUY_NOW.html#LV-EZ" target="blank"><u>Maxbotix ultrasonic rangefinder</u></a> and the <a href="http://www.acroname.com/robotics/parts/R287-SRF02.html" target="blank"><u>Devantech SRF02</u></a>. Sonar or ultrasonic sensors are often used in mobile robotics for obstacle avoidance, navigation, map building and in interactive art installations where you need to detect an object in a room or space. Ultrasonic range sensors work by emitting a short burst of ultrasonic sound, which is reflected by objects in front of the device and returned to the detector. The receiver amplifier sends these reflected signals (echoes) to a micro-controller which times them to determine how far away the objects are, in other words measures the distance.  </p>
<p>So, in the spirit of teaching I decided to write a post on the Arduino Pro Mini and the Parallax Ultrasonic sensor. I went the extra mile and added audio feedback via a 8-ohm mini speaker, visual feedback via a BlinkM Led and included an external power source. </p>
<p>You will need the following components to build an ultra-sonic beeping thingamajiggy:</p>
<li><a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9218" target="blank"><u>Arduino Pro Mini 328 &#8211; 5V/16MHz</u></a></li>
<li><a href="http://www.sparkfun.com/commerce/product_info.php?products_id=116" target="blank"><u>Break Away Headers &#8211; Straight</u></a></li>
<li><a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9115" target="blank"><u>FTDI Basic Breakout &#8211; 5V</u></a> or <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9718" target="blank"><u>FTDI Cable 5V</u></a></li>
<li><a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8579"><u>BlinkM RGB Led</u></a></li>
<li><a href="http://www.parallax.com/tabid/768/ProductID/92/Default.aspx" target="blank"><u>Parallax&#8217;s PING)))™ ultrasonic sensor</u></a></li>
<li><a href="http://www.radioshack.com/product/index.jsp?productId=2062406" target="blank"><u>8-ohm mini speaker</u></a></li>
<li><a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9567" target="blank"><u>Basic breadboard</u></a></li>
<li><a href="http://www.radioshack.com/product/index.jsp?productId=2062253" target="blank"><u>Enclosed 4 &#8220;AA&#8221; battery holder</u></a></li>
<li><a href="http://www.sparkfun.com/commerce/product_info.php?products_id=124" target="blank"><u>Jumper wire</u></a></li>
<p>Lets start by addressing the Arduino Pro Mini.<br />
The <a href="http://www.arduino.cc/en/Main/ArduinoBoardProMini" target="blank"><u>Arduino Pro Mini</u></a> is intended for advanced users who require flexibility, low-cost, and small size. Please note that there are two versions of the board, one that operates at 5V (like most Arduino boards), and one that operates at 3.3V. Both boards come with the minimum of components (no on-board USB or pin headers) to keep the cost down.  The <a href="http://www.arduino.cc/" target="blank"><u>Arduino Site</u></a> has an overview on the <a href="http://www.arduino.cc/en/Main/ArduinoBoardProMini" target="blank"><u>Arduino Pro Mini</u></a>, I suggest you at least review the <a href="http://www.arduino.cc/en/Main/ArduinoBoardProMini" target="blank"><u>page</u></a> to get familiar with the Inputs and Outputs of the board. The Arduino Pro Mini is programmed using a FTDI Cable or in my case a FTDI Basic Breakout board. </p>
<p>Since we are using a breadboard to prototype the ultra-sonic beeping thingamajiggy, I suggest you solder the break away headers to the Arduino, in fact, I always suggest creating connectors for sensors rather than just soldering them to the Arduino. A quick soldering tip is to stick the headers into a breadboard and place the Arduino board on top. Then solder all headers. Make sure you also solder headers to Analog 4 and 5, which you will find on the board near the ATmega328 chip. You should now have something that resembles this:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/Arduino_pro_mini.jpg" alt="" title="Arduino_pro_mini" width="550" height="413" class="aligncenter size-full wp-image-2815" /></p>
<p>Now, lets hook up some power. This part is very important, you don&#8217;t want to fry your board so pay close attention. Locate The power pins on your Arduino Pro Mini:</p>
<p>* RAW. For supplying a raw (unregulated) voltage to the board.<br />
* VCC. The regulated 3.3 or 5 volt supply.<br />
* GND. Ground pins.</p>
<p>The VCC pin on the Arduino Pro Mini is bypassing the regulator and you should NOT CONNECT ANY POWER SUPPLY ABOVE 5 VOLTS to this pin. In most cases you will use the RAW pin to connect a power supply (up to 12V) in need of regulation.</p>
<p>The <a href="http://www.radioshack.com/product/index.jsp?productId=2062253" target="blank"><u>enclosed 4 &#8220;AA&#8221; battery holder</u></a> from Radio Shack, has a voltage of 6 volts, therefore I have no other choice but to connect it to the unregulated RAW pin. </p>
<p>ONLY USE THE VCC PIN IF YOU ARE SURE YOU HAVE A REGULATED 5 VOLT POWER SOURCE. </p>
<p>I took the battery enclosure and added a <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8432" target="blank"><u>2 pin screw terminal</u></a> to the end of the wires to make the connection to the breadboard easier.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/Enclosed_battery.jpg" alt="" title="Enclosed_battery" width="550" height="413" class="aligncenter size-full wp-image-2822" /></p>
<p>Make sure the battery enclosure switch is on the OFF state and then connect the power supply to the breadboard.<br />
Add the ground (black) wire from the battery enclosure to the blue rail of the breadboard.<br />
Add the power (red) wire from the battery enclosure to the red rail of the breadboard.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/arduino_battery.jpg" alt="" title="arduino_battery" width="550" height="413" class="aligncenter size-full wp-image-2825" /></p>
<p>Grab some jumper wires and connect a wire from the blue rail of the breadboard to the GND pin on the Arduino (green wire in image below). Connect a wire from the red rail of the breadboard to the RAW pin on the Arduino (blue wire in image below).</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/Pro_mini_powered.jpg" alt="" title="Pro_mini_powered" width="550" height="413" class="aligncenter size-full wp-image-2834" /></p>
<p>Next lets connect the ultrasonic sensor.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/06/pRS1C-4174483w345.jpg" alt="" title="pRS1C-4174483w345" width="345" height="235" class="aligncenter size-full wp-image-2837" /><br />
You will notice the sensor only has 3 pins, the 5V pin, the GND pin and the SIG (signal) pin. You can easily place the senor onto your breadboard and wire it up. I had some extra <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9385" target="blank"><u>Male/Female jumper wires</u></a>, so I utilized them to free up some space on the breadboard.<br />
Connect the GND pin of the sensor to the blue rail on the breadboard (black wire in image below).<br />
Connect the 5V pin of the sensor to the red rail on the breadboard (red wire in image below).<br />
Connect the SIG pin of the sensor to pin 7 of the Arduino (blue wire in image below).</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/ultrasonic_sensor.jpg" alt="" title="ultrasonic_sensor" width="550" height="413" class="aligncenter size-full wp-image-2840" /></p>
<p>If connecting the sensor is all you cared about, you could stop here. All you have left to do is connect the FTDI Cable or FTDI Basic breakout board to the Arduino Pro Mini. Open up the Arduino IDE:<br />
Select Tools -> Serial Port<br />
and select the correct usb port (mac) /dev/tty.usbserial-XXXXXXXX.<br />
Select Tools->Board<br />
and select Lilypad Arduino w/Atmega328 (I&#8217;m running Arduino IDE 018 and for some unknown reason this selection is the only one which enables an upload without errors).<br />
Select File-> Examples->Sensors->Ping<br />
and upload the sketch.<br />
Your done! If you open up the Serial Monitor, it will display the distance being detected from the sensor in inches and cm&#8217;s. </p>
<p>Moving forward, lets add the BlinkM RGB Led. If you read my previous post on the <a href="http://www.codingcolor.com/microcontrollers/using-an-arduino-to-control-a-blinkm-with-a-photocell/" target="blank"><u>BlinkM</u></a> you will be familiar with the process.  If not, grab your BlinkM and lets get familar with it.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/tm_blinkm_getone.jpg" alt="" title="tm_blinkm_getone" width="300" height="225" class="aligncenter size-full wp-image-2551" /></p>
<p>The BlinkM is a networkable and programmable full-color RGB LED. It has a small AVR micro-controller on board to allow a user to digitally control the LED over a simple I2C interface. The Arduino will communicate with the BlinkM, using the Wire library so we don’t have to worry about the details of I2C. If you haven&#8217;t already checked out the BlinkM <a href="http://thingm.com/products/blinkm/quick-start-guide.html" target="blank"><u>support</u></a> page please do so, it has a bunch of perks you should be aware of. Take a look at the print on the board itself. You will notice PWR and I2C printed directly on the board, take mental note because that&#8217;s what we will be interfacing with shortly.  BlinkM needs two wires for power and two for data. We will be connecting to the Arduino analog pins 4 &#038; 5 which double as the I2C data signal (“SDA”) and clock signal (“SCL”), respectively.</p>
<p>Place the BlinkM onto your breadboard and connect a wire (red wire in image below) from the red rail on the breadboard to the positive (+) symbol printed on the BlinkM. Connect a wire (brown wire in image below) from the blue rail on the breadboard to the ground (-) symbol printed on the BlinkM. </p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/blinkm_powered.jpg" alt="" title="blinkm_powered" width="550" height="413" class="aligncenter size-full wp-image-2864" /></p>
<p>Proceed by connecting a wire (blue wire in image below) to analog 4 of the Arduino Pro Mini. Connect the other end of the wire to the ‘d’ (I2C SDA) symbol printed on the BlinkM.<br />
Connect another wire (green wire in image below) to analog 5 of the Arduino Pro Mini. Connect the other end of the wire to the ‘c’ (I2C SCL) symbol printed on the BlinkM.</p>
<p><a href="http://www.codingcolor.com/wp-content/uploads/2010/06/BlinkM_connected.jpg"><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/BlinkM_connected.jpg" alt="" title="BlinkM_connected" width="550" height="413" class="aligncenter size-full wp-image-2867" /></a></p>
<p>Next, lets hook up the 8 ohm mini speaker for audio feedback.<br />
Simply connect the black wire from the speaker to the blue rail of the breadboard. Connect the red wire from the speaker to digital pin 7 on the Arduino Pro Mini.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/thingamajiggy.jpg" alt="" title="thingamajiggy" width="550" height="404" class="aligncenter size-full wp-image-2870" /></p>
<p>And there you have it. We have completed our simple circuit for an ultra-sonic beeping thingamajiggy.<br />
Lets move onto the code.</p>
<p>The first thing you are going to want to do is connect the FTDI Cable or FTDI Basic breakout board to the Arduino Pro Mini. There are pros and cons to the FTDI Cable vs the FTDI Basic breakout board . The breakout board has TX and RX LEDs that allow you to actually see serial traffic on the LEDs to verify if the board is working, but the board requires a miniB cable. The FTDI Cable is well protected against the elements, but is large and cannot be embedded into a project as easily. The FTDI Basic breakout board uses DTR to cause a hardware reset where the FTDI Cable uses the RTS signal.</p>
<p>I prefer to use the FTDI Basic breakout board to connect to the Arduino pro Mini. Anyhow, connect either the FTDI Cable or Basic breakout board to the Arduino and open up the Arduino IDE.<br />
Select Tools -> Serial Port<br />
and select the correct usb port (mac) /dev/tty.usbserial-XXXXXXXX.<br />
Select Tools->Board<br />
and select Lilypad Arduino w/Atmega328.<br />
Select File->New<br />
and copy the following code to the sketch:<br />
<strong>*NOTE you can download my source below*</strong></p>
<p><code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
Code under (cc) by Manuel Gonzalez, www.codingcolor.com<br />
http://creativecommons.org/license/cc-gpl<br />
Digital pin 7 Parallax Ultrasonic Range Finder<br />
Digital Pin 4 8-ohm mini speaker<br />
Analog pins 4 (SDA),5(SCL) I2C communication with BlinkM<br />
*/</span><br />
<span style="color: #339933;">#include &lt;Wire.h&gt;</span><br />
<span style="color: #339933;">#include &quot;BlinkM_funcs.h&quot;</span><br />
<br />
<br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> pingPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> speakerPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> blinkm_addr <span style="color: #339933;">=</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//address to blinkM</span><br />
byte r<span style="color: #339933;">,</span>g<span style="color: #339933;">,</span>b<span style="color: #339933;">;</span><br />
<br />
<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><br />
&nbsp;<span style="color: #666666; font-style: italic;">// Serial.begin(9600);</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>speakerPin<span style="color: #339933;">,</span>OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; BlinkM_beginWithPower<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; BlinkM_stopScript<span style="color: #009900;">&#40;</span>blinkm_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//stop the BlinkM </span><br />
&nbsp; BlinkM_setFadeSpeed<span style="color: #009900;">&#40;</span>blinkm_addr<span style="color: #339933;">,</span> <span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//set up fade param</span><br />
&nbsp; BlinkM_fadeToRGB<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #208080;">0x00</span><span style="color: #339933;">,</span><span style="color: #208080;">0x00</span><span style="color: #339933;">,</span><span style="color: #208080;">0x00</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//fade all to black</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #993333;">long</span> duration<span style="color: #339933;">,</span> inches<span style="color: #339933;">;</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>pingPin<span style="color: #339933;">,</span> OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; digitalWrite<span style="color: #009900;">&#40;</span>pingPin<span style="color: #339933;">,</span> LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; delayMicroseconds<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; digitalWrite<span style="color: #009900;">&#40;</span>pingPin<span style="color: #339933;">,</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; delayMicroseconds<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; digitalWrite<span style="color: #009900;">&#40;</span>pingPin<span style="color: #339933;">,</span> LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>pingPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; duration <span style="color: #339933;">=</span> pulseIn<span style="color: #009900;">&#40;</span>pingPin<span style="color: #339933;">,</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// convert the time into a distance</span><br />
&nbsp; inches <span style="color: #339933;">=</span> microsecondsToInches<span style="color: #009900;">&#40;</span>duration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; Serial.print(inches);<br />
&nbsp; Serial.print(&quot;in, &quot;);<br />
&nbsp; Serial.println(); <br />
&nbsp; */</span><br />
&nbsp; displayColor<span style="color: #009900;">&#40;</span>inches<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> displayColor<span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> inches<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>inches <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">12</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;r <span style="color: #339933;">=</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;g <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;b <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; beep<span style="color: #009900;">&#40;</span>speakerPin<span style="color: #339933;">,</span> <span style="color: #0000dd;">2500</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;r <span style="color: #339933;">=</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;g <span style="color: #339933;">=</span> <span style="color: #0000dd;">249</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;b <span style="color: #339933;">=</span> <span style="color: #0000dd;">17</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; &nbsp;BlinkM_fadeToRGB<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> &nbsp;g<span style="color: #339933;">,</span> &nbsp;b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> beep<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> targetPin<span style="color: #339933;">,</span> <span style="color: #993333;">long</span> frequency<span style="color: #339933;">,</span> <span style="color: #993333;">long</span> length<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #993333;">long</span> delayValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">1000000</span><span style="color: #339933;">/</span>frequency<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// calculate the delay value between transitions</span><br />
&nbsp; <span style="color: #993333;">long</span> numCycles <span style="color: #339933;">=</span> frequency <span style="color: #339933;">*</span> length<span style="color: #339933;">/</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// calculate the number of cycles for proper timing</span><br />
&nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</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> numCycles<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; digitalWrite<span style="color: #009900;">&#40;</span>targetPin<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; delayMicroseconds<span style="color: #009900;">&#40;</span>delayValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; digitalWrite<span style="color: #009900;">&#40;</span>targetPin<span style="color: #339933;">,</span>LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; delayMicroseconds<span style="color: #009900;">&#40;</span>delayValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">long</span> microsecondsToInches<span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> microseconds<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// According to Parallax's datasheet for the PING))), there are</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// second). &nbsp;This gives the distance travelled by the ping, outbound</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// and return, so we divide by 2 to get the distance of the obstacle.</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf</span><br />
&nbsp; <span style="color: #b1b100;">return</span> microseconds <span style="color: #339933;">/</span> <span style="color: #0000dd;">74</span> <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></span></code></p>
<p>Take note of the include.<br />
<code class="codecolorer c mac-classic"><span class="c"><span style="color: #339933;">#include &quot;BlinkM_funcs.h&quot;</span></span></code><br />
It&#8217;s a BlinkM Arduino library written by <a href="http://thingm.com/" target="blank">Tod E. Kurt</a>.  You will need the BlinkM_funcs.h library which is also availbe in my download.</p>
<p>Upload the code to your Arduino Pro Mini disconnect the FTDI Cable or Basic breakout board and turn the power on.</p>
<p>If all went well, you should have an ultra-sonic beeping thingamajiggy.</p>
<p><a href="http://www.codingcolor.com/microcontrollers/an-arduino-pro-mini-ultra-sonic-beeping-thingamajiggy/"><em>Click here to view the embedded video.</em></a></p>
<p>Enjoy!<br />
<strong>Download Source:</strong><br />
<a class="downloadlink" href="http://www.codingcolor.com/downloads/UltraSonicBlinkM.zip" title="Version1.0 downloaded 40 times" >Ultra-Sonic beeping thingamajiggy (40)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingcolor.com/microcontrollers/an-arduino-pro-mini-ultra-sonic-beeping-thingamajiggy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using an Arduino to control a blinkM with a photocell</title>
		<link>http://www.codingcolor.com/microcontrollers/using-an-arduino-to-control-a-blinkm-with-a-photocell/</link>
		<comments>http://www.codingcolor.com/microcontrollers/using-an-arduino-to-control-a-blinkm-with-a-photocell/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 05:27:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Micro Controllers]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[blinkM]]></category>
		<category><![CDATA[BreadBoard]]></category>
		<category><![CDATA[Night Light]]></category>
		<category><![CDATA[pde]]></category>
		<category><![CDATA[Photocell]]></category>

		<guid isPermaLink="false">http://www.codingcolor.com/?p=2511</guid>
		<description><![CDATA[As mentioned in my previous post Arduino Night Light using a Photocell, I was awaiting the arrival of a BlinkM via snail mail. Days later the good ole mail man handed me the package which contained my BlinkM(s) and warranted this post. The night light prototype that we will be building basically, changes the color [...]]]></description>
			<content:encoded><![CDATA[<p>As mentioned in my previous post <a href="http://www.codingcolor.com/microcontrollers/arduino-night-light-using-a-photocell/"><u>Arduino Night Light using a Photocell</u></a>, I was awaiting the arrival of a <a href="http://thingm.com/products/blinkm.html" target="blank"><u>BlinkM</u></a> via snail mail.  Days later the good ole mail man handed me the package which contained my  BlinkM(s) and warranted this post. The night light prototype that we will be building basically, changes the color of the BlinkM based off the photocells reading.  For simplicity and for testing purposes, it only displays two states, night and day, and represents the states with the color red (day) and blue(night). </p>
<p>Make sure you have all the components needed to recreate this prototype:</p>
<li>Arduino Duemilanove</li>
<li>Breadboard</li>
<li>10 K resistor</li>
<li>BlinkM</li>
<li>Photocell</li>
<li>Breadboard wires</li>
<p>Lets start building the prototype. </p>
<p>The first thing I always do when prototyping, is connect the Arduino to a power source either via the USB or an external power supply and then I proceed to wire up the bread board to the Arduino power. </p>
<p>**Warning**<br />
I advise you to read datasheets for the components that you will be working with, be aware that you may need to disconnect all power before connecting or disconnecting components to a circuit.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/simplePower.jpg" alt="" title="simplePower" width="550" height="450" class="aligncenter size-full wp-image-2603" /></p>
<p>I simply take a Red wire from the Arduino 5v(+) and connect it to the red rail on the breadboard.  Then take a Black wire and connect it to the GND of the Arduino and connect it to the blue rail on the Breadboard. We now have power!</p>
<p>The next step is to connect the Photocell to the breadboard and wire it up to the Arduino.<br />
Add the Photocell to the board. Connect a Red wire from the red rail on the board to one of the leads of the Photocell.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/photocel_power.jpg" alt="" title="photocel_power" width="550" height="450" class="aligncenter size-full wp-image-2606" /></p>
<p>Now, lets add the 10k resistor. Connect one end of the resistor to the other lead of the Photocell.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/photocell_resistor.jpg" alt="" title="photocell_resistor" width="550" height="450" class="aligncenter size-full wp-image-2607" /></p>
<p>Connect a wire from the same Photocell lead that the resistor resides on to Analog 0 on the Arduino. Then ground the resistor by adding a black wire from the blue rail on the breadboard to the other end of the resistor, like so. </p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/photcell_final.jpg" alt="" title="photcell_final" width="550" height="450" class="aligncenter size-full wp-image-2609" /></p>
<p>You have successfully created a basic Photocell circuit. Now, for a sanity check copy the code below, open up the Arduino Ide and create a new photocellTest.pde file, compile, upload to your Arduino and view the output of the Photocell in the serial monitor.<br />
<code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
This example shows the output of an analogRead() of a Photocell.<br />
By M.Gonzalez<br />
www.codingcolor.com<br />
The example code is in the public domain<br />
*/</span><br />
<span style="color: #993333;">int</span> photocellPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Photocell connected to analog pin 0</span><br />
<span style="color: #993333;">int</span> photocellVal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define photocell variable</span><br />
<br />
<br />
<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><br />
&nbsp; Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<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><br />
&nbsp; photocellVal <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// read the analog from photocell</span><br />
&nbsp; Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span> photocellVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// print to screen</span><br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp;<span style="color: #009900;">&#125;</span></span></code></p>
<p>If the Photocell circuit is correct you should be viewing numbers printed in your serial monitor. Place your hand over the resistor to simulate darkness and witness the numbers change. We will be using the Photocell output as a switch to change the color of the BlinkM for red to blue. Grab your BlinkM and lets get familiar with it.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/tm_blinkm_getone.jpg" alt="" title="tm_blinkm_getone" width="300" height="225" class="aligncenter size-full wp-image-2551" /><br />
The BlinkM is a networkable and programmable full-color RGB LED. It has a small AVR micro-controller on board to allow a user to digitally control the LED over a simple I2C interface. The Arduino will communicate with the BlinkM, using the Wire library so we don’t have to worry about the details of I2C. If you haven&#8217;t already checked out the BlinkM <a href="http://thingm.com/products/blinkm/quick-start-guide.html" target="blank"><u>support</u></a> page please do so, it has a bunch of perks you should be aware of. Take a look at the print on the board itself. You will notice PWR and I2C printed directly on the board, take mental note because that&#8217;s what we will be interfacing with shortly.  BlinkM needs two wires for power and two for data. We will be connecting to the Arduino analog pins 4 &#038; 5 which double as the I2C data signal (“SDA”) and clock signal (“SCL”), respectively. Place the BlinkM onto your breadboard.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/blinkM_onBoard.jpg" alt="" title="blinkM_onBoard" width="550" height="450" class="aligncenter size-full wp-image-2563" /></p>
<p>Power up the BlinkM by connecting a Red wire from the red rail on the breadboard to the positive (+) symbol printed on the BlinkM.  Connect a Black wire from the blue rail on the breadboard to the ground (-) symbol printed on the BlinkM. If the power connections are correct, you will notice a pre-programmed color sequence which comes shipped as a default with the BlinkM. </p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/blinkM_powered.jpg" alt="" title="blinkM_powered" width="550" height="450" class="aligncenter size-full wp-image-2566" /></p>
<p>Proceed by hooking up the BlinkM to the Arduino. Grab a breadboard wire and connect it to analog 4 of the Arduino. Connect the other end of the wire to the &#8216;d&#8217; (I2C SDA) symbol printed on the BlinkM. Grab another breadboard wire and connect it to analog 5 of the Arduino. Connect the other end of the wire to the &#8216;c&#8217; (I2C SCL) symbol printed on the BlinkM.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/blinkM_complete.jpg" alt="" title="blinkM_complete" width="550" height="450" class="aligncenter size-full wp-image-2573" /></p>
<p>The prototype circuit is complete. Now, lets focus on the code. I wrote a simple sketch which utilizes <a href="http://thingm.com/" target="blank">Tod E. Kurt&#8217;s</a> BlinkM Arduino library to fade the colors of the LED from red to blue.  You will need the BlinkM_funcs.h library which you can obtain from the BlinkM example <a href="http://thingm.com/fileadmin/thingm/downloads/BlinkM_Examples.zip" target="blank"><u>files</u></a>. Better yet, I have made it available with my source code down below. Either way, you will have to place the BlinkM_funcs.h in the same directory as the Arduino sketch and “#include” it at the top to import all the functions. The BlinkM has many features, and I would advise you to download the <a href="http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf" target="blank"><u>datasheet</u></a> if your interested, anyhow, here&#8217;s my simple sketch.</p>
<p><code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
Code under (cc) by Manuel Gonzalez, www.codingcolor.com<br />
http://creativecommons.org/license/cc-gpl<br />
Analog pin 0 photocell<br />
Analog pins 4 (SDA),5(SCL) I2C communication with BlinkM<br />
*/</span><br />
<span style="color: #339933;">#include &lt;Wire.h&gt;</span><br />
<span style="color: #339933;">#include &quot;BlinkM_funcs.h&quot;</span><br />
<br />
<br />
<span style="color: #993333;">int</span> blinkm_addr <span style="color: #339933;">=</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default address to blinkM</span><br />
byte r<span style="color: #339933;">,</span>g<span style="color: #339933;">,</span>b<span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span> photocellPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Photocell connected to analog pin 0</span><br />
<span style="color: #993333;">int</span> photocellVal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define photocell variable</span><br />
<span style="color: #993333;">int</span> minLight <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//min light threshold</span><br />
<span style="color: #993333;">int</span> maxLight <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//max light threshold</span><br />
<span style="color: #993333;">int</span> ledState <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//state of the led 0 = dark, 1 = light </span><br />
<br />
<br />
<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><br />
&nbsp; <br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; BlinkM_beginWithPower<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; BlinkM_stopScript<span style="color: #009900;">&#40;</span>blinkm_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//stop the BlinkM </span><br />
&nbsp; BlinkM_setFadeSpeed<span style="color: #009900;">&#40;</span>blinkm_addr<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;">//set up fade param for a smooth fade</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<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><br />
&nbsp; <br />
&nbsp; photocellVal <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//get photocells value</span><br />
<br />
<span style="color: #666666; font-style: italic;">//conditional which determines if its dark or not</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>photocellVal <span style="color: #339933;">&lt;</span> minLight and ledState <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; displayColor<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>photocellVal <span style="color: #339933;">&gt;</span> maxLight and ledState <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; displayColor<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> displayColor<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> val<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//0;34;102 Royal Blue 5</span><br />
&nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> <span style="color: #0000dd;">34</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> <span style="color: #0000dd;">102</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ledState <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//255;0;0 RED</span><br />
&nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ledState <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; BlinkM_fadeToRGB<span style="color: #009900;">&#40;</span>blinkm_addr<span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> &nbsp;g<span style="color: #339933;">,</span> &nbsp;b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//call which fades to desired color</span><br />
<span style="color: #009900;">&#125;</span></span></code></p>
<p>You will notice in the displayColor() method I&#8217;m setting R,G,B values to change the color of the blinkM.  BlinkM supports two different color models: RGB and HSB. RGB is the color model most people are familiar with so that&#8217;s why I used it. Here&#8217;s an online resource I use for <a href="http://web.njit.edu/~kevin/rgb.txt.html" target="blank"><u>RGB mapping</u></a>.  And there you have it, a simple prototype using Arduino a photocell and a BlinkM to create an interesting nightlight or ambient light.<br />
Enjoy!.</p>
<p><strong>Download Source:</strong><br />
<a class="downloadlink" href="http://www.codingcolor.com/downloads/PhotoCell_BlinkM.zip" title="Version1 downloaded 44 times" >The Arduino, Photocell and BlinkM example. (44)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingcolor.com/microcontrollers/using-an-arduino-to-control-a-blinkm-with-a-photocell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>An Arduino LCD clock using the ChronoDot RTC</title>
		<link>http://www.codingcolor.com/microcontrollers/an-arduino-lcd-clock-using-a-chronodot-rtc/</link>
		<comments>http://www.codingcolor.com/microcontrollers/an-arduino-lcd-clock-using-a-chronodot-rtc/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 21:46:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Micro Controllers]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Chronodot]]></category>
		<category><![CDATA[Clock]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[LCD]]></category>
		<category><![CDATA[Military Time]]></category>
		<category><![CDATA[RTC]]></category>

		<guid isPermaLink="false">http://www.codingcolor.com/?p=2290</guid>
		<description><![CDATA[Well, here I go again, another adventure into the land of Arduino. I am in the midst of building an art project which requires the ability to keep accurate time. My first inclination was to create a software solution only using the Arduino. But after a few minutes of research I was aware of the [...]]]></description>
			<content:encoded><![CDATA[<p>Well, here I go again, another adventure into the land of Arduino. I am in the midst of building an art project which requires the ability to keep accurate time. My first inclination was to create a software solution only using the Arduino. But after a few minutes of research I was aware of  the disadvantage of a software solution, mainly, that the time would be incorrect if the Arduino was ever reset. I continued my research and found some cool <a href="http://www.evilmadscientist.com/article.php/bulbdialkit" target="blank"><u>Arduino clock kits</u></a> and some great threads on  RTC (REAL TIME CLOCK)  chips, mainly the more popular <a href="http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3128" target="blank"><u>DS1337</u></a> and <a href="http://parts.digikey.com/1/parts/603799-ic-rtc-serial-512k-8-dip-ds1307.html" target="blank"><u>DS1307</u></a>. </p>
<p>Unfortunately both DS1337 and DS1307 chips seem to drift minutes per month, especially in extreme temperature ranges. So, I continued scouring the internet, until I found a thread that mentioned the <a href="http://macetech.com/store/index.php?main_page=product_info&#038;cPath=5&#038;products_id=8&#038;zenid=3fcef172853283bba86b6a7ff347fc1f" target="blank"><u>ChronoDot RTC</u></a> by <a href="http://www.macetech.com/store" target="blank"><u>MaceTech</u></a>. The <a href="http://macetech.com/store/index.php?main_page=product_info&#038;cPath=5&#038;products_id=8&#038;zenid=3fcef172853283bba86b6a7ff347fc1f" target="blank"><u>ChronoDot RTC</u></a> is an extremely accurate real time clock module,  based on the DS3231 temperature compensated RTC (TCXO).  The ChronoDot claims to drift less than a minute per year, which makes the ChronoDot acceptable for my project. MaceTech provides a basic Arduino example using the Arduino <a href="http://arduino.cc/en/Reference/Wire" target="blank"><u>Wire library</u></a> which reads and prints the hours, minutes, and seconds from the ChronoDot. I basically borrowed the code and expanded upon it. I utilize a basic 16&#215;2 LCD to output the time and date from the ChronoDot and I added the ability to change the hour and minutes via 2 push buttons.</p>
<p>The Arduino communicates with the ChronoDot, using the I2C standard interface. I2C is a serial data bus protocol that allows multiple devices to connect to each other with fairly slow data transfer rates. Thanks to the Arduino Wire library we don&#8217;t have to worry about the details. Moving forward you will need the following items to recreate my example:</p>
<p>Arduino Duemilanove,<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/02/00666-03-L-300x300.jpg" alt="" title="00666-03-L" width="300" height="300" class="aligncenter size-medium wp-image-1373" /></p>
<p>a character LCD display with 0.1 header 16 pins long and a 10K potentiometer to adjust the contrast on the display,<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/lcd_parts-300x185.jpg" alt="" title="lcd_parts" width="300" height="185" class="aligncenter size-medium wp-image-2000" /></p>
<p>a  6&#8243; breadboard,<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/pRS1C-3721763w345.jpg" alt="" title="pRS1C-3721763w345" width="345" height="235" class="aligncenter size-full wp-image-2389" /></p>
<p>breadboarding wire,<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/02/JumperWire-Female-01-L_i_ma.jpg" alt="" title="JumperWire-Female-01-L_i_ma" width="188" height="188" class="aligncenter size-full wp-image-1403" /></p>
<p>a ChronoDot module,<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/ChronoDot_MED.jpg" alt="" title="ChronoDot_MED" width="240" height="240" class="aligncenter size-full wp-image-2353" /></p>
<p>2 10Kohm 1/2W 5% Carbon Film Resistors<br />
<a href="http://www.codingcolor.com/wp-content/uploads/2010/02/pRS1C-2160239w345.jpg"><img src="http://www.codingcolor.com/wp-content/uploads/2010/02/pRS1C-2160239w345-300x204.jpg" alt="pRS1C-2160239w345" title="pRS1C-2160239w345" width="300" height="204" class="aligncenter size-medium wp-image-1406" /></a></p>
<p>and 2 push buttons.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/pRS1C-2160419w345.jpg" alt="" title="pRS1C-2160419w345" width="345" height="235" class="aligncenter size-full wp-image-2356" /></p>
<p>The first thing you are going to do is connect your LCD to the Arduino. If you are not familiar with the steps, please read my post on <a href="http://www.codingcolor.com/microcontrollers/connecting-a-lcd-to-arduino/" target="blank"><u> connecting a character LCD to an Arduino</u></a>. I placed my LCD as far left as I could on my 6&#8243; breadboard to allow space for the other components.</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/LCD_hookup.jpg" alt="" title="LCD_hookup" width="550" height="384" class="aligncenter size-full wp-image-2397" /></p>
<p>The next step is to add the push buttons. The push buttons will enable the ability to set the hour and minutes, if needed. </p>
<p>Grab a push button, and place it on the breadboard. Grab a 10K resistor and connect one end of the resistor to the ground on the breadboard and then connect the other end of the resistor to one leg of the button, remember to leave space between the leg of the button and the resistor. Now, grab breadboarding wire and connect one end of the wire to the power supply on the breadboard, then connect the other end of the wire to the other leg of the button. You have completed a simple button circuit, grab the other push button and repeat the steps, your breadboard should resemble this:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/buttons.jpg" alt="" title="buttons" width="500" height="292" class="aligncenter size-full wp-image-2400" /><br />
Lets connect a button to the Arduino. Grab a breadboarding wire and connect one end of the wire to digital pin 6 of the Arduino. Connect the other end of the wire to the breadboard between the resistor and the one (grounded) leg of the push button.  You have now created a button connection for the hour. Do the same for the minutes button but instead connect one end of a breadboarding wire to digital pin 7 of the Arduino. Connect the other end of the wire to the breadboard between the resistor and the one (grounded) leg of the push button. Your breadboard should look like this:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/buttons_final.jpg" alt="" title="buttons_final" width="500" height="375" class="aligncenter size-full wp-image-2412" /></p>
<p>Now for the final piece, the good ole RTC Module. If you purchased the ChronoDot from Macetech, you should have received the lithium CR2032 battery. The battery allows timekeeping in the absence of external power, you will need to attach the battery to the module. The two solder pads on the PCB are marked + and -. The battery provided with the ChronoDot has a metal tab that runs from the top (positive, +) of the battery and down to the same level as the bottom (negative, -) side of the battery. You will need to solder the tab leading from the top of the battery to the + pad on the ChronoDot. The result should look like this:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/chronodot_photo2_sm.png" alt="" title="chronodot_photo2_sm" width="150" height="150" class="aligncenter size-full wp-image-2416" /></p>
<p>Now, place the RTC module on the breadboard. Run breadboarding wire from the ground of the breadboard to the ground of the RTC. Run another strand of wire from the power supply of the breadboard to the VCC line of the RTC:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/RTC_Module.jpg" alt="" title="RTC_Module" width="500" height="301" class="aligncenter size-full wp-image-2418" /></p>
<p>Grab two more wires to make the final connections to the RTC. Connect one end of a wire to the Arduino Analog pin 5. Now, connect the other end of the wire to the SCL pin of the ChronoDot RTC module on the breadboard. Grab the other wire and connect one end of the wire to the Arduino&#8217;s Analog pin 4. Connect the other end of the wire to the SDA pin of the RTC module on the breadboard. You have now connected the wires which communicate between the Arduino and the ChronoDot.  Your completed breadboard should resemble this:</p>
<p><img src="http://www.codingcolor.com/wp-content/uploads/2010/06/Complete_CHRONODOT_LCD_ARDUINO.jpg" alt="" title="Complete_CHRONODOT_LCD_ARDUINO" width="550" height="305" class="aligncenter size-full wp-image-2422" /></p>
<p>All there is left to do, is upload the example sketch below.<br />
<code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp;Code under (cc) by Manuel Gonzalez, www.codingcolor.com<br />
&nbsp;http://creativecommons.org/license/cc-gpl<br />
&nbsp;Pins 12, 11, 5, 4, 3, 2 to LCD<br />
&nbsp;Analog pins 4 (SDA),5(SCL) to Chronodot<br />
&nbsp;Pins 6 (hour), 7(min) buttons <br />
*/</span><br />
<br />
<span style="color: #339933;">#include &lt;Wire.h&gt;</span><br />
<span style="color: #339933;">#include &lt;LiquidCrystal.h&gt;</span><br />
<br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> hourButtonPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> minButtonPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span><br />
<br />
LiquidCrystal lcd<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span> hourButtonState<span style="color: #339933;">;</span><br />
<span style="color: #993333;">int</span> minButtonState<span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span> seconds<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//00-59;</span><br />
<span style="color: #993333;">int</span> minutes<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//00-59;</span><br />
<span style="color: #993333;">int</span> hours<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//1-12 - 00-23;</span><br />
<span style="color: #993333;">int</span> day<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//1-7</span><br />
<span style="color: #993333;">int</span> date<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//01-31</span><br />
<span style="color: #993333;">int</span> month<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//01-12</span><br />
<span style="color: #993333;">int</span> year<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//0-99;</span><br />
<br />
<br />
<br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>hourButtonPin<span style="color: #339933;">,</span>INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>minButtonPin<span style="color: #339933;">,</span>INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&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><br />
&nbsp; lcd.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">16</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; hourButtonState <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; minButtonState <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">////////////////////////////////</span><br />
&nbsp; seconds <span style="color: #339933;">=</span> <span style="color: #208080;">00</span><span style="color: #339933;">;</span><br />
&nbsp; minutes <span style="color: #339933;">=</span> <span style="color: #0000dd;">26</span><span style="color: #339933;">;</span><br />
&nbsp; hours <span style="color: #339933;">=</span> <span style="color: #0000dd;">17</span><span style="color: #339933;">;</span><br />
&nbsp; day <span style="color: #339933;">=</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span><br />
&nbsp; date <span style="color: #339933;">=</span> <span style="color: #0000dd;">31</span><span style="color: #339933;">;</span><br />
&nbsp; month <span style="color: #339933;">=</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">;</span><br />
&nbsp; year <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">//initChrono();//just set the time once on your RTC</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">///////////////////////////////</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; check_buttons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; get_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; get_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; display_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; display_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> display_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">char</span> buf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; lcd.<span style="color: #202020;">setCursor</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>hours <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; lcd.<span style="color: #202020;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Time &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">itoa</span><span style="color: #009900;">&#40;</span>hours<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; lcd.<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><br />
&nbsp; <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>minutes <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">itoa</span><span style="color: #009900;">&#40;</span>minutes<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; lcd.<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><br />
&nbsp; <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>seconds <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">itoa</span><span style="color: #009900;">&#40;</span>seconds<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> display_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">char</span> buf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; lcd.<span style="color: #202020;">setCursor</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Date &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>month <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">itoa</span><span style="color: #009900;">&#40;</span>month<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; lcd.<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><br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">itoa</span><span style="color: #009900;">&#40;</span>date<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; lcd.<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> <br />
&nbsp; <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>year <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; <br />
&nbsp; lcd.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">itoa</span><span style="color: #009900;">&#40;</span>year<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> initChrono<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; set_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; set_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #993333;">void</span> set_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">104</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>day<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>month<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>year<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> get_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">104</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</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;">//set register to 3 (day)</span><br />
&nbsp; Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">104</span><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;">//get 5 bytes(day,date,month,year,control);</span><br />
&nbsp; day &nbsp; <span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; date &nbsp;<span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; month <span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; year &nbsp;<span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> set_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">104</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;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><br />
&nbsp; &nbsp;Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>seconds<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>minutes<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;Wire.<span style="color: #202020;">send</span><span style="color: #009900;">&#40;</span>decToBcd<span style="color: #009900;">&#40;</span>hours<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> get_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; Wire.<span style="color: #202020;">beginTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">104</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; 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;">//set register to 0</span><br />
&nbsp; Wire.<span style="color: #202020;">endTransmission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Wire.<span style="color: #202020;">requestFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">104</span><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;">//get 3 bytes (seconds,minutes,hours);</span><br />
&nbsp; seconds <span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0x7f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; minutes <span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; hours <span style="color: #339933;">=</span> bcdToDec<span style="color: #009900;">&#40;</span>Wire.<span style="color: #202020;">receive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0x3f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp;<br />
<br />
<br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> setHour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; hours<span style="color: #339933;">++;</span> <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>hours <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">23</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;hours <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;seconds <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;minutes <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; set_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> setMinutes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; minutes<span style="color: #339933;">++;</span> &nbsp;<br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>minutes <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">59</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;minutes <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; seconds<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; set_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #993333;">void</span> check_buttons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; hourButtonState <span style="color: #339933;">=</span> digitalRead<span style="color: #009900;">&#40;</span>hourButtonPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; minButtonState <span style="color: #339933;">=</span> digitalRead<span style="color: #009900;">&#40;</span>minButtonPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>hourButtonState <span style="color: #339933;">==</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; setHour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>minButtonState <span style="color: #339933;">==</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; setMinutes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">///////////////////////////////////////////////////////////////////////</span><br />
<br />
byte decToBcd<span style="color: #009900;">&#40;</span>byte val<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>val<span style="color: #339933;">/</span><span style="color: #0000dd;">10</span><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>val<span style="color: #339933;">%</span><span style="color:#800080;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
byte bcdToDec<span style="color: #009900;">&#40;</span>byte val<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>val<span style="color: #339933;">/</span><span style="color: #0000dd;">16</span><span style="color: #339933;">*</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>val<span style="color: #339933;">%</span><span style="color:#800080;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">//////////////////////////////////////////////////////////////////////</span></span></code></p>
<p>Take a look at the setup method in particular<br />
<code class="codecolorer text mac-classic"><span class="text">////////////////////////////////<br />
&nbsp; seconds = 00;<br />
&nbsp; minutes = 26;<br />
&nbsp; hours = 17;<br />
&nbsp; day = 7;<br />
&nbsp; date = 31;<br />
&nbsp; month = 5;<br />
&nbsp; year = 10;<br />
&nbsp; //initChrono();//just set the time once on your RTC<br />
&nbsp; ///////////////////////////////</span></code></p>
<p>When you upload your sketch for the first time you will want to update the variables to the correct time then, un-comment out the initChrono() to set the clock, upload your sketch and now should have a running clock.<br />
Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingcolor.com/microcontrollers/an-arduino-lcd-clock-using-a-chronodot-rtc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Arduino Night Light using a Photocell</title>
		<link>http://www.codingcolor.com/microcontrollers/arduino-night-light-using-a-photocell/</link>
		<comments>http://www.codingcolor.com/microcontrollers/arduino-night-light-using-a-photocell/#comments</comments>
		<pubDate>Sun, 16 May 2010 01:29:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Micro Controllers]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[LED]]></category>
		<category><![CDATA[Light]]></category>
		<category><![CDATA[Night]]></category>
		<category><![CDATA[Night Light]]></category>
		<category><![CDATA[Photocell]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Radio Shack]]></category>

		<guid isPermaLink="false">http://www.codingcolor.com/?p=2101</guid>
		<description><![CDATA[Hooray It&#8217;s Saturday, instead of making music, painting or coding Actionscript I decided to tinker with my Arduino. I started off my day by heading down to the coffee shop and picking up a cup of Joe. As I was waiting in line, I decided I would take a drive up to my local Radio [...]]]></description>
			<content:encoded><![CDATA[<p>Hooray It&#8217;s Saturday, instead of making music, painting or coding Actionscript I decided to tinker with my Arduino. I started off my day by heading down to the coffee shop and picking up a cup of Joe. As I was waiting in line, I decided I would take a drive up to my local Radio Shack and peruse the component bins for some ideas. Now, this isn&#8217;t an ideal situation, since Radio Shacks components are a bit pricey, but since I had already perused my component bin at home and wanted something new to work on, I thought what the hell a few more dollars wouldn&#8217;t break my wallet. </p>
<p>Anyhow, at Radio Shack I bought a<a href="http://www.radioshack.com/product/index.jsp?productId=2062590" target="blank"><u> pack of Photocells</u></a>, which contained 5 cells of different sizes. I have used Photocells in the past when I built some DIY analog synths, so, I was familiar with them, but I have never integrated them with my Arduino. So, I set out on an adventure to make a simple electronic gadget. The first thing that came to mind was to control a basic LED. I figured it out rather quickly, and realized that I could make an interesting night light with it, if I swapped the basic LED with a <a href="http://macetech.com/store/index.php?main_page=product_info&#038;cPath=1&#038;products_id=1" target="blank"><u> ShiftBrite LED</u></a>, but that&#8217;s a recipe for another blog post.</p>
<p>So what the hell is a Photocell?<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/276-1657.jpg" alt="" title="276-1657" width="255" height="175" class="aligncenter size-full wp-image-2107" /><br />
A Photocell acts as a light sensor and is usually used when you just want to detect light.  Photocells are used in toys, appliances and in street lamps that turn themselves on at night. Sometimes known as photo resistors, they look like a small 0.5 to 2 inch disk with two leads out the back. They are inexpensive, low-power, and very easy to use.  Photocells are available from a number of online sources and as mentioned at your local <a href="http://www.radioshack.com/product/index.jsp?productId=2062590" target="blank"><u>Radio Shack (catalog number 276-1657 )</u></a>.<br />
Technically Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how much light the face is exposed to. Photocell properties vary widely from model to model, they tend to be very inaccurate and they shouldn&#8217;t be used to try to determine precise light levels you should only use them to determine basic light changes. Photocells are non-polarized and are pretty hardy as long as you avoid bending the leads right at the epoxied sensor.</p>
<p>Enough technical talk lets get crackin&#8217;.<br />
Make sure you have all the components needed to make this simple circuit:</p>
<li>Arduino Duemilanove</li>
<li>Breadboard</li>
<li>220-Ohm resistor</li>
<li>LED</li>
<li>Photocell</li>
<li>10k resistor</li>
<li>Breadboard wires</li>
<p>The first thing I always do when prototyping, is connect the Arduino to a power source either via the USB or external. I then proceed to power up the breadboard.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/power.jpg" alt="" title="power" width="550" height="450" class="aligncenter size-full wp-image-2121" /><br />
I simply take a Red wire from the Arduino 5v(+) and connect it to the red rail on the breadboard. Then I connect a Red wire from the red rail on the breadboard to the other red rail on the opposite side of the breadboard. I then take a Black wire and connect it to the GND of the Arduino and connect it to the blue rail on the Breadboard. I then follow suit by connecting a Black wire from the blue rail on the breadboard to the other blue rail on the opposite side of the breadboard. We now have power!</p>
<p>The next step is to connect the Photocell to the breadboard and wire it up to the Arduino.<br />
Add the Photocell to the board. Connect a Red wire from the red rail on the board to one of the leads of the Photocell, remember Photocells are non-polarized, which means you can connect them up &#8216;either way&#8217; and they&#8217;ll work just fine.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/photocellpower.jpg" alt="" title="photocellpower" width="550" height="450" class="aligncenter size-full wp-image-2128" /><br />
Now, lets add the 10k resistor. Connect one end of the resistor to the other lead of the Photocell.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/10kreisistor.jpg" alt="" title="10kreisistor" width="550" height="450" class="aligncenter size-full wp-image-2131" /><br />
Connect a wire from the same Photocell lead that the resistor resides on to Analog 0 on the Arduino. Then ground the resistor by adding a black wire from the blue rail on the breadboard to the other end of the resistor, like so.<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/photocellcomplete.jpg" alt="" title="photocellcomplete" width="550" height="450" class="aligncenter size-full wp-image-2133" /><br />
You have now created a basic Photocell circuit.<br />
The code below is all you would need to get values from the Photocell. You could easily copy the code below, create a new photocellTest.pde file, compile, upload to your Arduino and view the output of the Photocell in the Serial monitor.<br />
<code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
This example shows the output of an analogRead() of a Photocell.<br />
By M.Gonzalez<br />
www.codingcolor.com<br />
The example code is in the public domain<br />
*/</span><br />
<span style="color: #993333;">int</span> photocellPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Photocell connected to analog pin 0</span><br />
<span style="color: #993333;">int</span> photocellVal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define photocell variable</span><br />
<br />
<br />
<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><br />
&nbsp; Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<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><br />
&nbsp; photocellVal <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// read the analog from photocell</span><br />
&nbsp; Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span> photocellVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// print to screen</span><br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp;<span style="color: #009900;">&#125;</span></span></code></p>
<p>Due to the inaccuracies of the readings, I would recommend adding a conditional statement as a threshold like so.</p>
<p><code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
This example shows the output of an analogRead() of a Photocell.<br />
By M.Gonzalez<br />
www.codingcolor.com<br />
The example code is in the public domain<br />
*/</span><br />
<span style="color: #993333;">int</span> photocellPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Photocell connected to analog pin 0</span><br />
<span style="color: #993333;">int</span> photocellVal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define photocell variable</span><br />
<br />
<br />
<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><br />
&nbsp; Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<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><br />
&nbsp; photocellVal <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// read the analog from photocell</span><br />
&nbsp; Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span> photocellVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output to screen</span><br />
<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>photocellVal <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;lights on&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// output to screen</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>photocellVal <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;light out&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// output to screen</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp;<span style="color: #009900;">&#125;</span></span></code></p>
<p>Moving forward we will add the LED which will be the Night Light.<br />
Grab your LED. You will notice that one lead is longer than the other. The longer lead is the Anode (+) and shorter lead is the Cathode(-), its important to now this, since you need to make sure you are connecting the circuit correctly. Plug the LED into your breadboard taking note of which lead is the Anode (positive) and which is the Cathode (ground). Now run a black wire from the blue rail on the breadboard to the Cathode lead(-) of the LED. Connect one end of the 220 Ohm resistor to the Anode(+) lead of the LED. Connect a wire from the PMW 9 pin on the Arduino to the other end of the 220 Ohm resistor. Your board should now resemble the following:<br />
<img src="http://www.codingcolor.com/wp-content/uploads/2010/05/nightlightComplete.jpg" alt="" title="nightlightComplete" width="550" height="450" class="aligncenter size-full wp-image-2151" /><br />
We have finally completed the simple circuit. Lets now update or existing code. We will add variables for the LED, create a new function which fades the LED in and out, and revise the conditional threshold statement.</p>
<p><code class="codecolorer c mac-classic"><span class="c"><span style="color: #808080; font-style: italic;">/*<br />
This example shows the output of an analogRead() of a Photocell.<br />
By M.Gonzalez<br />
www.codingcolor.com<br />
The example code is in the public domain<br />
*/</span><br />
<br />
<span style="color: #993333;">int</span> photocellPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Photocell connected to analog pin 0</span><br />
<span style="color: #993333;">int</span> photocellVal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define photocell variable</span><br />
<span style="color: #993333;">int</span> ledPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// LED connected to digital pin 9</span><br />
<span style="color: #993333;">int</span> ledState <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//state of the led </span><br />
<span style="color: #993333;">int</span> fadeDown <span style="color: #339933;">=</span> <span style="color: #0000dd;">30</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//delay per fade</span><br />
<span style="color: #993333;">int</span> fadeUp <span style="color: #339933;">=</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//delay per fade</span><br />
<span style="color: #993333;">int</span> minLight <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//min light threshold</span><br />
<span style="color: #993333;">int</span> maxLight <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//max light threshold</span><br />
<br />
<br />
<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><br />
&nbsp;<span style="color: #666666; font-style: italic;">//Serial.begin(9600);</span><br />
&nbsp; pinMode<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; 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><br />
<span style="color: #009900;">&#125;</span><br />
<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><br />
&nbsp; photocellVal <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>photocellPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>photocellVal <span style="color: #339933;">&lt;</span> minLight and ledState <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; fadeLed<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Serial.println(&quot;fade up&quot;);</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>photocellVal <span style="color: #339933;">&gt;</span> maxLight and ledState <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; fadeLed<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Serial.println(&quot;fade down&quot;);</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <br />
<br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> fadeLed<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> num<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>num <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> fadeValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> fadeValue <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">;</span> fadeValue <span style="color: #339933;">+=</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp;analogWrite<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> fadeValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;delay<span style="color: #009900;">&#40;</span>fadeUp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp;ledState <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> fadeValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">255</span> <span style="color: #339933;">;</span> fadeValue <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> fadeValue <span style="color: #339933;">-=</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp;analogWrite<span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> fadeValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;delay<span style="color: #009900;">&#40;</span>fadeDown<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp;ledState <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <br />
<br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span></span></code></p>
<p>That pretty much concludes my Night Light prototype. Like I mentioned at the beginning of this post, I plan to tinker around with this base code and circuit and add a brighter LED. I&#8217;m awaiting on both a <a href="http://macetech.com/store/index.php?main_page=product_info&#038;cPath=1&#038;products_id=1" target="blank"><u> ShiftBrite LED</u></a> and a <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8579" target="blank"><u>BlinkM</u></a> to arrive in my mail box. Till then happy tinkering.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingcolor.com/microcontrollers/arduino-night-light-using-a-photocell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

