<?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>Memory Leak &#187; arduino</title>
	<atom:link href="http://www.foobert.com/blog/tag/arduino/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.foobert.com/blog</link>
	<description>That which fades into the ether.</description>
	<lastBuildDate>Sat, 26 Nov 2011 20:49:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Arduino battery capacity tester</title>
		<link>http://www.foobert.com/blog/2009/11/08/arduino-battery-capacity/</link>
		<comments>http://www.foobert.com/blog/2009/11/08/arduino-battery-capacity/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 07:03:09 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Geek-Stuff]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[arduino]]></category>

		<guid isPermaLink="false">http://www.foobert.com/blog/?p=471</guid>
		<description><![CDATA[<p style="text-align: left;">My first Arduino project was to build a battery capacity tester.  I&#8217;ve got a box of rechargeable AA batteries, and it seams they&#8217;ve been less and less effective. Since most applications require 4 batteries, invariably one problem battery makes the rest of them look bad.</p>
<p>The Atmel ATMega328 microcontroller has 6 analog inputs with [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">My first <a href="http://arduino.cc/en">Arduino</a> project was to build a battery capacity tester.  I&#8217;ve got a box of rechargeable AA batteries, and it seams they&#8217;ve been less and less effective. Since most applications require 4 batteries, invariably one problem battery makes the rest of them look bad.</p>
<p>The Atmel ATMega328 microcontroller has 6 analog inputs with 10-bit A-to-D converters and a external AREF that allows you to define what voltage 0x3FF represents. In other words, it&#8217;ll give you ~1.4mV precision measuring 0-1.5V when given a 1.5V analog reference. Plenty accurate for a battery capacity measurement.</p>
<p>The principle is fairly simple. Apply a known load to a battery, record the voltage periodically while the battery discharges, stop recording at some point, and integrate to arrive at the area under the curve in order to derive the amp-hours delivered from the battery.</p>
<p>Enough theory, let&#8217;s see how it works.  The UI starts with a helpful message:</p>
<p style="text-align: center;"><strong>Insert Battery</strong><br />
<img src="http://foobert.com/linked/2009/20091107215531_DSC_4782.jpg" alt="" width="696" height="700" /><br />
<span style="color: #999999;">NIKON D70, ISO 500, ƒ/2.0, 1/20sec, 50mm focal L.</span></p>
<p style="text-align: left;">While the battery discharges, the UI prints the voltage, real-time capacity measured thus far, and the duration that the measurement has been taking place.</p>
<p style="text-align: center;"><strong>Battery In</strong><br />
<img src="http://foobert.com/linked/2009/20091107221459_DSC_4783.jpg" alt="" width="671" height="700" /><br />
<span style="color: #999999;">NIKON D70, ISO 500, ƒ/2.0, 1/25sec, 50mm focal L.</span></p>
<p style="text-align: left;">When the cut-off voltage of 0.9V has been reached, the &#8220;usable&#8221; capacity is saved at the top.  The real-time data continues so the capacity below 0.9V is measured.  For the NiMH batteries I&#8217;ve tested, the capacity below 0.9V is minimal (~100mAH).</p>
<p>After the cut-off is reached, an LED starts blinking to attract attention that the test is effectively over.</p>
<p style="text-align: center;"><strong>Done</strong><br />
<img src="http://foobert.com/linked/2009/20091107215454_DSC_4780.jpg" alt="" width="494" height="700" /><br />
<span style="color: #999999;">NIKON D70, ISO 500, ƒ/2.0, 1/20sec, 50mm focal L.</span></p>
<p><a href="http://foobert.com/linked/2009/battery_test.pde">Here&#8217;s the code (click to download as text rather than the questionable HTML translated version pasted below):</a></p>
<blockquote><pre>
/*  Battery Characterization Tool

     11/7/2009 John Terry

     Compiled on Aurdino 0017 in 4930 bytes.

 This uses the analog in to measure the voltage of a battery under a known load and
 integrates the area under the curve to arrive at the useful capacity of the
 batterin in mAH.
*/

#include &lt;LiquidCrystal.h&gt;

// Set some constants -- these will need to be adjusted for your setup
//
// Connect a ~10K Ω (Rr) resistor from 3.3V supply pin to the AREF pin,
//   creating a voltage divder with the internal 32KΩ resistor on AREF.
//      aRefVoltage = supply_voltage * 32K / Rr
//   Alternatively, measure the actual AREF voltage applied with a good DMM:
//  my measured aRefVoltage = 2.62;
const float     aRefVoltage = 2.62;

// Connect a load resistor (Rl) to the battery. ~2.2 Ω restistor gives ~500mA drain which
//   is about right for a battery rated at 2500mAH.  Note, this should be a &gt;=1W resistor!
//   Dont trust your DMM to meausre such a low resistance accurately. I measured the current
//   and back-calculated the resistance. Best just to trust rated resistance.
//   my  resistance = 2.18;
//
// The integrator works by accumulating the sampled voltage values from the start until
//   hitting a pre-determined low-voltage threshold. Since they are sampled at a
//   known period, the number of samples taken cancels out and the accumulation of all samples
//   simply needs to be scalled by a factor
//
//   Let's make some definitions to show the derivation of how this is so:
//   I = current
//   V = load voltage
//   Rl = load resistance
//   samples = number of voltage samples made
//   sensor = value read out of analog A-&gt;D
//   rate = number of samples taken per second
//
//                                     ave(V)
//   capacity = ∫I ~= ave(I) * time = -------- * time
//                                       Rl
//
//   Where:
//                ∑(V)     ∑(sensor) * aRefVoltage/1024
//     ave(V) = ------- = ---------------------------------
//              samples              samples
//
//      time  =  samples/rate
//
//   Thus:
//               ∑(sensor) * aRefVoltage     samples
//   capacity = ------------------------- * ---------
//                samples * 1024 * Rl         rate
//
//               ∑(sensor) * aRefVoltage
//   capacity = ------------------------- = ∑(sensor) * quanta
//                  1024 * Rl * rate
//
//           quanta = aRefVoltage/(1024 * Rl * rate ) * 1000/3600   // scaled for mA Hours
const double quanta = 0.00030179; // 2.62/1024/2.355/3.6

// Define low voltage threshold where any remaining capacity is "unusable"
//        lowThreshold = 0.9V * 1024/aRefVoltage
const int lowThreshold = 354;

int sensorPin          = 0;    // select the analog input pin for the voltage measurement
int ledPin             = 13;   // select the pin for the LED
int fetGatePin         = 8;    // select the pin for the LED
int sensorValue        = 0;    // unscaled sensor output
float voltage          = 0;    // measured voltage
double mAH             = 0;    // Calculated current
long accumulator       = 0;    // sum of all unscalled sensor values sampled
int epoch              = 0;    // seconds since battery was inserted
int lowVolts           = 0;    // debounce the low voltage threshold
boolean done           = false;// Voltage has dropped below threshold
boolean batteryIn      = false;// Battery present

// initialize the the LCD library with the numbers of the interface pins
//  LCD Pins  --  RS, EN, D4, D5, D6, D7
LiquidCrystal lcd(12, 11,  5,  4,  3,  2);

void setup() {
  // declare the ledPin and fetGatePin as an OUTPUT:
  pinMode(ledPin,     OUTPUT);
  pinMode(fetGatePin, OUTPUT);  

  // set analog reference to external
  analogReference(EXTERNAL);

  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);

  // Print a helpful start-up message to the LCD.
  lcd.print("Insert battery");

  // DEBUG initialize serial communications at 9600 bps:
  // Serial.begin(9600);
}

void loop() {

  sensorValue = analogRead(sensorPin);    

  if (!batteryIn &#038;&#038; (sensorValue &gt; 100)) {
    // Initialize upon the insertion of a "fresh" battery
    batteryIn   = true;
    done        = false;
    epoch       = 0;
    accumulator = 0;
    lowVolts    = 0;
    digitalWrite(ledPin, LOW);
    digitalWrite(fetGatePin, HIGH);
    // clear out when LCD when starting over
    lcd.setCursor(0, 0);
    lcd.print("vlts  mAH  Time ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
  }
  else if (batteryIn &#038;&#038; done &#038;&#038; (sensorValue &lt;= 20)) {
    // consider the battery removed only after finishing the last measurement to
    // debounce a glitchy concact
    batteryIn=false;
  }
  else if (batteryIn) {
    // Running state during discharge state

    voltage = sensorValue*aRefVoltage/1024.0;

    // print voltage
    lcd.setCursor(0, 1);
    lcd.print(voltage);

    if (!done) {
      accumulator += sensorValue;
      // print mAH
      mAH = accumulator*quanta;
      if      (mAH &lt;   10) { lcd.setCursor(8, 1); }  // adjust to make it perdy
      else if (mAH &lt;  100) { lcd.setCursor(7, 1); }
      else if (mAH &lt; 1000) { lcd.setCursor(6, 1); }
      else                 { lcd.setCursor(5, 1); }
      lcd.print(int(mAH));

    }

    // print current time
    lcd.setCursor(11, 1);
    lcd.print(epoch/60.0);
    lcd.setCursor(15, 1);
    lcd.print("m");

    if (!done) {

      // lowVolts requires 10 seconds in the last 20 before being done
      if (sensorValue &lt; lowThreshold) { lowVolts++;}
      else if (lowVolts &gt; 0)          { lowVolts--;}  

      // If it's below threshold for 10 of 20 samples, bail out
      if (lowVolts &gt; 10) {
        done=true;
        digitalWrite(fetGatePin, LOW);
        lcd.setCursor(0, 0);
        lcd.print("    mAH in     ");
        lcd.setCursor(1, 0);
        lcd.print(int(mAH));

        // put the time, in minutes in the upper right
        lcd.setCursor(11, 0);
        lcd.print(epoch/60.0);
        lcd.setCursor(15, 0);
        lcd.print("m");

        // Clear out the bottom line
        lcd.setCursor(4, 1);
        lcd.print("v      ");
      }
    }

    epoch++;
  } // batteryIn -- main routine

  if (done) {
    // When done, flash the LED to get attention
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(499);
  } else {
    // Since the processing takes some time prior to the delay, we'll assume 1mS
    // This could stand to be improved with an interrupt routine that is kicked off
    // before all the processing starts for each loop
    delay(999);
  }

/* DEBUG

  Serial.print("\nsensor = " );
  Serial.print(sensorValue);
  Serial.print("\t lowvolrs = " );
  Serial.print(lowVolts);
*/

}
</pre>
</blockquote>
<p>Compiles into 4930 bytes.</p>
<p>Schematics, excluding the LCD display (pin-out is listed in the code).</p>
<p style="text-align: center;">
<img src="http://foobert.com/linked/2009/capacity_meter_bb.png" alt="" />
</p>
<p style="text-align: center;">
<img src="http://foobert.com/linked/2009/capacity_meter_schem.png" alt=""  /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.foobert.com/blog/2009/11/08/arduino-battery-capacity/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Hello Arduino</title>
		<link>http://www.foobert.com/blog/2009/11/06/hello-arduino/</link>
		<comments>http://www.foobert.com/blog/2009/11/06/hello-arduino/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 07:50:20 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Geek-Stuff]]></category>
		<category><![CDATA[arduino]]></category>

		<guid isPermaLink="false">http://www.foobert.com/blog/?p=466</guid>
		<description><![CDATA[<p style="text-align: left;">
Success has been had &#8230; finally!</p>
<p>A couple of days ago, a tiny little package arrived from Sparkfun Electronics with my first Arduino kit.  Valerie&#8217;s response upon finding said package in the mail box, &#8220;well, so much for seeing my husband for the rest of the week&#8221;.   In all fairness, I waited [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">
Success has been had &#8230; finally!</p>
<p>A couple of days ago, a tiny little package arrived from <a href="http://www.sparkfun.com/commerce/categories.php">Sparkfun Electronics</a> with my first Arduino kit.  Valerie&#8217;s response upon finding said package in the mail box, &#8220;well, so much for seeing my husband for the rest of the week&#8221;.   In all fairness, I waited till the second day (night, really) to get sucked in.  I&#8217;ll be back tomorrow&#8230;</p>
<p>Last night, I just plugged it into the USB port and loaded a pre-canned example that made the LED blink. Did some experimenting with modulating in and out with a few for loops just to refresh my C code.</p>
<p>Tonight was all about getting the LCD wired in. Soldered a header onto it to mash into the bread board to keep the wiring flexible. </p>
<p>Opened up the <a href="http://www.arduino.cc/en/Tutorial/LiquidCrystal">LiquidCrystal</a> tutorial and wired it all up and measured the resistance between power and ground to make sure I didn&#8217;t have any shorts. Finally plugged it in to the USB spigot and dumped the &#8220;hello, world&#8221; example program, and &#8230; nothing.</p>
<p>Hmmph.  Take some measurements &#8212; voltages are good. Contrast voltage bias is about half-way, that ought to be <em>close</em>. Let&#8217;s try a little higher: nope. Lower?  Nope.  </p>
<p>OK, rip it all out and move to a different section of the breadboard.  Still nothing.  The LCD doesn&#8217;t so much as blink.  Must be dead.</p>
<p>In fit of desperation, I grab pair of random resistors and jumper them across my voltage divider that&#8217;s biasing the contrast, first moving contrast closer to VDD, then moving it much closer to VSS and &#8212; presto &#8212; it was running all along.
</p>
<p style="text-align: center;"><strong>It&#8217;s alive!</strong><br />
<img src="http://foobert.com/linked/2009/20091106001935_DSC_4775.jpg" width=800 height=618/><br />
<span style="color: #999999;">NIKON D70, ISO 500, ƒ/1.8, 1/30sec, 50mm focal L.</span></p>
<p style="text-align: left;">For the record, this particular 5-volt LCD contrast likes to biased at about 1.3 volts.  2.5 volts get&#8217;s you nothing.</p>
<p>My first real &#8220;project&#8221; is to build a rechargeable battery characterization tool.  More on that later&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.foobert.com/blog/2009/11/06/hello-arduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

