Accessing Raspberry Pi GPIO using Lazarus/Free Pascal

Note, this post covers writing to a GPIO pin. If you want to read a GPIO pin, have a look at this post:

Reading Raspberry Pi GPIO Pins using Lazarus / Free Pascal

Last experiment for the day:

I want to simply turn an LED on and off from a Pascal program running on RPI.

First, I needed to setup an LED and just test it from the O/S to verify everything was working properly. I followed the following instructions from Getting Started with Raspberry Pi.

I connected an LED to the pin labeled #25 on the pi cobbler and connected the negative side of the LED to the ground pin. I then typed the following commands:

sudo su
echo 25 > /sys/class/gpio/export
cd /sys/class/gpio/gpio25
echo out > direction
echo 1 > value

This turns the LED on. To turn it back off:

echo 0 > value

That works fine, so I know everything is setup correctly at the hardware level.

Now on to Free Pascal. There is an FP wrapper for the wiringpi library which is written in C. More information on wiringpi can be found here:

The FP wrapper can be found at,17404.0.html

though you will need to have a login to download the wrapper.

The wrapper library includes a sample program. I attempted to compile this program and was getting linker errors when it tried to link to wiringpi.o. Some research and I found I needed to recompile wiringpi.c using instructions in the readme.txt file. Once that was done, the example program compiled fine.

I then cut out much of the example program to include just turning on the LED that I had installed. The wrapper includes constants for GPIO pins, so I tried to use P25, but it didn’t exist. So I just used 25. Well, that doesn’t work at all. The GPIO pin numbering used at the operating system level do not match those being used by the wiringpi.o library. The proper pin names can be found at

The pin I was calling GPIO25 up above is physical pin 22 which is GPIO6 (or P22) for wiringpi.o.

Once I had that straightened out, the program ran great and is super simple:

Initialize the library:

If wiringPiSetup = -1 then begin

indicate the pin will be used for output:

pinMode(p22, OUTPUT);

and then just turn the pin on and off:

digitalWrite(p22,HIGH); //Turn LED on P22 on

here is the complete program:

program raspberry;

{$mode objfpc}{$H+}

  {$IFDEF UNIX}{$IFDEF UseCThreads}
  Classes, hwiringpi
  { you can add units after this };

{$R *.res}


writeln('Starting RaspberryPi');

If wiringPiSetup = -1 then begin // Setup Hardware (Gordons magic)
   writeln('wirintPiSetup failed');

pinMode(p22, OUTPUT);
While true do begin
  digitalWrite(p22,HIGH); //Turn LED on P22 on
  digitalWrite(p22,LOW);  //Turn LED on P22 off


Mar 2015 Update:

Today I assembled a new RPI Model B. I had some issues getting Lazarus installed, apt-get install was failing. That fix is documented here:

apt-get install fpc lazarus fails on Raspberry Pi

I also had trouble getting the wiringPi library wrapper to work, the newly compiled pascal code would not link with the old wiringPi.o library. That fix is documented here:

lazwiringpi linker problem: Error Rasperry Uses VFP register arguments

This entry was posted in c-lazarus, c-rpi and tagged . Bookmark the permalink.

8 Responses to Accessing Raspberry Pi GPIO using Lazarus/Free Pascal

  1. fnemeth says:

    I use Lazarus on RPi (i think it’s the best IDE for Pi), and you answer all of my questions with your blog. Love it!

  2. Pingback: Make a Raspberry Pi Act on an Email Message | Big Dan the Blogging Man

  3. Pingback: Reading Raspberry Pi GPIO Pins using Lazarus / Free Pascal | Big Dan the Blogging Man

  4. Danny says:

    I can not get this to work..i have to use h2wiringpi not hwiringpi. It fails at If wiringPiSetup = -1 then begin

  5. Dan TheMan says:

    I haven’t used this stuff in nearly a year now, so none of it is fresh in my head. I would suggest looking at the code called by wiringPiSetup and track down why it might be failing initialization and returning a -1. Your answer is there somewhere.

  6. Danny says:

    I will keep looking, I am new to raspberry so pretty much lost at this point. lol

  7. virendra Shirdhankar says:

    showing error
    project1.lpr(9,12) Fatal: Can not find unit hwiringpi used by raspberry.

    please help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s