Digital I/O Project on AVR® Xplained 328PB


This hands-on project steps through a simple example: controlling an LED from a switch on the Xplained board. Controlling a digital I/O pin can be a rewarding project for anyone new to the AVR® Microcontroller. Successfully completing this project confirms that you have completed the software build, the hardware setup, and were able to program the microcontroller successfully. The Studio 7 debugger is introduced at the end to show how to control a circuit through Studio 7 control and how to view the internal registers within the device.

The ATmega328PB Xplained Mini evaluation kit is a hardware platform for evaluating the ATmega328PB microcontroller. A fully integrated programmer/debugger is included on the board. This provides seamless integration with Atmel Studio 7.


Hardware Tools (Optional)

Tool About Purchase
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Windows Linux Mac OSX
Atmel® Studio
Integrated Development Environment

Additional Files



The project controls an onboard LED using the push-button switch that is also on the board. The LED turns ON when the switch is pressed, default state (or unpressed switch) is LED OFF.


Task 1 - Project Creation

  • Open Atmel Studio 7 and select File > New > Project from the main menu.
  • Select GCC C Executable Project and give it the name Project1 and then choose a location to save the project on your computer. Click on OK.
  • The Device Selection window will appear. In the search bar, enter 328P and then, select the device Atmega328PB and click OK.


Task 2 - Main.c

The Main.c program controls the I/O pin through bit settings in the I/O control registers.

  • The main.c file is where the application code is added. The project has a main.c file already created but it only contains a while(1) statement. Modify main.c by entering the lines to match the program below:
int main(void)
    DDRB |= 1<<DDRB5;    // Direction of pin PB5 set as Output
while (1)
    if (PINB & (1<<PINB7))    //read PIN PB7
        PORTB &= ~(1<<PORTB5);    // PB7 is low so LED On
        PORTB |= 1<<PORTB5;    // By default PB7 is high, LED Off


Task 3 - Build and Program Project

  • Select Build > Build Solution from the top menu of Studio 7 to compile the code. You should see a "BUILD SUCCEEDED" message in the output window of Studio 7.


Task 3 - Program Explained Board

The Xplained Board is now ready to be programmed.

  • Connect the Xplained to your computer using the included USB cable.
  • In the Solution Explorer, right-click on the project name and select Properties.
  • Under Tool, select mEDBG and debugWire as the interface.
  • Select Debug > Start Without Debugging. The project will build and the program will be loaded into the Xplained board.

Note: You may see a Firmware Upgrade Message. If you see this message, select Upgrade and when the progress bar is complete, select Close. Restart by selecting Debug > Start Without Debugging.



Task 5 - DWEN Fuse

If the DWEN fuse is not enabled an error message is displayed. Click Yes and the Atmel Studio 7 will use the programming connection to set the fuse.



Task 6 - Program Ready

Wait until Studio 7 completes the programming and the status message says "Ready" at the lower left corner.



Task 7 - Execute

Press the switch on the Xplained board and the LED should light up. Release and the LED will shut off.



Task 8 - Debugging

Debugging a device is a useful way of determining how a program is running.

  • Select Debug > Start Debugging and Break. The project will build and the program will be loaded into the Xplained board.
  • The I/O View window will open up showing the various peripherals.
  • Click on the PORTB selection to open the I/O view for PORTB.
  • Select Debug > Continue to run the program on the Xplained board. Pressing the switch will light the LED the same as before debug.
  • Press Debug > Break All to stop the program.
  • The LED can now be controlled by the I/O View. Click on the PB5 LED (blue box in the picture). When the block is solid (Pin Status 1) the LED will light. When the block is hollow (Pin Status 0) the LED will be off.


Task 9 - Breakpoints

  • Click on the margin for the command line below the else statement to enable a breakpoint.
  • Select Debug > Continue and the application will run on the Xplained board.
  • Press the switch on the Xplained board and the debugger will stop at the breakpoint because the code detects that the switch was pressed.
  • Release the switch and select Debug > Continue again and the board will be reset and start running, waiting for you to press the switch.


Task 10 - Disable debugWIRE and Close

Once the project is complete, the debugWire fuse needs to be reset to program the Xplained board in the future. While still running in debug mode, select Debug > Disable debugWire and Close. This will release the debugWire fuse.


This simple project introduced how to control I/O on the AVR and also showed the basic steps for:

  • How to create a project in Studio 7
  • How to modify main.c
  • How to build and program a device
  • How to start a debug session
  • How to monitor port registers
  • How to use a breakpoint
  • How to disable debugWire mode
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.