About getting started with AVR programming

Like one post by a guy like me could possibly encompass everything you’d need to know. Pshaw.

I recall how overwhelming it felt when I decided to start programming AVRs (the microcontroller unit, or MCU, that is the heart and soul of Arduinos). I have an Arduino Uno and have lost many hours dinking with LEDs, servos, and even remote controls (scary stuff) but the thought of focusing solely on the AVR chip felt big, like there’s no way in hell I’ll be able to do that without an EE degree.

I’m happy to say that’s not the case. Programming AVRs is only slightly more complicated than programming Arduinos, so if you get the latter, you’re close to getting the former. In spite of what folks say, you will need a programming background to program AVRs, particularly C programming. And if you don’t have a C background but you understand enough Arduino programming to be dangerous, just brush up on what C’s all about first (it’s a very small language) and preferably keep a good C book close by. You’ll be fine.

I’m not here to teach you AVR programming or even C programming. Sorry. What I’m here to do is pass along some advice that I wish I had before getting started. It won’t be much, but I think it’ll help you in the long run. The advice will be random and probably not universally awesome, but it’s free.

Start a project

I doubt anyone wants to embark on learning microcontroller programming without some kind of project in mind. What’s the point? You must have something you want to make: a sick Christmas light show, a fully automated cemetery for Halloween, a home entertainment library device, something ridiculous and distracting to bolt onto your car…anything. Figure out what it is you want to build, then make that your goal. This will not only give you something to focus on, but it’ll also come in handy when you get to the next section.

If necessary, get free samples

Yeah, I’m going there already. This is one of the more potentially controversial topics: free samples of hardware components (integrated circuits, or ICs; LEDs; timers; etc). Many manufacturers offer these things, and when they do, the samples are intended for evaluation use and not production use or sell-on-eBay use. Take note: people that think they can accumulate a small Radio Shack of components by asking for samples willy nilly are a scourge to our industry and have been known to drive companies to stop offering samples. Totally understandable on the company side, totally inexcusable on the sample-hoarder side. Don’t do that, seriously.

Also, if you get a sample of a component and then weeks later ask for two more, then a week later ask for four more, you’re abusing the service. A sample is a sample. If your project needs four of something for evaluation, ask for four and then stop. If you need/want more, look into buying the component from a distributor like Mouser. You may be surprised to find out that the component that you’re repeatedly milking from the manufacturer costs only $0.30/unit. Don’t be a cheap bastard. Sample, then buy. If you don’t, you’re hurting all of us.

Back to the personal-Radio-Shack thing. Don’t contact manufacturers about samples without a project in mind. You will be asked during checkout what the samples will be used for, what your product’s estimated production date is, how many projected units will be produced, etc. Lots of info. Don’t fake it; they’ll know the 8-bit MCU with 8k of flash that you’re asking for won’t run Linux, so saying you need the samples to build your own Raspberry Pi will fall flat. You must have a project.

Last bit of advice on this subject: don’t use a freebie email account for your orders (Yahoo, Gmail, Hotmail, etc). Confession: I don’t understand this requirement. There are lots of people that build products professionally but don’t have their own company email address, or maybe they do but it’s something like “prodevicesinc@gmail.com” instead of “hal@prodevicesinc.com.” Rules are rules, though, so if you’re serious about building your project and continuing to build future projects, get a domain name and get email set up with the domain. You may already have a hosting account for a website and that account may come with a limited number of email addresses that you aren’t using because Gmail works just fine, thank you very much. Set up email with your own domain anyway. Use that email address for all project-related business, including sample requests.

With that out of the way, I will mention that Atmel, the company that produces AVR chips, has been very supportive of the maker community and does offer samples of its most popular AVRs. I would recommend at least asking for an ATMega168 (Make Magazine’s AVR Programming book focuses on that chip) and, if you’re thinking about motor control or components that would benefit from smooth motion or fading effects, maybe an ATTiny85. Also, a decent middle ground AVR that could be excellent to experiment with is the ATTiny2313 (one of Ladyada of Adafruit fame’s favorites), which has some nice serial communication features found mostly in ATMega AVRs but not in other ATTiny AVRs.

Don’t ask for 10 each! A basic sample = one, not 10. I have the original small set of AVRs that Atmel sent to me and I cherish each and every one of them, even if they are very affordable to purchase (Mouser has ATTiny85s for $1.50, I believe, and ATMega168s for around $5? I don’t recall. They’re very inexpensive).

Piggybacking on the sampling discussion, know your pin packages

I’m going to go out on a limb here and guess you’ll be breadboarding your designs while you’re learning and working out details. This means the components you need have to be breadboard-friendly. A few things will need to be remembered here:

  • For IC chips, stick with DIP.
  • For other components that use pins, the pins should be 0.10″ (2.54mm) apart. This is fairly common so you rarely need to check this, but it’s good to keep in mind.

