ATMega328 Blink Example

In this article, we will show how to program an ATMega328 in Amtel Studio and how to load the software to the microcontroller via a programmer (AVRISP). As a simple example, we will control an LED on the PIN 15 (PORTB1). This requires an LED and one resistor of 220 ohms. The basic information about the microcontroller can be found here.

ATMega328 Wiring shematics

In this example, the ATMega328 is used on a breadboard. In addition to the 5V power supply, the programmer is also connected via ISP. In addition, we also use an external clock with 16 MHz. Pin 15 (PORTB1) is connected to the anode of the LED and the cathode is connected to ground with a 220 ohm resistor. When you programming the microcontroller for the first time, make sure that the bootloader is already burned.

Amtel Studio: Create a project

If the hardware is completely connected, you can start with the development of the software. Therefore create a new porject in Amtel Studio. Choose project type "GCC C Executable Project" and define a name for your project. After clicking on "OK", a new window opens and ask you to select the correct microcontroller (ATMega328P or ATMega328). Then the necessary project files are generated. In the C-file (main.c) is already a part of the code, which can now be extended so that the LED flashes. The full code looks like this:

Code description

First, the clock frequency must be defined (#define F_CPU 16000000UL). This corresponds to the frequency of the oscillator which in this example is 16MHz (16000000Hz). In the other two lines, two libraries must be integrated into the project. IIn the main program, pin 15 (PORTB 1) is defined as the first digital output. Then the rest of the code in the while loop is executed permanently. PORTB1 switches now on, then the controller wait one second before PORTB1 goes off again. Before PORTB1 is switched on again, the controller wait one second.

Upload and Test

When the code is complete, you can start the project under "DEBUG". But first you have to define the connected Programmer, before you can continue your operation .During this process, the programmer flashes briefly and then the LED should start flashing. Amtel Studio tells you, when the build was successful.

About The Author

Alex, the founder of AEQ-WEB. He works for more of 10 years with different computers, microcontroller and semiconductors. In addition to hardware projects, he also develops websites, apps and software for computers.