Skip to content

Write and run applications on the PYNQ board

In this tutorial you will learn how to write and run an application on the PYNQ board.

We demonstrate how to write and run a general C program that doesn't use the PYNQ board's special features such as LEDs, buttons, etc. We will show how to do this using Visual Studio Code, MobaXTerm, and ssh. In essence, this is like programming on your laptop or any other computer.

The basic steps are:

  1. connect the PYNQ board and log in (using Visual Studio, MobaxTerm, or ssh).
  2. create and edit a file (the C program)
  3. compile the C program to create an executable
  4. run the executable
  5. fix any errors in your program ("debugging")
  6. copy your program to your laptop, for example to submit your homework.

The PYNQ board runs the Linux operating system. In Steps 3-4 we will use the terminal, which allows you to give commands for the Linux operating system. We will introduce the Linux commands that we need as we go along; an overview is given on the introduction to Linux page.

Step 1. Connect to the PYNQ board

  1. Connect all cables and switch on the PYNQ board..
  2. Start Visual Studio Code, MobaXterm, or ssh and log in to the PYNQ board.
  3. Open a terminal on the PYNQ board.

Step 2. Write & run a C program

2.1 Using Visual Studio Code menus

  1. Create a new file called hello.c in the home directory (/home/student). (Left-click in the Explorer pane to bring up the menu.)
    hello hello

    Click "Install" if you get the pop-up
    C/C++ extension
    You can close the tab when the extension has installed. 2. Type in the program, then open the Terminal tab and type the commands

    gcc hello.c -o hello
    ./hello
    

hello

2.2 Using the terminal in Visual Studio Code (or MobaXterm or ssh)

  1. Edit a new file called hello.c in the PYNQ home directory /home/student. When using ssh you have to edit using a Linux editor such as nano (or vi if you have Linux experience).
  2. Type in the following (or copy and paste):
    #include <stdio.h>
    int main(void)
    {
      printf("hello\n");
    }
    
  3. Save the program (it depends on your editor how to do this).
  4. The Linux ls command lists all the files in the directory. Run ls in the terminal: hello.c should be listed. (The other files may or may not be there; that depends on the SD-card image).

    ℹ️ Information See the Linux page for an overview of commands such as ls.

  5. Type cat hello.c or more hello.c to show the program on the terminal. Write and run hello.c

  6. Compile the C program to create an executable by typing gcc hello.c -o hello in the terminal.
  7. If you get an error, then edit your C program and double check that it is correct.
  8. Type ls again to check that the executable (called hello) is in the directory.
  9. Execute the program by typing ./hello. The output should be Hello on the PYNQ!).

3. Keep programming

  1. Congratulations! You've written and run your first program.
  2. You can download the C program to your computer, for example if you need to submit it as homework, by right-clicking on hello.c in the Explorer:
    Write and run hello.c
  3. You can keep programming!
  4. When you have finished you must safely shut down the the PYNQ board.

4. Using the PYNQ board hardware (input/output)

See the libpynq for C programming page for how to install and use the libpynq library to access the PYNQ board's input/output.

5. Shut down and disconnect the PYNQ board

Always safely shut down and disconnect your PYNQ board to avoid corruption of the SD card and data loss.