Developing in C for the ATmega328P: Raspberry Pi and VS Code Setup Part 2

6 minute read

Where I demonstrate how to use VS Code on your PC to develop code on your Raspberry Pi for the Arduino Uno (ATmega328P).

Introduction

Now that you have the best setup for developing code for the Arduino Uno, how do you use it?

Know

Good to Know

Two programs

You will be using two programs on your PC to interact with the Raspberry Pi, VS Code and your terminal program (Windows Terminal, macOS Terminal/iTerm/Warp) also known as your CLI. The former is your coding environment and the latter provides administrative capabilities along with some applications like a serial monitor (tio).

The best way to use them is to set them up side-by-side on your PC screen, so that each one covers the vertical half of the screen. This allows you to quickly go from one to another, without losing focus.

Your screen needs to look like this:

VS Code and the Terminal Program on One Screen

VS Code and the Terminal Program on One Screen

Large Version to see detail

Its common to be able to do the same task in both applications. For example, you can make flash, which is the process of compiling and uploading code to the Uno, in both VS Code and the CLI. There isn’t a preferred way to do it, use which version comes naturally to you.

There are also extensions to VS Code which provide a capability already provided in the CLI. For example, you could use the Microsoft Serial Monitor extension instead of tio. I have found tio to be vastly superior to the extension. In this case, I recommend using the CLI approach instead of VS Code.

VS Code

This application has a lot of icons and indicators. Its very helpful, to understand them as they can quickly point out problems. Follow the numbers in the image below:

  1. File Explorer - quickly open files
  2. Search - find all uses of a variable, value etc
  3. Git - shows file changes per Git
  4. Run/Debug - not used
  5. Extensions - add/delete/show extensions install/to install
  6. Remotes - quickly open your remote(s)
  7. Remote Status - green bar shows if you are connected
  8. Editor - edit files
  9. Terminal Window - the same as your CLI window
  10. Status Bar - look here for problems and warnings VERY IMPORTANT
VS Code Annotated

VS Code Annotated

Large Version to see detail

The best place to start is #10, the Status Line. I’ve found it invaluable in understanding when I have an error in any aspect of VS Code. If something doesn’t appear to be working, look here first!

To the left of it, is the Remote Connection status(7). Look here to confirm you are connected to the Raspberry Pi.

The Terminal (9) window isn’t as valuable as your CLI window. I recommend depending on your terminal application as compared to this window. This window is also where specific issues will appear, such as compilation errors or upload errors when building code. So it is important to look for errors in this window.

The Editor (8) is self-explanatory. It is where you will edit code or VS Code configurations. Most of your work will be performed in this window, along with a quick Ctrl/Cmd-Shift-b to build and upload your application to the Uno.

The remaining aspects of VS Code will be noted as we need to use them.

Terminal (CLI)

The CLI is a much more simple window. It’s power resides in the commands which you use in the window. For example, shown in the image above is tree which provides a quick, graphical image of a folder.

This is where the book, Learning the Command Line becomes so important. I recommend reading this book along-side of your computer and attempting the commands as they are introduced. In no time, you will begin to understand why so many programmers love and do everything in the CLI!

At a minimum you will want to be proficient in the following:

Description Command/key Comments
Change directories cd change folders
Present working directory pwd What folder are you in?
List contents of directory ls Show what is in the folder
List folder in tree format tree Show what is in the folder
Open serial monitor tio [ acm | usb ] Pick one of the two options based on your controller board serial port
Previous command Up arrow display previous command
Expand current text to a folder name Tab Type first few letters then hit Tab

VS Code First Program

As was done in the previous CLI example, the first program to compile/link/upload will be blink. To do so, follow the following steps:

(For these steps, there will be key combinations to press. The key Ctrl/Cmd, means on Windows, press Ctrl and on macOS, press Cmd.)

Steps

  1. Open VS Code
  2. Press Ctrl/Cmd-Shift-p to pull up the Tools menu
  3. Type “SSH”, to bring up Remote-SSH: Connect to Host
  4. Enter your hostname as in hostname.local
  5. A new window will open with SSH: hostname in the Status Bar in bottom left-hand corner of the window
  6. It will also ask to open a folder, click on or enter “AVR_C”
  7. In the Explorer Window, click on examples to expand it.
  8. Click on blink to expand it.
  9. Click on main.c to open it in the Editor
  10. Press Ctrl/Cmd-Shift-b then Enter to accept the default build process, this will automatically compile, link and upload the hex file to the Uno.

The last step, prior to pressing return, needs to look like this:

VS Code AVR_C build task

VS Code AVR_C build task

Large Version to see detail

If you have problems

Make sure you use the Frequently Found Errors page.

VS Code Success

Success is having your Uno blink about once per second. To confirm success, change the value on line 10 to either be much smaller or much larger. Then Ctrl/Cmd-Shift-b again.

Do you see a significant difference in the speed of the blinking?

CLI Make Commands

You are able to perform the same build tasks in the CLI as you do in VS Code. The key is to now the specific commands. As is typical in many Linux programs, you are able to enter a command and either –help or help to receive guidance as to how to use the command.

In this case, I’ve added help to the make command, such that you may enter make help and it wil tell you the possible commands to use. Enter make help in your CLI:

make help
make compile - compile only (Arduino verify)
make flash - show program size and flash to board (Arduino upload)
make clean - delete all non-source files in folder
make complete - delete all .o files in folder & Library then verbose flash, for complete rebuild/upload
make verbose - make flash with more programming information for debugging upload
make env - print active env.make variables
make help - print this message

Try the commands above and observe the results in the command line. Depending what you are doing, sometimes its faster to type a make command in the CLI than it is to swtich to VS Code, make sure you are in the right file and use the build command.

Next: Raspberry Pi and VS Code Setup Part 3

Comments powered by Talkyard.