keepcalculator

06 Numbers and Powers

Pascal Programming on Raspberry Pi: Setup & Code

Card No. 06·20643 · Updated · Standard formula, shown below

Writing Pascal programs on a Raspberry Pi is straightforward: install the Free Pascal compiler with sudo apt install fpc, write your code in any text editor, and compile with fpc program.pas. This gives you a native, fast, and type-safe language that runs directly on the Pi’s ARM processor, ideal for learning programming or building lightweight utilities.

Why Pascal on Raspberry Pi?

Pascal is often overlooked in favor of Python or C on the Raspberry Pi, but it has distinct advantages. The language enforces structured programming and strong typing, which helps beginners avoid common errors. Free Pascal (FPC) is a mature, open-source compiler that supports multiple dialects, including Object Pascal, making it suitable for both simple scripts and complex applications. The Raspberry Pi’s Linux-based operating system (Raspberry Pi OS) includes FPC in its official repositories, so installation is trivial. Additionally, Pascal compiles to native machine code, so programs run fast and do not require an interpreter or virtual machine.

Installing Free Pascal on Raspberry Pi OS

The easiest method is using the package manager. Open a terminal and run:

sudo apt update
sudo apt install fpc

This installs the compiler, linker, and standard libraries. To verify, type fpc -i which shows the version and target CPU. If you need the latest version, you can download the ARM Linux tarball from the official Free Pascal website, but the repository version is usually stable and sufficient for most projects. For cross-compiling to other architectures (like x86), you would need additional packages, but native compilation is the norm.

Writing and Compiling Your First Program

Use any text editor—nano, vim, or a graphical one like Geany. Create a file named hello.pas with:

program Hello;
begin
  writeln('Hello, Raspberry Pi!');
end.

Compile it with:

fpc hello.pas

This produces an executable named hello in the same directory. Run it with ./hello. The compiler also generates a .o object file and a .ppu unit file, which you can ignore. If you want to keep the source directory clean, use the -o flag to specify an output path, or compile in a separate build directory.

Common Pitfalls and How to Avoid Them

  • Missing semicolons before end or else—Pascal is strict about this. Always place a semicolon after the last statement before an end except when followed by else.
  • Case sensitivity—Pascal is case-insensitive, but variables must be declared before use. Forgetting to declare a variable results in a compile error.
  • Using the wrong unit—For basic I/O, you need writeln which is in the System unit (automatically used). For math functions like sin or sqrt, you must add uses Math;.
  • Running the executable without ./—Unlike Windows, Linux does not search the current directory by default. Always run ./program.

Using Lazarus IDE for GUI Development

If you prefer a visual development environment, install Lazarus, a free Pascal IDE similar to Delphi. Run:

sudo apt install lazarus

Lazarus provides a form designer, code editor, and integrated debugger. It is heavier than a text editor but excellent for building graphical applications with buttons, menus, and dialogs. The IDE runs on the Pi’s desktop, and you can create both console and GUI programs. Note that the first launch may be slow, and compiling a GUI app takes longer than a console one, but the result is a native GTK or Qt application.

Performance and Limitations

Pascal compiles to ARM machine code, so performance is comparable to C. The Raspberry Pi’s CPU is modest, but for typical educational or utility programs, speed is not an issue. However, Pascal is not ideal for low-level hardware access—for that, C or assembly is better. Also, the standard library lacks some modern conveniences like built-in JSON parsing or HTTP clients, but you can use third-party libraries or bind to C libraries via the ctypes unit. The Pi’s limited RAM (especially on older models) means you should avoid memory-intensive applications, but Pascal’s deterministic memory management helps.

Frequently Asked Questions

Can I use Pascal to control GPIO pins?

Yes, you can access the GPIO via the wiringPi or sysfs interfaces, but you need to use the ctypes unit to call C functions. There is no built-in Pascal GPIO library, but you can write a small wrapper. Alternatively, use the libgpiod library through bindings. For simple projects, calling shell commands like gpio from within Pascal using fpSystem works too.

Is Free Pascal the same as Turbo Pascal?

Free Pascal is highly compatible with Turbo Pascal 7.0, but it also supports Object Pascal and many extensions. Most Turbo Pascal programs will compile without changes, but some DOS-specific units (like Crt) have limited functionality on Linux. You can use the crt unit for console control, but it may not support all features.

How do I debug a Pascal program on the Pi?

You can use the gdb debugger, but it is low-level. A better option is to use the integrated debugger in Lazarus, which allows breakpoints, watches, and step-by-step execution. For console programs, you can also insert writeln statements to print variable values—a simple but effective method.

Can I cross-compile for other platforms?

Yes, Free Pascal supports cross-compilation. For example, to compile for Windows x86 from the Pi, you need the appropriate cross-binutils and a Windows Pascal run-time library. This is advanced and requires setting up the cross-compiler environment. For most users, native compilation is simpler.

What is the best way to learn Pascal on the Pi?

Start with the classic “Hello, World� and then move to small programs that read input, use loops, and manipulate arrays. The official Free Pascal documentation is comprehensive, and there are many online tutorials. The book “Pascal Programming� by Wikibooks is free and covers the language thoroughly. Practice by writing small utilities like a temperature converter or a simple calculator.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →