<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6144971837550808157</id><updated>2012-02-17T14:09:07.533+10:00</updated><title type='text'>Make The Future</title><subtitle type='html'>&lt;p&gt;The future's not what it used to be.    Technology's Promises of Smarter, Better, Cooler are late, dammit.&lt;/p&gt;

&lt;p&gt;Doing It Yourself is Making the Future&lt;/p&gt;</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.unixbigot.id.au/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default'/><link rel='alternate' type='text/html' href='http://blog.unixbigot.id.au/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>unixbigot</name><uri>http://www.blogger.com/profile/02358053867280926657</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_N-TT1bhNS4U/TAGZtA4E8WI/AAAAAAAAAcQ/cktvaoz9anU/S220/profile+picture.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6144971837550808157.post-5612282973443033073</id><published>2011-03-19T22:27:00.004+10:00</published><updated>2011-12-02T16:35:46.878+10:00</updated><title type='text'>Flat Mate - protect your LiPo cells from over-discharge</title><content type='html'>&lt;p&gt;This project monitors the voltage of a battery and displays a 5-element bar-graph indicating battery charge state.  A MOSFET can be added if desired cut off supply at low voltage, to protect the battery from over-discharge.&lt;/p&gt;&lt;p&gt;It is particularly applicable for multi-cell Lithium-ion Polymer (LiPo) packs, which are easily ruined if over-discharged.&lt;/p&gt;&lt;p&gt;It is intended for use in high power applications such as portable lighting, robotics and power tools where a charged SLA or LiPo battery is discharged in the presence of a human operator.  The first deployment was for a high-power bicycle headlamp.  Design was inspired by the simple state-of-charge indicators present on some electric bicycles.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5536451489/" title="Untitled by Christopher Biggs, on Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5216/5536451489_35d8a38bfc.jpg" width="500" height="374" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;The advantage over existing inexpensive "off the shelf" protection circuits is that this circuit gives a visual indication of state-of-charge, presented as an easy-to-read bar-graph.  Existing protection circuits (often designed for flashlights or model aircraft) either have no display (sudden surprise cutoff), digital voltage display (requires memorizing cutoff voltages) or audio-only alert (no visual display).&lt;/p&gt;&lt;p&gt;An Atmel ATTiny45 microcontroller is used to sample the battery voltage and control the LED bargraph.  Power for the microcontroller is derived from a 78L05 regulator connected to the main battery. Battery voltage is monitored using an analog input via a voltage divider and compared to the microcontroller's 1.1v internal voltage reference.&lt;/p&gt;&lt;p&gt;The bargraph will show 4 LEDs when at "full charge" and  1 LED when at minimum usable voltage.  When voltage is critical a 5th "alert" LED will illuminate (and output supply can be terminated).&lt;/p&gt;&lt;p&gt;The circuit can be used purely as a voltage indicator by leaving off the optional MOSFET switch components.  If a MOSFET is fitted this can be used to disconnect the main load when the battery is depleted.&lt;/p&gt;&lt;h2&gt;Hardware Details&lt;/h2&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5539220839/" title="Flat Mate Schematic"&gt;&lt;img src="http://farm6.static.flickr.com/5134/5539220839_34b714ea7f.jpg" width="500" height="227" alt="Flat Mate Schematic" /&gt;&lt;/a&gt;  &lt;p&gt;The bargraph consists of 5 3mm LEDs (green, yellow and red) arranged GYYYR.  The first four (GYYY) form the bargraph and the 5th (red) is the critical alert.  The first three are connected to dedicated output pins, the final two share a single pin in a totem-pole arrangement. The fourth LED shares state with the output FET (if fitted), when this LED is lit, power is good and the FET is enabled.  When this output pin is cleared, the fifth LED (the complementary red CRITICAL alert) is illuminated and the FET is disabled.&lt;/p&gt;&lt;p&gt;As the monitor circuit is indended for use in high-drain situations (0.5--10A), the approx 20mA consumed by the monitoring circuit is considered negligible.  In particular the voltage divider is not isolated from supply between samples, so presents a constant drain of around 850uA.  Current through the bargraph elements is limited by a 1kΩ resistor to around 3mA per element, so the bargraph consumes up to 12mA when at FULL level.&lt;/p&gt;&lt;h2&gt;Software Details&lt;/h2&gt;&lt;p&gt;Software is configurable for the type and number of cells.   The initial application of a 3-cell (11.4v nominal) Lithium-ion Polymer (LiPo) battery will be used to describe the code.&lt;/p&gt;&lt;p&gt;Four voltage thresholds are configured (in millivolts), depending on battery type. For a 3S LiPo these are:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;    BMV_FULL = 12.000v
    BMV_GOOD = 11.000v
    BMV_LOW  = 10.000v
    BMV_CRIT =  9.000v
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The code converts these thresholds to analog sample values at compile time, defining corresponding VL_FULL..CRIT constants.&lt;/p&gt;&lt;p&gt;These thresholds give the following LED readouts:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;    if &amp;gt;= FULL output GYYY_
    if &amp;gt;= GOOD output _YYY_
    if &amp;gt;= LOW  output __YY_
    if &amp;gt;= CRIT output ___Y_
    if  &amp;lt; CRIT output ____R
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Setup&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Configure analog pin as input&lt;/li&gt;
&lt;li&gt;Configure LED pins as outputs&lt;/li&gt;
&lt;li&gt;Strobe the LEDs for 1s to see that they all work&lt;/li&gt;
&lt;li&gt;Configure timer 1 to give 4Hz interrupts&lt;/li&gt;
&lt;li&gt;Configure ADC for internal 1v1 reference, selecting single-ended channel 2 &lt;/li&gt;
&lt;li&gt;Set ADC clock prescaler to give 125kHz, take and discard one sample&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Main loop&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;do nothing, go straight to sleep mode until timer interrupt.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Timer interrupt handler&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Read battery voltage &lt;ul&gt;&lt;li&gt;initiate conversion&lt;/li&gt;
&lt;li&gt;busy-wait until conversion complete&lt;/li&gt;
&lt;li&gt;read completed value&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Update sample value &lt;ul&gt;&lt;li&gt;add sample value to accumulator&lt;/li&gt;
&lt;li&gt;if 4 samples are accumulated, calculate the average and reset accumulator&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Update the display whenver accumulator is reset &lt;ul&gt;&lt;li&gt;Clear all LEDs&lt;/li&gt;
&lt;li&gt;Light the CRITICAL LED (and kill power) if level is &lt;code&gt;&amp;lt;=VL_CRIT&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set the bargraph appropriately if level is &lt;code&gt;&amp;gt;VL_CRIT&lt;/code&gt; (see above)&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Since samples are accumulated at 4Hz, and each 4 samples are averaged, the outputs will be changed only once per second.  Averaging is used to avoid falsely detecting a CRITICAL state when transient spikes such as motor start occur.&lt;/p&gt;&lt;h1&gt;Construction&lt;/h1&gt;&lt;p&gt;The circuit was prototyped on a solderless breadboard, with battery simulated using a 24v supply and an adjustable-output regulator (LM317T).&lt;/p&gt;&lt;p&gt;A selection of LEDs with one leg replaced by a current-limiting resistor have been made to simplify use of LEDs in breadboard prototypes.&lt;/p&gt;&lt;p&gt;For in-circuit programming I use the tuxgraphics AVRUSB500v2, as this programmer features a 5-pin SIL programming header, which is very convenient for breadboards.   &lt;/p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5509231700/" title="IMG_2933 by Christopher Biggs, on Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5218/5509231700_bf039f7708.jpg" width="378" height="500" alt="IMG_2933" /&gt;&lt;/a&gt;  &lt;p&gt;Once the design was proven, a version was constructed on through-hole prototyping board with components arranged to take advantage of pin-bridging where possible.  This turned out to be difficult since the chosen double-sided prototype board featured a solder mask that was &lt;em&gt;VERY&lt;/em&gt; resistant to bridging (this would normally be a laudable feature).  A plain copper single-sided protoboard would have actually been a better choice.&lt;/p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5492709078/" title="Flat Mate by Christopher Biggs, on Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5213/5492709078_a13bd9b440.jpg" width="500" height="382" alt="Flat Mate" /&gt;&lt;/a&gt;  &lt;p&gt;The prototype has been deployed for use as a bicycle headlamp battery pack.&lt;/p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5537032178/"
   title="Untitled by Christopher Biggs, on
   Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5132/5537032178_6c9ced2f6a.jpg"
         width="374" height="500" alt="" /&gt;&lt;/a&gt;  &lt;a href="http://www.flickr.com/photos/22634189@N06/5537032200/"
   title="Untitled by Christopher Biggs, on
   Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5053/5537032200_a089d8db26.jpg"
         width="500" height="374" alt="" /&gt;&lt;/a&gt;   &lt;p&gt;Subsequent revisions will use ATTiny25 in SMD package, and SMD components.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5539804348/" title="Flat Mate PCB by Christopher Biggs, on Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5213/5539804348_ab2bf86213.jpg" width="261" height="92" alt="Flat Mate PCB" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Full code and design files are available on &lt;a href="https://github.com/unixbigot/Flat-Mate"&gt;GitHub&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; Here is the etched and drilled PCB for the surface mount version (with through-hole headers and MOSFET), alongside a SOIC to DIL breakout board constructed to allow programming the ATTiny25:&lt;p&gt; &lt;p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/5567231893/" title="Flat Mate PCB and breakout by Christopher Biggs, on Flickr"&gt;&lt;img src="http://farm6.static.flickr.com/5011/5567231893_09cf8fed7f.jpg" width="500" height="204" alt="Flat Mate PCB and breakout" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Thanks to &lt;a href="http://vlxs.wordpress.com/2010/02/14/soic-to-dil8-breakout-board/"&gt;Warranty Voider&lt;/a&gt; for the breakout board design.&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;&lt;b&gt;Update 2:&lt;/b&gt; Here is the assembled SMD prototype:&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/6146301763/" title="SMD Assembly"&gt;&lt;img src="http://farm7.staticflickr.com/6074/6146301763_b179a16736.jpg" width="500" height="374" alt="SMD Assembly"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/22634189@N06/6146850166/" title="SMD Assembled"&gt;&lt;img src="http://farm7.staticflickr.com/6074/6146850166_fb857fac8d.jpg" width="500" height="373" alt="SMD Assembled"&gt;&lt;/a&gt;&lt;p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6144971837550808157-5612282973443033073?l=blog.unixbigot.id.au' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='https://github.com/unixbigot/Flat-Mate' title='Flat Mate - protect your LiPo cells from over-discharge'/><link rel='replies' type='application/atom+xml' href='http://blog.unixbigot.id.au/feeds/5612282973443033073/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6144971837550808157&amp;postID=5612282973443033073' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default/5612282973443033073'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default/5612282973443033073'/><link rel='alternate' type='text/html' href='http://blog.unixbigot.id.au/2011/03/flat-mate-protect-your-lipo-cells-from.html' title='Flat Mate - protect your LiPo cells from over-discharge'/><author><name>unixbigot</name><uri>http://www.blogger.com/profile/02358053867280926657</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_N-TT1bhNS4U/TAGZtA4E8WI/AAAAAAAAAcQ/cktvaoz9anU/S220/profile+picture.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://farm6.static.flickr.com/5216/5536451489_35d8a38bfc_t.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6144971837550808157.post-7728269975673228637</id><published>2009-11-09T21:52:00.002+10:00</published><updated>2009-11-09T21:56:16.702+10:00</updated><title type='text'>Drive an RGB led with software PWM</title><content type='html'>&lt;p&gt;This project illustrates how to use an ATTiny microcontroller and an
