But wait a second... the AVR328p is a 3 dollar chip! And the AVR ATMega48 (pin compatible but with a LOT less space) is 2 bucks (for 10)! And if your requirements are extremely minimal, there's the ATTINY13 which comes in as little as 60 cents -- but in an inconvenient package (you'll be better off spending another 4 dimes and getting one for 1 buck).
Let's hack up a bunch of these chips so next project we'll have lots of choices...
Note: If you've figured out the hardware part and just need some software help check out this posting: AVRez -- "arduino" style APIs for other chips
The first step is picking and getting your chip. This can be tricky because paradoxically there are both too many parts and too few. By "too many" I mean is that since there are literally hundreds of AVR parts to choose from, it can be hard to find what you need. You are overwhelmed with data. So your first stop should be the Atmel "parametric" selector here (most vendors have parametric selectors on their web sites but they can be hard to find). If you use my link you'll see that I've already selected the basic stuff in that link (like 8 bit AVR) but there are still hundreds of possibilities.
Another excellent way to narrow it down is to pick choices that other people have already used. These can be found by looking in the Arduino or Adafruit forums, www.avrfreaks.net, or the eye candy sites like www.hackaday.com.
Also its a safe bet to pick from these popular choices: ATMEGA328 (Uno), ATMEGA168 (like Uno but 16k flash), ATMEGA48 (Uno, 4k flash). ATTINY24, ATTINY13 (no serial), ATTINY2313 (a "tiny" with serial).
Navigating Parts Suppliers
Once you've found some candidates, you'll want to head over to www.findchips.com to see if there are any for sale in a package you can handle. This is where you may find there are "too few" parts -- that awesome device perfect for your job is not stocked and only available in 1000 units! :-(
So you'll need to do a bit of back-and-forth between the parametric table and the marketplaces to find what you need.
Another method is to head directly to www.digikey.com which has a large selection and a great interface. Basically, type your part name prefix in (like ATTINY, or ATTINY13). You should see a product selector. Click "in stock" and then refresh. Now for the secret that took me a year to discover: head over to the "price" column (far right), click on the triangles, and enter ascending sort with 5-10 units (never buy just one or 2, if you do Murphy's law guarantees you'll burn them out!) Personally, I find that digikey has the best prices on average, but other suppliers sometimes sell particular parts for significantly cheaper.
Once you've picked out your part, pop up the datasheet just to make sure the parametric table was correct and to verify that it is "in-system programmable" -- or ICSP (in-circuit-serial-programming). I think all AVRs have this feature but you never know... Its also good if they have an internal oscillator so you don't have to hook up a crystal. Again, I think they all have it :-).
Most importantly, it will be nerd-enhancing for you to actually read (actually skim) the datasheet; you'll learn valuable data mining skills :-).
If you bought a thru-hole version, you'll want a breadboard to solder it on to (or use a solderless breadboard). If you bought a SMT chip (congrats!), you'll need to either buy or make a break-out board and to learn how to bake surface mount parts.
Next, you need a device programmer. You can actually use your Arduino as described here http://arduino.cc/en/Tutorial/ArduinoISP, or buy a standalone one like the one I use: http://www.ladyada.net/make/usbtinyisp/. You'll also want the standard electronics stuff; a bunch of jumper wires, a soldering iron, a multimeter.
Ok, it will take a few days for the stuff to arrive... when it does we will hook up the chip, write a simple program and upload it.
Using The Chips
Ok, everything arrived? Next let's hook it up!
The first step is to mount the chip into a solderless (or soldered) breadboard. If you bought a SMT chip, you'll have to first bake it onto a breakout board.
Next, take a look at the datasheet of your chip. Right at the top of AVR datasheets you'll see a section called "Pin Configuration" that shows the physical layout of the chips. Here is what the ATMEGA 328/168/88/48 PDIP package looks like:
On all chips, we have power and ground (yellow), reset (orange) and a SPI programming interface (red). When the chip is in reset, the SPI interface can be used to upload programs.
Now take a look at the Arduino ICSP header:
Some caveats: If you are using the ArduinoISP project to program your chip, don't hook the "reset" lines together. The "reset" in the Arduino's ICSP header will reset the Arduino. You want to hook a general purpose IO to the chip's reset so that the ArduinoISP can put it into reset. For more details: http://arduino.cc/en/Tutorial/ArduinoISP
The ICSP header is symmetrical, (and the "top" view vs the "bottom" view will mirror the pins) so if you can't tell where pin 1 is, use a multimeter to look for the 5 volt difference across the power and ground pins (2 & 6). That way you can be sure you've found the correct orientation!
Ok, so here I've hooked my USBtinyISP up to a MEGA48. I've also added a resistor and LED in series connecting to one of the pins so I can try the "hello world" of embedded programming -- blinking a LED.
This might be hard to make out, because I can't focus on both the programming header and the chip... its just the foreground center chip, and connections, the other stuff is just that -- other stuff :-).
Ok, next step is to use avrdude to verify that we can talk to the chip!
Programming the Chip
You've got to get the software first. On Debian descended Linux distros (Ubuntu, Mint) you can run the following:
sudo apt-get install avrdude gcc-avr avr-libc binutils-avr
But on other OSes, if you've got the Arduino IDE installed and working, then you've got these programs somewhere.
Next, let's just check to make sure you hooked it up correctly.
First, plug your programmer in and verify that you see +5v across the power (VCC) and ground (GND) pins. Next run:
avrdude -c usbtinyIt will complain that you did not specify a chip and provide you a list of options. Pick your chip out of the list and run the same command with -p
avrdude -c usbtiny -p t24It should verify the chip's device ID and then quit. It can also complain in a bunch of different ways. Check out my AVR programming cheatsheet if you get an error.
Next let's figure out what the compiler thinks your chip is named. Run:
avr-g++ -mlist-devices --target-help
and pick your chip out of the list. You'll need that in the next section.
Writing A Simple Program
Next, we need to write a simple program to get started. This should be the subject of an entirely new blog entry, so I'll just skip it for now and point you to a simple framework for building the code, located on github, named avrez. I'm not entirely sure what "social coding" is :-) but github is still pretty cool.
Next "fork" that onto your machine and then run:
make CPU=So for the chips I'm using its one of:
make CPU=attiny24Did it work or did it put up an error message saying "Sorry I don't know that chip!" If the latter, be happy because you're off the reservation -- you can ADD the chip to the project and feel happy about being the first! :-)
This make line will build all the source files in the directory AND upload them to your board using the attinyISP programmer (by default, you can change that in the Makefile).
If it seems to have worked, then hook up a resistor (100-1kohm) and LED as follows:
where PB0 is a pin on the chip (check out the pin outs in the datasheet). If your chip does not HAVE a PB0, you'll have to change the code to a pin you do have. I've drawn the LED with the long wire on the left to remind you to put it in that way.
You should see the LED blink once every 10 to 20 seconds. If it is not (and you are new to electronics), let me recommend that you flip the LED around, or even put 2 in in opposite directions...
Hooray!! You successfully uploaded a program to the chip! And I've written one monster-long blog post whose length shall never be repeated!!! :-)
If you are a noobie programmer, stay tuned for another SEPARATE posting about how to hack that "avrez" project into a real program -- if you are a software person then jump right in!