First contact with ATmega8 microcontroller - part 1

Tags: ATmega8, AVR, Gentoo, Hardware, Linux, Microcontroller

Last week I bought an ATmega8 microcontroller. I plan to use it to build an arcade USB Joystick (Wayback Machine), but first I need to understand how it works and how to use it.

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), source-code.

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 in many electronic devices, including microwave ovens and washing machines. They are simple enough, chep enough 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 (such as SpokePOV, The Brain Machine and Do you know how to use and ARRR-duino yet?).

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 (Wayback Machine). ATtiny, in special, tend to be smaller and to have less 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 build (Wayback Machine) 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 and how to use a multimeter. 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.

Want to learn more about basic electronics skills?

Objectives

The main objectives in this post are:

  • to make the microcontroller work
  • to make it work using my Linux system (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

Fortunately, all objectives were met.

What you need

Hardware

Update on 2011-08-10: This list is obsolete. It has been left here for historical reasons.

  • 1 × ATmega8
  • 1 × 28-pin socket (optional)
  • 1 × USB-B connector (optional)
  • 1 × DB-25 male connector (plus case)
  • 1 × 220 Ω resistor
  • 3 × 330 Ω resistors
  • about 1 meter of cable (with 5 wires or more)
  • a pair of male/female connectors with 5 pins or more
  • 1 × breadboard (AKA protoboard)
  • many short wires
  • some LEDs and resistors
  • basic electronic tools: 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 knowledge).

Software

Installing them on Gentoo:

emerge -av crossdev tavrasm avrdude uisp
crossdev -t avr

If 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 on 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 reasons.

  • R$ 9.90 - 1 × ATmega8
  • R$ 1.00 - 1 × 28-pin socket
  • R$ 4.00 - 1 × USB-B connector
  • R$ 4.50 - 1 × DB-25 male connector (plus case)
  • R$ 0.20 - 1 × 220 Ω resistor
  • R$ 0.60 - 3 × 330 Ω 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 - 2 × 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 on 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 electronics store near my house. I haven’t bought it yet, but I’m probably going to.

Update on 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 on 2011-08-10: It turns out I’ve never used this USB-B connector, and I don’t think I will ever use it.)

Update on 2014-06-12: If you live in São Paulo, or if you want to order online, you can try buying AVR microcontrollers at Mult Comercial.

Overall plan of this project

Since I wanted to learn how to use 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 in this project:

  1. Build a simple, cheap and working parallel port AVR Programmer (part 2)
  2. Build and/or buy a simple, cheap and working USB port AVR Programmer (part 2.1) (added at 2011-08-10)
  3. Write a simple “Hello, world” for the microcontroller (part 3)
  4. Compile the “Hello, world” (part 3)
  5. Mount the microcontroller on the breadboard (part 4)
  6. Use the AVR Programmer to write the “Hello, world” to the microcontroller (part 4)
  7. Be happy and watch the blinking LEDs! (part 4)

See you at the next part!

Update on 2008-02-02: I’ve uploaded a video of this project at YouTube. Everybody loves videos, so check it out!

Update on 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 on 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), source-code.

comments powered by Disqus