N.B. Please take care if connecting your own circuits to the Raspberry Pi, not exceeding voltage or current limits, avoiding short circuits etc.
Let’s make a short BASIC program to demonstrate using GPIO pins on a Raspberry Pi. We’re going to use GPIO 24 as an output, set high (so an LED connected through a resistor would light up), and set GPIO 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
The GPIO connections 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 GPIO 24 and GPIO 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 GPIO 24 high.
SYS "GPIO_WriteData", 24, 1 : REM 1 means high, 0 means low
And we also wanted to read the state of input GPIO 25.
SYS "GPIO_ReadData", 25 TO state%
PRINT "GPIO 25 is ";
IF state% = 1 THEN PRINT "high"
IF state% = 0 THEN PRINT "low"