RGB LED to create a coloured light that cyles continuously through
random colours, but changes colour dramatically at the press of a
switch. &lt;/p&gt;

&lt;h2&gt;Motivation&lt;/h2&gt;

&lt;p&gt;Why?  My kids had this $2 novelty pen that did this employing a teensy
circuit along with 3x3mm LEDs and some alkaline coin-cells.  They
broke it almost instantly, losing some critical small parts.&lt;/p&gt;

&lt;p&gt;"Daddy, my light-up pen broke!  WAAAAAAAAAAAAAAHHH!!!"  &lt;/p&gt;

&lt;p&gt;Having deprived themselves of the captivating coloured lights, they
then drew on the walls with the
instrument-formerly-known-as-the-light-up-pen.  Then they experimented
with strangling each other with the pen's narrow ribbon lanyard.  I
decided to replicate the pretty light without the other problems.&lt;/p&gt;

&lt;h2&gt;Circuit Overview&lt;/h2&gt;

&lt;p&gt;&lt;img src="http://farm3.static.flickr.com/2645/4088665281_45e7e71656_o.png" alt="Schematic" title="" /&gt;&lt;/p&gt;

&lt;p&gt;This circuit produces coloured light by using an 8-pin ATTiny13
microcontroller and Pulse-width-modulation
(PWM) to vary the brightness of three LEDs (Red, Green and Blue) that
are closely located, or share a common package.   I used a
common-anode 5mm RGB led, which looks just like a normal 5mm LED but
has 4 leads instead of two.&lt;/p&gt;