I mention this because if you don’t know what you’re looking for, when you order a sample or purchase a component you may end up with a “surface mount” component, and that won’t do you much good in a breadboard. Some companies only offer samples in surface mount pin packages, mostly because they’re cheaper to produce and the electronics world is moving in that direction anyway. Just be careful when you order components if you intend to use them in breadboards.

Side note: For AVR chips or any DIP chip that may be plugged/unplugged frequently, I highly recommend you get “zero-insertion force” sockets (a.k.a. “ZIF”). The benefit of this will become apparent when you program and test your AVR: prying the chip out of a breadboard, plugging it into a development board for the actual programming, prying the chip out of the development board and shoving it back into the breadboard, on and on and on will take its toll on your AVR. One of my early purchases was a pair of 28 pin ZIF sockets, one for my breadboard and one for a development board for programming, and I have never regretted it. Some people will recommend you simply program the AVR chip in the breadboard where you’re prototyping. I found that the programmer device (more on that later) interfered with the operation of my prototype. Now, I keep everything separate. ZIF sockets make it a snap.

eBay: the greatest electronics store in the world, sorta

I remember the day I discovered that you could buy bulk electronic components from Asia via eBay and pay next to nothing (or just a little bit more than nothing) for shipping. Before anyone grouses at this, remember where many of the components you could buy in the US originate from. Yeah, they’re mostly made over there. I’ve been told to stay away from name brands in Asian eBay stores because there is a risk of getting counterfeited products, so don’t go to Asia for AVRs or name-brand ICs, okay? You may be fine, but it’s not worth the risk.

That said, holy moly, everything is so inexpensive there! Those ZIF sockets that I mentioned earlier? I could have paid $3/each in the US + shipping but I got mine through eBay instead and paid $1.19 for two of them and free shipping. I’ve also bought 10 16MHz crystals for $1.50 + $1.00 shipping, a single ubiquitous 16×2 LCD display (you’ll need one, I promise) for $1.94 and free shipping (they’re about $6.00 in the US), 10 each of 8/16/28 pin DIP sockets for about $1.50/set (yep, free shipping), a USB to RS232 serial converter (you will definitely need one of these) for $1.73 and free shipping…oh man, I could go on but I may short out my keyboard from all the drool.

My basic rule—which I have violated only a few times but will never violate again—is this: if you need one of something, buy at least two. Why? That free or almost-free shipping only goes one direction: from China to the US. If you need to ship something back for a refund or exchange, expect to pay $10 or more to ship the thing back, more if the item is heavy. Also, when possible, don’t get things that have lots of parts to break. DIP sockets, crystals, tweezers, etc. are fairly easy to justify buying from Asia; things like breadboard power supplies and displays, not so much. I have already fried one breadboard power supply and I could buy five for what I would pay to ship this one item back. Choose carefully, and if in doubt about quality or you really don’t want multiples, purchase the items individually from Mouser, Adafruit, Sparkfun, etc. instead.

I’ve mentioned Make’s “AVR Programming” book, right?

Get this book. It expects a lot after the first 50 or so pages, but there are answers out there to the things that confuse you. It’s as gentle an introduction to AVR programming as you’re going to get. Also, skim the book and build a complete parts list, then get everything so you don’t have to stop your momentum while waiting for a component to ship to you from China (possibly 2-4 weeks, unless you happen to order at just the right time). I’ll say up front that you should stock up, way up, on LEDs. They seem pointless but no matter how many I have I always manage to find myself needing more. It’s pretty annoying.

An incomplete list of hardware that I use frequently

