Getting started with AVR || ATtiny85
This is start of new series of tutorials on the AVR family MCUs. I will start with the famous ATtiny85 MCU and later if necessary, I will cover some 32 bit AVR MCUs.
This particular tutorial will cover the basic things you need to know before writing your first code. We will see how to blink the LED at the end of this tutorial.
I will also cover how to setup the USB ASP AVR programmer with the microchip studio. This is not an officially supported programmer by the AVR MCUs, but since its very cheap compared to others, I will use this one.
As I mentioned I will use the ATtiny85 to get started with AVR. It is a 8 bit MCU, which means that all the registers in it are 8 bit wide. The pinout for the MCU is shown below
The board only has one port (PORTB) with only 6 pins available for the interface. You can see above the same pin can be used for different purposes depending on what peripheral you are using.
For example, the pin PB0 can be used as the I/O pin, or a MOSI for the SPI, or SDA for the I2C, etc.
We will cover all these functionalities for these pins in the upcoming tutorials. Today we will only see how to use the pin as the output and how to control its logic (High or Low).
Create your first Project
We will use the microchip studio available at https://www.microchip.com/en-us/tools-resources/develop/microchip-studio#
After installing the IDE, click File -> New -> Project
The click on XC8 C Application Project. Give the name to the project, check the project location and click OK.
Next in the device selection window, search for the MCU. The select it and click OK.

Below is the final project window.
If you build the pre generated code, the output window should show 0 errors.
Configure the USB ASP
As I mentioned in the beginning, I am using the USB ASP to program the AVR MCU. This programmer is not officially supported, but we can still configure it to be used with the microchip studio.
The instructions for the same can be found at https://www.instructables.com/Programming-Microcontrollers-With-a-USBasp-Program/
Step1
Download the Zadig software for windows from https://zadig.akeo.ie/
This is used to install the libUSB drivers for the device.
Open the software, click on “list all devices“.
Select USBasp and install the libusbK (v3.1.0.0) driver (or whatever version available).
I have already installed the driver that is why it is showing the reinstall option.
Step2
Download the avrdude from https://github.com/mariusgreuel/avrdude/releases. At the time of writing this, the version 7.1 is the latest.
extract the files into a directory but keep in mind where you are extracting them.
I have extracted them in the Documents -> Atmel Studio -> avrdude folder.
Step3
Open the IDE, go to tools and click external tools
A new window will pop up where we have to add the configuration for the programmer. It is shown below

- First give some name to this tool
- In the command window, give the path of the avrdude.exe file. In my case, it is C:\Users\admin\Documents\Atmel Studio\avrdude\avrdude.exe
- In the argument window copy the following:- -c usbasp -p t85 -Uflash:w:”$(ProjectDir)Debug\$(TargetName).hex”:i
- Make sure to check the “use output window” and “prompt for arguments“.
Click apply and the tool will be saved.
Connection
The USB ASP uses the SPI to program the flash memory of the ATtiny85. The connection between the two is shown below
The Attiny85 board I am using has a LED connected to the pin PB1. So I don’t need to connect any external LED for this tutorial.
The Code
In order to blink the LED, we first need to set the pin as the output pin. Then continuously set the pin as HIGH and LOW after a certain delay.
Configure the direction
The Direction of the pin can be configured in the DDRB register.
In order to set the pin PB1 as output, we need to write a 1 to the DDB1 bit of the register DDRB.
// set PB1 to be output
DDRB = 1<<DDB1;
Set the pin HIGH/LOW
The pin can be driven HIGH/LOW using the PORTB Register.

To set the pin PB1 HIGH, we write a 1 to the 1st position in the Port B Register. Similarly to pull the pin PB1 low, write a 0 to the 1st position.
// set PB1 high
PORTB |= 1<<PB1;
// set PB1 low
PORTB &= ~(1<<PB1);
Here I am using the bitwise OR (|) and bitwise AND (&) operators. You can learn more about them https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
Using these operators will make changes in the pin PB1 only. The rest of the pins of the PORTB will remain unaffected. If instead I use PORTB = 1<<PB1
, It will simply set the PB1 to HIGH and rest of the pins of this port will reset to LOW. This is not something we want here. We only want to make changes for the pin PB1 without affecting the other pins.
The final code
Below is the final version of the main file.
#include <xc.h>
#define F_CPU 1000000 // 1MHz CPU clock
#include <util/delay.h>
int main(void)
{
// set PB1 to be output
DDRB = 1<<DDB1;
while(1)
{
// set PB1 high
PORTB |= 1<<PB1;
_delay_ms(1000);
// set PB1 low
PORTB &= ~(1<<PB1);
_delay_ms(1000);
}
}
- The
xc.h
header file includes the necessary files needed to program using the IDE. - The F_CPU is the frequency at which the system clock is running. By default the ATtiny85 runs at 1MHz.
- I have included the
delay.h
header file, so to use the delay functions for us and ms delays. - In the main function, we will first set the pin PB1 as the output.
- Then in the while loop, set the Pin PB1 HIGH and LOW with a delay of 1000 ms.
Build and Flash
Before we can load the code to our controller, we need to compile it to check for any errors. Click on the build button to compile the project.
After clicking the build button, the output windows shows the output of the compilation. In th picture above, the project is successfully build with program memory size of 128 bytes.
Once the build is successful, we will load it to the controller.
Go to Tools-> usbASP (The name you provided for the tool). A new window will pop up, just click OK.
The code will flash to the board and you can see the success in the output window.
RESULT
Below is the gif showing the LED blinking every 1 second.