&lt;p&gt;Since the ATtiny13 only has one timer with 2 PWM channels, it cannot
control 3 PWM channels in hardware.  Instead, I do the PWM logic in
software, timing the pulse modulation via a regular tick (interrupt)
from the ATTiny's hardware timer.  (Nowadays you can get the ATTiny45
and friends which have 3 PWM channels, but I could not obtain them
when I first built the prototype for my middle child a couple of years
ago).   My youngest child is now 2, so I dug this project out of the
mothball bunker for her to play with.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;WARNING&lt;/em&gt;: modern multi-colour LEDs can be PAINFULLY bright.  If
children will play with this, choose one of the following: ensure your
LED is not a superbright, or use a diffuse led, or diffuse a clear one
(eg cover it with half a table tennis ball, or enclose it in resin or wax.
You could also use higher-value resistors to limit current, or lower
&lt;code&gt;PWM_OC_MAX&lt;/code&gt; to limit the PWM duty-cycle (LED brightness) via software.&lt;/p&gt;

&lt;p&gt;Source code and schematic are available as usual from &lt;a href="http://github.com/unixbigot/bogo-pwm"&gt;github.com&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Pulse-Width Modulation provides colour choice&lt;/h2&gt;

&lt;p&gt;The microcontroller should be configured to obtain the system clock
from the 8MHz internal RC oscillator.  The ATTiny13 factory fuse
settings include a divide-by-8 of the RC oscillator giving 1MHz, so
remember to change that fuse to get the original 8MHz signal..&lt;/p&gt;

&lt;p&gt;The overflow interrupt from the hardware 8-bit timer triggers an
interrupt service routine (roughly 32000 times per second) which
increments each of 3 separate 8 bit counters.  These counters, and
corresponding "Output Compare" (OC) values are used to generate 3
Pulse-Width-Modulated waveforms, with each counter being associated
with a particular output pin.  Every time a counter reaches zero, the
corresponding pin is turned on.  When the counter reaches its &lt;code&gt;PWM_OC&lt;/code&gt;
value it is turned off.  When the counter reaches its &lt;code&gt;PWM_TOP&lt;/code&gt; value
(255) it wraps around to zero and the cycle repeats.&lt;/p&gt;

&lt;p&gt;The effect of all this is that each of the 3 output pins is driven
with a square-wave having a wave-legnth of 256 timer ticks (about
1/128 of a second), which is equivalent to a frequency of 128Hz
(cycles-per-second).  The &lt;code&gt;OC&lt;/code&gt; value controls the "duty-cycle" (ratio
of on-period to off-period) of the wave, with 0 giving always off, 255
always on, and any intermediate value giving a wave that is on for
x/255 of a cycle and off for the remainder of each cycle.  If an
electric light is powered by such a square wave, with a frequency of
more than about 50Hz, the human eye does not percieve any
flickering---instead changes in the duty cycle are perceived as
changes in brightness (this is how most household dimmers work).  With
3 leds in Red, Green and Blue, and the ability to vary the perceived
brighness of each, you can produce many different colours, exactly as
is done in modern TVs and projectors.&lt;/p&gt;

&lt;p&gt;So using 3 PWM outputs, we have a coloured light that we can set to
any brightness and colour (denoted by a particular [R=x, G=y, B=z]
tuple) that we desire.&lt;/p&gt;

&lt;p&gt;BTW, it's not &lt;em&gt;strictly&lt;/em&gt; necessary to use 3 separate counters.  A
single counter could be used with 3 different OC values to control
each output pin independently.  I chose to implement 3 distinct
channels (rather than 1 channel with 3 Output-compare values) out of
some voodoo idea that running the channels out-of-phase will lower the
peak current draw and give slightly longer battery life.  For example,
if you set &lt;code&gt;PWM_TOP&lt;/code&gt; to 240 and &lt;code&gt;PWM_OC_MAX&lt;/code&gt; to 80, then if the PWM
counters are initialized at 0,80 and 160, the 3 output pins will never
be on simultaneously, and peak output current will never be more than
20mA.  This also means you can get away with ONE resistor instead of
3.&lt;/p&gt;

&lt;h2&gt;Colour change by drifting PWM parameters&lt;/h2&gt;

&lt;p&gt;A coloured light isn't very interesting.  But I have it on good (2
year old) authority that a pulsing and constantly changing light is captivating.&lt;/p&gt;

