N.B. Please take care if connecting your own circuits to GPIO pins, not exceeding voltage limits, and avoiding short circuits etc.
Let’s make a short BASIC program to demonstrate using GPIO pins on a Raspberry Pi. We’re going to set pin 24 as an output, set high (so an LED connected through a resistor would light up), and set pin 25 as an input, reading its current value.
The first line makes any errors reported nicely.
ON ERROR ON ERROR OFF : PRINT REPORT$;" at line ";ERL : END
Pins on the Raspberry Pi can be used for things other than GPIO, so we need to tell it first that we’re going to do GPIO on pins 24 and 25.
SYS "GPIO_WriteMode", 24, 0 : REM 0 means GPIO, other values select things like I2C or UART
SYS "GPIO_WriteMode", 25, 0
We also need to tell it which is an input, and which is an output.
SYS "GPIO_WriteOE", 24, 0 : REM 0 means output
SYS "GPIO_WriteOE", 25, 1 : REM 1 means input
Now we’re all ready to go.
First we wanted to set the output pin 24 high.
SYS "GPIO_WriteData", 24, 1 : REM 1 means high, 0 means low
And we also wanted to read the state of input pin 25.
SYS "GPIO_ReadData", 25 TO state%
PRINT "Pin 25 is ";
IF state% = 1 THEN PRINT "high"
IF state% = 0 THEN PRINT "low"