Sunday, April 26, 2009

Starting with the AVR microcontroller

I thought it'd be fun to do some microcontroller (uC) programming.  I've been following the Make blog (and a few others) for a while & love the idea of interactive electronics or even something just a little more complicated than vibrating yogurt pots and wired up LEDs.  I can program already, so it seemed like the logical next step.

Well, firstly I spent a fair bit of time being confused by the huge amount of choice (Arduino, BASIC stamp, PIC, AVR etc.).  The Arduino platform is featured heavily in the Maker community, but it seems a bit heavyweight for what I've been thinking about and besides, if you want to make a few "objects of interest" each one would require their own Arduino board which, at $35 a pop and multiple kids on the rampage, could get very expensive.  PIC and BASIC stamp are both good choices but I decided on the AVR series of uCs for a number of reasons:
  1. They're cheap ($1.50 upwards) and readily available.
  2. You can program them in C (one of the languages I understand).
  3. The compiler and programmer software is available free (I'm using WinAVR, the GNU toolchain is available from the Atmel website).
  4. There's a large and active community of users (e.g. AVRFreaks).
  5. There's an Eclipse plug-in for AVR programming (Eclipse is my favourite IDE).
Unfortunately, in my rush to dive in, I didn't do quite enough reading and ended up buying a batch of 10 ATtiny13s (from eBay). These are great because they have 8 pins (small size), but the problem I found was that they only have two PWM (pulse width modulation) outputs - more on that in a later post though.

So, on top of the software and the chip themselves you also need a way to program the chips.  There are dedicated boards available for this, but I chose to use a USB ISP (In-System Programmer).  With these you can have the chip already placed on a breadboard with the project all wired up, program the chip and see what the effects are immediately.  With the dedicated board, you'd have to program the chip, take it out and place it in the project before you could see the effects.  I made another mistake when buying the USB ISP; in my love of eBay I ended up buying an STK500 copy rather than something genuine from DigiKey (e.g. AVAVRISP2); my knock-off has some compatibility issues with the Eclipse AVR plug in which has meant that I now program and build in Eclipse but use AVR Studio to send the program to the chips.  Ah well, you live and learn.

Anyway, onto the obligatory "hello world" for micro-controllers; the blinking LED!

Firstly, rather than re-iterate what others have said very well, just do what I did and follow the first couple of sessions of the sparkfun tutorials.  I love how ghetto it looks having the wires poked into the programming cable and then into the breadboard; I love it so much that I still have it set-up this way.  The only differences are where to attach the leads to the chip, you can work it out by looking at the ATtiny13 datasheet and comparing the pin layout with that of the ATmega8 used by sparkfun.

The picture below shows what I ended up with.  I'd already added the power supply suggestion from the first lesson, which is the purpose of all the electronics at the head of the breadboard, but I'm using a 9v battery rather than an ac adapter.

Here's the code to make the single LED blink:

#include <avr/io.h>
#include <avr/delay.h>

//LED is wired into pin 7 (PB2)
#define LED PB2

int main(void){
//set data direction register for pin 7 to output
DDRB |= _BV(DDB2);

//infinite loop
while (1) {
//turn on the LED
//wait for 1/4 of a second
//turn off the LED
//wait for 1/4 second

Compile (Project->Build in Eclipse), upload to chip (using AVR Studio) and voila! A blinking LED... not particularly amazing, but it's a good start.

Update: Whilst writing this post I figured out how to get Eclipse to program the uC as well!  AVR Studio can autodetect your programmer, apparently I have an AVR ISP V2.  AVR Studio will also tell you what port it's connected on (is there a way to get the Eclipse plugin to autodetect this?).  In Eclipse I had to open up Project->Properties->AVR->AVRDude->Programmer and edit the configuration.  I set the programmer to "Atmel AVR ISP V2" and overrode the default port (first dialogue box below the programmer list) to com3 - which is the one reported by AVR Studio for my set-up.

Drawing Vibrobots

This was one of my first excursions into building a useful or interesting electronic thing for the kids to play with. The idea is really simple, use the vibrations from a motor to shake a yogurt pot with pens on it. The result should be some interesting patterns drawn on paper (and probably the floor).

I sourced the motors from some dollar store fans and a Nintendo rumble pack (found for $1.50 in a thrift store). The fan motors were great as the were already wired up to a switch and all I had to do was solder on a battery pack. I glued the battery pack on the insides of the pots and poked some holes in the top to attach the wires to the motors. I then weighted one of the fan blades with glue to set up a weight offset and increase the amplitude of vibration.

We made a mistake with the first plastic pot in that we glued on the pen legs to the pot, which meant that they weren't easily changeable (not for kids anyway). I say "we" because the gluing was done by my wife, who is far more adept with a glue gun that I am. 'er indoors also had the idea of putting in some motion activated flashing lights scavenged from various kiddie toys and gluing them onto the inside of the pots. That was a nice touch; thanks love!

For the second pot we glued on the pen lids instead:

For some reason this larger one reminds me of those ships in Tron...

It turns out that the results from the drawing actually makes for some good wrapping paper! Bonus!

And, finally, a little video:



My better half, reminded me about Noisebridge a few weeks back. I heard about it when they first set-up but then promptly forgot. Noisebridge is a hackerspace located in the Mission district of San Francisco (much like NYC-Resistor; see for a comprehensive list).

As well as being a general space for people to go and work on projects, they also offer a few tutorials/courses to the general public. I decided to head along to one of the "Circuit Hacking Mondays" to meet the crew, hone my soldering skills and make something interesting. This session was hosted by Mitch Altman (of Brain Machine and tvbgone fame - gizmodo with tvbgone at CES2009), who gave an easy to follow introduction to general electronics and some tips on soldering.

Mitch got out a load of examples of the kits he'd bought with him for people to make. I chose to make a "Trippy RGB Waves" kit (also available at the makershed), but was tempted by the Mini POV kit and, of course, the TV-B-Gone kit. I think I may go again just to make another kit in an easygoing environment with some interesting people.

Putting the kit together was pretty simple, I did get off to a slightly shaky start as Mitch decided to use me as the "demo bunny", I'm glad I'd soldered before so I didn't look like too much of a tool ;) After the initial intro, people just got stuck into soldering their kits. It was a lot of fun. Great to meet such a diverse group of people as well - there were Noisebridge regulars popping in and out all the time to work on their personal projects or just to see who was around.

I'd definitely recommend popping by to anyone who lives nearby! Another interesting evening is the Machine Learning get together every Wednesday night. I managed to make it to a couple of the neural net themed ones, and am planning on heading back soon. I got quite a kick out of coding up a perceptron in the first tutorial.

LED Power Meter

For one of the kids playdates, my lovely wife requested I make a power meter (the playdate was electricity themed). Well, this was my first foray into electronics, so it took me a while to pick up the basics (maybe that should be a separate post), then I had to learn how to solder, buy some components (mainly from RadioShack and eBay) and finally design the circuit and put it all together... phew.

Well that was a great journey, which I heartily recommend!

I have to admit that I didn't come up with the design from scratch. I googled for ideas and settled on a simple chained diode approach. With this design you connect a set of diodes in series with LEDs forking off to from the anode of each. Each diode creates a voltage drop of ~1V (depending on your choice of diode), so, as you increase the input voltage, you light up more LEDs. Genius ;)

I put together a couple of prototypes on a breadboard (bough from eBay) to make sure I was getting it right and then soldered everything together on a prototype PCB board (another eBay purchase).

To make it more interesting for the kids, I decided to use 3 different colour LEDs: red for "DANGER - low power!"; orange for "medium power"; and green for "WOOT!".

The result? Voila!
The extra diodes at the base create a bridge-rectifier; I needed this as I was powering the circuit from a re-purposed hand-crank torch which I'd bought for $2 from savers.

The "who can light up the most LEDs" game turned out to be quite popular :) Having just two green LEDs at the high end made for quite a challenge; only the "big kids" managed to light them up. Below is a couple of pictures of the finished product in action:

Yes, that's a "Stand Back! I'm going to try Science!" t-shirt, from xkcd.