<?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>Brett&#039;s Build Log &#187; boarduino</title>
	<atom:link href="http://brettinman.com/tag/boarduino/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettinman.com</link>
	<description>Hardware tinkering with the occasional bit of software</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:24:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Driving Speakers with Arduino</title>
		<link>http://brettinman.com/2009/05/30/driving-speakers-with-arduino/</link>
		<comments>http://brettinman.com/2009/05/30/driving-speakers-with-arduino/#comments</comments>
		<pubDate>Sat, 30 May 2009 09:30:36 +0000</pubDate>
		<dc:creator>brettinman</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[boarduino]]></category>
		<category><![CDATA[buzzer]]></category>

		<guid isPermaLink="false">http://brettinman.com/?p=56</guid>
		<description><![CDATA[In my quest to try out everything the Arduino can possibly do, I decided to hook up a buzzer and try messing around with generating some tones. Since my little 8ohm buzzer seemed to have gone missing, I substituted one of Sparkfun&#8217;s audio jack breakout boards and a cheap pair of earbuds I had lying [...]]]></description>
			<content:encoded><![CDATA[<p>In my quest to try out everything the Arduino can possibly do, I decided to hook up a buzzer and try messing around with generating some tones. Since my little 8ohm buzzer seemed to have gone missing, I substituted one of Sparkfun&#8217;s audio jack <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8827">breakout boards</a> and a cheap pair of earbuds I had lying around. DO NOT use a quality set of earbuds or headphones, or any set that you wouldn&#8217;t want damaged &#8211; we&#8217;ll see why in a minute. Just connect ground to ground and either the left or right channel (or both) to one of the digital pins &#8211; I used 13 so the LED would light up with the rhythm of the music. I eventually found a couple more buzzers and hooked them in.</p>
<p><div id="attachment_60" class="wp-caption aligncenter" style="width: 610px"><img src="http://brettinman.com/blog/wp-content/uploads/2009/05/buzzer.jpg" alt="Arduinos with Buzzers" title="buzzer" width="600" height="450" class="size-full wp-image-60" /><p class="wp-caption-text">Arduinos with Buzzers</p></div><br />
My new Boarduino USB from <a href="http://www.adafruit.com/">Adafruit</a> is on the left in a breadboard and the Duemilanove is on the right.</p>
<p>The most basic way to create audio using the Arduino is essentially bit-banging one of the digital pins &#8211; write high and low at the frequency you wish to generate. The downside is that this creates a nearly square wave, which sound very harsh and unnatural. Also, the &#8220;high&#8221; is 5V, which is a very high amplitude to be supplying to headphones. Constant use at this amplitude (volume) will probably damage the drivers in earbuds &#8211; use the cheap earbuds that came with your MP3 player. Using a standard 8ohm buzzer is of course the preferred method if you have one lying around. I found a buzz function online to control this &#8211; pass it the pin to buzz on, the frequency, and the length, and it does the work for you. </p>
<p><code>void buzz(int targetPin, long frequency, long length) {<br />
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions<br />
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since<br />
  //// there are two phases to each cycle<br />
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing<br />
  //// multiply frequency, which is really cycles per second, by the number of seconds to<br />
  //// get the total number of cycles to produce<br />
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...<br />
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram<br />
    delayMicroseconds(delayValue); // wait for the calculated delay value<br />
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram<br />
    delayMicroseconds(delayValue); // wait againf or the calculated delay value<br />
  }<br />
}<br />
</code></p>
<p>The downside is that this uses the delay function. That means your controller can't do anything else while waiting for the end of the delay. You would also have to use a delay function between the notes to create spaces. This is sufficient for a small buzzer project, but if you're looking to do more complex things (especially signals other than square wave),  try consulting the Arduino <a href="http://www.arduino.cc/playground/Main/InterfacingWithHardware#Audio">Playground</a>.</p>
<p>I was able to find a script for the Mario Bros theme online, and covert it into a series of buzz and delay calls. Not all the notes are correct and some of the spacing is off, but it's good enough to get the idea. The entire Arduino script is available <a href="http://www.brettinman.com/elec/buzzer.pde">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettinman.com/2009/05/30/driving-speakers-with-arduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
