/ / Parts
- A breadboard
- wire
- 7805 Voltage regulator 2 LEDs
- 2 220 Ohm resistors
- 1 10k Ohm resistor
- 2 10 uF capacitors
- 16 MHz clock crystal
- 2 22 pF capacitors
- small momentary normally open ("off") button
- USBTiny
/ / Building the circuit
- Add power and ground wires at the bottom of your board connecting each rail.

- Add the 7805 power regulator. The regulator is a TO-220 package where the IN-line from the external power supply goes IN on the left, ground is in the middle and the 5V OUT is on the right (when facing the front of the regulator).
Also, add a 10uF capacitor between the IN of the regulator and the ground as well as a 10uF capacitor on the right rail between power and ground. The silver strip on the capacitor signifies the ground leg.
- Add an LED across from the voltage regulator. An LED attached to power like this is a great troubleshooting trick. You'll always know when your board is being powered as well as quickly know if your board is being shorted.

- The red wire is for the POWER and the black wire is for the GROUND. Be sure to only attach a power supply that is between 7-16V. Any lower and you won't get 5V out of your regulator. Any higher and your regulator may get too hot. A 9V battery, 9V DC power supply, or 12V DC power supply is good.

- Before moving on, look at this image to see what each of the pins on your Atmega chip does in relation to the Arduino's functions.

- Add your socket across the center of the breadboard
- Insert the chip. Make a note of the notch so you know where the top is.
- Add a 10k ohm resistor "up" (to power) on the RESET pin in order to prevent the chip from resetting itself during normal operation. The RESET pin reboots the chip when pulled down to ground.
- Make the following connections:
- Pin 7 - Vcc - Digital Supply Voltage
- Pin 8 - GND
-
Pin 22 - GND
- Pin 20 - AVcc - Suppply voltage for the ADC converter. Needs to be connected to power if ADC isn't being used and to power via a low-pass filter if it is (a low pass filter is a circuit that cleans out noise from the power source, we aren't using one)
- Pin 7 - Vcc - Digital Supply Voltage
- Add the 16 MHz external clock between pin 9 and 10, and add two 22 pF capacitors running to ground on each of those pins.
The crystal is the heartbeat of the microcontroller.
- Add the tactile switch so you can reset the Arduino whenever you'd like and prepare the chip for uploading a new program. A quick momentary press of this switch will reset the chip when needed.
Add the switch just above the top of the Atmega chip crossing the gap in the breadboard. Then, add a wire from the bottom left leg of the switch to the RESET pin of the Atmega chip and a wire from the top left leg of the switch to ground.
- The blink_led program that comes with Arduino blinks pin 13. Pin 13 on the Arduino is NOT the AVR ATMEGA8-16PU/ATMEGA168-16PU pin 13. It is actually pin 19 on the Atmega chip. Look at the pin mapping.
Add an LED. The long leg or the anode connects to the red wire and the short leg or the cathode connects to the 220 ohm resistor going to ground.
/ / Making An Arduino
- If you haven't added male headers to your AVR programming adapter, do it now. This adapter breaks out the 6 pins from the programmer to 6 inline pins for easy attachment to the breadboard. All the pins are also labeled making it very easy to connect it up to your chip.

- You will burn the bootloader with the USBTiny programmer. You need to figure out the pins:

- Be sure to refer to the Arduino pin mapping for help wiring this up.
The MISO pin of your adapter will go to pin 18 or Arduino digital pin 12 of your Atmega chip.
The SCK pin of your adapter will go to pin 19 or Arduino digital pin 13 of your Atmega chip.
The RESET pin of your adapter will go to pin 1 of your Atmega chip.
The MOSI pin of your adapter will go to pin 17 or Arduino digital pin 11 of your Atmega chip.

- Disconnect the led plugged into pin 19
- Plug in a USB cable to your USB breakout board and plug the 6-pin plug of your AVR programmer to your AVR programming adapter. The black nub of the 6-pin head must be facing upwards towards the Atmega chip.

- Open Arduino. Select the chip and then select Burn Bootloader>w. USBTinyISP
- Remove the AVR breakout board from the breadboard.
- With an FTDI cable, connect RX to TX and TX to RX, GND to GND and 5V to power
- Upload the blink program or another program.
/ / Pin Mapping


/ / Programming an AVR
Your microcontroller doesn't do anything yet- you need to load a program before it's useful. We'll start with the hardware equivalent of Hello World - a blinking light.
Look at the pin map.

Arduino pin 13 is PB5 on the ATmega328 - part of port B. To use this pin, port B must first be set to be an output pin. There are various ways to do this
- writing to Port B Data Direction Register at address 0x24
- the lazy/better way, using the DDRB macro.
- Open Eclipse

- Create a new project

- Select Empty Project and give your project a name:

- Configuring Project Settings
- Select your project then click on Project>Properties
- Select Target Hardware under AVR. Select your chip and speed
- Select AVRDude under AVR
- Select Edit or New for Programmer and select the USBTinyISP from the list. Name the configuration

- Open the fuse calculator:http://www.engbedded.com/fusecalc/
Select the device and match these properties:
- Int. RC 0sc. 8Mhz; Start-up time PWRDWN/RESET: 6CK/14 CK++65ms

-
Brown out detection=2.7v

- Check Serial program downloading enabled
-
Set Boot Flash section size to =1024
- Int. RC 0sc. 8Mhz; Start-up time PWRDWN/RESET: 6CK/14 CK++65ms
- Get the fuses from the bottom of the page:
And enter the numbers in the Fuses section
- Select your project then click on Project>Properties
- Create a new C file and give it a name with the c extension

- Paste the following into the file:
This code tells all of Port B to become outputs by writing 0xFF (binary 1111 1111) to DDRB which is the data direction register for port B. Each bit is mapped to a port B pin - a '1' written means that pin should be an output. A '0' means the pin is an input. So you're making all of Port B outputs.
#include <avr/io.h> #include <util/delay.h> int main(void){ // Set Port B pins as all outputs DDRB = 0xff; // Set all Port B pins as HIGH PORTB = 0xff; return 1; }
The next statement is similar in that you're sending 0xFF to PORTB - again, each bit is mapped to a Port B pin. So writing 0xFF sets all of Port B as high (logical '1'). - Add a while loop:
while(1){ } - Place the code that sets the pins high inside the while loop:
// Set all Port B pins as HIGH PORTB = 0xff;
- Add a delay after setting the pins high:
_delay_ms(500);
- Set the pins low and add another delay
- Click on Project>Build Project
- If you haven't added male headers to your AVR programming adapter, do it now. This adapter breaks out the 6 pins from the programmer to 6 inline pins for easy attachment to the breadboard. All the pins are also labeled making it very easy to connect it up to your chip.

- Be sure to refer to the Arduino pin mapping for help wiring this up.
The MISO pin of your adapter will go to pin 18 of your Atmega chip.
The SCK pin of your adapter will go to pin 19.
The RESET pin of your adapter will go to pin 1.
The MOSI pin of your adapter will go to pin 17.
Also connect GND and 5V - Upload the program to your microcontroller
Source: Copyright © 2008,2009 Chris Kuethe
http://imakeprojects.com/Projects/avr-tutorial/
http://imakeprojects.com/Projects/avr-tutorial/