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:

https://projects.drogon.net/raspberry-pi/wiringpi/

The FP wrapper can be found at

http://forum.lazarus.freepascal.org/index.php/topic,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

https://projects.drogon.net/raspberry-pi/wiringpi/pins/

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+}

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

{$R *.res}

begin

writeln('Starting RaspberryPi');

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

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

end.

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

Nov 2016 Update:

The latest wiringpi wrapper is here:

http://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=17404.0;attach=11201

You will almost certainly need to be logged into the lazarus forum first for this link to work.

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

13 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.

  8. Thomas Eriksson in Sweden says:

    I am using RPi B+, Rasbian Jessie, fpc 2.6.4 and Lazarus 1.2.4.
    When using the latest WiringPi in my pascal-program, I can access the GPIO-pins by using WiringPiSetupSys() in code.

    But,
    when using WiringPiSetup() in code instead, it stops at this line and exits the program.
    The program compiles without problems.

    I am really confused !
    I have tried to run with root-priviledges, but have not had any success.

    Any idea that could help ?

    //Thomas

  9. Dan TheMan says:

    Hi Thomas,

    It is not clear to me if you are using lazwiringpi to make the calls to the wiringpi library. If you are not, then that would explain the problem. Since wiringPiSetupSys passes no parameters, you can directly call it w/o issue, but trying to call any procedure that passes parameters is going to have issues. This is because Pascal programs cannot directly call C programs. That is the function of lazwiringpi – to act as a translation between Pascal parameter passing conventions and C parameter passing conventions.

    Also, the version of lazwiringpi has changed since I initially wrote this post. If you are using lazWiringPi, there is now a laz2wiringPi. The link to it is near the end of the thread I refer to. The actual link to this library is: http://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=17404.0;attach=11201 (I imagine you need to be logged in to the forum to get this link to work).

    If you have reached this point and still have an error, then the problem is probably somehow related to the actual wiringpi module since lazwiringpi doesn’t do anything except declare how to call wiringpi.

    If it were me, I’d start looking at the wiringpi code and seeing if there was some way I could debug it.

    If that’s not an option for you, you would be best served by searching other forums for possible problems with the actual wiringpi library. I believe it is very heavily used so there should be quite a lot of help available.

    Good luck!

    One other thing just occurred to me. Look at the declarations in laz2wiring and those in wiringpi.h. Make sure all of the parameters are there and of the same data type. It may be that laz2wiring has become outdated.

  10. Thomas Eriksson in Sweden says:

    Thanks Bob for your answer.

    I am using the wrapper laz2wiringP. I have checked that its unit h2wiringpi exists. This unit is working since I can use it when calling WiringPiSetupSys.
    I think it could be a “memory-location-problem” when trying to access hardware I/Os through a call to WiringPiSetup.
    According to your advice, I will check the wiringpi module and how it accesses the different memory areas.
    Could it be that RPi (B+) and RPi (newer versions) handles the memory different ?

  11. sohrab says:

    Hi,
    Does’nt work with RPi 3.
    Pi hangs on debug run. Get a msg ‘uSD ejected’
    Any work around
    Thanks

    • Dan TheMan says:

      Be sure you have the latest wiringpi and laz2wiringpi. I can’t say for a fact I’ve tried this code on my RPI 3 or not and I probably won’t have time to mess with it any time soon.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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