&lt;p&gt;For each of the 3 channels, at the end of every PWM cycle (when the
counter reaches 255), the duty cycle of the particular channel changes
by a small amount.  This causes the colour to change.  The rate that
each of the PWM channels changes duty-cycle is chosen semi-randomly
such that an ever changing progression of colour is produced.&lt;/p&gt;

&lt;h2&gt;User interface&lt;/h2&gt;

&lt;p&gt;A pulsing coloured light is nice, but when you're 2, a BUTTON TO PUSH
is best of all.&lt;/p&gt;

&lt;p&gt;If the CHANGE button is pressed, the LEDs are reset to all off
(&lt;code&gt;OCx=0&lt;/code&gt;), and the cycle-rate of one channel is changed to a new
random value, giving an abrupt change of colour, followed by a new and
different progression of colour changes.  Small children love this and
will sit and frob the button for ages, before they break it or lose
it.  Then you can make them something else.&lt;/p&gt;

&lt;p&gt;A second button acts as an "on/off switch" (actually triggering a low-power sleep
mode, and waking from same).   &lt;/p&gt;

&lt;p&gt;Both buttons trigger interrupts via the pin-change interrupt facility.
The buttons are "debounced" in software, by masking further interrupts
for a certain interval after any button interrupt.&lt;/p&gt;

