Developing in C for the ATmega328P: Raspberry Pi and VS Code Setup Part 2
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:
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:
- File Explorer - quickly open files
- Search - find all uses of a variable, value etc
- Git - shows file changes per Git
- Run/Debug - not used
- Extensions - add/delete/show extensions install/to install
- Remotes - quickly open your remote(s)
- Remote Status - green bar shows if you are connected
- Editor - edit files
- Terminal Window - the same as your CLI window
- Status Bar - look here for problems and warnings VERY IMPORTANT
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
- Open VS Code
- Press Ctrl/Cmd-Shift-p to pull up the Tools menu
- Type “SSH”, to bring up Remote-SSH: Connect to Host
- Enter your hostname as in
hostname.local - A new window will open with SSH: hostname in the Status Bar in bottom left-hand corner of the window
- It will also ask to open a folder, click on or enter “AVR_C”
- In the Explorer Window, click on examples to expand it.
- Click on blink to expand it.
- Click on main.c to open it in the Editor
- 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.)
- 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:
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.
Comments powered by Talkyard.