Instructables

Ghetto Programming: Getting started with AVR microprocessors on the cheap.

Featured

Microprocessors are so cheap these days. If only there were a way to program them up just as cheaply...

*wavy dream-sequence lines*

In this instructable, find out how to build up a complete AVR microprocessor toolchain: compiler, programmer software, programmer hardware, and some simple demos to get your feet wet.

From there, it's just a hop, skip, and a jump to world domination.

The endpoint is not quite as swanky as Atmel's suite, but it's gonna run you about $150 less and take only a little more work to get it set up.

This instructable is based on the Atmel ATtiny 2313 chip, mostly because it's one of the smaller chips (in size) while still being beefy enough to do most anything. And at $3 a pop (non-bulk), they don't break the bank.

That said, most of the steps are applicable across the AVR family, so you'll be able to re-use most everything when your programming needs outgrow the ATtiny and you reach for the $8-$12 ATmegas.



Update Aug 24, 2007: I've gone USB! It's neither ghetto nor particularly deluxe, but a nice middle ground. If you've got a computer without a parallel port, check this http://www.instructibles.com/id/EDRQZ56F5LD8KDX/ out.

If you've got a parallel port, or are just feeling cheap, read on!


 
Remove these adsRemove these ads by Signing Up

Step 1: Go Order Parts!

Necessary Parts List (stuff to buy or scrounge):
Atmel ATtiny2313 chip:       $2.50 - $3Socket for chip:             $1Parallel port connector (DB25):       $3 at Radio Shack or $0.95 at SparkfunHeader Pins:                 $1-$2                (at least one strip each male & female, maybe 2 female)Some LEDs and resistors:     $1

= Around $10, even if you go deluxe. Half that if you can find an old parallel port cable.

Add-ons

A pushbutton switch or two: $1.
A piezo speaker: $1-$5
Light sensitive photocells: $2-$6
Breadboard for making complex circuits: $8-$10(?)

Other Stuff You Oughta Have

Computer: The older the better because it needs a parallel port.
Hookup wire, solder, soldering iron
Super-duper glue
Source of ~5v DC: Batteries will work, and old computer power supplies are perfect.

I get a lot of stuff from Sparkfun because they're
fast, reliable, and fairly priced. They carry AVR chips (but seem to be out
of ATtiny13's at the moment). They've got everything on the "Necessary"
list in one place, nice website.

If you're gonna be ordering a lot of chips, you can get a deal from Digi-key or similar. For instance: ATtiny 2313 for $2.36 each. They've only got 27,853 more in stock, so order quick. (Make sure you get the DIP form-factor.)

Go order stuff now, and we can set up the software side while you wait.

The waiting is the hardest part.

Step 2: Get and Install Software: Linux Version

If you run Windows, skip this page. Or install Linux, then come back. :)

The Linux AVR software toolchain used is:
1) an editor of your choice (emacs, gedit, kate, etc)
2) AVR-GCC compiler and libraries
3) AVRdude programmer
4) A nice Makefile to tie it all together

May 2008 Edit: Ubuntu has packages for the whole toolchain. Your installation is now as easy as: "sudo apt-get install avrdude avr-libc binutils-avr gcc-avr". Bam! The versions that come with 8.04 are good all around. Woot Ubuntu!

If you want to do it "by hand", the script below automates most of the work for you. Note that as of May 2008, it's a year old, and doesn't support the newer ATTinyx5 series chips. You can mess around with updated versions of all the software is you'd like. (It's saved with a .txt extension b/c Instructables wants it. Doesn't matter.)

You're going to have to edit the first command in the install script depending on whether you're using an apt-based package system or a yum-based one. Just uncomment the relevant line. It needs to make sure you've got some packages which are required for the compilation stage.

Have a read through the script to make sure it's not doing anything stupid, then get root and run it and you should be golden.

It will create a directory /usr/local/AVR and install all the AVR tools there.

The last thing the script does is to add avr-gcc to your path so that you can run avr-gcc directly by adding a line to the tail of your /etc/profile.



The other file, setupParport.txt, is just a (very) simple script to set up your parallel port for user use. If you're savvy, you can add the same commands to a boot script so that it enables the parallel port every time you boot up. Otherwise, just run it by hand.



Websites with similar, but different and possibly outdated, procedures include: Psychogenic.com, AVRWiki, and this (old) Linux Focus article.

Alternatively, I hear cdk4AVR has a complete set of compiler tools in RPM format. I haven't tried it, but it could save you the 30 min of compiling time on a slow, old laptop.

Step 3: Get and Install Software: Windows Version

