<?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>Psychic Origami &#187; Arduino</title>
	<atom:link href="http://www.psychicorigami.com/category/arduino/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.psychicorigami.com</link>
	<description>folding with my brain</description>
	<lastBuildDate>Wed, 03 Aug 2011 19:13:30 +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>&#8220;Ultimate&#8221; Arduino Doorbell &#8211; part 2 (Software)</title>
		<link>http://www.psychicorigami.com/2010/10/16/ultimate-arduino-doorbell-part-2-software/</link>
		<comments>http://www.psychicorigami.com/2010/10/16/ultimate-arduino-doorbell-part-2-software/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 18:45:00 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Application]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[chumby]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.psychicorigami.com/?p=380</guid>
		<description><![CDATA[As mentioned in the previous post about my arduino doorbell I wanted to get the doorbell and my Chumby talking. As the Chumby runs Linux, is on the network and is able to run a recent version of Python (2.6) it seemed like it would be pretty easy to get it to send Growl notifications [...]]]></description>
			<content:encoded><![CDATA[<p>As mentioned in the <a href="http://www.psychicorigami.com/2010/09/06/ultimate-arduino-doorbell-part-1-hardware/">previous post about my arduino doorbell</a> I wanted to get the doorbell and my <a href="http://www.chumby.com/">Chumby</a> talking.</p>
<p>As the Chumby runs Linux, is on the network and is able to run a recent version of Python (2.6) it seemed like it would be pretty easy to get it to send <a href="http://growl.info/">Growl</a> notifications to the local network.</p>
<p><center><img src="http://www.psychicorigami.com/wp-content/uploads/2010/10/growl-doorbell.png" alt="" title="growl doorbell notification" width="421" height="117" class="alignnone size-full wp-image-381" /></center></p>
<p>This did indeed prove fairly easy and involved three main activities:</p>
<ul>
<li>Listening to the serial port for the doorbell to ring</li>
<li>Using <a href="http://www.apple.com/support/bonjour/">Bonjour (formally Rendezvous)</a>/<a href="http://www.zeroconf.org/">Zeroconf</a> to find computers on the network</li>
<li>Sending network Growl notifications to those computers</li>
</ul>
<p>Getting <a href="http://www.psychicorigami.com/2010/06/26/chumby-to-arduino-communication-using-pyserial/">Python and PySerial running on the Chumby</a> is pretty easy.  The Chumby simply listens for the doorbell to send the string &#8216;DING DONG&#8217; and can then react as needed.</p>
<pre><code>
def listen_on_serial_port(port, network):
    ser = serial.Serial(port, 9600, timeout=1)
    try:
        while True:
            line = ser.readline()
            if line is not None:
                line = line.strip()
            if line == 'DING DONG':
                network.send_notification()
    finally:
        if ser:
            ser.close()
</code></pre>
<p><code>port</code> is the string representing the serial port (e.g. <code>/dev/ttyUSB0</code>). <code>network</code> is an object that handles the network notifications (see below).</p>
<p>The network object (an instance of <code>Network</code>) has two jobs.  Firstly to find computers on the network (using PicoRendezvous &#8211; now called <a href="http://www.taoofmac.com/projects/picobonjour">PicoBonjour</a>) and secondly to send network growl notifications to those computers.</p>
<p>A background thread periodically calls <code>Network.find</code>, which uses multicast DNS (Bonjour/Zeroconf) to find the IP addresses of computers to notify:</p>
<pre><code>
class Network(object):
    title = 'Ding-Dong'
    description = 'Someone is at the door'
    password = None

    def __init__(self):
        self.growl_ips = []
        self.gntp_ips = []

    def _rendezvous(self, service):
        pr = PicoRendezvous()
        pr.replies = []
        return pr.query(service)

    def find(self):
        self.growl_ips = self._rendezvous('_growl._tcp.local.')
        self.gntp_ips  = self._rendezvous('_gntp._tcp.local.')
        def start(self):
        import threading
        t = threading.Thread(target=self._run)
        t.setDaemon(True)
        t.start()

    def _run(self):
        while True:
            self.find()
            time.sleep(30.0)
</code></pre>
<p><code>_growl._tcp.local.</code> are computers that can handle Growl UDP packets and <code>_gntp._tcp.local.</code> those that can handle the newer <a href="http://www.growlforwindows.com/gfw/help/gntp.aspx">GNTP protocol</a>.  Currently the former will be Mac OS X computers (running Growl) and the latter Windows computers (running <a href="http://www.growlforwindows.com/">Growl for Windows</a>).</p>
<p>I had to <a href="http://github.com/lilspikey/doorbell/commit/33bbfa13cbd81691b5dcac2a48b84aea38a4c0f0">tweak PicoRendezvous slightly</a> to work round a bug on the Chumby version of Python, where <code>socket.gethostname</code> was returning the string <code>'(None)'</code>, but otherwise this worked ok.</p>
<p>When the doorbell activates <a href="http://www.taoofmac.com/projects/netgrowl">netgrowl</a> is used to send Growl UDP packets to the IP addresses in <code>growl_ips</code> and the <a href="http://github.com/kfdm/gntp">Python gntp library</a> to notify those IP addresses in <code>gntp_ips</code> (but not those duplicated in <code>growl_ips</code>).  For some reason my Macbook was appearing in both lists of IP addresses, so I made sure that the <code>growl_ips</code> took precedence.</p>
<pre><code>
    def send_growl_notification(self):
        growl_ips = self.growl_ips

        reg = GrowlRegistrationPacket(password=self.password)
        reg.addNotification()

        notify = GrowlNotificationPacket(title=self.title,
                    description=self.description,
                    sticky=True, password=self.password)
        for ip in growl_ips:
            addr = (ip, GROWL_UDP_PORT)
            s = socket(AF_INET, SOCK_DGRAM)
            s.sendto(reg.payload(), addr)
            s.sendto(notify.payload(), addr)

    def send_gntp_notification(self):
        growl_ips = self.growl_ips
        gntp_ips  = self.gntp_ips

        # don't send to gntp if we can use growl
        gntp_ips = [ip for ip in gntp_ips if (ip not in growl_ips)]

        for ip in gntp_ips:
            growl = GrowlNotifier(
                applicationName = 'Doorbell',
                notifications = ['doorbell'],
                defaultNotifications = ['doorbell'],
                hostname = ip,
                password = self.password,
            )
            result = growl.register()
            if not result:
                continue
            result = growl.notify(
                noteType = 'doorbell',
                title = self.title,
                description = self.description,
                sticky = True,
            )

    def send_notification(self):
        '''
        send notification over the network
        '''
        self.send_growl_notification()
        self.send_gntp_notification()
</code></pre>
<p>The code is pretty simple and will need some more tweaking.  The notification has failed for some reason a couple of times, but does usually seem to work.  I&#8217;ll need to start logging the doorbell activity to see what&#8217;s going on, but I suspect the Bonjour/Zeroconf is sometimes finding no computers.  If so I&#8217;ll want to keep the previous found addresses hanging around for a little longer &#8211; just in case it&#8217;s a temporary glitch.</p>
<p>You can see the full code in my <a href="http://github.com/lilspikey/doorbell">doorbell repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychicorigami.com/2010/10/16/ultimate-arduino-doorbell-part-2-software/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Ultimate&#8221; Arduino Doorbell &#8211; part 1 (Hardware)</title>
		<link>http://www.psychicorigami.com/2010/09/06/ultimate-arduino-doorbell-part-1-hardware/</link>
		<comments>http://www.psychicorigami.com/2010/09/06/ultimate-arduino-doorbell-part-1-hardware/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 17:30:53 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Craft]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.psychicorigami.com/?p=346</guid>
		<description><![CDATA[After a first couple of small Arduino projects I felt the need to make something a bit more useful and permanent. I had seen Roo Reynolds talking about hacking his doorbell to get it onto Twitter and as our doorbell is a bit rubbish I thought this seemed like a good project. His hack only [...]]]></description>
			<content:encoded><![CDATA[<p>After a <a href="http://www.psychicorigami.com/2010/05/26/my-first-arduino-project-morse-code/">first</a> <a href="http://www.psychicorigami.com/2010/06/13/arduino-tone-generatortheremin/">couple</a> of small <a href="http://www.arduino.cc/">Arduino</a> projects I felt the need to make something a bit more useful and permanent.</p>
<p>I had seen Roo Reynolds talking about <a href="http://rooreynolds.com/2008/05/14/hacking-the-doorbell/">hacking his doorbell</a> to get it onto <a href="http://twitter.com/">Twitter</a> and as our doorbell is a bit rubbish I thought this seemed like a good project.  His hack only updated Twitter, so there was no direct physical sign that the doorbell had been rung.  Given that our current doorbell is pretty rubbish anyway (it should ring, but just makes a rattling sound), I decided to start off with the physical/audible side of things and then later move on to pushing notifications out to laptops on the local network or beyond&#8230;</p>
<p>I&#8217;ve been <a href="http://www.flickr.com/photos/lilspikey/sets/72157624576532196/">documenting my progress on Flickr</a> now for a while.</p>
<h3>Take apart wireless doorbell</h3>
<p>Following Roo&#8217;s approach, I picked up a cheap wireless doorbell from Tesco for about £9 and took it apart:</p>
<p><center><object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=5f4e19d9bc&#038;photo_id=4752349287"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=5f4e19d9bc&#038;photo_id=4752349287" height="300" width="400"></embed></object></center></p>
<p>I was able to verify that I could turn an LED on/off with the doorbell at this stage, so I knew I could use that as a signal in the Arduino.  Using a multimeter also let me figure out some more details about the voltage and current used to power the speaker:</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4752984700/" title="Testing wireless doorbell with multi-meter by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4143/4752984700_126d5f59e0.jpg" width="500" height="375" alt="Testing wireless doorbell with multi-meter" /></a></center></p>
<p>This was about 3.1 V and 75mA output.  That&#8217;s a few milliamps more than the Arduino  can tolerate, so I knew I had to make sure I put a resistor (22KOhm) in between the doorbell and the Arduino.  The doorbell would also need to draw power from the Arduino, but luckily the Arduino has a separate 3V output, so that wasn&#8217;t an issue.</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4788279184/" title="Testing connecting Arduino to doorbell by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4121/4788279184_2ab296c772.jpg" width="500" height="375" alt="Testing connecting Arduino to doorbell" /></a></center></p>
<p>From there I verified that when the doorbell was activated I could read a drop in voltage from the wire on the doorbell labelled &#8220;SPI&#8221;.  At this point I had the input for my Arduino.</p>
<h3>Servos and prototypes</h3>
<p>The next step was to provide some sort of audible output.  Originally I had thought about just adding a buzzer, but fancied something a bit more old-fashioned.  I figured it&#8217;d be good fun if the nice 21st century tech of the Arduino used some rather more ancient technology to create sound &#8211; a bell.</p>
<p>Using a <a href="http://www.oomlout.co.uk/servo-micro-p-195.html">micro servo</a> from <a href="http://www.oomlout.co.uk/">Oomlout</a> I created a couple of prototypes.</p>
<p>I made a pretty simple circuit to hook up the servo, doorbell and arduino:</p>
<p><center><a href="http://www.psychicorigami.com/wp-content/uploads/2010/08/doorbell-circuit_bb.png"><img src="http://www.psychicorigami.com/wp-content/uploads/2010/08/doorbell-circuit_bb-300x234.png" alt="" title="doorbell-circuit_bb" class="alignnone size-medium wp-image-355" /></a></center></p>
<p>The first prototype used some lego and a bell from a Christmas ornament:</p>
<p><center><object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=53bdf364c4&#038;photo_id=4791359380"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=53bdf364c4&#038;photo_id=4791359380" height="300" width="400"></embed></object></center></p>
<p>This worked, but the servo was probably louder than the bell!</p>
<p>Next I got hold of a small brass bell from a music shop.  Luckily the top unscrewed and would let me easily attached it to a piece of lego.  So another prototype was created using the small brass bell:</p>
<p><center><object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=bc47ae4dda&#038;photo_id=4816173880"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=bc47ae4dda&#038;photo_id=4816173880" height="300" width="400"></embed></object></center></p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4815509199/" title="Prototype doorbell with brass bell by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4094/4815509199_86bf9619ab.jpg" width="375" height="500" alt="Prototype doorbell with brass bell" /></a></center></p>
<p>This worked much better!</p>
<p>The bell is about 100g in weight, which isn&#8217;t massive, but when it&#8217;s flung around the lego prototype would tend to move too.  Not so good when this will live on the kitchen window sill.  So I&#8217;d need something sturdier for the finished version.</p>
<h3>A little soldering</h3>
<p>As well as trying out a better bell I also made the circuit for the bell and servo more permanent, by soldering everything up on a piece of stripboard.  I also used a few headers, so most of the circuit would plug into the Arduino &#8211; like a rudimentary shield.  The wire to the pin controlling the servo, was left to be plugged in separately.  I hadn&#8217;t soldered anything since school, so I ended up swearing quite a bit trying to do this.  For me it was probably the trickiest part of the whole exercise, but it did work in the end.  Just glad I had a multi-meter with a continuity mode to find the short-circuits &#8211; a knife worked well to clear up some of those.</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4804125867/" title="Stripboard soldering by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4101/4804125867_6332e767a1.jpg" width="500" height="375" alt="Stripboard soldering" /></a></center></p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4809442783/" title="Full soldered doorbell + arduino by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4138/4809442783_47187ef64e.jpg" width="500" height="375" alt="Full soldered doorbell + arduino" /></a></center></p>
<h3>Woodwork</h3>
<p>I next turned to my (t)rusty woodwork skills to create a sturdier version.  First off some pieces of wood to mount the Arduino, doorbell circuit and servo:</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4830629857/" title="Arduino and doorbell mounted on wood block by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4137/4830629857_ee660268cb.jpg" width="375" height="500" alt="Arduino and doorbell mounted on wood block" /></a></center></p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4830623385/" title="Servo mounted in wooden block by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4140/4830623385_1e39e364c1.jpg" width="500" height="375" alt="Servo mounted in wooden block" /></a></center></p>
<p>Next came the base and first part of the bell moving arm:</p>
<p><center><object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=91d599f22c&#038;photo_id=4885839014"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=91d599f22c&#038;photo_id=4885839014" height="300" width="400"></embed></object></center></p>
<p>Then I added the top piece of the arm as well as the picture wire to make the bell swing properly:</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4890557080/" title="Closeup of bell innards by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4079/4890557080_c5bf59a7d0.jpg" width="375" height="500" alt="Closeup of bell innards" /></a></center></p>
<p>At this point everything worked as expected:</p>
<p><center><object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=118f68d3a7&#038;photo_id=4889121194"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=118f68d3a7&#038;photo_id=4889121194" height="300" width="400"></embed></object></center></p>
<h3>Tidying up</h3>
<p>After the sawing and sanding was finished there was still some general tidying up to do.</p>
<p>Once I&#8217;d taped down the servo wire and antena wire with electrical tape I needed to deal with the movement of the whole piece when the arm swings out.  First I got hold of some <a href="http://sugru.com/">Sugru</a> and made some small rubberised feet to prevent the bell sliding around:</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4934949688/" title="Sugru rubber feet on bell by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4143/4934949688_8fbb5ce2f4.jpg" width="375" height="500" alt="Sugru rubber feet on bell" /></a></center></p>
<p>The next task was to add a counter weight so the bell wouldn&#8217;t tip over.  For this I made two piles of fifteen pennies, which I wrapped in electrical tape and taped to the ledge on the reverse side from the Arduino:</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4957542998/" title="Counter weights on back/front of doorbell by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4110/4957542998_bc4d7e1b74.jpg" width="375" height="500" alt="Counter weights on back/front of doorbell" /></a></center></p>
<p>Eventually these will be covered up and will probably actually make up part of the internal structure of the outer decoration.  This should also mean they&#8217;ll end up being attached better.  For now though electrical tape has proved sufficient.</p>
<p>One final tweak involved inserting some small nylon washers either side of the arm:</p>
<p><center><a href="http://www.flickr.com/photos/lilspikey/4957545726/" title="Washer and tidier wire by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4150/4957545726_6ca9f5a433.jpg" width="500" height="375" alt="Washer and tidier wire" /></a></center></p>
<p>This was done to help make the movement of the arm more consistent and slightly smoother.  Previously I was finding that the arm would shift position slightly, which sometimes caused problems for the servo.</p>
<h3>Deployment</h3>
<p>With the hardware finished I then set about actually getting it all up and running as our real doorbell.  So imagine my horror when upon placing the doorbell on our kitchen window sill and connecting the power everything went haywire and the arm just constantly moved!  After some tweaking of the code and kitchen-side debugging I realised that I really should make use of the Arduino&#8217;s <a href="http://www.arduino.cc/en/Tutorial/DigitalPins">internal pullup resistors</a> for the doorbell pin.  Up until that point I had left it &#8220;floating&#8221;.  When powered by the USB this rarely drifted to zero, so I thought it was working fine.  However on mains power it regularly dropped quite low, making the Arduino think the button had been pressed, triggering the servo.  Configured the pullup resistor and tweaking the activation threshold (as it was now higher than zero) sorted out this problem perfectly:</p>
<p><center><object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=643bb81e07&#038;photo_id=4957539834"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=643bb81e07&#038;photo_id=4957539834" height="300" width="400"></embed></object></center></p>
<h3>Conclusion</h3>
<p>So we now have a slightly Heath Robinson doorbell.  It works better that our old one.  There&#8217;s also plenty of scope for making it do more too.  In fact phase two will be mostly about connecting the doorbell to my <a href="http://www.chumby.com/">Chumby</a> to get it communicating with the local network.</p>
<p>Check out my <a href="http://github.com/lilspikey/doorbell">doorbell git repository</a> on github for the code, as well as a <a href="http://fritzing.org/">fritzing</a> circuit file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychicorigami.com/2010/09/06/ultimate-arduino-doorbell-part-1-hardware/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Chumby to Arduino communication using PySerial</title>
		<link>http://www.psychicorigami.com/2010/06/26/chumby-to-arduino-communication-using-pyserial/</link>
		<comments>http://www.psychicorigami.com/2010/06/26/chumby-to-arduino-communication-using-pyserial/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 20:52:03 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[chumby]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.psychicorigami.com/?p=278</guid>
		<description><![CDATA[So here are a few notes on getting a Chumby to talk to an Arduino using PySerial (and Python). It&#8217;s pretty easy, but I&#8217;ll document it to make it obvious. As the Chumby is sometimes a bit slow with a few of these steps, it&#8217;s good to know it will work in the end. First [...]]]></description>
			<content:encoded><![CDATA[<p>So here are a few notes on getting a <a href="http://www.chumby.com/">Chumby</a> to talk to an <a href="http://www.arduino.cc/">Arduino</a> using <a href="http://pyserial.sourceforge.net/">PySerial</a> (and <a href="http://www.python.org/">Python</a>).  It&#8217;s pretty easy, but I&#8217;ll document it to make it obvious.  As the Chumby is sometimes a bit slow with a few of these steps, it&#8217;s good to know it will work in the end.</p>
<p>First you&#8217;ll want <a href="http://wiki.chumby.com/mediawiki/index.php/Python">Python on the Chumby</a>.  At the time the latest version already compiled for the Chumby is Python 2.6, so it&#8217;s pretty up-to-date.</p>
<p>Once you&#8217;ve got Python installed and on a USB stick you&#8217;ll also want to download PySerial &#8211; I picked the latest version <a href="http://sourceforge.net/projects/pyserial/files/pyserial/2.5-rc2/pyserial-2.5-rc2.tar.gz/download">PySerial-2.5-rc2</a>.</p>
<p>With PySerial expanded and on the USB stick (alongside Python) you&#8217;ll want to put the USB stick in the Chumby and connect to it <a href="http://wiki.chumby.com/mediawiki/index.php/Chumby_tricks#Hidden_screen_in_Control_Panel">via SSH</a>.</p>
<p>Change to the directory for the USB stick (e.g. <code>cd /mnt/usb</code>).</p>
<p>You should see (at least) two directories, one for Python and one for PySerial:</p>
<pre>
<code>
chumby:/mnt/usb-EC5C-3D0A# ls -l
drwxr-xr-x    6 root     root         4096 Jun 26 21:15 pyserial-2.5-rc2
drwxrwxrwx    4 root     root         4096 Jan 10 13:51 python2.6-chumby
</code>
</pre>
<p>You&#8217;re first instinct may be to try and install PySerial via the usual call to <code>python setup.py install</code>.  However this appears not to work.</p>
<p>All is not lost though &#8211; just manually copy the relevant directory (serial) from PySerial to the python site-packages directory, e.g.:</p>
<pre>
<code>
cp -r pyserial-2.5-rc2/serial python2.6-chumby/lib/python2.6/site-packages/
</code>
</pre>
<p>Know to check that&#8217;s worked open a python prompt and try to import the serial library:</p>
<pre>
<code>
chumby:/mnt/usb-EC5C-3D0A# python2.6-chumby/bin/python
Python 2.6.2 (r262:71600, May 23 2009, 22:28:43)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>>
</code>
</pre>
<p>If you don&#8217;t see any errors (as above) then everything has installed ok.</p>
<p>Next step is to try out connecting an Arduino.</p>
<p>So first off you&#8217;ll want to ensure the Arduino has a program that can read from the serial port &#8211; to show that everything is working ok.  In my case I picked my <a href="http://www.psychicorigami.com/2010/05/26/my-first-arduino-project-morse-code/">Morse Code program</a>.  This will read bytes from the serial port and toggle the built-in LED (on pin 13), so it&#8217;s handy for verifying the serial port is working.</p>
<p>So now you have a program loaded on the Arduino, connect it to the Chumby.  The Arduino should get enough power from the Chumby to start up ok.</p>
<p>You need to work out which serial port the Arduino is using on the Chumby.  List the tty&#8217;s in /dev and pick the most likely looking one (should have USB in it&#8217;s name):</p>
<pre>
<code>
chumby:/mnt/usb-EC5C-3D0A# ls /dev/tty*
/dev/tty      /dev/ttyS00   /dev/ttyS02   /dev/ttyS1    /dev/ttyS3
/dev/ttyS0    /dev/ttyS01   /dev/ttyS03   /dev/ttyS2    /dev/ttyUSB0
</code>
</pre>
<p>On my Chumby the serial port was <code>/dev/ttyUSB0</code>.</p>
<p>Now open up a python prompt again and try talking to the Arduino.  You&#8217;ll need to configure the baud-rate of the serial port to match the program on the Chumby.  In my case this was 9600.</p>
<pre>
<code>

chumby:/mnt/usb-EC5C-3D0A# python2.6-chumby/bin/python
Python 2.6.2 (r262:71600, May 23 2009, 22:28:43)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
>>> ser.write('sos')
3
</code>
</pre>
<p>If you are using the same morse code program you should see the LED blink out &#8216;DOT-DOT-DOT DASH-DASH-DASH DOT-DOT-DOT&#8217; &#8211; confirming that the serial port works.</p>
<p>This is really quite handy, as the Chumby is a small low-power Linux server.  Coupled with Python this means it&#8217;s really easy to get any Arduino based project online, without needing a &#8220;normal&#8221; computer constantly running.  Not quite as self-contained as using an <a href="http://www.arduino.cc/en/Main/ArduinoEthernetShield">Ethernet Shield</a>, but the Chumby is fairly small, so it and an Arduino will easily sit on a window ledge (for example).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychicorigami.com/2010/06/26/chumby-to-arduino-communication-using-pyserial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Arduino Tone Generator/&#8221;Theremin&#8221;</title>
		<link>http://www.psychicorigami.com/2010/06/13/arduino-tone-generatortheremin/</link>
		<comments>http://www.psychicorigami.com/2010/06/13/arduino-tone-generatortheremin/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 10:26:54 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.psychicorigami.com/?p=274</guid>
		<description><![CDATA[So here&#8217;s another quick Arduino project. It is a &#8220;theremin&#8221; of sorts, but whereas a real theremin uses antennas to control pitch and volume this on uses a potentiometer for pitch and a push button for volume. The tone is output via a piezo buzzer (as used for generating morse tones in the previous project). [...]]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s another quick <a href="http://www.arduino.cc/">Arduino</a> project.  It is a &#8220;<a href="http://en.wikipedia.org/wiki/Theremin">theremin</a>&#8221; of sorts, but whereas a real theremin uses antennas to control pitch and volume this on uses a <a href="http://www.arduino.cc/en/Tutorial/Potentiometer">potentiometer</a> for pitch and a <a href="http://www.arduino.cc/en/Tutorial/Pushbutton">push button</a> for volume.  The tone is output via a piezo buzzer (as used for <a href="http://www.psychicorigami.com/2010/05/26/my-first-arduino-project-morse-code/">generating morse tones</a> in the previous project).</p>
<p><center><br />
<a href="http://www.flickr.com/photos/lilspikey/4695187823/" title="Arduino &quot;Theremin&quot; by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4056/4695187823_bc1738d170.jpg" width="500" height="375" alt="Arduino &quot;Theremin&quot;" /></a><br />
</center></p>
<p>The voltage from the potentiometer is read via <a href="http://www.arduino.cc/en/Reference/AnalogRead">analogRead</a>, then is mapped to a freqency to output on the buzzer.  To make things sound nicer the frequencies are limited to the twelve &#8220;well tempered&#8221; notes.  As the potentiometer is turned the note played changes.  There&#8217;s a little bit of smoothing (legato) in between note transitions too (so it sounds a bit more theremin-like).</p>
<p>All code for this <a href="http://github.com/lilspikey/arduino_sketches/blob/master/theremin/theremin.pde">Arduino &#8220;theremin&#8221;</a> is available on github, as part of my <a href="http://github.com/lilspikey/arduino_sketches">Arduino Sketches</a> project.</p>
<p>Here&#8217;s a demo of the tone generator in action:</p>
<p><center><br />
<object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=bc9bd6b0dc&#038;photo_id=4695837166&#038;flickr_show_info_box=true"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=bc9bd6b0dc&#038;photo_id=4695837166&#038;flickr_show_info_box=true" height="300" width="400"></embed></object><br />
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychicorigami.com/2010/06/13/arduino-tone-generatortheremin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My First Arduino Project (Morse Code)</title>
		<link>http://www.psychicorigami.com/2010/05/26/my-first-arduino-project-morse-code/</link>
		<comments>http://www.psychicorigami.com/2010/05/26/my-first-arduino-project-morse-code/#comments</comments>
		<pubDate>Wed, 26 May 2010 19:26:03 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.psychicorigami.com/?p=260</guid>
		<description><![CDATA[So I finally have an Arduino. I&#8217;ve been hankering after one for a while now, after seeing what Build Brighton and others have been able to do with them. So I was very happy when my better half paid attention to the hints I&#8217;d been dropping and got me an Arduino starter kit for my [...]]]></description>
			<content:encoded><![CDATA[<p>So I finally have an <a href="http://www.arduino.cc/">Arduino</a>.  I&#8217;ve been hankering after one for a while now, after seeing what <a href="http://www.buildbrighton.com/wiki/Main_Page">Build Brighton</a> and others have been able to do with them.  So I was very happy when my better half paid attention to the hints I&#8217;d been dropping and got me an <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9284">Arduino starter kit</a> for my birthday.  That was only a few days ago and thanks to how wonderfully easy the <a href="http://arduino.cc/en/Main/Software">Arduino IDE</a> is, it hasn&#8217;t taken long for me to get things working.  The simplicity of writing the code, hitting upload and seeing the effect on the Arduino within seconds makes it really quick to try out different ideas.  The only bit that&#8217;s tricky (for me at least) is the electronics side of things.  Due to a slight fear of accidentally burning out the <a href="http://www.atmel.com/products/avr/">8bit AVR chip</a> at the core of the Arduino I&#8217;m being super cautious.  Though this is due to my at best high school knowledge of electronics.  That and the fact that I&#8217;m pretty much a software boy.</p>
<p>So what to make first?  Well morse code seemed like a good starting point.  I&#8217;d written an app to <a href="http://www.psychicorigami.com/2009/03/01/5k-morse-code-app-using-capslock-led/">output morse on the capslock LED</a> before and the starter kit had LEDs, so it seeded like a good starting point.  On closer inspection there was also a piezoelectric buzzer &#8211; so even better it was possible to generate a tone too!</p>
<p>Getting the <a href="http://arduino.cc/en/Tutorial/Blink">LED to turn on and off</a> was pretty easy and it forms the standard &#8220;hello world&#8221; program for the Arduino.  Getting the buzzer to generate a tone was marginally trickier, but <a href="http://www.faludi.com/2007/04/23/buzzer-arduino-example-code/">not that hard</a> and basically just consisted of turning it on and off quick enough to cause a vibration and therefore make a sound.  With the help of this <a href="http://www.instructables.com/id/Play-the-French-Can-Can-Using-an-Arduino-and-Buzze/">guide to making the Arduino play the Can-Can</a> I also had details of how to wire the circuit correctly (in particular adding in a suitable resistor to avoid overloading the AVR chip).</p>
<p>Using the starter kits breadboard, jumper wires, a 330 Ohm resistor, an LED and the piezoelectric buzzer I put together this:</p>
<p><center><br />
<a href="http://www.flickr.com/photos/lilspikey/4641973105/" title="Arduino Morse Code Circuit by lilspikey, on Flickr"><img src="http://farm5.static.flickr.com/4049/4641973105_b90d4983ab.jpg" width="500" height="375" alt="Arduino Morse Code Circuit" /></a><br />
</center></p>
<p>The buzzer had it&#8217;s positive pin wired in to pin 4 on the Arduino and the LED was in turn wired in to pin 13.  In the picture above, I arbitrarily used yellow jumper leads for &#8220;ground&#8221; and green for positive.  So one green lead goes to each pin (4 and 13) and only one yellow lead goes to the &#8220;ground&#8221; on the Arduino.</p>
<p>Initially I hard-coded the message the Arduino was going to output, but having to re-compile each time to change the message seemed a little tedious.  So I decided to get the Arduino to read from the serial port, store the characters it read in, then output the relevant morse.  To achieve this the program has a 512 byte buffer that it fills up with data read from the serial port.  If the buffer is full it signals this by writing back a -1 down the serial port &#8211; otherwise it echos back what it has read in.  As it plays the morse it moves along the buffer looking for the next character.  Once all characters are output it resets the buffer.  The program is actually able to read from the serial port whilst it is turning the LED on/off and generating tones.  It&#8217;s only limited by the size of the buffer it is using and the fact that it can read faster from the serial port than it can output morse code.</p>
<p>Most of the work in the code revolved around trying to both read from the serial port and generate tones.  If the morse was known ahead of time, it would be pretty easy to right several for-loops to run through each character generating tones until we were finished.  However the program would not start reading from the serial port until it had finished outputting the morse.  So apart from using some basic state-machine style antics to keep track of which character we were on and whether we were currently during a dot, dash or silent period the major a-ha moment for me was realizing how to easily generate a consistent tones without using delays in the code.  In <a href="http://www.faludi.com/itp/arduino/buzzer_example.pde">previous code I&#8217;d seen for generating tones</a>, the code looked like:</p>
<pre>
<code>
for (long i=0; i < numCycles; i++){
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
</code>
</pre>
<p>This is nice and easy to follow, but all those calls to <code>delayMicroseconds</code> mean you are hanging around waiting for the air to move to make a sound.</p>
<p>As I wasn't too concerned with the particular fidelity of the sound from the buzzer I opted for the following instead:</p>
<pre>
<code>
void morse_pulse_on() {
  digitalWrite(LED_PIN, HIGH);
  unsigned long period = micros() % buzzer_delay;
  digitalWrite(BUZZER_PIN, period < (buzzer_delay>>1)? HIGH : LOW);
}
</code>
</pre>
<p>This was called every time through the <code>loop()</code> function if a sound needed to be played.  As there is no delay in calling it, one could then immediately perform other tasks, such as checking for new serial data or turning the LED on and off.  It's quite simple real - the modulus operator (<code>%</code>) is used to get the sub-divide the current time in microseconds into a number between <code>0</code> and <code>buzzer_delay</code> (a value pre-calculated to let us generate the right frequency) representing the current "period".  If we are halfway through this period then we set the buzzer to <code>HIGH</code>, otherwise we set it to <code>LOW</code>.  By repeatedly calling <code>morse_pulse_on</code> we end up with (in this case) a glorious 700Hz tone.</p>
<p>All code for this <a href="http://github.com/lilspikey/arduino_sketches/blob/master/morse/morse.pde">Arduino morse code</a> program is available on github, as part of my <a href="http://github.com/lilspikey/arduino_sketches">Arduino Sketches</a> project.  Hopefully I'll add more code to that project as I get a chance to delve deeper into what the Arduino can do.</p>
<p>Here's a demo of the whole thing in action:</p>
<p><center><br />
<object type="application/x-shockwave-flash" width="480" height="480" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=b4783f8477&#038;photo_id=4642700206&#038;flickr_show_info_box=true"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=b4783f8477&#038;photo_id=4642700206&#038;flickr_show_info_box=true" height="480" width="480"></embed></object><br />
</center></p>
<p>UPDATE: not long after finishing this, I discovered the <a href="http://arduino.cc/en/Reference/Tone">tone</a> and <a href="http://arduino.cc/en/Reference/NoTone">noTone</a> functions.  These produce a better tone than the technique outlined above.  Though they do involve use of one of the interrupts on the arduino, that's normally used for <a href="http://arduino.cc/en/Tutorial/PWM">PWM</a> on pins 3 and 11 - not that it's a problem for this project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychicorigami.com/2010/05/26/my-first-arduino-project-morse-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