&lt;h2&gt;Hardware&lt;/h2&gt;

&lt;p&gt;I prototyped this on a solderless breadboard, and programmed the
ATTiny with an &lt;a href="http://www.tuxgraphics.org/electronics/200705/article07052.shtml"&gt;AVRUSB500v2
programmer&lt;/a&gt;,
which has the useful advantage of using a 1x5 connector instead of the
more common 2x3 or 2x5, allowing simple in-circuit programming of
breadboarded AVRs.&lt;/p&gt;

&lt;p&gt;Once I had the software working, I built a soldered prototype on a
scrap of pad-per-hole proto-board.  With only 5 data pins, and 2
power, you can just plonk the components down next to the chip socket,
and either use point-to-point wiring and/or chains of bridged pads.
If you're bridging pads, use the cheap phenolic protoboard, the
high-end green fiberglass boards with solder masking are commendably
resistant to solder bridging.&lt;/p&gt;

&lt;p&gt;You can use any simple voltage regulator (or none).  The ATTiny chips
come in a 5v version that runs to 20MHz, or a low voltage version that
will work off as low as 1.8v (but is limited to 4 or 10MHz) .  I used an
LP2950 low-dropout regulator, which will easily give you 5v from 4
alkaline cells, or 3.3v from 3 cells.  If you're using a 9v battery,
the cheap and common 78L05 (which needs an input of at least 7 volts)
works fine too.  You could even leave the regulator off entirely, if
you use a 4.5v alkaline pack or a 3.7v lithium.  The low-votage
ATTiny13v is remarkably forgiving (but remember to set the brown-out
fuses to an appropriate low-voltage cutoff).&lt;/p&gt;

