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.
WiringPiAccording 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:-
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
Run As RootNote: we need to run as root to use the GPIO library. So we can either launch Gambas from a terminal like this:-
...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 DeclarationsOn 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:-
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()
Me.text = "WiringPi setup failed - run as root?"
If we want to set GPIO pin 2 to be an output:-
...and set the output high like this:-
...or low like this:-
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.
For info on Gambas using the alternative pigpio library see: http://captainbodgit.blogspot.co.uk/2016/08/raspberry-pi-gpio-gambas-pigpio-library.html