The MS Windows software toolchain is based on the same compiler as Linux one, but with some optional extra GUI friendliness.

WinAVR is the compiler and programmer software. It also includes a nice programming editor, Programmer's Notepad.

Download and install it. It's that easy. Almost.

As the last step in setup, you'll need to make the parallel port user-accessible. WinAVR includes a program for you to do this. If you accepted the default installation options, run C:\winAVR\bin\install_giveio.bat by double-clicking on it. A window will flash up and down, and then you're done.

Step 4: Make Programmer Cable

The cable you're going to make is a "Direct AVR Parallel Access" or DAPA cable.

I think I got the pinouts from somewhere else, but this site has a nice schematic of the parallel port pins for your reference.

Mine goes something like this:
Parallel Port  	 AVR Function  Color2                MOSI          Orange/Grey11               MISO          Orange1                SCK           Green16               RESET         Brown18 	         GND           Brown/Grey

Only tricky bit here: Note that pin 1 (SCK) is on the upper-right hand side when you are looking at the solder pins from the back. It's upper-left when you're looking at it here, and in the circuit diagram.

Also, the guy's website above has ground connected to 20 and 21 while mine (and others) use 18. Many of the pins connect to ground, and it doesn't matter which of them you pick, as long as you get ground.

If you look around the web, you'll find that most people put resistors in either the cable or the cradle (next step) to protect their computer's parallel port from excessive voltages on the AVR chip for use when programming it in-circuit. We will be using strictly 5v here, so there's no such worry, and I leave them out for simplicity.

However, if your chip ever comes near >5v, DO NOT USE THIS CABLE WITHOUT RESISTORS! A computer with a burnt-out parallel port is no fun. That said, I've been using it without incident for 6 months now.

Step 5: Make the programming cradle

ghetto programmer 00008.jpg
ghetto programmer 00012.jpg
ghetto programmer 00007.jpg
Now use the 20-pin socket and female headers to make a cradle which connects the pins from the cable to the right pins on the chip.

The first thing to do is superglue all the headers and the socket to the circuit board. That way, it's easier to solder to. You can even make an extension for the header like I did if you need more room on your circuit board.

The wiring is as follows:

Cable     ATtiny2313 PinMOSI          17MISO          18SCK           19RESET         1GND           10

Get yourself the short ATtiny2313 overview to double-check the pinouts.

And remember: you're soldering up the wires on the underneath, and it's mirror-image. It might help to mark where pin 1 is on the bottom side before soldering. (I did it wrong once. Once.)

That said, the wiring is very simple. Just pin to pin, and then connect all the header pins to the closest pins on the socket. Fortunately for me, this pre-printed circuit board from Radio Shack did the trick.

The cradle is versatile too. See the last picture for my ATtiny13 programmer cradle. I have another for an ATMega8 too.

Step 6: Make some Blinkenlights

An integral part of the Ghetto Development Environment is the plug-in LED.

The AVR chips can all source a fair amount of juice (50mA or more), which is enough to burn out your standard red LED, so it's a good idea to put protective resistors inline with your LEDs. Enter the Ghetto Blinkenlight.

To make them, simply solder a smallish (150 ohm) resistor to the negative lead of an LED, then you can just plug it straight into 5v and it won't burn out. (5 - 1.4) v / 150ohm = 24mA, which is just about right. Make a few -- I made eight. We only use one here.

Step 7: Set up the programming project


Linux and Windows, the procedure for starting a new project is basically the same.

1) Make a new directory. Here, call it LED_Demo or something.
2) Copy your Makefile into that directory.
3) Download the code (LED_Demo.c) into the directory, or start writing new code from scratch.
4) Edit the Makefile to reflect the chip you're using and the name of the project.

Then you're ready to program.



Editing the Makefile

Windows: The Makefile's set up for you. All you have to do to make the Makefile work is remove the ".txt" from the end of the filename. Open it up in Wordpad or Programmer's Notepad anyway, just to have a look at the options you can change later.

Linux: Rename the Makefile.txt to Makefile. Then open the Makefile up in an editor. Un-comment the line that has "/dev/parport0" and comment out (with a #) the line that has "lpt1".

Step 8: Get your first demo circuit set up

Put the chip in the cradle. You'll notice that the pins all angle outwards a bit when the chips come from the factory. Bend them in (gently) on the table so that they're all even and parallel. Then it fits in the socket nicely.

Notice the alignment of the chip. The little dot marks pin 1. Is it where pin 1 should be?

Plug in your 5v power and the programmer cable. Plug the programming cable up to your computer.

Take a Ghetto Blinkenlight and put it between pins 8 and 10 so that the resistor (negative) is in pin 10, the ground for the chip.





Step 9: Program the Chip

Linux:
1) Get in the LED_Demo directory.
2) Cross your fingers.
3) Type "make"