&lt;p&gt;I haven't bothered to design a PCB yet, as it wouldn't be
significantly more compact than the protoboard version.  I suppose an
SMD version would be interesting---it could be made not much larger
than the coin cells that power it, giving some interesting
swallowing-hazard fun to be had.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://farm3.static.flickr.com/2646/4088509867_1cee78e018.jpg" alt="Protoboard Light Up Thingy" title="" /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6144971837550808157-7728269975673228637?l=blog.unixbigot.id.au' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://github.com/unixbigot/bogo-pwm' title='Drive an RGB led with software PWM'/><link rel='replies' type='application/atom+xml' href='http://blog.unixbigot.id.au/feeds/7728269975673228637/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6144971837550808157&amp;postID=7728269975673228637' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default/7728269975673228637'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default/7728269975673228637'/><link rel='alternate' type='text/html' href='http://blog.unixbigot.id.au/2009/11/driving-rgb-led-from-microcontroller.html' title='Drive an RGB led with software PWM'/><author><name>unixbigot</name><uri>http://www.blogger.com/profile/02358053867280926657</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_N-TT1bhNS4U/TAGZtA4E8WI/AAAAAAAAAcQ/cktvaoz9anU/S220/profile+picture.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://farm3.static.flickr.com/2646/4088509867_1cee78e018_t.jpg' height='72' width='72'/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6144971837550808157.post-5958880290743454133</id><published>2009-11-06T21:15:00.002+10:00</published><updated>2009-11-07T13:14:01.960+10:00</updated><title type='text'>A simple presence-sensing LED lighting controller</title><content type='html'>&lt;h1&gt;Low power LED night-light&lt;/h1&gt;

&lt;img src="http://farm4.static.flickr.com/3569/4082138528_285ca8798e.jpg"&gt;

&lt;p&gt;Every time I see household lighting left on overnight for "security",
I think of the coal-smoke belching into the atmosphere to power
it. This kind of silliness has got to stop, besides the environmental
cost it will soon become prohibitively expensive if carbon-taxing ever
gets off the ground. My kids, however, (aged variously 1-7) have a
habit of getting up several times in the night to bump their way to
the bathroom, and they always leave every light switch they encounter
on the way ON (not actually being awake enough to deal with turning
them off).&lt;/p&gt;

&lt;p&gt;We've been leaving a (low wattage) light on to give enough light to
get around. Now, you can go out and buy motion-sensitive night-light
devices for basically peanuts (eg, dealextreme.com), but the motion
sensors are often fake (just a visible light sensor, which does not
work in darknesS).  Besides, I never did like to do anything the easy
way when I could make a production of it, so I designed my own. It
makes a neat trainer project for the Atmel ATtiny
microcontrollers. The primary design elements are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pre-built Passive-Infrared motion detector module&lt;/li&gt;
&lt;li&gt;Atmel ATTiny45 Microcontroller.  Also works on Arduino &amp;amp; variants.&lt;/li&gt;
&lt;li&gt;0.5 watt high-brightness LED (or an SMD LED-array)&lt;/li&gt;
&lt;li&gt;Ability to override sensor with an on/off switch&lt;/li&gt;
&lt;li&gt;Adjustable brightness via holding down on/off switch&lt;/li&gt;
&lt;li&gt;Timed fade-out to allow enough time to leave the room&lt;/li&gt;
&lt;li&gt;Also suitable (without PIR sensor) for fishtank lighting&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;LED Lighting&lt;/h2&gt;

&lt;p&gt;I orginally chose a 10mm 0.5W LED (from &lt;a
href="http://secure.oatleyelectronics.com//product_info.php?cPath=73&amp;amp;products_id=696&amp;amp;osCsid=00d3c7b2bdcd169658b44d27a120d9f2"&gt;Oatley
Electronics&lt;/a&gt;), because it is bright enough to light a bathroom or
hallway at night, yet does not require any heatsink.  A resistor is
used to limit the maximum current to around 150mA at 5v.&lt;/p&gt;

