Developing in C for the ATmega328P: Edit and Build

4 minute read

Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P) and make the connection to all software development.


Sometimes it helps to understand the context as to how or why to perform a task. For example, understanding a plant’s need for water and nutrition can help one be a better gardener.

In this case, I want to provide context as to the process of creating code for an embedded microcontroller. I also want to demonstrate that understanding this process will also help someone understand the required concepts for most, if not all software development.

The Arduino IDE

We use the Arduino Uno as the hardware platform for developing C programs on embedded microcontrollers. The Uno is a phenomenal tool for this in that it is easily available, relatively inexpensive, and there is tremendous amount of information available on it and the microcontroller it uses, the AVR ATmega328P.

That said, we specifically do NOT use the Arduino IDE. Why is that? We don’t for the following reasons:

  • It uses C++ and we want to specifically use only the C language
  • Its code editor is light-weight and specific to the Arduino
  • It hides the process of building programs

I do advise installing the IDE as it is a time-tested method of confirming you have a known, good connection between your Uno and your PC. It is typically easier to debug that connection using the IDE than the command line tools. And, it can also be used as “check” to confirm, “yes, my board is working”".

And in fairness, you are able to specify external editor’s and you can specify “verbose” in the build process, however, the top issue remains. And I want to help you understand software development, which using the IDE, doesn’t do well.

The Five Steps

There are five specific steps which the IDE enables and any one must take, in order to develop software for the Uno:

  1. Editing the code
  2. Compiling the code
  3. Linking the code
  4. Locating (sometimes referred to as loading)
  5. Uploading the code to the Uno

The middle three steps compile/link/locate are typically called the build process, which can simplify the five steps to three:

  1. Edit
  2. Build
  3. Upload

I find it best to keep the three steps separate, as it is in these three steps, where most errors occur. For a detailed and very worthwhile description of these steps, I highly recommend this page, Compiling, Link and Locating: Barr Group.

1. Editing

A very good code editor can help prevent errors in the remaining steps of the process, by syntactically highlighting the code. For example, in the screen shot below, you can see that “digitalWrit” is in a different color on line 14 vs. line 16. Both lines need to be quite similar and the color difference indicates I’m referencing a different function. [Note: The function digitalWrit doesn’t exist and will cause an error when I attempt to link the code in step 3.]

Wrong function name, digitalWrit

Wrong function name, digitalWrit

Large Version to see detail

An even better code editor will provide the probable function name which I’m attempting to use, as Sublime Text does with the tabnine plug-in. See image. At this point, I hit return and it fills in the proper function.

Suggested function name, digitalWrite

Suggested function name, digitalWrite

Large Version to see detail

I use Sublime Text 4 (ST4) as I like it, its fast, and it has a large number of plug-ins to enhance its behavior. I’ve also used it for over 10 years, so I’m comfortable in it. I use it to edit Python, Forth, C, Julia, and markdown text (as in this website).

The last point is the most important point, as a software developer, I need a tool which I am able to use on different languages and in different situations. Its valuable to me to use the same tool for editing this website, for developing Python scripts to image recognition and for developing code for embedded systems. I am also able to use ST4 on all three platforms, Linux, macOS and Windows.

I don’t use nor advocate using an IDE such as Visual Studio or Visual Studio Code. I attempted to use VSC to develop for the Uno, several years ago, however, I spent more time configuring it than made it worthwhile to use. MicroChip also has an IDE for the AVR series of microcontrollers and I only use it to reprogram the ATmega328P (using the IPE, not the IDE).

There are other code editors:

  • Windows: Notepad++ - free, and very good for Windows
  • All: Vim - steep learning curve, very powerful
  • All: bluefish I haven’t tried this one, however, it appears quite capable
  • All: Atom - sun-setted by GitHub, not recommended
  • All?: zed I’m hopeful that zed continues to evolve, as we need a robust community of code editors.

One last note on ST4, yes, it costs $99. That said, I’m in it 20-30 hours per week, 52 weeks a year (for a 3 year subscription). So yeah, I’ll say its worth it.

To finish on editing, I always have my editor window open while I’m developing code. It covers the left half of the screen, while my terminal program covers the top right quarter of the screen and a serial program covers the bottom right quarter. This covers 95% of my development needs.

Comments powered by Talkyard.