Windows:
1) Open up LED_Demo.c with Programmer's Notepad.
2) Cross your fingers.
3) Select "Tools...Make Program"

Both:

Watch the log as the text goes scrolling by. There are two things you're looking out for here.

One is if the program compiled sucessfully. If there are no errors, it did. If it didn't, why? Did you mess around with my code?

Two is if the chip programmed sucessfully. For this, it writes the code in, then verifies the chip's memory. It should say "Contents Verified." If it says something about the parallel port or "giveio.sys", did you enable the parallel port back a couple steps ago?



Success? Yay! There's nothing like the sweet smell of blinking LED's in the morning.

Step 10: Explaining the software

The code's pretty simple, as far as AVR code goes, but still displays a few tricks of the trade. If you're still basking in the glory of your blinking light, you can come back later.

The #include lines load up some of the extra functions and definitions in the AVR-libC suite. In particular, the delay function _delay_ms() is in delay.h. Interrupt.h has all the pin definitions which make life easier. You're always going to want to include it.

The program always starts from the function main(). In this simple case, it's the only function we have.

The first command in main(), DDRD = _BV(PD4);, seems pretty cryptic, but here's what it's doing. DDRD is the Data Direction Register for port D. All the input/output pins are broken up into different ports for easier access. The one we're using happens to be in D. We need to enable the pin PD4 for output for the LED.

The DDRs are set up so that they have 8 bits, one for each pin in the port. You set a pin up for input by writing 0 to its bit in the register. You can set it to output by writing a 1. We want the contents of DDRD to be 00001000, or output only on pin 4 (read right to left). So how do we do this?

_BV(i) takes a number, i, and converts it to an 8-bit binary number where the i'th bit is a 1 and the rest are zeros. Just exactly what we need. And PD4 is the number corresponding to pin 4 on this port. So we set DDRD to _BV(PD4), and then pin 4 (and only pin 4) is set up for output -- blinking our LED.

The rest of the program repeats forever in a loop. It alternates between turning pin D4 on and off, with a delay in-between.

You can turn the individual pins on (and off) by writing a 1 (0) to the PORT register. The syntax is just like above with the DDR -- PORTD = _BV(PD4) sets the fourth pin in port D to 1.

The _delay_ms() function then waits for a bit. It may not be quite 1ms, though. Depending on what clock speed your chip is set at, it may be a lot faster. The timing's not critical here, so let's overlook that for now.

Finally, PORTD &= ~_BV(PD4); turns pin PD4 off without affecting the rest of the values in PORTD. Let's look in detail at how it does it.

_BV(PD4) creates a binary number with the 4th bit (from the right) as a 1 -- 00001000. "~" is the logical complement operator. It turns the 00001000 into 11110111. "&" is the bitwise "and" operator. It compares two bits, and returns a 1 if and only if both bits are 1. If either is 0, it returns a 0.

The "&=" in PORTD &= ~_BV(PD4) is a very common shortcut. It stands for PORTD = PORTD & ~_BV(PD4). This last command compares the current value of PORTD (00001000) and the value (11110111) described above. The zero in the 4th place in ~_BV(PD4), when used with the & operator, always makes the 4th bit of the result = 0, effectively turning off bit PD4.

The 1s in the rest of ~_BV(PD4) make it so that the & operator doesn't clobber the rest of the contents of PORTD. Since the & returns 1 only if both inputs are 1, the remaining bits in PORTD are re-assigned whatever value they already have -- leaving them unchanged.

Could we have set PORTD = 0? Sure. Since we're only using the one pin in D, it would turn it off just fine. But it would have the side-effect of turning off _all_ the pins on port D, and it wouldn't have provided such a nice example. The bit-masking techniques ( &= ~_BV() and its opposite, |= _BV() ) are pretty useful to learn for chip-level programming.

The last part of the code, return(0), never gets reached. The while(1) command ensures that the chip is always going to be stuck in the while loop. I just included the return() command because the compiler complained when I didn't include it -- the main C function in a program is always supposed to have a return value, even if the chip will never get there.

That's a lot of programming for one day. Take some time to admire the simple beauties of the flashing LED.

Step 11: The End & Web Resources

So there you go -- your first AVR application.

I'll post up some more tutorials and code showing off different aspects of the ATtiny2313. At least one for the Analog/Digital converter, some on Timers, and one for using its built-in serial interface.

