This tutorial describes how to use the Arduino environment with a standalone AVR ATMega8.

It will help you understand how to use the Arduino as an AVR ISP (In-System Programmer), and  how to use the Arduino IDE to write code for a standalone AVR microchip.

To use the Arduino board as an AVR ISP, first you have to have an Ardino Board, in this tutorial I used the duemilanove, if you don't have an Arduino board, you can still use the same technique but, you will have to have a programmer to program the chip.



Open the Arduino IDE, and just burn it to the board, and you're done, your Arduino is now an ISP.



Now get you r ATMega, and connect it to the board as in the picture.



This picture have the ATMega168, but it has the same pin arrangement as the ATMega8, for more info check the Arduino tutorial here.

the pins are as follows:

Arduino pin 13 to pin 19 (SCK)

Arduino pin 14 to pin 18 (MISO)

Arduino pin 15 to pin 17 (MOSI)

Arduino pin 16 to pin 1 (Reset)

and you can take power from the Arduino using the 5V and GND pins.

Now you can use the avrdude to program the ATMega8, here is a sample command for the AVRDude:

avrdude.exe -b 19200 -p m8 -P com1 -c arduino -U flash:w:atmega8.hex

I have set the baud rate to "19200", and the com port to "com1", and the file name "atmega8.hex", change this depending on your own values.

Now let's go to the Arduino Environment, and see how to make work for us.

First we need to make it compile to our chip, so we need to change the "boards.txt" file in this manner:

The "boards.txt" file is located in the arduino folder under "hardware\arduino"

add this to the end of the file

############################################################## Custom ATMega8



I had my ATMega8 fuse set that it uses the internal clock at 8Mhz, so in this case, set the correct value of the "f_cpu" to ""

Open the Arduino IDE, and in the Tools Menu select the board we have just created "Arduino Custom ATmega8".


Now write the code below, it is a very simple code that reads from the potentiometer on analogue pin 0, which reads a range from 0 to 1023, converts it to the PWM range which is from 0 to 255, and outputs the value to digital output pin 9.

The code for the Arduino IDE

int potpin = 0;
int val;


void setup()  {
pinMode(9, OUTPUT);

void loop()  {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);

We need to compile this code to get our hex file to write to the ATMega, so press the "Verify" button while holding the "Shift" button, this makes the Arduino IDE writes the output to the bottom window.

Now you can copy the file location and navigate to it, and use the AVRDude to write it to the ATMega.

One final thing to notice is the pins on the chip are not the same as the pins on the board.

Here is the pin map, again it is for the ATMega168, which is similar to the ATMega8, and the ATMega328, all are the same pin order, except for the ATMega 168 has more memory, 16K, and the ATMega 328 has 32K, and also more PWM pins.


So now our analogue input pin 0 in the Arduino is pin 23, and the digital pin 9 in the Arduino is pin 15.

Finally, we connect the breadboard, the potentiometer to pin 23, and the MOSFET, or an LED to pin 15, and here we go, a simple PWM controller that we can use to fade an LED, or drive a high power DC motor, like in my case I even used another separate 12 V. power supply to drive the motor, with both the grounds connected together.

The MOSFET have 3 pins, from left to right they are called Gate, Drain, and Source.

Connect the Gate to pin 15, Drain to the Motor, and the source to the negative pole of the battery because I'm using N-Channel MOSFET, and also connect the source to the ground, and finally, connect the motor to the positive pole of the battery.

It is very important to notice that the MOSFET gets really hot, so be careful not to touch it, and use a heat-sink with it.

One more thing, the Arduino analogWrite function generates PWM signal that has a frequency of approximately 490 Hz, so this makes audible noise, the motor acts as a speaker, so it makes some noise that can be heard, to overcome this you, should make a function that will run on 20 kHz, or more.