The pins on the Raspberry Pi are number according to Broadcom’s GPIO numbering:
To access the gpio pins import the library gpio
:
import gpio
Open()
Open(chipname)
Open gpio access to the chip specified by the optional parameter
chipname
. If chipname
is not provided, the
standart gpiochip of the Raspberry Pi will be opened.
chipname
gpiochip0
SetInput(PinNumber)
Sets GPIO pin PinNumber
as an input. The internal
pullup-resistor will be set automaticly. Before using a GPIO pin, you
should use either SetInput
or SetOutput
.
PinNumber
SetOutput(PinNumber)
Sets GPIO pin PinNumber
as an output. Before using a
GPIO pin, you should use either SetInput
or
SetOutput
.
PinNumber
Write(Pin, Level)
If Pin
is configured as output, the voltage level
level
can be set to ground (low) or +3.3V (high).
Pin
Level
Example:
Write(4, 0) ' Sets pin 4 to low (ground)
Write(4, 1) ' Sets pin 4 to high (+3.3V)
Status = Read(Pin)
If Pin
is configured as input, the voltage level can be
read. If the specified pin is at ground (low), Status
will
be 1
. If the pin is at +3.3V (high) Status
will be 0.
Pin
Status
Example:
Read(4) ' Reads the voltage level of pin 4 Status =
Trigger(Pin)
Trigger(Pin, Duration)
Trigger(Pin, Duration, Level)
If pin Pin
is configured as output, a trigger pulse at
this pin will be emitted with a Duration
in microseconds
and a voltage level Level
of low or high.
Pin
Duration
Level
Example:
4, 50, 1) ' A 50 µs Trigger pulse at pin 4 with +3.3V (high) Trigger(
Status = WaitForTrigger(Pin)
Status = WaitForTrigger(Pin, Timeout)
Wait for a raising edge event at the specified pin Pin
.
Timeout
is in seconds. Maximum timeout is 255s.
Timeout
an optional parameter.
Pin
Timeout
Status
Integer: -1, 0, 1
Value | Status |
---|---|
-1 | error |
0 | time out |
1 | rising edge detected |
Example:
4, 1) ' Pin 4; timeout 1s Status = WaitForTrigger(
Close()
When closing a SmallBASIC program, gpio access will be automatically closed. If you want to manually close gpio access, use this function.
In the following image you can see how to wire a LED.
Depending on the type of LED you need a certain resistor. When using the LED without a resistor, you will destroy the LED and maybe even parts of your Raspberry Pi.
When you buy a LED, look for two important values in the specification: Forward Voltage and Forward Current. The third important value is the Supply Voltage. In case of a Raspberry Pi it is 3.3V. Online you can find many LED resistor calculators. But if you want to see your LED blinking without studying to much and you don’t expect maximum brightness, then go for 220 Ohms or even 1000 Ohms.
Connect the resistor to pin 4 and the LED to ground.
import gpio
Open()
gpio.4)
gpio.SetOutput(
for ii = 1 to 10
v = !vWrite(4, v)
gpio.delay(100)
next
In the following image you see the wiring of a push button. When you press the button, the circuit will be closed, otherwise the circuit is open. The button is connected to pin 4 and ground. An internal pullup resistor will be enabled automatically.
import gpio
0
v =
Open()
gpio.4)
gpio.SetInput(
while(1)
print gpio.Read(4)
delay(500)
wend
import gpio
0
v =
Open()
gpio.4)
gpio.SetOutput(
for ii = 1 to 10
4)
gpio.Trigger(delay(200)
next
import gpio
0
v =
Open()
gpio.4)
gpio.SetInput(
4, 5)
result = gpio.WaitTrigger(
select case result
case 0: print "Time out"
case 1: print "Rising edge detected"
case -1: print "Error"
end select