You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SmallBASIC-PiGPIO is a plugin for SmallBASIC to use the GPIO connector of a Raspberry Pi. The following article will give you a short introduction to the plugin. Detailed information is available on the [project website](https://joe7m.github.io/SmallBasicPIGPIO)
5
+
SmallBASIC-PiGPIO is a plugin for SmallBASIC to use the GPIO connector of a Raspberry Pi.
6
+
The following article will give you a short introduction to the plugin. Detailed information
7
+
is available on the [project website](https://joe7m.github.io/smallbasic.pigpio2/index.html)
6
8
7
9
## Blinking LED
8
10
9
-
In this example we will connect a LED to the Raspberry Pi and let it blink with a short SmallBASIC program.
10
-
11
+
In this example we will connect a LED to the Raspberry Pi and let it blink with a short#
12
+
SmallBASIC program.
11
13
12
14
### Wiring
13
15
14
16
The wiring of the LED is shown in the following image
Depending on the type of the 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. If you want to see your LED blinking without studying to much and you don’t expect maximum brightness, then go for a resistor with 220 Ohms or even (saver) 1000 Ohms.
20
+
Depending on the type of the LED you need a certain resistor. When using the LED without
21
+
a resistor, you will destroy the LED and maybe even parts of your Raspberry Pi. If you
22
+
want to see your LED blinking without studying to much and you don’t expect maximum
23
+
brightness, then go for a resistor with 220 Ohms or even (saver) 1000 Ohms.
19
24
20
-
Connect the resistor to pin 4 and the LED to ground of the Raspberry Pi.
25
+
Connect the resistor to pin 7 (GPIO4) and the LED to ground of the Raspberry Pi.
21
26
22
27
### Software
23
28
24
29
To let the LED blink the following SmallBASIC program can be used.
25
30
26
31
```smallbasic
27
-
import SmallBasicPIGPIO as gpio
28
-
29
-
' LED is connected to pin GPIO4
30
-
const PIN_GPIO4 = 4
31
-
32
-
' Set pin as an output
33
-
gpio.GPIO_SetOutput(PIN_GPIO4)
34
-
35
-
for ii = 1 to 5
36
-
print(ii)
37
-
'Set GPIO pin to high -> 3.3V
38
-
gpio.GPIO_Write(PIN_GPIO4, 1)
39
-
delay(500)
40
-
'Set GPIO pin to low -> ground
41
-
gpio.GPIO_Write(PIN_GPIO4, 0)
42
-
delay(500)
43
-
next
44
-
```
45
-
46
-
The plugin supports Pulse Width Modulation (PWM). Using this technique the intensity of the LED can tuned as shown in the next example.
32
+
import gpio
47
33
48
-
```smallbasic
49
-
import SmallBasicPIGPIO as gpio
34
+
gpio.Open()
35
+
gpio.SetOutput(4)
50
36
51
-
' LED is connected to pin GPIO4
52
-
const PIN_GPIO4 = 4
37
+
for ii = 1 to 10
38
+
v = !v
39
+
gpio.Write(4, v)
40
+
delay(100)
41
+
next
53
42
54
-
' Set pin as an output
55
-
gpio.GPIO_SetOutput(PIN_GPIO4)
43
+
print "done"
44
+
```
56
45
57
-
' Duty cycle is a value between 0 and 255.
58
-
' 0 -> LED is off
59
-
' 128 -> LED half brightness
60
-
' 255 -> LED max. brightness
46
+
The plugin supports Pulse Width Modulation (PWM). Using this technique the intensity
47
+
of the LED can be tuned as shown in the next example. Connect a LED with a 1 KOhm
48
+
resistor to pin 12 (PWM0)
49
+
' and pin 6 (GND)
61
50
62
-
for DutyCycle = 0 to 255 step 20
63
-
print(DutyCycle)
64
-
gpio.GPIO_Pwm(PIN_GPIO4, DutyCycle)
65
-
delay(200)
51
+
```smallbasic
52
+
import pwm
53
+
54
+
' Initialize
55
+
pwm.open("pwmchip0")
56
+
pwm.Period(1000000) ' in ns -> 1kHz
57
+
pwm.DutyCycle(0) ' Active time in ns -> 0 = off
58
+
pwm.Enable() ' enable pwm0
59
+
60
+
' Fade from dark to bright
61
+
for ii = 1 to 1000000 Step 10000
62
+
pwm.DutyCycle(ii)
63
+
delay(10)
66
64
next
67
65
```
68
66
69
67
## Push Buttons
70
68
71
-
Reading the state of a push button is for many Raspberry Pi project important and really easy with SmallBASIC PiGPIO.
69
+
Reading the state of a push button is for many Raspberry Pi project important and
70
+
really easy with SmallBASIC PiGPIO.
72
71
73
72
### Wiring
74
73
75
-
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 of the Raspberry Pi. An internal pullup resistor will be enabled automatically.
74
+
In the following image you see the wiring of a push button. When you press the button,
75
+
the circuit will be closed, otherwise the circuit is open. The button is connected to
76
+
pin 4 and ground of the Raspberry Pi. An internal pullup resistor will be enabled
@@ -81,85 +83,84 @@ In the following image you see the wiring of a push button. When you press the b
81
83
To read the state of the button, the following example program can be used.
82
84
83
85
```smallbasic
84
-
import SmallBasicPIGPIO as gpio
85
-
86
-
const PIN_GPIO4 = 4
87
-
88
-
' Set GPIO pin 4 to INPUT. When a pin is set to input,
89
-
' the internal pullup resistor will be enabled automaticly
90
-
gpio.GPIO_SetInput(PIN_GPIO4)
91
-
92
-
repeat
93
-
key = inkey()
94
-
95
-
' Read the state of the pin. It will return 0 or 1.
96
-
status = gpio.GPIO_Read(PIN_GPIO4)
97
-
locate 1,1: print(status)
98
-
99
-
showpage()
100
-
delay(50)
101
-
102
-
until key <> ""
86
+
import gpio
87
+
88
+
gpio.Open()
89
+
gpio.SetInput(4)
90
+
91
+
while(1)
92
+
print gpio.Read(4)
93
+
delay(500)
94
+
wend
103
95
```
104
96
105
97
## OLED Display
106
98
107
-
In case the Raspberry Pi is used without a big screen, it would be great to have a small display to print information or even show nice graphics. The plugin supports SSD1306 compatible OLED displays (quite common and really cheap).
99
+
In case the Raspberry Pi is used without a big screen, it would be great to have a
100
+
small display to print information or even show nice graphics. The plugin supports
101
+
SSD1306 compatible OLED displays (quite common and really cheap).
108
102
109
103
### Wiring
110
104
111
-
For running this example, you need a SSD1306 compatible OLED display. OLEDs with 128x64 or 128x32 pixels are supported. SmallBASIC-PiGPIO is using the I2C-protocol for communication.
105
+
For running this example, you need a SSD1306 compatible OLED display. OLEDs with
106
+
128x64 or 128x32 pixels are supported. SmallBASIC-PiGPIO is using the I2C-protocol
0 commit comments