<?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>JeeLabs &#187; AVR</title>
	<atom:link href="http://jeelabs.org/category/avr/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeelabs.org</link>
	<description>Computing stuff tied to the physical world</description>
	<lastBuildDate>Wed, 01 Feb 2012 23:01:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The PWR vs the +3V pin</title>
		<link>http://jeelabs.org/2012/01/25/the-pwr-vs-the-3v-pin/</link>
		<comments>http://jeelabs.org/2012/01/25/the-pwr-vs-the-3v-pin/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 23:01:03 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17658</guid>
		<description><![CDATA[JeeNodes and plugs use 6-pin headers with the following pin assignment: (there&#8217;s usually a big printed dot near the PWR pin for orientation) There has been some confusion about what PWR is w.r.t. +3V and how to use it, so let me elaborate a bit. First: &#8220;+3V&#8221; is the main regulated power reference for most [...]]]></description>
			<content:encoded><![CDATA[<p>JeeNodes and plugs use 6-pin headers with the following pin assignment:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/port.png" alt="Port" border="0" width="350" height="44" /></p>

<p>(there&#8217;s usually a big printed dot near the PWR pin for orientation)</p>

<p>There has been some confusion about what PWR is w.r.t. +3V and how to use it, so let me elaborate a bit.</p>

<p>First: <strong>&#8220;+3V&#8221; is the main regulated power reference for most chips and circuits.</strong> And to make it more complicated: it&#8217;s actually 3.3V, not 3.0V. The &#8220;+3V&#8221; label merely identifies the pin (VCC would be too confusing).</p>

<p>The ATmega on the JeeNode, as well as most (but not all) chips on attached plugs run on this 3.3V voltage level. It is also the reference level for all digital I/O (0 = 0V, 1 = 3.3V), as well as for the ADC when used for analog inputs (1023 steps of 3.22580645161 millivolt each).</p>

<p>As for the other supply pin: <strong>&#8220;PWR&#8221; is usually a higher voltage from which +3V is derived</strong>.</p>

<p>There&#8217;s a voltage regulator on the JeeNode which can take any input voltage on &#8220;PWR&#8221; up to about 13V as input and which produces the 3.3V regulated voltage on the &#8220;+3V&#8221; pin.</p>

<p>The JeeNode regulator has some limits: it&#8217;s can&#8217;t supply more than 250 mA <em>and</em> it can&#8217;t dissipate more than about 600 mW. It&#8217;ll shutdown if either of these are exceeded (and it&#8217;ll get damaged if you feed it more than 13V). These figures are fairly limiting: if you run a JeeNode off 12V on the PWR pin, then it actually won&#8217;t be able to supply more than 70 mA. With 5V on the PWR pin, the limit will be current, not power dissipation, i.e. 250 mA max.</p>

<p>That&#8217;s for a JeeNode with through-hole components, i.e. an MCP1702 regulator in a TO-92 package. With the JeeNode SMD and JeeNode USB, you get even less power from the regulator: at most about 250 mW of heat dissipation. That&#8217;s 150 mA @ 5V, and only 28 mA @ 12V. In short: the JN SMD and JN USB are not really meant to be powered from more than 5V &#8211; or at least not without an extra external regulator (on the plus side: the MCP1703 supports up to 16V on the PWR pin). Note also that the JeeNode USB doesn&#8217;t support more than 7V on the PWR pin, due to limitations of the on-board LiPo charger &#8211; but that should not be an issue since it&#8217;s really only meant to be powered from 5V via USB.</p>

<p>As you can see, there are several limiting factors as to what voltage is supported on the PWR pin. Keep in mind that JeeNodes were designed for ultra-low power consumption, and the regulator was selected for its extremely low idle current draw, not its &#8220;high-end&#8221; characteristics.</p>

<p><em>But that&#8217;s not all&#8230;</em></p>

<p>One trap for the unwary is with the <a href="/ju3">JeeNode USB</a>: it doesn&#8217;t actually feed 5V to the PWR pin when connected to USB but only 4.2V. This is due to the LiPo charger circuit, which allows a LiPo battery to be connected between PWR and GND. With a LiPo battery, the JeeNode USB becomes a very convenient stand-alone unit: low-power <em>untethered</em> operation while not connected to USB, and automatic LiPo charging while connected to USB. It even has a voltage divider on board to monitor the LiPo battery voltage (tied to PC6, i.e. analog 6 in Arduino-speak).</p>

<p>The trouble here is that when you connect plugs to the JeeNode USB, you won&#8217;t get the full 5V you might expect. This affects all those plugs which don&#8217;t run entirely off +3V but expect some higher voltage on PWR.</p>

<p><em>And here&#8217;s another tricky situation&#8230;</em></p>

<p>With the <a href="/aa2">AA Power Board</a>, you can run JeeNodes off a single AA cell. In this case, there isn&#8217;t a voltage higher than 3.3V anywhere in the circuit &#8211; so what to do with the PWR line? Well, there&#8217;s a solder jumper on the AA board to let you decide: it&#8217;s normally open, but you can connect it to either +3V or to the battery &#8220;+&#8221; (i.e. 1.2..1.5V). Obviously, this setup will be even less suited for plugs which expect a higher voltage than 3.3V to operate.</p>

<p>As you see, the +3V pin is solidly specified as always being 3.3V, but the PWR pin level can be all over the map.</p>

<p>The following plugs are especially affected:</p>

<ul>
<li><p>the <a href="/lp1">LCD Plug</a> will work, when used with a 3.3V display (as shipped by JeeLabs), but will only be suitable with 5V displays if PWR carries 5V</p></li>
<li><p>the <a href="/gb1">Graphics Board</a> assumes that PWR is 5V, because the display needs it (it&#8217;ll probably still work with 4.2V, but PWR should not be above 5V)</p></li>
</ul>

<p>Most plugs will be fine, though, since they only use the +3V supply. The only issue here is to make sure that the total current consumption off +3V is not too high.</p>

<p><strong>Using a JeeNode with an unregulated power supply</strong></p>

<p>The maximum allowed voltage on the PWR pin is about 13V. Unfortunately, many 12V power bricks are fairly badly calibrated, and often deliver substantially more than 12V under no-load conditions. You could easily damage the JeeNode&#8217;s on-board regulator with a cheap 12V power brick &#8211; better use a 5V or 9V brick.</p>

<p>If you do want to power a JeeNode off 12V (perhaps you need 12V on the rest of the circuit for LEDs, relays, or motor power), then the best approach is to insert a 5V regulator and feed that 5V level to PWR. This can easily be done with a &#8220;7805&#8243; regulator chip, which is available from many electronics parts sources.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2012/01/25/the-pwr-vs-the-3v-pin/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Putting the JNµ to sleep</title>
		<link>http://jeelabs.org/2012/01/18/putting-the-jnu-to-sleep/</link>
		<comments>http://jeelabs.org/2012/01/18/putting-the-jnu-to-sleep/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 23:01:41 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LowPower]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17520</guid>
		<description><![CDATA[The Sleepy::loseSomeTime() code in the Ports library was never tested on a JeeNode Micro. Turns out that only a minor library difference kept it from working (the arduino-tiny library has its own version of millis(), etc): So now, the JNµ can be put to sleep as well. Here&#8217;s the jouleTest sketch I used, tweaked to [...]]]></description>
			<content:encoded><![CDATA[<p>The <em>Sleepy::loseSomeTime()</em> code in the Ports library was never tested on a <a href="/jm1">JeeNode Micro</a>. Turns out that only a minor library difference kept it from working (the <a href="http://code.google.com/p/arduino-tiny/">arduino-tiny</a> library has its own version of <em>millis()</em>, etc):</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-14-at-23.55.34.png" alt="Screen Shot 2012 01 14 at 23 55 34" border="0" width="432" height="337" /></p>

<p>So now, the JNµ can be put to sleep as well. Here&#8217;s the <a href="https://github.com/jcw/jeelib/blob/master/examples/RF12/jouleTest/jouleTest.ino">jouleTest</a> sketch I used, tweaked to run on the JNµ as well:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-14-at-23.55.09.png" alt="Screen Shot 2012 01 14 at 23 55 09" border="0" width="376" height="450" /></p>

<p>And sure enough, about once every 10 seconds it shows that familiar packet-transmit current consumption:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/SCR88.png" alt="SCR88" border="0" width="604" height="476" /></p>

<p>The blue line is the AC-coupled supply voltage, a 3x AA Eneloop battery pack in this case. It supplies about 3.8V, but when running off a 1000 µF cap, it looks like this continues to work down to 1.8V (well below the RFM12B&#8217;s minimum 2.2V specs) &#8211; although with only about half the transmit power by then.</p>

<p>This current use <a href="http://jeelabs.org/2011/12/01/rf12-power-optimization/">fingerprint</a> is almost identical to the ATmega328 running this same code. Not surprising really, since it&#8217;s the RFM12B which determines most of the power consumption, not the ATmega vs ATtiny difference.</p>

<p><em>Onwards!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2012/01/18/putting-the-jnu-to-sleep/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Browsing the Arduino run-time code</title>
		<link>http://jeelabs.org/2012/01/10/browsing-the-arduino-run-time-code/</link>
		<comments>http://jeelabs.org/2012/01/10/browsing-the-arduino-run-time-code/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 23:01:47 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17394</guid>
		<description><![CDATA[The Arduino IDE is a thin wrapper around the avr-gcc compiler and the avr-libc run-time library. It also includes a fairly basic IDE, i.e. a text editor and conventions for managing projects, in the form of &#8220;sketches&#8221; and libraries. I prefer to use my own programmer&#8217;s editor, because it supports multiple programming languages and has [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://arduino.cc/en/Main/Software">Arduino IDE</a> is a thin wrapper around the <a href="http://gcc.gnu.org/">avr-gcc</a> compiler and the <a href="http://www.nongnu.org/avr-libc/">avr-libc</a> run-time library. It also includes a fairly basic IDE, i.e. a text editor and conventions for managing projects, in the form of &#8220;sketches&#8221; and libraries.</p>

<p>I prefer to use my own programmer&#8217;s editor, because it supports multiple programming languages and has a lot more features for software development (such as Git integration, code folding, and save-on-lose-focus). The Arduino IDE supports external editors by disabling its own one &#8211; which is an option in the preferences:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/external-edit.png" alt="External edit" title="external-edit.png" border="0" width="467" height="147" /></p>

<p>Now I can simply use my own editor and switch to the Arduino IDE for compiling and uploading.</p>

<p>One advantage of using an external editor, is that you can look at <em>other</em> source code than just your own sketches. In the rest of this post, I&#8217;m going to describe how to look at one of the most interesting parts of the Arduino IDE: its run-time library, i.e. the <a href="http://wiring.org.co/">Wiring</a> code which adds supports for everything which makes an <em>Arduino</em> different from the <em>ATmega&#8217;s</em> on which it is based.</p>

<blockquote>
  <p>Note: what follows is specific for Mac OSX, but apart from the location of these files and the editor used, you should be able to transpose all of this to your own beloved computer environment.</p>
</blockquote>

<p>The first task, is to figure out <em>where</em> the Arduino IDE&#8217;s run-time code is located. In Mac OSX, this code is located <em>inside</em> the Arduino application. To view this area, you can right-click on the Arduino app:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-09-at-19.29.48.png" alt="Screen Shot 2012 01 09 at 19 29 48" title="Screen Shot 2012-01-09 at 19.29.48.png" border="0" width="337" height="62" /></p>

<p>This leads to the following directory structure:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-09-at-19.28.16.png" alt="Screen Shot 2012 01 09 at 19 28 16" title="Screen Shot 2012-01-09 at 19.28.16.png" border="0" width="275" height="379" /></p>

<p>The interesting bits are inside the &#8220;hardware&#8221; folder:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-09-at-19.48.091.png" alt="Screen Shot 2012 01 09 at 19 48 09" title="Screen Shot 2012-01-09 at 19.48.09.png" border="0" width="268" height="208" /></p>

<p>Here are the first few lines of &#8220;Arduino.h&#8221;, for example (this used to be &#8220;WProgram.h&#8221;):</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-09-at-19.55.09.png" alt="Screen Shot 2012 01 09 at 19 55 09" title="Screen Shot 2012-01-09 at 19.55.09.png" border="0" width="220" height="312" /></p>

<p>These source files are where <em>everything</em> specific to the Arduino IDE&#8217;s runtime happens. The &#8220;Serial&#8221; object, and the &#8220;millis()&#8221; code, for example. If you want to really understand what the Arduino is about, then it&#8217;s well worth going through some of these files. As you&#8217;ll find out, there&#8217;s <a href="http://jeelabs.org/2011/12/15/the-steepness-dilemma/">no magic</a> once you start looking in the right places.</p>

<p><em>Try it!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2012/01/10/browsing-the-arduino-run-time-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Getting ready for OptiBoot 4.4</title>
		<link>http://jeelabs.org/2012/01/07/getting-ready-for-optiboot-4-4/</link>
		<comments>http://jeelabs.org/2012/01/07/getting-ready-for-optiboot-4-4/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 23:01:49 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[ISP]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17344</guid>
		<description><![CDATA[Ok, it&#8217;s official &#8211; starting this week, all new ATmega&#8217;s here will be flashed with the OptiBoot 4.4 boot loader. It&#8217;s going to take a while for all current inventory units to &#8220;flush through the system&#8221; so to speak (both DIPs and SMDs), but at some point this month all ATmega&#8217;s will be running the [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, it&#8217;s official &#8211; starting this week, all <em>new</em> ATmega&#8217;s here will be flashed with the <a href="http://code.google.com/p/optiboot/">OptiBoot 4.4</a> boot loader.</p>

<p>It&#8217;s going to take a while for all current inventory units to &#8220;flush through the system&#8221; so to speak (both DIPs and SMDs), but at some point this month all ATmega&#8217;s will be running the same boot loader as the Arduino Uno. Faster, smaller, and &#8211; <em>hopefully</em> &#8211; no more troubles with Windows being unable to upload sketches through FTDI.</p>

<p>One of the things I&#8217;ve done is to turn one of the new <a href="/jb1">JeeNode Blocks</a> into a dedicated Portable ISP Programmer for DIP-28&#8242;s. It&#8217;s the same <em>isp&#95;repair2</em> sketch as before (modified for the  Block&#8217;s minor pin allocation diff&#8217;s):</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/DSC_2847.jpg" alt="DSC 2847" border="0" width="604" height="464" /></p>

<p>Note the 16 MHz resonator behind the ZIF socket. Here&#8217;s the wiring:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/DSC_2848.jpg" alt="DSC 2848" border="0" width="604" height="420" /></p>

<p>There&#8217;s no 10 kΩ pull-up resistor for RESET, because ATmega&#8217;s have a (weak) built-in pull-up.</p>

<p>To avoid the delay-on-hardware-reset, I&#8217;ve added a push-button which <em>briefly</em> shorts +3V and GND together through a 10 µF electrolytic cap. Enough to force the JeeNode Block into a hardware reset. There&#8217;s a 10 kΩ resistor across the cap to discharge it afterwards. This is really quick because the reset occurs on button <em>press</em> i.s.o. <em>release</em>.</p>

<p>The savings are minimal &#8211; <em>just 1..2 seconds more for the standard bootstrap</em> &#8211; but for production, it matters. Total flash time, including boot loader, RF12demo sketch, and setting all the fuses is now just a few seconds:</p>

<ul>
<li>insert DIP chip</li>
<li>close ZIF socket</li>
<li>press button</li>
<li>wait for two LED blinks</li>
<li>open ZIP socket</li>
<li>remove programmed chip</li>
<li>rinse and repeat&#8230;</li>
</ul>

<p>When a serial port is connected via FTDI, you can see the progress of it all:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2012/01/Screen-Shot-2012-01-04-at-20.01.421.png" alt="Screen Shot 2012 01 04 at 20 01 42" border="0" width="559" height="213" /></p>

<p>Now let&#8217;s just hope that this version of OptiBoot doesn&#8217;t lead to the same mess as the previous one.</p>

<p>If you have older JeeNodes or other ATmega328 boards running previous bootstrap loaders, I suggest looking at the recent <a href="http://jeelabs.org/2012/01/04/isp-programmers/">ISP programmer</a> post and the older <a href="http://jeelabs.org/2011/05/29/summary-of-isp-options/">summary</a>. You might consider bringing all units up to date, because with a mix of boot loaders you end up <em>constantly</em> using the wrong one in the IDE and having to adjust board types each time.</p>

<p>Just be careful when messing with boot loaders. If the process goes wrong <em>and</em> you pick the wrong fuse settings, then you can end up with a &#8220;bricked&#8221; unit (only a high-voltage programmer can recover from such a state).</p>

<p><em>But apart from that: enjoy the doubled 115.2 Kbaud upload speed and the 1.5 Kb extra space for sketches!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2012/01/07/getting-ready-for-optiboot-4-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ISP Programmers &#8211; part 2</title>
		<link>http://jeelabs.org/2012/01/05/isp-programmers-part-2/</link>
		<comments>http://jeelabs.org/2012/01/05/isp-programmers-part-2/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 23:01:33 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[ISP]]></category>
		<category><![CDATA[LowPower]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17225</guid>
		<description><![CDATA[In yesterday&#8217;s post, I presented my latest ISP programmers, based on the isp&#95;repair2 sketch. I made a few small improvements to that sketch: the RFM12B is powered down at the end, so that the unit only consumes a few µA&#8217;s once done the programming rate has been improved by getting rid of those horribly slow [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://jeelabs.org/2012/01/04/isp-programmers/">yesterday&#8217;s post</a>, I presented my latest ISP programmers, based on the <a href="https://github.com/jcw/jeelib/blob/master/examples/Ports/isp_repair2/isp_repair2.ino">isp&#95;repair2</a> sketch.</p>

<p>I made a few small improvements to that sketch:</p>

<ol>
<li>the RFM12B is powered down at the end, so that the unit only consumes a few µA&#8217;s once done</li>
<li>the programming rate has been improved by getting rid of those <a href="http://jeelabs.org/2010/01/06/pin-io-performance/">horribly slow</a> <em>digitalWrite()</em> calls, etc.</li>
<li>updated RF12demo and OptiBoot to the latest version (v8 and v4.4, respectively)</li>
</ol>

<p>Furthermore, I switched from the funky switches to plain jumpers, with the following layout:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/isp-pins1.png" alt="Isp pins" border="0" width="529" height="406" /></p>

<p>Another change is that the default with no jumpers is now to burn RF12demo w/ OptiBoot for use with a 16 MHz resonator &#8211; this is a good default for JeeNodes, JeeNode USB&#8217;s, and JeeNode SMD&#8217;s. To select the other options, just hook this up to USB, change the jumpers, and watch the serial output report on reset.</p>

<p>This is the output with no jumpers and no target board attached:</p>

<blockquote>
  <p><img src="http://jeelabs.org/wp-content/uploads/2011/12/Screen-Shot-2011-12-28-at-11.49.57.png" alt="Screen Shot 2011 12 28 at 11 49 57" border="0" width="567" height="103" /></p>
</blockquote>

<p>This is the output after a normal programming cycle (again, the default case, no jumpers installed):</p>

<blockquote>
  <p><img src="http://jeelabs.org/wp-content/uploads/2011/12/Screen-Shot-2011-12-28-at-11.50.15.png" alt="Screen Shot 2011 12 28 at 11 50 15" border="0" width="558" height="211" /></p>
</blockquote>

<p>Programming takes only a few seconds. Note that this programmer is fully self-contained and includes its own LiPo battery, so all you need to do is press the 6 pins on the ISP header pads. The neat thing is that due to the normally-discharged cap on the target board, the brief power dip caused by touching the ISP pads will generate a hardware <em>power-on reset</em> in the programmer, which then immediately starts its programming cycle.</p>

<p>So the the whole workflow is now as follows:</p>

<ul>
<li>grab this thing &#8211; and let&#8217;s call it a &#8220;Portable ISP Programmer&#8221; (PIP!)</li>
<li>press the pins against the ISP header of the unit to be re-flashed</li>
<li>watch the initial LED blink on the programmer as it comes out of reset</li>
<li>wait 2..3 seconds</li>
<li>watch the <em>second</em> LED blink, indicating that it has successfully completed programming</li>
</ul>

<p>There is no power switch, since the programmer enters total power down when done. To re-charge, plug the programmer into a USB port and wait for the &#8220;charge&#8221; LED to turn off. Note that pressing the reset button also works, but that it adds a small boot loader delay before the <em>isp&#95;repair2</em> sketch gets started.</p>

<p>This has become so convenient, that I can now take any old JeeNode lying around here, and reset it to a well-known state in just a few seconds, before re-using it for some project or experiment.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2012/01/05/isp-programmers-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ISP programmers</title>
		<link>http://jeelabs.org/2012/01/04/isp-programmers/</link>
		<comments>http://jeelabs.org/2012/01/04/isp-programmers/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 23:01:04 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[ISP]]></category>
		<category><![CDATA[LowPower]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17216</guid>
		<description><![CDATA[ISP (re-)programming is going to become more important in the future, so I&#8217;ve built a few more of these: The problem was to come up with a robust way to connect to the target board, but I think I&#8217;ve found a solution: Take a 4-pin header, slightly enlarge the holes in the plastic, and then [...]]]></description>
			<content:encoded><![CDATA[<p>ISP (re-)programming is going to become more important in the future, so I&#8217;ve built a few more of <a href="http://jeelabs.org/2011/05/28/more-bootstraps/">these</a>:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_28441.jpg" alt="DSC 2844" border="0" width="604" height="424" /></p>

<p>The problem was to come up with a <em>robust</em> way to connect to the target board, but I think I&#8217;ve found a solution:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2843.jpg" alt="DSC 2843" border="0" width="604" height="290" /></p>

<p>Take a 4-pin header, slightly enlarge the holes in the plastic, and then gently-but-forcefully press a couple of <a href="http://en.wikipedia.org/wiki/Pogo_pin">Pogo pins</a> in there. I&#8217;m using the type with a large head with sharp edges. Here&#8217;s the whole assembly:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2839.jpg" alt="DSC 2839" border="0" width="604" height="426" /></p>

<p>After that, it&#8217;s a matter of attaching all the wires and tying / glueing things together:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2841.jpg" alt="DSC 2841" border="0" width="604" height="429" /></p>

<p>These units are all refurbished ones with a defective radio, since that&#8217;s not needed here.</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2840.jpg" alt="DSC 2840" border="0" width="604" height="401" /></p>

<p>The ZIP straps hold the battery and wires in place. The hot glue does the rest:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2842.jpg" alt="DSC 2842" border="0" width="604" height="516" /></p>

<p>These programmers are considerably more effective than you might think &#8211; <em>tomorrow, I&#8217;ll explain why&#8230;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2012/01/04/isp-programmers/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Tempus fugit &#8230;</title>
		<link>http://jeelabs.org/2011/12/31/tempus-fugit/</link>
		<comments>http://jeelabs.org/2011/12/31/tempus-fugit/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 23:01:51 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17137</guid>
		<description><![CDATA[Another year is about to end, and the next one is already anxiously waiting to carry us along into the future&#8230; A fitting moment to get that Dutchtronix clock working (a lot easier than this geek version): I bought this little kit long ago, not realizing that a low-end USB scope front-end can&#8217;t deal with [...]]]></description>
			<content:encoded><![CDATA[<p><em>Another year is about to end, and the next one is already anxiously waiting to carry us along into the future&#8230;</em></p>

<p>A fitting moment to get that <a href="http://www.dutchtronix.com/ScopeClockH3-1-Enhancedfaq.htm">Dutchtronix clock</a> working (a lot easier than this <a href="http://www.dutchtronix.com/CRTClocks.htm">geek</a> version):</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2832.jpg" alt="DSC 2832" border="0" width="604" height="453" /></p>

<p>I bought this little kit long ago, not realizing that a low-end USB scope front-end can&#8217;t deal with it. Besides, it turns out that it didn&#8217;t work back then because of some bad solder joints (ground planes and 15W soldering irons don&#8217;t go well together &#8211; even my <a href="http://jeelabs.org/2010/06/08/smd-hand-soldering-tools/">current</a> soldering iron has trouble heating up some of those pads!).</p>

<p>Anyway, this is as far as the Hameg HMO2024 will go:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/SCR602.png" alt="SCR60" border="0" width="594" height="448" /></p>

<p>Recognizable, but a <a href="http://www.knutsel.org/2010/09/19/dutchtronix-avr-oscilloscope-clock/">far cry</a> from what analog scopes can do. That&#8217;s what you get when digital sampling, waveform refresh rates, and vector drawing clash (bumping up the sampling rate causes a fuller, but flickering, image).</p>

<p>This design was from 2007, I think &#8211; which goes to show that fun stuff (especially clocks) can be time-less!</p>

<p><em>I wish you a healthy, safe, and happy 2012 &#8211; with lots of opportunities to tinker, learn, and create.</em></p>

<p><strong>Update</strong> &#8211; for another example of how such X-Y displays differ between analog and high-end vs low-end DSO&#8217;s, see <a href="http://www.eevblog.com/2011/03/08/eevblog-153-youscope-demo-on-a-digital-scope/">this video</a> on Dave Jones&#8217; EEVblog.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2011/12/31/tempus-fugit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Out with the old &#8211; in with the new!</title>
		<link>http://jeelabs.org/2011/12/29/out-with-the-old-in-with-the-new/</link>
		<comments>http://jeelabs.org/2011/12/29/out-with-the-old-in-with-the-new/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 23:01:32 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=17098</guid>
		<description><![CDATA[Le roi est mort &#8230; It&#8217;s time to move on. I&#8217;m dropping all support and maintenance of the Ports and RF12 libs, as well as some others. Well, as far as subversion is concerned. All files are kept read-only at svn.jeelabs.org &#8211; for reference. &#8230; vive le roi! But the good news is that all [...]]]></description>
			<content:encoded><![CDATA[<p><em>Le roi est mort &#8230;</em></p>

<p>It&#8217;s time to move on. I&#8217;m dropping all support and maintenance of the <a href="http://jeelabs.net/projects/cafe/wiki/Ports">Ports</a> and <a href="http://jeelabs.net/projects/cafe/wiki/RF12">RF12</a> libs, as well as <a href="http://jeelabs.net/projects/cafe/wiki/Libraries">some others</a>. Well, as far as subversion is concerned. All files are kept read-only at <em>svn.jeelabs.org</em> &#8211; for reference.</p>

<p><em>&#8230; vive le roi!</em></p>

<p>But the good news is that all this software has now been moved and updated on <a href="https://github.com/">GitHub</a> &#8230; and is <em>fully</em> supported.</p>

<p>All libraries on GitHub have been adjusted to work with Arduino IDE 1.0 (they may or may not still work on 0022 or 0023, but that will be supported only to the point of accepting patches to increase backward compatibility).</p>

<p>There is one change which I&#8217;ve been meaning to get to for a long time: the Ports and RF12 libraries have been combined into a new library called <a href="https://github.com/jcw/jeelib">JeeLib</a>. This means that you no longer have to include &#8220;Ports.h&#8221; <em>and</em> &#8220;RF12.h&#8221; at the start of your sketches (although those still continue to work). Instead, insert this one line instead:</p>

<pre><code>    #include &lt;JeeLib.h&gt;
</code></pre>

<p>All the examples from both libraries have been retained, but make sure that the old Ports and RF12 are gone.</p>

<p>There are three other libraries for the JeeNodes which you may be using: <a href="https://github.com/jcw/ethercardhttps://github.com/jcw/ethercard">EtherCard</a>, <a href="https://github.com/jcw/glcdlib">GLCDlib</a>, and <a href="https://github.com/jcw/rtclib">RTClib</a>. These too are now on GitHib and have all been adapted for use with Arduino IDE 1.0.</p>

<p>So how does one start <em>using</em> any of these libraries? Ah, glad you asked :)</p>

<ul>
<li>go to <a href="https://github.com/jcw">GitHub</a>, and pick the library you&#8217;re interested in to get to its home page</li>
<li>at the bottom of each home page is a README section with the latest info &#8211; always worth a check</li>
<li>one of the links at the top is marked &#8220;ZIP&#8221; &#8211; you <em>could</em> click it to download everything as ZIP archive</li>
<li><em>&#8230; but you shouldn&#8217;t !</em></li>
</ul>

<p>The reason for this is <a href="http://jeelabs.org/2011/12/27/the-convenience-of-git/">Git</a>. If you download a ZIP archive, unpack it, and install it in the right place (which is in the &#8220;libraries&#8221; folder where all your sketches are), then you&#8217;ll get a <em>copy</em> of the code your after, but no way to stay up to date. That might sound like a minor detail, but with open source, there are usually <em>many</em> changes over time. Bug fixes as well as new features. If you grabbed a ZIP file, then you&#8217;ll be forced to re-install the next version over the previous one every time there is an update. This quickly gets very boring, and can lead to all sorts of awful mistakes, such as overwriting a file you had <em>fixed</em> in some way (after a lengthy debug session, long ago).</p>

<p>I can&#8217;t stress it enough: don&#8217;t grab open source software and install it on your disk by just downloading and unpacking it. Not just the stuff from JeeLabs &#8211; any open source software distributed in source code form. It&#8217;s a nuisance, a ticking time bomb, a mine field, a disaster waiting to happen &#8211; <em>get the message?</em></p>

<p>There is only one <a href="http://jeelabs.org/2011/12/27/the-convenience-of-git/">sane / long-term approach</a> to working with source code, and that&#8217;s through a <a href="http://en.wikipedia.org/wiki/Revision_control">Version Control System</a>. In this case Git &#8211; in combination with <a href="https://github.com/jcw">GitHub</a>, which is the web site where all source code is stored.</p>

<p>So instead of downloading a ZIP or TAR archive, you need to create a &#8220;clone&#8221; of the original code on GitHub. The difference with the ZIP archive is that a clone knows where it came from, and gives you the ability to find out what changed, to instantly update your clone, or to revert to <em>any</em> older version, since GitHub keeps track of <em>all</em> versions of the code &#8211; past, present, and future.</p>

<p>The trick is to get started with Git and GitHub without drowning in the capabilities they offer. <em>How</em> depends on the environment you&#8217;re in:</p>

<ul>
<li><p>On <em>Windows</em>, you can install <a href="http://code.google.com/p/tortoisegit/">TortoiseGit</a> &#8211; then you need to get that clone onto your machine. For JeeLib, you&#8217;ll probably need to enter this path to it somewhere: <code>git://github.com/jcw/jeelib.git</code></p></li>
<li><p>On <em>Mac OSX</em>, you need to install the Xcode developer tools, which includes Git (it&#8217;s a free install from the App Store). If you have an account at GitHub (or don&#8217;t mind getting one, it&#8217;s free), then I highly recommend getting the <a href="http://mac.github.com/">GitHub for Mac</a> application. If not, you can always use the command line, like so:</p>

<pre><code>    cd ~/Documents/Arduino/libraries
    git clone git://github.com/jcw/jeelib.git
</code></pre></li>
<li><p>On <em>Linux</em>, you need to install git through whatever package manager you&#8217;re using. On Debian / Ubuntu, this command line will probably do it:</p>

<pre><code>    sudo apt-get install git-core
</code></pre>

<p>After that, it&#8217;s the same as the Mac OSX command-line version, i.e. &#8220;cd &#8230;&#8221; and &#8220;git clone &#8230;&#8221; (see above).</p></li>
</ul>

<p>So far, this is only a little more involved than grabbing that ZIP file. The gains start when you want to update your copy to the latest version. In git-speak, this is called &#8220;pulling&#8221; the latest changes (to your local disk). I encourage you to search around on the web, it&#8217;s usually as simple as doing &#8220;git pull&#8221; from inside the cloned area (from the command-line, some GUI, whatever). The difference with re-installing is absolute security &#8211; &#8220;git pull&#8221; will <em>never</em> overwrite or lose any changes you made, if they conflict with the original.</p>

<p>If all you do is track changes from time to time, then this is all you need to benefit from Git and GitHub.</p>

<p>If you actually make changes, then you&#8217;ll need to dig a little deeper when a &#8220;conflict&#8221; occurs, meaning you&#8217;ve made a change which interferes with changes made in the original on GitHub. But on the plus side, you&#8217;ll also be able to <em>easily</em> submit your changes (GitHub calls this &#8220;making a pull request&#8221;, i.e. pulling your changes back <em>into</em> the original).
Why bother? <em>Because contributions, no matter how small, are what make open source software fly!</em></p>

<p><em>Welcome to the world of open source software collaboration &#8211; where code lives, evolves, and grows.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2011/12/29/out-with-the-old-in-with-the-new/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The JeeNode, as seen from 15.24 km</title>
		<link>http://jeelabs.org/2011/12/19/the-jeenode-as-seen-from-15-24-km/</link>
		<comments>http://jeelabs.org/2011/12/19/the-jeenode-as-seen-from-15-24-km/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 23:01:34 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[JeeNode]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=16963</guid>
		<description><![CDATA[(that&#8217;s 50,000 feet, or 9.47 miles &#8211; if those units mean more to you) This post was prompted by a message on the forum, about what this whole &#8220;JeeNode&#8221; thing is, really. Here are a JeeNode v6 and an Arduino Duemilanove, side by side: Let me start by saying, tongue-in-cheek: it&#8217;s all Arduino&#8217;s fault! Because [...]]]></description>
			<content:encoded><![CDATA[<p><em>(that&#8217;s 50,000 feet, or 9.47 miles &#8211; if those units mean more to you)</em></p>

<p>This post was prompted by a message on the <a href="http://forum.jeelabs.net/node/742">forum</a>, about what this whole &#8220;<a href="http://jeelabs.net/projects/hardware/wiki/JeeNode">JeeNode</a>&#8221; thing is, really.</p>

<p>Here are a JeeNode v6 and an Arduino Duemilanove, side by side:</p>

<p><img src="http://jeelabs.org/wp-content/uploads/2011/12/DSC_2826.jpg" alt="DSC 2826" border="0" width="604" height="436" /></p>

<p>Let me start by saying, tongue-in-cheek: <em>it&#8217;s all Arduino&#8217;s fault!</em></p>

<p>Because &#8211; let&#8217;s face it &#8211; the core of each Arduino and each JeeNode is really 95% the same: an Atmel AVR ATmega328P chip, surrounded by a teeny little bit of support circuitry and placed on a printed circuit board. So part of the confusion comes from the fact that the Arduino introduced its own conventions, moving it further away from the underlying <em>common</em> ATmega technology.</p>

<p>The differences between an Arduino Duemilanove and a JeeNode v6 &#8211; which resemble each other most &#8211; are:</p>

<ul>
<li>the JeeNode has a &#8220;skinnier&#8221; shape, incompatible with Arduino &#8220;shields&#8221;</li>
<li>the Arduino runs at 5V, whereas the JeeNode runs at 3.3V (this carries through to all I/O pins)</li>
<li>the JeeNode includes a wireless radio module, called the RFM12B by HopeRF</li>
<li>the Arduino includes an FTDI &lt;-> USB interface, while the JeeNode relies on an external one</li>
</ul>

<p>There are many other differences, of course &#8211; so let&#8217;s continue this list a bit:</p>

<ul>
<li>the Arduino&#8217;s &#8220;eco-system&#8221; is far, <em>far</em> bigger than the JeeNode&#8217;s (translation: everyone who finds out about JeeNodes probably already knows about the Arduino platform, and usually already has one or more of &#8216;em)</li>
<li>this carries through to articles, websites, books, and discussion forums &#8211; Arduino is everywhere</li>
<li>you can do lots of stuff with an Arduino without ever touching a soldering iron, whereas the JeeNode is really not usable without <em>some</em> soldering (even if just to solder on a few pin headers)</li>
<li>different pinouts&#8230; it&#8217;s one big conspiracy to confuse everyone, of course! <em>(just kidding: see below)</em></li>
</ul>

<p>My reasons for coming up with the JeeNode have been documented in the past on this weblog, and can be summarized as: 1) running at 3.3V for lower power consumption and to better match modern sensors and chips, and 2) supporting a simpler form of expandability through the use of &#8220;plugs&#8221; &#8211; little boards which can be mixed and matched in many different combinations.</p>

<p>On the software side, JeeNodes remain fully compatible with the Arduino IDE, a convenient software environment for Windows, Mac, and Linux to develop &#8220;sketches&#8221; and upload them to the board(s).</p>

<p>The biggest stumbling block seems to be the way pins are identified. There are 4 conventions, all different:</p>

<ul>
<li>Atmel&#8217;s hardware documentation talks about pins on its internal hardware ports, in a logical manner: so for example, there is a port &#8220;D&#8221; with 8 I/O pins numbered 0..7 &#8211; the sixth one would be called <strong>PD5</strong>.</li>
<li>Then there is the pin on the chip, this depends on which chip and which package is being referred to. On the 28-pin DIP package used for an ATmega328P, that same PD5 pin would be identified as <strong>pin 11</strong>. That&#8217;s the 11th pin, counting from the left side of the chip with pin 1 at the top.</li>
<li>The Arduino run-time library has software to control these pins. For a digital output pin, you can set it to &#8220;1&#8243; for example, by writing <strong>digitalWrite(5,1)</strong>. This resembles PD5, but it fails for other pins (PB0 is &#8220;8&#8243; in Arduino-land, and PC1 is &#8220;1&#8243; if used as an analog input, or &#8220;15&#8243; if used otherwise &#8211; go figure&#8230;).</li>
<li>The JeeNode organizes several pins as part of 6-pin &#8220;Ports&#8221; (no relation to Atmels terminology!), each of them having 1 digital and 1 analog-or-digital pin.</li>
</ul>

<p>The thing about JeeNode Ports is that there are 4 of them, and they can all be used for plugs in the same way. To support this, there&#8217;s a <a href="http://jeelabs.net/projects/cafe/wiki/Ports">Ports library</a> which lets you define port objects. This is an abstraction layer <em>on top</em> of the Arduino runtime. The reason is that it lets you associate a port object with a header on the JeeNode:</p>

<pre><code>    Port myport (2);
</code></pre>

<p>Then you can connect your hardware / sensor / plug / whatever to the header marked &#8220;P2&#8243; on the JeeNode, and access it as follows:</p>

<pre><code>    myport.digiWrite(1);
</code></pre>

<p>This happens to be the same pin as in the examples above, i.e. PD5 of an ATmega, pin 11 of the 28-pin DIP chip, and digitalWrite(5,1) on an Arduino. This also means that there are <em>numerous</em> ways to perform the same action of setting pin 11 of the chip to a logical &#8220;1&#8243; (i.e. 3.3V or 5V):</p>

<ul>
<li><p>the &#8220;raw&#8221; C-level access, using Atmel&#8217;s register conventions and definitions (fastest, by far):</p>

<pre><code>    PORTD |= 1 &lt;&lt; 5;  // or ...
    PORTD |= _BV(5);  // same thing
    bitSet(PORTD, 5); // same thing, using an Arduino macro
</code></pre></li>
<li><p>the Arduino way of doing things:</p>

<pre><code>    digitalWrite(5, 1);
</code></pre></li>
<li><p>the JeeNode Ports library way of doing things, as shown above:</p>

<pre><code>    Port myport (2);
    myport.digiWrite(1);
</code></pre></li>
<li><p><em>&#8230; let&#8217;s throw in an extra bullet item, since every other list in this post appears to come in fours ;)</em></p></li>
</ul>

<p>The one (minor) benefit you get from using the Ports approach on a JeeNode, is that if you attach your hardware to a different port, say port 3, then you only need to change a single line of code (to &#8220;<code>Port myport (3);</code>&#8221; in this case). The rest of the code, i.e. everywhere where its pins are being read or written, can then remain the same.</p>

<p>For an overview of all pinout differences, see also <a href="http://jeelabs.org/2011/11/10/pins-damned-pins-and-jeenodes/">this weblog post</a>. For full details, see the <a href="http://jeelabs.net/attachments/388/JN5-pinout.pdf">JeeNode PDF docs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2011/12/19/the-jeenode-as-seen-from-15-24-km/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Developing a low-power sketch</title>
		<link>http://jeelabs.org/2011/12/13/developing-a-low-power-sketch/</link>
		<comments>http://jeelabs.org/2011/12/13/developing-a-low-power-sketch/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 23:01:00 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LowPower]]></category>

		<guid isPermaLink="false">http://jeelabs.org/?p=16464</guid>
		<description><![CDATA[As you&#8217;ll know if you&#8217;ve been reading this weblog for more than a few nanoseconds, I put a lot of time and effort into making the ATmega-based JeeNode use as little power as possible &#8211; microwatts, usually. In the world of ultra-low power, the weakest link in the chain will determine whether your sketch runs [...]]]></description>
			<content:encoded><![CDATA[<p>As you&#8217;ll know if you&#8217;ve been reading this weblog for more than a few nanoseconds, I put a lot of time and effort into making the ATmega-based JeeNode use as little power as possible &#8211; microwatts, usually.</p>

<p>In the world of ultra-low power, the weakest link in the chain will determine whether your sketch runs days, weeks, months, or years&#8230; low power can be a surprisingly elusive goal. <em>The last <a href="http://en.wikipedia.org/wiki/Coulomb">microoulombs</a> are the hardest!</em></p>

<p>But it&#8217;s actually quite easy to get some real savings with only a little effort. Here are some things to avoid:</p>

<ul>
<li><p>Don&#8217;t optimize in the wrong place &#8211; it&#8217;s tempting to start coding in a way which <em>seems</em> like a good idea in terms of power consumption, although more often than not the actual gains will be disappointing.</p></li>
<li><p>Don&#8217;t leave the lights on &#8211; the ATmega is amazingly easy to power down, which instantly reduces its consumption by several orders of magnitude. Make sure you do the same for every major power consumer.</p></li>
<li><p>Don&#8217;t just sit there, waiting &#8211; the worst thing you can do in terms of power consumption is <em>wait</em>. Unfortunately, that&#8217;s precisely what the Arduino runtime&#8217;s <em>delay()</em> and <em>delayMicroseconds()</em> calls do.</p></li>
</ul>

<p>Ok, with this out of the way, I&#8217;ll describe a very simple way to get power consumption down &#8211; and hence battery lifetimes up (<em>waaay</em> up in fact, usually).</p>

<p>The trick is to use a convenience function from <a href="https://github.com/jcw/jeelib">JeeLib</a> (a.k.a. the Ports library). It&#8217;s in the &#8220;Sleepy&#8221; class, and it&#8217;s called <em>loseSomeTime()</em>. So if you have this in your code:</p>

<pre><code>    delay(100);
</code></pre>

<p>&#8230; then you should replace it with this:</p>

<pre><code>    Sleepy::loseSomeTime(100);
</code></pre>

<p>You also need to include the following code at the top of your sketch to avoid compilation and run-time errors:</p>

<pre><code>    #include &lt;JeeLib.h&gt;

    ISR(WDT_vect) { Sleepy::watchdogEvent(); }
</code></pre>

<p>As the name indicates, the timing is not exact. That&#8217;s because the ATmega is put into a power down mode, and then later gets woken up by the watchdog timer (this is hardware, part of the ATmega). This timer can be several percent off, and although the milliseconds timer will automatically be adjusted by <em>loseSomeTime()</em>, it won&#8217;t be as accurate as when updated by the crystal or the ceramic resonator often used as system clock.</p>

<p>The second issue with the watchdog is that it can only delay in multiples of ≈ 16 ms. Any call to <em>loseSomeTime()</em> with an argument less than 16 will cause it to return immediately.</p>

<p>Furthermore, <em>loseSomeTime()</em> can only work with argument values up to 60,000 (60 seconds). If you need longer delays, you can simply create a loop, i.e. to wait 120 minutes in ultra-low power mode, use this:</p>

<pre><code>    for (byte i = 0; i &lt; 120; ++i)
      Sleepy::loseSomeTime(60000);
</code></pre>

<p>One last aspect of <em>loseSomeTime()</em> to be aware of, is that it will abort if an interrupt occurs. This doesn&#8217;t normally happen, since the ATmega is shut down, and with it most interrupt sources. But not all &#8211; so if <em>loseSomeTime()</em> returns prematurely, it will return 0. Normally, it returns 1.</p>

<p>The trade-off of <em>loseSomeTime()</em> is power consumption (system clock and timers are shut down) versus accuracy.</p>

<p>But the gains can be huge. Even this simple LED blink demo will use about 10 mA less (the ATmega&#8217;s power consumption while running at 16 MHz) than a version based on <em>delay()</em> calls:</p>

<pre><code>    void loop () {
      digitalWrite(4, 1);
      Sleepy::loseSomeTime(250);
      digitalWrite(4, 0);
      Sleepy::loseSomeTime(250);
    }
</code></pre>

<p>To reduce this even further, you could shorten the blink ON time as follows:</p>

<pre><code>    void loop () {
      digitalWrite(4, 1);
      Sleepy::loseSomeTime(50);
      digitalWrite(4, 0);
      Sleepy::loseSomeTime(450);
    }
</code></pre>

<p>The LED may be somewhat dimmer, but the battery will last 10x longer vs. the original <em>delay()</em> version.</p>

<p><em>Battery-powered operation isn&#8217;t hard, you just have to think a bit more about where the energy is going!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jeelabs.org/2011/12/13/developing-a-low-power-sketch/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.628 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-04 20:53:26 -->
<!-- Compression = gzip -->