In the meantime, if you want to learn more about the AVR chips, here's some good web resources:

AVR Freaks is the motherlode: A community of friendly users with a forum.

Cornell's EE476 class webpage is a tremendous source for info, and their final projects are a treasure-trove of crazy, cool project ideas, all well-documented.

Psychogenic.com is good for AVR & Linux.

The AVR-libC demo program is not a bad one to learn from either, but maybe a bit advanced if you're just beginning with microprocessors or C.

Just found Building a USB Sensor Interface which has good instructions, and heads in the direction of USB connectivity.

Dec 2 Update: If you run Linux with KDE and want an integrated GUI environment, have a look at KontrollerLab. It's very similar-looking to AVRs AVR Studio for Windows.

Jan 2, 2007: AVR Tutor has a short getting-started-with-AVR tutorial written in C. AVR Beginners has a very complete tutorial, but it's based on assembly language instead of C, so I'd consider it an advanced tutorial. You might consider looking at them in order.

The photo is of me using the ghetto dev kit to run a 2400 baud wireless transmitter. Takes only a couple minutes to run a few wires to the breadboard, and off you go!
1-40 of 369Next »
can you please put a schmatic
I _could_. But then you'd only know how to hook up one particular chip out of the whole AVR family. What you really need to know is that the pin on the programmer labelled MOSI connects to the pin on the chip labelled MOSI, etc. And then that works for _every_ chip.

But I'll see if I can whip up something helpful and graphic.






