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?
Links for Reading
Know
- Learning the Command Line
- Editing Remotely Using VS Code
- Developing in C on the AVR ATmega328P: Frequently Asked Questions (FAQ)
- Developing in C on the AVR ATmega328P: Frequently Found Errors (FFE)
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:
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 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
- 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 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:
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.
Comments powered by Talkyard.