<?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>Aitch&#039;s Place ... &#187; microcontroller</title>
	<atom:link href="http://howardsandford.com/blog/tag/microcontroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://howardsandford.com/blog</link>
	<description>... somewhere on the interwebs for &#039;H&#039; to have his say</description>
	<lastBuildDate>Mon, 21 Jun 2010 22:26:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Electronic Robot Car &#8211; L293D &#8211; 5v Motors &#8211; ATTiny &#8211; AVR</title>
		<link>http://howardsandford.com/blog/electronic-robot-car-l293d-5v-motors-attiny-avr/</link>
		<comments>http://howardsandford.com/blog/electronic-robot-car-l293d-5v-motors-attiny-avr/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 16:59:55 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[ATTiny]]></category>
		<category><![CDATA[AVR]]></category>
		<category><![CDATA[car]]></category>
		<category><![CDATA[L293D]]></category>
		<category><![CDATA[microcontroller]]></category>
		<category><![CDATA[robot]]></category>
		<category><![CDATA[vehicle]]></category>

		<guid isPermaLink="false">http://howardsandford.com/blog/?p=51</guid>
		<description><![CDATA[The other day, well actually quite a lot of days ago, Thomas, one of my twin boys, asked about making an electronic car.  It seems that they might be making an electronic car at school soon, and he wanted to get a head start &#8230; awww, bless him, he&#8217;s only 8  
So I thought [...]]]></description>
			<content:encoded><![CDATA[<p>The other day, well actually quite a lot of days ago, Thomas, one of my twin boys, asked about making an electronic car.  It seems that they might be making an electronic car at school soon, and he wanted to get a head start &#8230; awww, bless him, he&#8217;s only 8 <img src='http://howardsandford.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>So I thought I&#8217;d better get prepared.</p>
<p><div style="position: relative;float: left; margin-right: 10px;"><script type="text/javascript"><!--
google_ad_client = "pub-9779393397850606";
/* howardsandford-big-image-300x250, created 21/04/10 */
google_ad_slot = "8024658496";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div></p>
<p>Now, we didn&#8217;t really have much in the electronics box that we could use, apart from an L293D.  This is a DC motor driver, a simple IC that can control DC motors via an H-Bridge circuit.  Now, if we&#8217;re going to be connecting up some 5v hobby motors to a microcontroller chip such as an ATTiny45, we need to protect the chip from i) drawing too much current, and ii) problems associated with switching inductive loads such as motors.</p>
<p>An H-Bridge is simply a set of four switches with protective flyback diodes, and the L293D provides a very convenient way to control two motors via a microcontroller chip.  Yay!</p>
<p><img class="alignright size-medium wp-image-52" title="Salvaged micro-switches" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-21-07.52.05-300x225.jpg" alt="Salvaged micro-switches" width="300" height="225" />However, it turns out I didn&#8217;t have much else &#8230;</p>
<p>&#8230; apart from a USB missile launcher which hadn&#8217;t worked in years &#8230; hmmm.  Taking the USB missile launcher apart gave us some wonderful pieces to play with.  Not only did it have two low voltage DC motors, but some very useful microswitches which will come in handy later (I&#8217;m thinking of bump/collision detection here!)</p>
<p>So, two motors, three microswitches, ATTiny45, L293D &#8230; what else do we need?</p>
<p>WHEELS!</p>
<p><img class="alignleft size-medium wp-image-58" title="Wheel glued to the DC motor" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-21-07.51.37-300x225.jpg" alt="Wheel glued to the DC motor" width="300" height="225" />Of course.  Good job Thomas had a Konnects building set which just so happened to have some wheels included, and he was happy to donate them to the project.  It was an easy case of gluing the wheels to the DC motors.  Also, I have some standard stripboard which we can use for the chassis and to hold all the electronics.  This is looking good!</p>
<p>Now we have all our bits and pieces, we just need to get started on building the circuit.  The code itself is going to be simply a case of making control pins go HIGH and LOW, though for reading the microswitches I&#8217;m going to use another simple technique rather than constantly polling the input pins.</p>
<p>Effectively I&#8217;m going to create a resistor ladder with all the microswitches connected into the resitor ladder circuit to act as a voltage divider or potential divider, and then measure the voltage at the input pin relative to the source voltage.  By running an ADC conversion at the input pin, we can determine which microswitch has been pressed.  More on this in a later post.</p>
<p>So, we have all the pieces ready.  All we have to do now is build the circuit, program the ATTiny, and let Thomas take it in to his &#8220;show and tell&#8221; session at school &#8230;</p>
<p>&#8230; but first, perhaps, I should wait for Thomas to help me build the electronic robot car, eh? <img src='http://howardsandford.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   That will be the basis of the next post in this series &#8230;</p>
<p><img class="alignleft size-medium wp-image-69" title="Stripboard, ATTiny45, L293D, 5v DC motors, microswitches, wheels ... robot car is ready to build!" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-21-07.53.54-300x225.jpg" alt="Stripboard, ATTiny45, L293D, 5v DC motors, microswitches, wheels ... robot car is ready to build!" width="300" height="225" /></p>
]]></content:encoded>
			<wfw:commentRss>http://howardsandford.com/blog/electronic-robot-car-l293d-5v-motors-attiny-avr/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flashing LEDs &#8211; Audio Meter &#8211; AVR ATTiny &#8211; Guitar Pickup Wiring &#8211; Sound Following Circuit</title>
		<link>http://howardsandford.com/blog/flashing-leds-audio-meter-avr-attiny-guitar-pickup-wiring-sound-following-circuit/</link>
		<comments>http://howardsandford.com/blog/flashing-leds-audio-meter-avr-attiny-guitar-pickup-wiring-sound-following-circuit/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 23:11:38 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[ATTiny]]></category>
		<category><![CDATA[AVR]]></category>
		<category><![CDATA[guitar]]></category>
		<category><![CDATA[guitar customisation]]></category>
		<category><![CDATA[guitar modification]]></category>
		<category><![CDATA[LED]]></category>
		<category><![CDATA[microcontroller]]></category>

		<guid isPermaLink="false">http://howardsandford.com/blog/?p=23</guid>
		<description><![CDATA[You just gotta love the title, eh?  
Recently I&#8217;ve been working on modding my guitar, in particular add some flashing LEDs to the guitar innards.  I&#8217;ve got another project on the go too, but I&#8217;ll save that for a later post when I&#8217;ve finished it.





Anyway, the effect I wanted was to have the LEDs [...]]]></description>
			<content:encoded><![CDATA[<p>You just gotta love the title, eh? <img src='http://howardsandford.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Recently I&#8217;ve been working on modding my guitar, in particular add some flashing LEDs to the guitar innards.  I&#8217;ve got another project on the go too, but I&#8217;ll save that for a later post when I&#8217;ve finished it.</p>
<p><div style="position: relative;float: left; margin-right: 10px;"><script type="text/javascript"><!--
google_ad_client = "pub-9779393397850606";
/* howardsandford-big-image-300x250, created 21/04/10 */
google_ad_slot = "8024658496";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div></p>
<p>Anyway, the effect I wanted was to have the LEDs flash in time with the music I play, or rather in time to my strumming, and then I thought it would be good to follow the loudness peaks from my guitar pickups.  In a sense I&#8217;ve built an audio meter that&#8217;s using the guitar output as an input and the LED array brightness as an audio meter indication.</p>
<p>I used an AVR ATTiny85 as part of the circuit, though you don&#8217;t need anything as big as this, it&#8217;s just that I had a few in my spare parts box; you can easily fit the program into an ATTiny25.  All you need in the microchip is essentially an ADC (Analog to Digital Counter/Convertor).<img class="alignright size-full wp-image-13" title="The Stripped-down Telecaster!" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-11-29-15.25.04_320.0_-3.0_40.0.jpg" alt="The Stripped-down Telecaster!" width="256" height="192" /></p>
<p>I&#8217;ve also used a simple l0w voltage audio power amplifier, an LM386N-1, to boost the signal from the pickups to the ADC on the ATTiny.</p>
<p>Essentially what happens is, the signal from the guitar pickups gets amplified by the LM386 by 20, feeds it to the ATTiny which counts up to convert the analog signal into a digital one.  This signal is tracked over time to find out a moving average.  If the signal rises 30% above the average it&#8217;s considered a peak so the ATTiny goes through a flash cycle and turns on the LED circuit.  Now, because the LED circuit has multiple LEDs on it, it would draw too much current through the ATTiny, and so instead the pin on the ATTiny drives a simple NPN transistor to turn on the current flowing through 8 LEDs.</p>
<p>So, how do you go about building one of these for your own guitar?</p>
<p>Well, I&#8217;ll assume you already know about how to program an AVR ATTiny microprocessor.  If not, you can check out the excellent startup tutorial at <a title="IMakeProject.com" href="http://imakeprojects.com/Projects/avr-tutorial/" target="_blank">IMakeProjects.com</a> web-site.</p>
<p><a href="http://howardsandford.com/blog/wp-content/uploads/2009/12/tCad1.png"><img class="alignleft size-medium wp-image-29" title="Guitar Pickup Flashing LED Circuit" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/tCad1-300x197.png" alt="Guitar Pickup Flashing LED Circuit" width="300" height="197" /></a>The circuit and code itself was based on a part of the circuit and code I found at <a href="http://negativeacknowledge.com/2008/06/final-lightbar-controller/" target="_blank">Negative Knowledge</a>.  One point to notice is that the LEDs are connected in parallel, usually a no-no, but in this case all the ultra blue LEDs are of the same specification.  Usually what happens is one of the LEDs would draw more than its maximum current, be the only one turned on, and blow, repeated over and over until you have none left.  With the same specification LEDs it&#8217;s more likely all will be well.</p>
<p>Also, one thing that caused no end of trouble, was making sure all the -ve/GND connections were made.  Both the ATTiny&#8217;s and LM386&#8217;s GNDs were connected together, as well as linking the guitar signal&#8217;s -ve/GND connection at pin 2 of the LM386.  If you don&#8217;t do that, you&#8217;ll have loads of fun with awful hum when you play guitar, or worse, a lovely high pitch squeal &#8230; or maybe nothing at all <img src='http://howardsandford.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p><img class="alignright size-full wp-image-18" title="Stripboard Circuit" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-08-00.31.27_187.0_-24.0_17.0.jpg" alt="Stripboard Circuit" width="256" height="192" />The circuit is easy enough to put together on a piece of stripboard.  You just have to make sure that you make it all small enough to fit inside your guitar casing!</p>
<p>In fact, one of the biggest problems I had was fitting everything inside the cramped space inside my Custom Telecaster.  So much so, I had to connect the battery via a plug socket on the outside of the guitar, just near to the audio jack output.  It seemed like a shame to drill a hole in the guitar, but I&#8217;d gone so far that nothing was going to stop this LED flasher from working!</p>
<p>Once the circuit is in place, I made sure I had a simple startup routine in the code to flash the LEDs twice.  I made sure I did this so that I had a visual indication that the circuit was up and running.  Without it I wouldn&#8217;t have known if I&#8217;d trapped the 5V cable when screwing the scratchplate back on (yes, I did this), or if the battery was dead (yes, I did this too), or any other problem during installation.</p>
<p><img class="alignleft size-full wp-image-21" title="Battery power socket on the outside of the guitar body" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-08-01.05.31_99.0_-11.0_-62.0.jpg" alt="Battery power socket on the outside of the guitar body" width="256" height="192" />Also, when putting the scratchplate back on the guitar, you really, really, really need to watch out how those wires underneath are sitting.  Make sure they all sit in there nice and snug &#8230;</p>
<p>&#8230; after all, you don&#8217;t want to break a ground connection halfway through your first gig with the guitar, do you? (like what <em>I</em> did &#8230; ho hum!)</p>
<p>LEDs, even ultra bright ones, have a small viewing angle, so keep this in mind when placing them.  I simply put the LEDs on a small piece of stripboard, stuck them to the bottom of the casing inside the pickup cutout, with the LEDs themselves bent into slightly different angles so that the audience would see a little of the LEDs no matter what their viewing angle was.  Also, to help add some more light difusion, I added some tissue paper to difuse the bright lights to a strip rather than individual bright spots.</p>
<p><img class="alignleft size-full wp-image-35" title="The LEDs laid bare!" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-08-00.32.19_317.0_-13.0_-15.0.jpg" alt="The LEDs laid bare!" width="256" height="192" /></p>
<p><img class="size-full wp-image-20" title="Diffused LEDs" src="http://howardsandford.com/blog/wp-content/uploads/2009/12/2009-12-08-00.32.00_322.0_-4.0_-14.0.jpg" alt="Diffused LEDs" width="256" height="192" /></p>
<p>The code is simple enough:</p>
<blockquote>
<pre><span style="font-style: italic; color: #82a282;">// Based on Lightstrip Controller</span>
<span style="font-style: italic; color: #82a282;">// by Adam Greig, 2008-06-04</span>
<span style="font-style: italic; color: #82a282;">// CC BY-SA-NC 3.0</span>

#define F_CPU 8000000UL  <span style="font-style: italic; color: #82a282;">// 8MHz</span>
#include &lt;util/delay.h&gt;
#include &lt;avr/interrupt.h&gt;
#include &lt;avr/io.h&gt;

<span style="font-style: italic; color: #82a282;">// The LED circuit is connected to PB4 on the ATTiny85</span>
#define LEDS 4

<span style="font-style: italic; color: #82a282;">// Some helper MACROS</span>
#define SET |=
#define CLR &amp;=~
#define TOG ^=

<span style="font-style: italic; color: #82a282;">//Converge - how fast the average will converge to the current value. Default: 10</span>
#define CONVERGE 10

<span style="font-style: italic; color: #82a282;">//Peak - what multiple of the average will be considered a peak. Default: 1.3</span>
#define PEAK 1.3

<span style="font-style: italic; color: #82a282;">//Store duty cycle, background average, current result, difference between result and average.</span>
volatile unsigned char duty;
volatile unsigned char avg;
volatile unsigned char result;
volatile signed char diff;

int main(void)
{
  <span style="font-style: italic; color: #82a282;">// set LEDS pin to output</span>
  DDRB SET (1 &lt;&lt; LEDS);
  PORTB = 0x00;

  <span style="font-style: italic; color: #82a282;">// flash the LEDs twice to signify we're on</span>
  PORTB SET (1 &lt;&lt; LEDS);
  _delay_ms(1000);
  PORTB CLR (1 &lt;&lt; LEDS);
  _delay_ms(1000);
  PORTB SET (1 &lt;&lt; LEDS);
  _delay_ms(1000);
  PORTB CLR (1 &lt;&lt; LEDS);

  <span style="font-style: italic; color: #82a282;">//Initialise registers. Note that the compiler optimises each to an integer, so the 0s just make life easier.</span>
  ADMUX = (0&lt;&lt;REFS0) | (1&lt;&lt;ADLAR) | (1&lt;&lt;MUX1) | (1&lt;&lt;MUX0);
  ADCSRA = (1&lt;&lt;ADEN) | (0&lt;&lt;ADSC) | (0&lt;&lt;ADATE) | (0&lt;&lt;ADIF) | (0&lt;&lt;ADIE) | (1&lt;&lt;ADPS2) | (1&lt;&lt;ADPS1) | (0&lt;&lt;ADPS0);
  DIDR0 = (0&lt;&lt;ADC0D) | (0&lt;&lt;ADC1D) | (0&lt;&lt;ADC2D ) | (1&lt;&lt;ADC3D);

  <span style="font-style: italic; color: #82a282;">//Initialise ADC, ignore first result</span>
  ADCSRA SET (1&lt;&lt;ADSC);
  while( ADCSRA &amp; (1&lt;&lt;ADSC) ) {}

  <span style="font-style: italic; color: #82a282;">// reset the ADC</span>
  ADCSRA SET (1&lt;&lt;ADIE);
  ADCSRA SET (1&lt;&lt;ADSC);
  sei();

  <span style="font-style: italic; color: #82a282;">// infinite loop -- all the work is done in the ADC interrupt routine</span>
  while (1) {}

  return 1;
}

<span style="font-style: italic; color: #82a282;">// interrupt routine for when the ADC gets a result</span>
ISR(ADC_vect)
{
  result = ADCH;

  <span style="font-style: italic; color: #82a282;">//Find the difference and converge the average</span>
  diff = result - avg;
  avg += diff / CONVERGE;

  <span style="font-style: italic; color: #82a282;">// Determine if it's a peak,</span>
  <span style="font-style: italic; color: #82a282;">// and if so turn the LEDs</span>
  if( result &gt; avg * PEAK )
  {
    duty = (100*result)/avg;
  }
  else
  {
    duty = 0;
  }

  <span style="font-style: italic; color: #82a282;">//Execute the duty cycle</span>
  if (duty != 0 )
  {
    PORTB SET (1 &lt;&lt; LEDS);
  }

  for( int j=0; j&lt;duty; j++ )
  {
    asm( "nop\n\t" "nop\n\t" "nop\n\t" :: );
  }

  PORTB CLR (1 &lt;&lt; LEDS);
  for( int j=100; j&gt;duty; j-- )
  {
    asm( "nop\n\t" "nop\n\t" "nop\n\t" :: );
  }

  <span style="font-style: italic; color: #82a282;">// and set it off again</span>
  ADCSRA SET (1&lt;&lt;ADSC);
}</pre>
</blockquote>
<p>So, there you have it.  A flashing LED audio meter for your guitar pickups.  If you have any questions, just ask!</p>
<p>[All files are released under <a title="Creative Commons BY-SA-NC 3.0" href="http://creativecommons.org/licenses/by-nc-sa/3.0/" target="_blank">Creative Commons BY-SA-NC 3.0</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://howardsandford.com/blog/flashing-leds-audio-meter-avr-attiny-guitar-pickup-wiring-sound-following-circuit/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