&lt;p&gt;Since I first created this project LED arrays have become stupidly
cheap, typically sold as automotive interior and underbody lighting.
Examples are &lt;a
href="http://dealextreme.com/details.dx/sku.11303"&gt;here&lt;/a&gt; and &lt;a
href="http://www.dealextreme.com/details.dx/sku.3874"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Arrays of 10-100 SMD leds, either in grids or strings are available
pre-assembled with integrated current-limiting resistors suitable for
12v power.  Typically 4 or 5 LEDs are chained with 1 resistor, and 1-20
chains are connected in parallel.  The entire array consumes only tens
or hundreds of milliamps at 12v, and produces a quite impressive
amount of light.  The version of the project presented here is capable
of driving LEDs from either 5v (via regulator) or from the external
voltage input (eg 12v).&lt;/p&gt;

&lt;h2&gt;PIR module&lt;/h2&gt;

&lt;p&gt;Pre-assembled Passive Infra-red sensor modules are available that
combine a PIR sensor, control circuirty and lens.   You simply supply
power and monitor a TTL output line.   One inexpensive source of these modules is
&lt;a href="http://futurlec.com/PIR_Sensors.shtml"&gt;futurlec.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Circuit and software details&lt;/h2&gt;

&lt;img src="http://farm3.static.flickr.com/2560/4079553269_063e38f1e5_o.png"&gt;

Source code and hardware files are available at &lt;a href="http://github.com/unixbigot/pir-lite"&gt;GitHub&lt;/a&gt;.

&lt;h3&gt;Voltage regulator&lt;/h3&gt;

&lt;p&gt;You can power the circuit from battery, from a regulated power source
(say an old phone charger), or use a regulator such as a 7805 or
MCP1700 to get 5v from an unregulated supply.  If you are powering
your LED from the regulator (rather from the main supply), choose a
regulator that can supply enough current.&lt;/p&gt;

&lt;h3&gt;Microcontroller&lt;/h3&gt;

&lt;p&gt;An 8-pin ATTiny45 is used here, althoug it is hardly worked hard.  The
code can be made to run on an Arduino (or other ATmega device) easily
enough.  Only about 400 bytes of flash is used, and there are several
pins spare.  There's enough capacity left over to implement a
communications interface, so your household LED lighting could be
remote-controlled.  I plan to work on this enhancement soon.&lt;/p&gt;

&lt;h3&gt;LED control&lt;/h3&gt;

&lt;p&gt;A transistor is used to control power to the LED.  A general purpose
medium power TO92 switching transistor capable of handling around
500ma (eg 2N2222, or 2n3907) is suitable.&lt;/p&gt;

&lt;p&gt;The pads for the transistor are arranged to allow use of a TO92 or
TO220 package.&lt;/p&gt;

&lt;p&gt;A jumper selects whether the LED and transistor are powered from
regulated 5v or external (12v) power.&lt;/p&gt;

&lt;p&gt;A current limiting resitor may be fitted to the board, or replaced
by a link when using a LED array with integral resistors.&lt;/p&gt;

&lt;p&gt;The transistor itself may be used to limit LED current if operated in
analog mode.  I'm a digital guy, so I didn't try that.  A good guide
to selecting a base-current resistor value is at &lt;a
href="http://www.kpsec.freeuk.com/trancirc.htm#npn"&gt;The Electronics
Club&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The microntroller drives the transistor with a Pulse-Width Modulated
(PWM) signal, allowing the LED (or LED array) brightness may be varied
over 256 steps from 0 to 255.  This allows the main LED to be left in
a low-brightness state as a night light, and also allows the light to
be faded in and out gradually, which is kinder on sleep-deprived eyes.&lt;/p&gt;

&lt;h3&gt;PIR sensor interface&lt;/h3&gt;

&lt;p&gt;Aa 3 pin header provides power and ground to the PIR module, and reads
the sensor's the TTL output signal.  The PIR signal is connected to an
input pin and monitored via a pin change interrupt.&lt;/p&gt;

