-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugins_pigpio.html
More file actions
210 lines (210 loc) · 15.6 KB
/
Copy pathplugins_pigpio.html
File metadata and controls
210 lines (210 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | plugins_pigpio</title>
<meta name="description" content="SmallBASIC | One more basic">
<link rel="canonical" href="/plugins_pigpio.html">
<link rel="keywords" href="plugins_pigpio">
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<script src="/clipboard.js"></script>
</head>
<body>
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<script src="/backtotop.js"></script>
<div class="wrapAll clearfix">
<nav class="navigation">
<div class="logo">
<a href="/"><img src='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<div class="navlinks">
<a href="/pages/download.html">Download</a>
<a href="/pages/news.html">News</a>
<a href="/pages/community.html">Community</a>
<a class='active' href="/pages/articles.html">Resources</a>
<a href="/pages/reference.html">Language Reference</a>
<a href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<div class="mainsection">
<div class="tabs clearfix">
<div class="tabsRight">
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/pages/plugins_pigpio.markdown">Edit</a>
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/pages/plugins_pigpio.markdown">History</a>
</div>
</div>
<div class="article">
<h1
id="smallbasic-pigpio---plugin-to-use-the-gpio-pins-of-a-raspberry-pi">SmallBASIC-PiGPIO
- Plugin to use the GPIO pins of a Raspberry Pi</h1>
<p><img
src="https://joe7m.github.io/SmallBasicPIGPIO/images/logo_smallbasicpigpio.png" /></p>
<p>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 <a
href="https://joe7m.github.io/smallbasic.pigpio2/index.html">project
website</a></p>
<h2 id="blinking-led">Blinking LED</h2>
<p>In this example we will connect a LED to the Raspberry Pi and let it
blink with a short SmallBASIC program.</p>
<h3 id="wiring">Wiring</h3>
<p>The wiring of the LED is shown in the following image</p>
<figure>
<img
src="https://joe7m.github.io/SmallBasicPIGPIO/images/LED_wiring.png"
alt="LED wiring" />
<figcaption aria-hidden="true">LED wiring</figcaption>
</figure>
<p>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.</p>
<p>Connect the resistor to pin 7 (GPIO4) and the LED to ground of the
Raspberry Pi.</p>
<h3 id="software">Software</h3>
<p>To let the LED blink the following SmallBASIC program can be
used.</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="pp">import</span> gpio</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>gpio.<span class="fu">Open</span>()</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>gpio.SetOutput(<span class="dv">4</span>)</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="kw">for </span>ii = <span class="dv">1</span> <span class="kw">to</span> <span class="dv">10</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> v = !v</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> gpio.<span class="fu">Write</span>(<span class="dv">4</span>, v)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">delay</span>(<span class="dv">100</span>)</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="kw">next</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="st">"done"</span></span></code></pre></div>
<p>The plugin supports Pulse Width Modulation (PWM). Using this
technique the intensity of the LED can be tuned as shown in the next
example. Connect a LED with a 1 KOhm resistor to pin 12 (PWM0) ’ and pin
6 (GND)</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="pp">import</span> pwm</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co">' Initialize </span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>pwm.<span class="fu">open</span>(<span class="st">"pwmchip0"</span>)</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>pwm.Period(<span class="dv">1000000</span>) <span class="co">' in ns -> 1kHz</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>pwm.DutyCycle(<span class="dv">0</span>) <span class="co">' Active time in ns -> 0 = off</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>pwm.Enable() <span class="co">' enable pwm0</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="co">' Fade from dark to bright</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="kw">for </span>ii = <span class="dv">1</span> <span class="kw">to</span> <span class="dv">1000000</span> Step <span class="dv">10000</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> pwm.DutyCycle(ii)</span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">delay</span>(<span class="dv">10</span>)</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a><span class="kw">next</span></span></code></pre></div>
<h2 id="push-buttons">Push Buttons</h2>
<p>Reading the state of a push button is for many Raspberry Pi project
important and really easy with SmallBASIC PiGPIO.</p>
<h3 id="wiring-1">Wiring</h3>
<p>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.</p>
<figure>
<img
src="https://joe7m.github.io/SmallBasicPIGPIO/images/PushButton_wiring.png"
alt="Button wiring" />
<figcaption aria-hidden="true">Button wiring</figcaption>
</figure>
<h3 id="software-1">Software</h3>
<p>To read the state of the button, the following example program can be
used.</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="pp">import</span> gpio</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>gpio.<span class="fu">Open</span>()</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>gpio.SetInput(<span class="dv">4</span>)</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="kw">while(</span><span class="dv">1</span>)</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> gpio.<span class="fu">Read</span>(<span class="dv">4</span>)</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">delay</span>(<span class="dv">500</span>)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="kw">wend</span></span></code></pre></div>
<h2 id="oled-display">OLED Display</h2>
<p>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).</p>
<h3 id="wiring-2">Wiring</h3>
<p>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.</p>
<figure>
<img
src="https://joe7m.github.io/SmallBasicPIGPIO/images/ssd1306_wiring.png"
alt="OLED wiring" />
<figcaption aria-hidden="true">OLED wiring</figcaption>
</figure>
<h3 id="software-2">Software</h3>
<p>The following example shows how to use basic graphic commands to draw
lines or print text.</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="pp">import</span> ssd1306</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="dt">const</span> ADDRESS = <span class="dv">0x3C</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="kw">Print</span> <span class="st">"Connect to SSD1306 OLED display"</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">Open</span>(<span class="st">"/dev/i2c-1"</span>, ADDRESS)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="kw">Print</span> <span class="st">"Connection established"</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">2000</span>)</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">Cls</span>()</span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">Circle</span>(<span class="dv">25</span>, <span class="dv">40</span>, <span class="dv">16</span>, <span class="dv">1</span>, <span class="dt">true</span>)</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">At</span>(<span class="dv">50</span>,<span class="dv">5</span>): ssd1306.<span class="kw">Print</span>(<span class="st">"I2C with"</span>, <span class="dv">1</span>)</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">At</span>(<span class="dv">50</span>,<span class="dv">14</span>): ssd1306.<span class="kw">Print</span>(<span class="st">"SMALLBASIC"</span>, <span class="dv">1</span>)</span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">line</span>(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">127</span>, <span class="dv">63</span>)</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a>ssd1306.<span class="fu">rect</span>(<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">127</span>,<span class="dv">63</span>)</span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a><span class="co">' Transfer framebuffer to display</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a>s = <span class="fu">ticks</span></span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a>ssd1306.Display()</span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="fu">ticks</span> - s</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">2000</span>)</span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a><span class="co">' Set brightness</span></span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a>ssd1306.SetBrightness(<span class="dv">20</span>)</span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">1000</span>)</span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a>ssd1306.SetBrightness(<span class="dv">255</span>)</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">1000</span>)</span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-29"><a href="#cb4-29" aria-hidden="true" tabindex="-1"></a><span class="co">' Display on/off</span></span>
<span id="cb4-30"><a href="#cb4-30" aria-hidden="true" tabindex="-1"></a>ssd1306.DisplayOff()</span>
<span id="cb4-31"><a href="#cb4-31" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">1000</span>)</span>
<span id="cb4-32"><a href="#cb4-32" aria-hidden="true" tabindex="-1"></a>ssd1306.DisplayOn()</span>
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">1000</span>)</span>
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true" tabindex="-1"></a><span class="co">' Set inverse</span></span>
<span id="cb4-36"><a href="#cb4-36" aria-hidden="true" tabindex="-1"></a>ssd1306.InvertDisplay()</span>
<span id="cb4-37"><a href="#cb4-37" aria-hidden="true" tabindex="-1"></a><span class="fu">delay</span>(<span class="dv">1000</span>)</span>
<span id="cb4-38"><a href="#cb4-38" aria-hidden="true" tabindex="-1"></a>ssd1306.InvertDisplay()</span>
<span id="cb4-39"><a href="#cb4-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-40"><a href="#cb4-40" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="st">"done"</span></span></code></pre></div>
<h1 id="links">Links</h1>
<ul>
<li><a
href="https://joe7m.github.io/smallbasic.pigpio2/index.html">SmallBASIC
PiGPIO project website</a></li>
<li><a href="https://github.com/Joe7M/smallbasic.pigpio2">Github
repository</a></li>
<li><a href="https://smallbasic.discourse.group/">Feedback and
discussion</a></li>
<li><a href="https://smallbasic.github.io/">SmallBASIC website</a></li>
</ul>
</div>
<div class="pagefooter">
This page was last edited on Sat, 2 Aug 2025 14:21:01 +0200
|
<a href="https://en.wikipedia.org/wiki/Markdown" target="_blank" rel="nofollow">Markdown</a>
processed with
<a href="https://pandoc.org/MANUAL.html#pandocs-markdown" target="_blank" rel="nofollow">pandoc 3.1.12.1</a>
</div>
</div>
</div>
</body>
</html>