Using Visual Studio Code on Raspberry PLC

Set up Visual Studio Code on a Raspberry Pi PLC
April 5, 2024 by
Using Visual Studio Code on Raspberry PLC
Boot & Work Corp. S.L., Francesc Nosas

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.

from rpiplc_lib import rpiplc


def digital_read_write():

    rpiplc.pin_mode("I0.0",rpiplc.INPUT)

    read_value=rpiplc.digital_read("I0.0")

    print("The I0.0 is reading: {}".format(read_value))


    rpiplc.pin_mode("Q0.0",rpiplc.OUTPUT)

    rpiplc.digital_write("Q0.0",rpiplc.HIGH)

    rpiplc.delay(1000)

    rpiplc.digital_write("Q0.0",rpiplc.LOW)

    rpiplc.delay(1000)


def analog_read_write():

    rpiplc.pin_mode("I0.2",rpiplc.INPUT)

    read_value=rpiplc.analog_read("I0.2") # 0 - 2047

    print("The I0.2 is reading: {}".format(read_value))


    rpiplc.pin_mode("A0.0",rpiplc.OUTPUT) 

    rpiplc.analog_write("A0.0",1024) # 2.5v Output

    rpiplc.delay(2000)

    rpiplc.analog_write("A0.0",4095) # 10v Output

    rpiplc.delay(2000)

    rpiplc.analog_write("A0.0",0)


def relay_test():

    rpiplc.pin_mode("R0.1",rpiplc.OUTPUT)

    rpiplc.digital_write("R0.1",rpiplc.HIGH)

    rpiplc.delay(1000)

    rpiplc.digital_write("R0.1",rpiplc.LOW)

    rpiplc.delay(1000)


def main():

    rpiplc.init("RPIPLC_57R")


    while True:

        digital_read_write()

        analog_read_write()

        relay_test()


if __name__ == "__main__":

    main()
    


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.


​Search in our Blog

Using Visual Studio Code on Raspberry PLC
Boot & Work Corp. S.L., Francesc Nosas April 5, 2024
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>