Sunday 3 November 2013

RaspberryPi GPIO; Using Gambas

The digital IO on the RaspberryPi is very useful for reading switches and turning on relays, lights and driving small motors....


...and controlling the digital IO with Gambas turns out to be really easy.


Here is a very basic example which makes use of the wiringPi library written by Gordon Henderson.

WiringPi

According to Gordon's website:-
"WiringPi is an Arduino wiring-like library written in C and released under the GNU LGPLv3 license which is usable from C and C++ and many other languages with suitable wrappers..."

In this post I will only show how to use the digital IO, but do take a look at the full capabilities of wiringPi on the website 

I must confess that I don't fully understand the GPIO numbering/identity scheme. So to make things simple (...or possibly take "confusion" to another level) I offer this basic table.


The wiringPi PIN identities shown are only for the 8 basic IO that we will be using.

CAUTION: When using one of these connections as an output, please remember that this is a 3.3V interface with a 15mA maximum current capability. Do not exceed these ratings, unless you know what you are doing.
Also see: http://captainbodgit.blogspot.co.uk/2015/01/raspberrypi-gpio-mixing-voltage-levels.html.

First we need to download and install wiringPi by visiting this link:-

https://git.drogon.net/?p=wiringPi;a=summary

Then look for the link marked snapshot at the right-hand side. You want to click on the top one.

This will download a tar.gz file with a name like wiringPi-98bcb20.tar.gz. Note that the numbers and letters after wiringPi (98bcb20 in this case) will probably be different – they’re a unique identifier for each release.

You then need to do this in a terminal to install:-

tar xfz wiringPi-98bcb20.tar.gz
cd wiringPi-98bcb20
./build

Run As Root

Note: we need to run as root to use the GPIO library. So we can either launch Gambas from a terminal like this:-

gksu gambas3

...or change you menu launcher properties so that Gambas always runs as root. Example:-
 Start > Programming > right click on Gambas3 > Properties ...then change command to: gksu gambas3

Gambas Declarations

On Linux, shared libraries written in C can be accessed and utilised by Gambas. First we need to tell Gambas where the library resides on our system. We do this by providing the base name of the library file & path without including the extension:-

Library "/usr/local/lib/libwiringPi"

The next step is to include declarations for each procedure from this library.

In this example I don't need all the functionality available, so I'm just going to declare 5 routines. We do this using EXTERN so Gambas knows to look in the external library already declared.

Public Extern wiringPiSetup() As Integer
 'initialises wiringPi (assumes calling program uses virtual Pin numbers)

Public Extern pinMode(pin As Integer, pud As Integer)

 'sets Pin mode to either INPUT, OUTPUT, (PWM_OUTPUT or GPIO_CLOCK if applicable)

Public Extern pullUpDnControl(pin As Integer, mode As Integer)
 
'sets the input resistor (50k) mode (PUD_OFF, PUD_DOWN, PUD_UP)

Public Extern digitalRead(pin As Integer) As Integer        
 'returns the input state (low=0, high=1)
 

Public Extern digitalWrite(pin As Integer, value As Integer)
 'sets the output state

We may like to define Constants for Pin Mode:-

Public const PIN_INPUT as Integer = 0
Public const PIN_OUTPUT as Integer = 1

...and for levels:-

Public const PIN_LOW as Integer = 0
Public const PIN_HIGH as Integer = 1

Now we can initialise wiringPi using wiringPiSetup.

If wiringPi is not installed, or you are not running as root, or there is some other problem, an error will be raised. So maybe our code needs to look something like this:-

Public Sub Form_Open()
    wiringPiSetup()
Catch
     Me.text = "WiringPi setup failed - run as root?"
End

If we want to set GPIO pin 2 to be an output:-

 pinMode(2,PIN_OUTPUT)

...and set the output high like this:-

 digitalWrite(2,PIN_HIGH)

...or low like this:-

 digitalWrite(2,PIN_LOW)

So as you can see, it couldn't be easier!

Full credit goes to Gordon Henderson for the wiringPi library and CWRoos (from RPi forum) for the original Gambas example code that I used as a starting point.

...and also


For info on Gambas using the alternative pigpio library see: http://captainbodgit.blogspot.co.uk/2016/08/raspberry-pi-gpio-gambas-pigpio-library.html

6 comments:

  1. Excellent!! Works on Raspberry as advertised!
    Not working on my Banana Pi; library may need to be compiled for Banana Pi, OR, I may have made and error.

    ReplyDelete
  2. How can i set the frequency and dutycycle in PWM-mode?

    ReplyDelete
  3. That's a good question.

    Please take a look at this post: http://captainbodgit.blogspot.co.uk/2016/02/pwm-on-raspberrypi-with-gambas.html

    ReplyDelete
  4. Thanks for this post...I was able to get it to work with pi3b and gambas3. Clicking a gambas button now toggles an LED on and off. Its disappointing tht theres on one pwm pin - I see peope have posted that they have used I2C boards to contro servos and I suspect I wi be doing this.

    ReplyDelete
  5. hi, i would like to ask you. when i press a physical emergency button on a panel, then the form in gambas will change. how can i make it like that?
    thanks

    ReplyDelete
    Replies
    1. Once you have the basic setup described in this post, I suggest you use a Gambas timer component to monitor the GPIO input connected to the switch.

      The timer interval could be (say) 200ms. Then in the timer routine, check if the switch has been operated using digitalRead(switch_pin_id), where switch_pin_id is just the identity (name) you have setup to represent the switch input.
      If the switch has been operated, do something.

      Can I suggest that you ask your question again on this Gambas forum (https://gambas.guru/forum14) where we will be able to give you more detailed help.

      Good luck with your project.

      Delete