In case it’s interesting, here’s most of what I use to do AVR programming outside of the AVR development suite and a text editor:

  • A Sainsmart USBASP for programming. This doesn’t support all AVR chips, but so far it’s worked with the ones I’ve used (there are questions re: how well they’ll work with ATMega328 AVRs, which are what Arduino Unos use). They’re a couple of bucks on eBay. That’s cheap, so get two. I believe you will need to download/install an FTDI driver for your computer before it’ll work.
  • My development board (the board the AVR is plugged into for flashing with new programs) is actually two boards. One is just a breadboard with a ZIF socket, a 0.01uF capacitor, an LED w/ 220 ohm resistor, and 6 wires that connect to my USBASP programmer. The other is a PCB that I soldered up that has basically the same stuff except it has a 28 pin DIP socket instead of a ZIF socket, a 10-pin header that my USBASP plugs into directly, and two LED + resistor combos for power indication and testing. Both are wired identically to the USBASP programmer and both work perfectly (development boards are actually very simple to make). I would eventually like to take that ZIF socket out of the breadboard and solder up a new board that uses it instead of a plain 28 pin DIP socket, but I’m in no rush. And I was in the process of creating a third development board to handle my 8- and 16-pin ATTiny AVRs but I decided to wait until I bought more ZIF sockets. I may be done with plain DIP sockets on development boards. ZIF sockets are just too nice to pass up.
  • A PL2303-based USB to RS232 converter/adapter. These come in many shapes and sizes and they’re essential for serial communication with your AVR at least during testing/prototyping/debugging. You will need to download/install the Prolific driver before this will work (search the site for Mac OS if you have a Mac. The Windows driver should be easy to find). These are very inexpensive on eBay so get a few of ’em. I bet you’ll eventually wish you had two anyway.
  • At one time I had a 3v3/5V MB102 breadboard power supply for my full-size MB102 breadboard, but as I mentioned earlier I fried it. My fault, now that I think about it. For now, I’m just using a simple USB breakout board and a micro-USB cable to power my breadboard. I use the 5V and ground pins from the breakout board to do this. Works like a charm. I would like another breadboard power supply, though. Soon.
  • A Raspberry Pi Model B. Microcontrollers are great for talking to hardware, but for processing you should have a computer handy. The Raspberry Pi is a perfect complement to microcontroller projects because it is a legitimate computer, perhaps not as powerful as a laptop or desktop but powerful enough to crunch numbers and do silly things like connect your microcontroller to a network. If you use AVR chips that have built in USART support (serial communication) like the Mega series or the ATTiny2313, a USB converter between your microcontroller and the Pi may be all that’s needed other than some coding to get your Pi and microcontroller chatting. And at ~$40, the Raspberry Pi is hardly going to clean out your bank account. My Pi is running the latest version of Raspian, in case that matters. I love these things, they’re too damn cool.
  • An old, used Linksys E1000 wireless router flashed with DD-WRT and set up as a “client bridge.” DD-WRT is a free Linux-based wireless router firmware that can be flashed to some but not all wireless routers. There is a database of compatible routers on the DD-WRT website. What “client bridge” does is it transforms the wireless router into a wireless network adapter, just like a USB 802.11n wireless adapter that you’d plug into a USB port in your laptop (except you connect to this router using an ethernet cable). The cool thing about DD-WRT routers in “client bridge” mode is you can connect multiple devices to it, essentially making it a wireless hub. Nifty stuff. Raspberry Pis have ethernet ports but no built-in wireless support, so this is how I get mine onto our network and the Internet. I also have an ASUS wireless hub that I flashed with DD-WRT, but that one’s being used to put our Xbox and PlayStation 3 on our network so it’s off limits. If you live near a Frys or don’t mind ordering routers online, Frys sells ASUS wireless routers pre-flashed with DD-WRT. They cost more, but the price may be worth it depending on your needs. And I’ve seen other DD-WRT-compatible 802.11n routers at thrift stores that cost less than $10. Just sayin’. Don’t spend too much on a DD-WRT router. More $$ won’t net you more benefit.
  • An old Fluke 27 multimeter. Seriously, folks, get a multimeter. Even just for simple things like tracing what wire on one end of a cable connects to what pin on the other end of the cable, these things are indispensable. I got this thing on eBay about 10 years ago when the military was dumping old equipment during a system-wide equipment upgrade. Back then, you could get these for around $20 (I did). They’re no longer flooding the market so $20 won’t cut it, but other limited but decent multimeters are available for around that much money.
  • An Arduino Uno, as I mentioned earlier. Yes, I still use it. For testing a new display or other component, it’s a lot faster to wire something to an Arduino than it is to find a breadboard, wire it up, flash an AVR, put the AVR in the breadboard, etc. With the Arduino, just plug the damn thing into a USB port, load a sample sketch that utilizes the component, add the wires where they need to go to connect your component and blam-o, you’re testing. It’s also nice to have a quick prototyping board that gets out of your way. AVR programming is significantly more cost effective ($10-$30 Arduino vs $1.50-$8 AVR) but for quick-and-dirty experiments Arduinos shine.

Beyond the items above, I have tons of random stuff that I’ve accumulated through eBay, from disassembled dead computer equipment, from old Basic Stamp kits, and to a lesser extent through legitimately procured manufacturer samples. I also have spools of solid-core wire which is very breadboard-friendly (highly recommended), heat shrink tubing for sealing up and hiding my solder work, various hacked together cables and connectors and other geegaws that help me make my AVRs speak with non-AVR hardware. And breadboards. Lots of breadboards. Get the ones with two power rails along each edge if you’re just now stocking up. Don’t settle for one 1/2 size board either, get at least a full-size board and some smaller ones with power rails. I’ve been pondering getting a handful of the tiny breadboards that don’t have power rails just so I can make modules that are easy to plug together, but I’m not sure I’ll do that. We’ll see.

I’ll stop here. There are typos, I just know it. I apologize for that, but I doubt I’m going to fix them any time soon. It’s the thought that counts.

Now, stop reading and start programming. This is fun stuff.

This entry was posted in Arduino, AVR, hardware.

Leave a Reply

Your email address will not be published. Required fields are marked *