This tutorial continues on from ATmega8 breadboard circuit Part 1 and ATmega8 breadboard circuit Part 2. So far we've built a power supply, added the microcontroller, added some plumbing to make it work and added the ISP interface, but it really doesn't do anything. The next step is to add some I/O devices and upload some firmware.

atmega8 breadboard circuit

A lot of tutorials will use a "Hello World" program which consists of an LED that blinks at 1Hz. For this tutorial we will build on this and have an LED that blinks 3 times when a button is pressed. The first thing we need to do is add the LED and button to the breadboard. The images below shows the schematic and the components on the breadboard.

atmega8 switch and LED atmega8 on breadboard with switch and LED

The PC5 pin will be “pulled up” and when the button is pressed, this will ground the pin and initiate the blinking of the LED. The LED is connected to PC4 via a 150 ohm resistor. I calculated the resistor size using the LED Resistor Calculator on

To write the firmware, we will be using WinAVR. This is a collection of tools that includes AVR-GCC compiler, AVR-LibC, AVRdude and more.

WinAVR has a utility called “mfile” which makes it easier to create a makefile. From the “Makefile” menu, select the following options then save the file in a folder for this project.

  • MCU Type: ATmega8
  • Port: usb

Next we want to edit the makefile and change the following entries

  1. F_CPU = 1000000

We need to modify the AVRDUDE_PROGRAMMER entry because the mfile utility does not have “usbasp” in the programmers list. This of course assumes that you are using a USBASP Programmer. If you are using another programmer, you will need to modify this section of the makefile with values that are suitable for that programmer.

The F_CPU entry specifies the microcontroller clock speed and is used by the _delay_ms function. The ATmega8 out of the box runs on a 1MHz internal clock, but this can be changed if required.

Using programmers notepad (Another tool bundled with WinAVR), create main.c with the following content.

  2. #include <avr/io.h>
  3. #include <util/delay.h>
  6. //Define functions
  7. //======================
  8. void ioinit(void);
  9. void led_on(void);
  10. void led_off(void);
  11. //======================
  13. int main (void)
  14. {
  15.     ioinit(); //Setup IO pins and defaults
  17.    while (1)
  18.    {
  19.       if (bit_is_clear(PINC, 5))
  20.       {
  21.          for (int i=0;i<3;i++)
  22.          {
  23.             if (i>0)
  24.                _delay_ms(500);
  25.             led_on();
  26.             _delay_ms(500);
  27.             led_off();
  28.          }
  29.       }
  30.    }
  31. }
  34. void ioinit (void)
  35. {
  36.     DDRC  = 0b11011111; //1 = output, 0 = input
  37.    PORTC = 0b00100000; //Enable pin 5 internal pullup
  38. }
  40. void led_on(void)
  41. {
  42.    PORTC |= _BV(PC4);
  43. }
  45. void led_off(void)
  46. {
  47.    PORTC &amp;= ~_BV(PC4);
  48. }

To compile the firmware

  • Open a command prompt
  • Make sure you are in the folder containing the makefile and main.c
  • type “make” and press enter
compiling the firmware

To upload you firmware, first connect the to the USB port and breadboard circuit as shown in the photo below.

Connecting a USBasp programmer

In the command prompt enter the following command then press enter

  • make program
writing the compiled firmware to the atmega8 microcontroller

The system should now be ready for testing. Connect it up to your power source, press the button and watch the LED blink 3 times.