s005002 years ago
Can you please post a makefile for mac?
crjeea3 years ago
seeing thoughs LED's with resistors soldered to them I was just thinking wouldn't it be lovely if they were sold like that (:
An LED with presoldered 1K resistor... or even a resistor built in to the LED it's self... extra just an idea
Eirinn crjeea3 years ago
Well... depending on the voltage you supply to the LED you'll need different resistors. That's why they don't come integrated with a resistor :)
crjeea Eirinn3 years ago
Maybe a kind of zena LED might be a more useful idea.
I have seen LED's that can flash, think I would also like to see LED's that have pwm built in based on voltage so they can be dimmed with only two pins. Or maybe tri colour LED's that cycle through thier colours according to voltage.
kbchiew3 years ago
Thank you. Abit confusing here.
In the picture above, you shown PIN1= Reset.
In the listing, PIN1= SCK. PIN11 = Reset.
twenglish15 years ago
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0x12 != 0x00
avrdude: verification error; content mismatch

avrdude done.  Thank you.

has anyone else had this problem?

See the registry update I posted in the comments...
Thanks for the help, but I am now using a USB programmer. I haven't had any problems yet
The Real Elliot (author)  twenglish14 years ago
I should make up an Instructable on what all the errors mean...

Verification errors mean that the chip didn't get the data right.  Check for loose connections here or there.

Also double-check the power supply that you're using for the chip.  The chips draw power in a spikey, unpredictable way, and that sometimes can cause trouble.  A simple solution that often works for me is to put a buffer capacitor across the AVR's power pins.  That is, take a capacitor of moderate size (0.1uF to 10uF?) and connect the positive end of the cap to the AVR's VCC pin and the negative end of the cap to the AVR's GND pin.

The capacitor acts as a little rechargeable battery, smoothing out the AVR's power demands. 

(Real engineers almost always start out with a buffer capacitor in their designs for reliability.  I omitted one here b/c I'm cheap and I can usually get away with it.  But you should try adding it if things go wrong.)

thanks, i will try that, but its weird the power supply i am using was working fine before? any idea why all of a sudden?
i tried adding the capacitor and i am still getting the same error, and all the connections are good
 Erase the chip before programming... Should do it.
If you use the parallel programmer under Windows XP be sure to use the below registry hack to prevent windows from interruping your programming signals.

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Parport\Parameters]
"DisableWarmPoll"=dword:00000001

Paste the above into Notepad and saveas dapa.reg
then right click MERGE intos registy. Will save you lots of problems with using parallel ports under windows with AVRDUDE for instance.
please define your pin outs in the pictures and define better in board thks
The Real Elliot (author)  fostersfriend3 years ago
How do you mean "better"? Is there something specific I can write that'd help?
Just label all the out puts on all the pictures
robot13983 years ago
can you tell how did you connect it to the 8 pin socket
jld13 years ago
I've downloaded the WinAVR just to try to play with C/C++. I don't have any uC right now but I plan to. I wrote a simple program using Programer's Notepad in C. I can't seem to compile the program? Please advise.

thanks
Joseph
matstermind3 years ago
would you happen to have a schematic of parallel port pins to microcontroller? i find your current explanation a it confusing.
shanakaplus3 years ago
um looking for elevator avr program. Do you have example program ?
lpciprian3 years ago
OMG, I tried to make this for two weeks. I finally did it! I must say that my first mistake was that I didn't use an external source for Vcc. Second: I used a too long paraller cable. After I used a power source from one of my USB's and cutting my dapa cable to about 18 cm long (it was about 1,2 meters) I finally got a succesfull firmware flashing on my attiny2313...whew! Thank you The Real Elliot for your instructable!
What does this mean?

"A Direct AVR Parallel Access, or DAPA cable, is an incredibly simple and cheap programming method. You can build one very quickly for a few bucks worth of parts, but the convenience comes with a few gotchas. The first is that you must have a parallel port on your computer; something that modern laptop and some modern desktops don’t have. But if you’ve got an old PC around that has one this will get you up and programming in no time.

In fact, the first AVR prototyping I did was with one of these cables. That is, until I discovered another gotcha. This will only program low-speed chips. If you try to run the chip’s clock at full speed (by changing fuse settings… more in Part 3) you won’t be able to use a DAPA cable to talk to it any longer. There’s also the possibility of damaging your parallel port or worse if you do something wrong. But if you want to go for it anyway, here’s how I built mine"

I got this info from here: http://hackaday.com/2010/10/25/avr-programming-02-the-hardware/
Does this mean that if i modify the speed of the internal oscillator at the first programming, the programmer won't program the chip anymore?
beehard444 years ago
i head you can burn a bootup code on an ATmega and use it as a standalone arduino
The Real Elliot (author)  beehard444 years ago
Very true. An Arduino is really just an AVR with some of the pins dedicated and a bootloader and some timing routines pre-configured for you in the firmware.

Only some of the AVR chips have bootloader capability, though, so you should check the datasheet to make sure. I know the Mega88 does, for instance, but the M48 doesn't.

The Tiny2313 does bootload, but you'll want to use a small bootloader like this one: http://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_type=project&item_id=625
It's pretty hard to fry them. I bet the chip is fine. Anyway, I learnt much from your essay. And I think that some IC components can be find on ChinaICMart.
Funnymonk4 years ago
Hello I think I've followed all of your instructions to the letter but there is no Make prgram button in tools for me. How do I fix this?
bha1234 years ago
 guys where can i find these micro controller and ics actually i am IT student but for my project i need them can any body suggest where can i get them

Ebay...
is this parralell connector work for picaxe to connect it with pc
cdousley4 years ago
will this program attiny13
The Real Elliot (author)  cdousley4 years ago
Yup.  You just need to hook up the six programming lines to the right pins.  See the datasheet for where to route them.

www.atmel.com/dyn/resources/prod_documents/2535s.pdf

Thanks for the help all i need now is the ic socket
elbriga4 years ago
HI!

I think I just 'fried' 2 attiny2313 :(

The only thing not quite equal from your instructable is that my led has the resistor (330r) on the + pin of the led, so i connected the ground pin of the led to ground and the other + resistor to PD4, then the avrdude stoped working, not recognizing the chip anymore...

What could have 'fried' my AVR????
The Real Elliot (author)  elbriga4 years ago
It's pretty hard to fry them.  I bet the chip is fine.

Generally, it shouldn't matter whether the resistor is on the + or - side of the LED -- it limits the total current flow just fine in either location.  Something else is going on...

Double-check your wiring?  Do you have all the programming lines going to the right places?  

Another possibility is that you've got flakey power supplied to the chip.  The solution is to buffer the power supply.  That is, place a 1uF - 10uF capacitor across the positcheap trickive and negative power rails, or even better across the Tiny's VCC and GND pins.  This will smooth out voltage spikes in the power line that might be accidentally resetting your chip.

suyog144 years ago
thnks for detail description but I hav to interface it wid  PIC18F4550.
Can u please fwd ckt dig+c/hex file on suyog.mahajan14@gmail.com?
Yakumo4 years ago
thanks for this Instructable, haven't done all of it but it was a great help making my own cradle :)

@brooklynlord
while programming getting 5V from the USB-Port is the most direct approach so I think you are talking about the running project.
well i only used atmega 168 till now but I think other chips work pretty much the same. You can power the chip with lower than 5V the only disadvantage is you can't run it at the same speed (less MHz). look at the datasheet of your processor.
bha1234 years ago
 sorry to specify in banglore ,india where can i find these components
cdousley bha1234 years ago
allelectronics.com
ebay
1-40 of 369Next »