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?

One program for both

You will be using VS Code on your computer (Mac or Windows) to interact with the Raspberry Pi. It provides both the code editor as well as the terminal to perform administrative exercises along with CLI applications like a serial monitor (tio).

The best way to use VS Code is to set up the editor and terminal 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 with editor and terminal
VS Code with editor and terminal

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

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 is your CLI window. In this image, it is at the bottom of the screen, I prefer it to be on the right-hand side. You can right click on the word, Terminal in the terminal window, then Panel Position -> Right. 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.

This is where the book, Conquer the command line can be 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 (or directories)
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 Detailed WalkThrough

As was done in the previous CLI example, the first program to compile/link/upload will be blink. This program will blink the on-board LED at a particular rate. This explanation will walk through how to do this in detail:

(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

Some of these steps will refer to information you gathered in the previous page

  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 and click on Flash to start the default build process, this will automatically compile, link and upload the hex file to the Uno. (This is the same as Upload on the Ardiuno IDE.)
  11. If you don’t see the output of the process, make sure you are viewing the terminal window with a checkmark, as indicated by the magenta arrow in the image.

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

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 5 times per second. To confirm success, change the value on line 10 to either be much larger (2000). 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.

First, make sure you are in a window indicated by a cube with a blue side. If you hover over the cube, it will show bash in the top line of the tool tip, indicating your are in a bash shell.

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 switch to the code editor, just 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.