First contact with ATmega8 microcontroller - part 1
Thursday, October 25, 2007 3:00:00 PM
This and the following posts are an attempt to document my first contact with this microcontroller and to describe all needed hardware and software so you can start using a microcontroller too.
Go to: part 1, part 2, part 2.1 (video), part 3, part 4 (video).
What is a microcontroller?
A microcontroller is a simplified CPU, plus some amount of RAM, plus some amount of (re)programmable ROM, plus some I/O ports (including some analog I/O ports), and all of this in a single small chip.
Microcontrollers are used at many electronic devices, including microwave ovens and washing machines. They are simple and useful enough to be used in many DIY (do it yourself) projects. They usually can run at a clock rate of a few MHz.
What is ATmega8?
ATmega8 is a microcontroller from Atmel AVR family. Like (all?) other microcontrollers from that family, it has an 8-bit RISC CPU core. Both ATmega8 and ATmega16 are very popular and inexpensive microcontrollers.
The very popular Arduino project uses ATmega16, but has an ATmega8 version. Make Magazine often features some projects using AVR microcontrollers (1) (2) (3) (4).
Atmel company provides some software tools to work with AVR microcontrollers, but there are also enough free software tools for that.
Why ATmega8 and not ATtiny or some other model?
There are many AVR models. ATtiny, in special, tend to be smaller and to have smaller memory and fewer I/O pins. I really don't know too much about them (or any other AVR microcontroller), but there is one big reason why I've choosen ATmega8: Cerne-Tec (the place where I bought the microcontroller)
only has two AVR models: ATmega8 and ATmega16.
Why ATmega8 and not ATmega16?
There are two main reasons why I've choosen ATmega8 over ATmega16: it's cheaper and it's the microcontroller used in the project I want to make sometime soon.
Before you continue...
I don't have much experience with electronics (yet), so please don't think everything here is 100% correct (actually, if something is wrong, please tell me!). If you are not careful enough, you may damage the microcontroller or even the USB port or the parallel port from your computer.
I use Gentoo/Linux system on x86 arch. I don't have access to Windows, Mac or BSD computers, so I don't know exactly what you must do on those systems.
This post is intended to be a very quick introduction to ATmega8 microcontroller. This post (and probably future posts about the same subject) can be viewed as crash course about ATmega8.
In this post I'm going to assume you have some very basic electronics background: you know what are DC, Vcc and GND; you can recognize a resistor, a capacitor, a LED and other components; you know how to solder (video and PDF) and how to use a multimeter (video and PDF). I'm also going to assume you can use your computer, whatever the system is, but I'm going to focus on Linux. Finally, I'm going to assume you can understand C programming, Makefile and assembly.
Objectives
The main objectives in this post are:
- to make the microcontroller work
- to make it work using my Linux (without the need of Windows or any proprietary software)
- the total cost of components must be low (the project must be as cheap as possible)
- it must be simple
What you need
Hardware
Update at 2011-08-10: This list is obsolete. It has been left here for historical purposes.
- 1x ATmega8
- 1x 28-pin socket (optional)
- 1x USB-B connector (optional)
- 1x DB-25 male connector (plus case)
- 1x 220 ohms resistor
- 3x 330 ohms resistors
- about 1 meter of cable (with 5-wires or more)
- a pair of male/female connectors with 5 pins or more
- 1x breadboard (AKA protoboard)
- many short wires
- some LEDs and resistors
- basic electronic tools (1) (2): soldering iron, solder, wire cutter, wire stripper (with practice, a pair of scissors can be used to strip wires), pliers, helping hands (1) (2)...
For the final version of any project, consider either making a PCB or using a stripboard. The project from this post, however, is just for learning the basics and, thus, won't generate anything useful at the end (except the knowledge acquired).
Software
- A compiler: avr-gcc or tavrasm
- A programmer: avrdude (old homepage) or uisp (or maybe PonyProg, but I haven't tried it)
- ppdev Linux kernel module (Device Drivers -> Character devices -> Support for user-space parallel port device drivers) (Update at 2011-08-10: This is not needed if you don't use a parallel port programmer.)
- giveio (direct download) (only for Windows, I don't know how this works nor how to install it)
Installing them on Gentoo:
emerge -av crossdev tavrasm avrdude uisp crossdev -t avrIf you use a parallel port programmer, run modprobe ppdev and add your user (your ordinary user, not the root) to the lp group, or instead change the permissions of /dev/parport0.
Total cost
I already had the breadboard, some resistors, some leds and some wires. The other components I bought at Cerne-Tec (I went to their physical office in Rio de Janeiro). The prices here are in Brazillian Reais (R$). Today, US$ 1.00 = R$ 1.78.
Update at 2011-08-10: This shopping list was meant for building a parallel port programmer. If you don't build one, then this list has no use for you. It has been left here for historical purposes.
- R$ 9.90 - 1x ATmega8
- R$ 1.00 - 1x 28-pin socket
- R$ 4.00 - 1x USB-B connector
- R$ 4.50 - 1x DB-25 male connector (plus case)
- R$ 0.20 - 1x 220 ohms resistor
- R$ 0.60 - 3x 330 ohms resistors
- R$ 2.00 - about 1 meter of cable (with 5-wires or more)
- R$ 1.50 - a pair of male/female connectors with 5 pins or more
- R$ 23.70 - Subtotal (US$ 13.30)
- R$ 4.60 - 2x subway tickets for transportation
- R$ 28.30 - Total (US$ 15.90)
The ATmega16 was priced at R$ 24.90, which is a bit more than the total cost of all these components. There was also a large stripboard available for sale at around R$ 20.00 ~ R$ 25.00, which is almost the price of all these componentes together. Since I don't want to double this project cost, I'm using the breadboard I already have for now.
Note: There is no guarantee the prices will be the same at the time you go buy your components. I'm not affiliated with Cerne-Tec. The prices listed here are just for illustration.
Update at 2007-10-26: I found a small stripboard (about the exact size I need, maybe just a bit larger) for sale at R$ 2.50 at an electronic store near my house. I haven't bought it yet, but I'm probably going to.
Update at 2007-12-29: The USB-B connector turned out to not be needed for these experiments and was substituted by a simple USB cable (priced at R$ 2.79, or US$ 1.57). However, I know I'm going to use it sometime in future, in other project. (Update at 2011-08-10: It turns out I've never used that USB-B connector, and I'm not even sure if I will ever use it.)
Overall plan of this project
Since I wanted to learn how to use the ATmega8, I wanted to make a simple "Hello, world" program and put it on the microcontroller. Well, my microcontroller does not have a terminal, a screen or an LCD to print something, so the "Hello, world" is going to be just some blinking LEDs. Also I need something to write the program into the microcontroller, so I need to build such tool.
So, these are the steps to be followed in this project:
- Build a simple, cheap and working parallel port AVR Programmer (part 2)
- Build and/or buy a simple, cheap and working USB port AVR Programmer (part 2.1) (added at 2011-08-10)
- Write a simple "Hello, world" for the microcontroller (part 3)
- Compile the "Hello, world" (part 3)
- Mount the microcontroller on the breadboard (part 4)
- Use the AVR Programmer to write the "Hello, world" to the microcontroller (part 4)
- Be happy and watch the blinking LEDs! (part 4)
See you at the next part!
Update at 2008-02-02: I've uploaded a video of this project at YouTube. Everybody loves videos, so check it out!
Update at 2011-08-10: Parallel ports are from the past, and nobody uses them anymore. Thus I've revisited this series of posts and added part 2.1, that shows how I built USBasp. It also has a video.
Update at 2011-08-11: The source-code for this "blinking LEDs" firmware (see part 3) is now available at: https://bitbucket.org/denilsonsa/atmega8-blinking-leds
Go to: part 1, part 2, part 2.1 (video), part 3 (part 4, video).








Anonymous # Monday, July 7, 2008 5:23:27 PM
Anonymous # Thursday, March 19, 2009 8:07:30 PM
Anonymous # Monday, June 7, 2010 3:53:10 PM
Anonymous # Sunday, November 21, 2010 5:19:22 PM
Mad Scientistqlue # Friday, December 17, 2010 12:35:10 PM
I'm about to design and build a control circuit for a laser engraver. I chose the Arduino Uno as my starting point.
Just waiting for delivery. (half the order came on Wednesday. Yesterday was a public holiday here, so I expect the rest on Monday or Tuesday.
Denilson Figueiredo de SáCrazyTerabyte # Friday, December 17, 2010 12:47:56 PM
In other words, basically Arduino is easier than my posts.
Mad Scientistqlue # Saturday, December 18, 2010 7:44:57 AM
These posts will help me to understand better though. I will be tinkering at the low level later on.
I chose Arduino because of it's built in usb interface, a vital component for my project.
I was originally planning on using the Mbed platform. But since they were not in stock with my supplier, I chose the Arduino instead. It was also cheaper, and the software is available in the Ubuntu repositories, rather than 'in the cloud' as with Mbed.
Anonymous # Saturday, October 1, 2011 3:44:52 PM