1. Prerequisites
Visual Studio Code is a source-code editor, and as such it requires a desktop environment to draw its full potential. For that, we will have to make sure our OS image has a desktop environment. One way of checking is to connect the PLC to a PC screen using an HDMI cable.
If we are sure the image on our PLC does not have a desktop environment, we will have to burn a new OS image on the SD card. Careful! Doing so will remove all data stored in it, so we might want to do a backup of the important files and store them somewhere else for later retrieval.
Our personal choice is starting with the "Raspberry Pi OS (Legacy, 32-bit)" found on the official Raspberry Pi imager, and build the PLC image in it. For further reading on how to do that, check the following blog:
How to build a Raspberry PLC image from a Raspberry Pi OS
2. Install VScode
Installing Visual Studio Code on a Debian based OS is as easy as it gets. Simply type the following command on the terminal:
~$ sudo apt-get install code
From then on, Visual Studio Code will be accessible from the Raspberry Pi desktop.
However, we can't program our PLC yet. We will need to download the appropriate libraries.
3. Install libraries
For our PLC to work, we need some libraries to make it manageable for us to code in it. None of the following libraries have been designed specifically for Visual Studio Code, since it is just a text editor and doesn't require special add-ons.
The single mandatory library is the librpiplc for the C language. This will allow us to code at least in C. Follow the steps on the following link to install it:
https://github.com/Industrial-Shields/librpiplc
If we want to code using Python, we will need to install both librpiplc and python3-librpiplc. This will allow us to compile python code for Raspberry Pi PLCs on any text editor of our choice, which also includes VSCode. Follow the steps on the following link to install it:
https://github.com/Industrial-Shields/python3-librpiplc
If everything has been installed correctly, we can now start using Visual Studio Code to program the PLCs.
4. Usage Example
We will try the following python code, to ensure both installed libraries are working correctly.
After saving the file, we can open a terminal directly from VS Code in the following tab:
Using the terminal as a regular windowed one, we can execute files as you would usually do.
Using Visual Studio Code on Raspberry PLC