&lt;p&gt;The PIR module itself inhibits re-triggering for an interval after
each trigger which can be adjusted via a trimmer pot.&lt;/p&gt;

&lt;p&gt;If the LED array is off, and the PIR triggers, the LED array is
faded on quickly.  A 5 minute timer is started after which the LED array is
faded off again.   If a PIR trigger occurs while the array is lit,
the 5 minute timer is reset.   The effect of this is for the light
to remain on while you are moving about, and to slowly fade off off five
minutes after you leave the room.    The on/off switch can
override the timer for fast-off or remain-on.&lt;/p&gt;

&lt;h3&gt;Push-button Switch&lt;/h3&gt;

&lt;p&gt;Internal pullup is used to hold the button input pin high
unless the push-button connects the pin directly to ground.
De-bouncing is performed in software.&lt;/p&gt;

&lt;p&gt;If the LED is off, the push button causes it to fade on quickly
(over about 1 second).  A release or second push while fading on
freezes the light at that brightness.&lt;/p&gt;

&lt;p&gt;If the LED is on, the push button causes it to begin fading off
(over about 4 sec).  A second push during fade-off freezes the
light at that brighness (a dimmer).  A third push resumes the
fade-to-black (off).&lt;/p&gt;

&lt;h3&gt;Diagnostic LED and spare pins&lt;/h3&gt;

&lt;p&gt;There are at least 2 pins spare (more if you disable the RESET pin, or
use fewer inputs).  One of them has a normal LED connected to act as a
power-on status light, handy for finding the on-switch in the dark if
you do not employ a PIR sensor.&lt;/p&gt;

&lt;p&gt;Headers are included for other spare pins for future use.  &lt;/p&gt;

&lt;p&gt;Some possible expansion ideas&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a photosensor to turn LEDs off if room lighting or daylight is
present&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a communication interface (I2C, DOW or RS422) to allow remote
control of lighting modules by home-automation systems, and use of
the PIR sensors in as an alarm system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a Sonar or IR object sensor as a no-touch switch &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a potentiometer connected to an analog input pin for setting minimum
brightness level for 'night light' mode.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Construction&lt;/h2&gt;

&lt;p&gt;The circuit was prototyped and tested on a solderless breadboard,
where the microcontroller was programmed in-circuit.&lt;/p&gt;

&lt;p&gt;A version was then constructed on proto-board, of the type whose
tracks are laid out like a solderless breadboard.  This is a very
handy way of migrating a breadboarded circuit to a soldered
equivalent.&lt;/p&gt;

&lt;p&gt;Once the design was proven to be workable, a PCB version was etched.
A few shortcomings were made evident by the first PCB version - some
of the tracks could have been made wider to give resistance to lifting
due to component torquing.   It would have been handy to run 5v to all
the input jumpers, so that active sensors could be connected to any of
the spare pins.&lt;/p&gt;

&lt;p&gt;More pictures of construction and deployment in my &lt;a href="http://www.flickr.com/photos/22634189@N06/sets/72157622626263125/"&gt;pir-lite photoset&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6144971837550808157-5958880290743454133?l=blog.unixbigot.id.au' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://github.com/unixbigot/pir-lite' title='A simple presence-sensing LED lighting controller'/><link rel='replies' type='application/atom+xml' href='http://blog.unixbigot.id.au/feeds/5958880290743454133/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6144971837550808157&amp;postID=5958880290743454133' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default/5958880290743454133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6144971837550808157/posts/default/5958880290743454133'/><link rel='alternate' type='text/html' href='http://blog.unixbigot.id.au/2009/11/simple-presence-sensing-led-lighting.html' title='A simple presence-sensing LED lighting controller'/><author><name>unixbigot</name><uri>http://www.blogger.com/profile/02358053867280926657</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/_N-TT1bhNS4U/TAGZtA4E8WI/AAAAAAAAAcQ/cktvaoz9anU/S220/profile+picture.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://farm4.static.flickr.com/3569/4082138528_285ca8798e_t.jpg' height='72' width='72'/><thr:total>5</thr:total></entry></feed>
