Developing in C for the ATmega328P: Setup Introduction

4 minute read

Where I describe the steps required to setup the Standard C toolchain for the ATmega328P on Windows, Linux, or macOS using a Linux-based approach.

Command Line Required

Executing the tool chain and performing this work, will require you to work in your operating system’s command line interface. If you aren’t familar with the command line, it need not be a hindrance, there are only a few commands you will need to execute. I recommend reviewing these command line tutorials before you start. Practice a little bit prior to performing the commands below.

As a guide, here are the commands that I believe you will need to know and understand:

# present working directory - find out where you are 
$ pwd
/Users/lkoepsel/Sites/wellys.com
# list - find out what is in the current directory
$ ls
data        deploy.sh   layouts     public      resources   static      themes
# make directory - create a new directory
$ mkdir temp
# change directory - in this case go "up" one level
$ cd ..
$ pwd
/Users/lkoepsel/Sites

Introduction

Setting up the tool chain for C for the AVR family of processors isn’t difficult, if you are using a Linux-based operating system or one that is quite similar to Linux, i.e; Unix-based, macOS or Windows Subsystem for Linux (WSL). With this in mind, this tutorial will use the following approach for installing the tool chain required for developing in Standard C for the Arduino Uno (AVR ATmega328P):

  • Linux - use apt as the package manager and use the command line interface (Console)
  • macOS - install homebrew, a Linux-like package manager and use the command line interface (Terminal)
  • Windows 10 using WSL - install Windows Subsystem for Linux (WSL1) and use WSL as the command line interface. This approach is almost identical to Linux, above.
  • Windows 10/11 using a third party download - install a special tool chain packaged designed for Windows

To test the tool chain is working, I’ll use a simple program from Freedom Embedded. The program is basic and doesn’t require any other additional libraries (outside of the standard C library, avr-libc) The goal is exactly the same as that of the Arduino, demonstrate one can get the LED to blink.

Once, its confirmed the tool chain is working, I’ll use a Makefile from Elliot William’s book (GitHub site) (the book is recommended if this topic is interesting to you.) which works extremely well. You might have to make slight changes for your specific setup, however, the Makefile offers an easy and complete automation tool.

In summary the steps will be:

  1. Install the tool chain.
  2. Test the tool chain using specific commands for each step.
  3. Automate using a Makefile, so that the compile/link/load step is one command.

Before You Start, Install the Arduino IDE

If it isn’t already installed, go ahead and install the Arduino IDE, as we’ll want to use it to confirm or ensure the serial ports are working properly with the Arduino Uno. We will not be using it to develop code, however, its great for confirming your physical hardware is working and you might want to use the Serial Monitor.

CHECK AND CONFIRM

Let’s use the Arduino Uno and the Arduino IDE to confirm the specific port we will be using as well as ensure we have a good physical connection between our Windows system and the Arduino.

Connect your Arduino Uno to your computer and open the Arduino IDE. Go to Tools -> Port -> ???(Arduino Uno) to confirm the port for the Uno. In Windows, it was COM3, in macOS, it was /dev/cu.usbmodem3101 and in Linux, it was /dev/ttyACM0. Write down this port name as you will need it for the step later using the averdude command.

Go to File -> Examples -> 01. Basics -> Blink to open the Blink sketch. Click on Upload and ensure the file is uploaded to your Uno and the built-in LED is blinking. If everything is working, close the Arduino IDE. Note: The IDE must be closed for the command line application avrdude to access the port. If you have connections issues with avrdude connecting, confirm the IDE is not running.

Specific Operating System Tutorials

Note: The images used to demonstrate are from the WSL installation, there will be minor differences between operating systems, however the main text will be the same.

Comments powered by Talkyard.