<?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>Thu, 12 Aug 2010 06:27:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 7 times" >Ultra-Sonic beeping thingamajiggy (7)</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 [...]]]></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 4 times" >The Arduino, Photocell and BlinkM example. (4)</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  [...]]]></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>itoa<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>itoa<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>itoa<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>itoa<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>itoa<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>itoa<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>You should now 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>0</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>
