How to Program a Microcontroller

Emet Amil
6 min readFeb 2, 2023

--

Moving beyond the Arduino

Setup of an ATMEGA168 at my desk

A big issue I had when trying to move away from the Arduino was a lack of clear tutorials on where to go next. After some research and a lot of debugging efforts, I finally figured out the best microcontrollers to start with, best IDE to learn, and the best microcontroller programmer to use. But this was a journey through Quora pages, sketchy websites, and many many tutorials. Now that I know what to use and how to use it, I want to share it with you so that hopefully, it will be easier to learn. Here, I will show you how to program a microcontroller from scratch using an ATMEGA168, USBasp, and Eclipse IDE. Let’s get started!

What you will need:

My main concern when getting started was that I didn’t want to use a platform that was infrequently used in industry. But I also didn’t want to use a combination of hardware and software that provided no tutorials. So I ended up choosing the following:

  1. Microcontroller (MCU): ATMEGA168 20U (A cheap but highly capable AVR MCU — similar to the one on an Arduino) — https://www.mouser.com/ProductDetail/556-ATMEGA168-20PU
  2. Software: Eclipse IDE (Can be used cross-platform and some Indeed embedded engineer job offerings like to see applicants with experience with Eclipse IDE’s) [Download instructions later in the tutorial]
  3. A computer: (Windows or Mac, but since I own a Mac, some parts of this tutorial may be Mac specific)
  4. A microcontroller programmer: USBasp (technically I used a knockoff version from Amazon — but most tutorials seemed to use that one) — https://www.amazon.com/dp/B00AX4WQ00?psc=1&ref=ppx_yo2ov_dt_b_product_details
  5. Standard electronics equipment: Wires, breadboard, voltmeter, usb plug adapter to your computer, LED, 220 ohm resistor

1. Download Eclipse IDE and AVR Dude

Go to https://www.eclipse.org/downloads/packages/ and download “Eclipse IDE for C/C++ Developers”. Next, install AVR-GCC and AVR Dude from https://www.obdev.at/products/crosspack/index.html for Mac or https://winavr.sourceforge.net/download.html for Windows.

AVR Dude is a utility used to upload code to your MCU and Eclipse just provides an IDE so you won’t have to work on your terminal all the time.

To connect the two entities together, Eclipse provides a plug-in that you can access by going to Help->Install New Software. Paste http://avr-eclipse.sourceforge.net/updatesite/ into the “Work with” box and click next until you reach a final Finish button.

Connecting Eclipse with AVR Dude

2. Connect the USBasp and the ATMEGA168

There should be a grey multi-port connector that comes with the USBasp. Connect the multi-port. There is a pink side of the multi-wire which should connect both to the VCC on the USBasp and the VCC of the output end.

An easy way to line it up is shown below.

Showing which ports on the multi-wire line up with the USBasp

You can see that the right side of the end of the multi-wire has a part that juts out. This side should face the USB end of the USBasp when lining the ports up.

Now connect the VCC, MOSI, SCK, RST, MISO, and GND to the corresponding ports on the ATMEGA168 (Ports 7, 18, 19, 1, 17, and 8 respectively). The dataset can be found here: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2545-8-bit-AVR-Microcontroller-ATmega48-88-168_Datasheet.pdf

It’s worth noting that the RST and SCK ports only correspond to the ones on the right side.

A connection schematic is shown below:

Connection schematic between USBasp and ATMEGA168

3. Reduce the clock speed of USBasp

The USBasp frequently has an issue with programming new MCU’s and typically the issue is due to the high clock speed. You can reduce the clock speed through shorting the board or you can do it programmatically. Disclaimer though — for me, changing the clock speed programmatically did not work. Connect your USBasp to your computer to power it on first whether you are following Option 1 or 2.

Option 1: Shorting the JP3 port on the board

Short the JP3 ports by adding a wire between the two ports as shown below. This allows the programmer to allow for slower clock speed, which means it will readily program a wider array of MCUs.

Shorting the JP3 port

Option 2: Send a terminal command to AVR Dude

If you are skeptical about the first method (I was also), you can also programmatically reduce the speed of the programmer by using the -B command on the terminal. The number after -B signifies the what times you want it to be slowed by. Here is the command for reducing the speed by 5 times:

avrdude -p m168 -c usbasp -P usb -B 5  -t

4. Make a project in Eclipse

Connect your USBasp to your computer via USB. Then make a project in Eclipse by clicking on the C folder icon and clicking on ‘C Project’.

Making a new C project

Give your project a name. Since I’ll be showing you how to make an LED blink here, I’ll call it ‘atmega168_blink3’ (because I already used 1 and 2 lol). Go under ‘AVR Cross Target Application’ and choose Empty Project with the AVR-GCC Toolchain.

Naming the C project

Create a new source file called main.c

Creating the main.c file

Then add the following code for a blink program. I borrowed it from Harry King on Youtube (https://www.youtube.com/watch?v=yhhGPLo3uTI):

#include <avr/io.h>
#include <util/delay.h>

int main() {

DDRB = (1 << PB0);

for (;;) {
PORTB ^= (1<<PB0);
_delay_ms(1000);
}

}

5. Set up AVR Dude to work with USBasp

Right click on your project and click on properties. Click on AVRDude. Under Programmer Configuration, click on edit. Choose any ‘usb clone with correct VID/PID’ and hit okay.

Configuring the programmer

Then choose the right microcontroller and MCU Clock frequency for your purpose. Here, I chose the ATMEGA168 and 8 MHz.

Setting the target hardware

6. Connect an LED to your microcontroller

Connect the anode (+) of your LED (longer leg) to PB0. In this case, it would be Pin 14. Connect your cathode to any available row on your breadboard. Then, use a 200–300 ohm resistor (it could change depending on what your optimal LED current is) to connect your cathode leg to the ground of your MCU. Here it is Pin 8 for the ATMEGA168. My setup is shown below.

Final setup for blink program

7. Upload the project!

Build by clicking on the hammer and choosing ‘Release’.

Building the project

Then upload the project by clicking on AVR and the green down arrow.

Toolbar showing the option to upload to the Atmel target

And enjoy the results of your hard work! You have now not only made an LED blink but also gained the skills and setup to program a large variety of microcontrollers, reduced the potential size of your inventions, and minimized the cost of future product production.

It blinks!

Final notes

I know this is a long tutorial and it is very likely you may have encountered a bug along the way. Please let me know in the comments if you have any issues and I’ll try my best to provide you the resources or suggestions for what to try next.

Also, if this article helped in any way, please smash the 👏 button! Please also comment if you have further advice for others or need clarification on any steps.

--

--

Emet Amil

I currently do biology research. I also want to write helpful articles on cool subjects.