Newer
Older
See :ref:`qwiic_stemma_grove_addons`.
A link to the appropriate I2C controller can be found at ``/dev/play/qwiic/i2c``.
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
.. todo::
The above link may changed or outdated!!
OLED Display using QWIIC
=========================
Let's see a simple way to use an I2C QWIIC OLED from Sparkfun with only minor
modifications to the source code. (They will probably have this working by default in the future)
The Sparkfun Qwiic OLED Display Library Comes in 3 Parts:
- QWIIC_I2C_Py - We will need to modify this
- QWIIC-OLED-Base
- QWIIC-OLED-Display
The reason we need to modify Qwiic_I2C_Py is that by default, the library expects only one
I2C Bus to be present for something like a Raspberry Pi, but our Beagle has many!
Specifically, we want to use I2C-5 which is the bus connected to the QWIIC header.
Wiring/Connection
===================
Make the connection as shown below.
.. figure:: images/qwiic/beagleplay-qwiic-oled-connection.*
:width: 600
:align: center
:alt: BeaglePlay QWIIC OLED Connection
BeaglePlay QWIIC OLED Connection
You can check what bus a device is connected to by scanning it.
First lets see what buses are available.
.. code:: shell-session
debian@BeaglePlay:~$ ls /dev/ | grep "i2c"
i2c-0
i2c-1
i2c-2
i2c-3
i2c-5
You can now scan each bus as follows:
.. code:: shell-session
i2cdetect -y -r 0
The ``0`` corresponds to ``i2c-0``. we can then replace ``0`` with each bus untill we find the oled,
in this case, we know we are looking for a device at address ``0x3C``.
.. code:: shell-session
debian@BeaglePlay:~$ i2cdetect -y -r 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Note that when we see a ``UU``, this indicates that there is a device which is
currently being used by another linux process.This is most likely another I2C device that
the Beagle uses, such as the EEPROM. You can safely ignore this, but it's helpful to know
what you're looking at.
Moving on, let's see Bus 5 (Hint, I2C-5 is the QWIIC connector):
.. code:: shell-session
debian@BeaglePlay:~$ i2cdetect -y -r 5
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Using Python libraries to display on OLED.
===========================================
let's install sparkfun Qwiic_I2C_Py Library.
.. code:: shell-session
git clone https://github.com/virtualRadish/Qwiic_I2C_Py_LC
Change directory to ``Qwiic_I2C_Py_LC``.
.. code:: shell-session
cd Qwiic_I2C_Py_LC/
Install ``setup.py``.
.. code:: shell-session
sudo python setup.py install
Install python libraries for OLED Displays.
.. code:: shell-session
sudo pip install sparkfun-qwiic-oled-base
sudo pip install sparkfun-qwiic-oled-display
Let's create a file ``HelloWorld.py`` to display some text on display.
.. code:: shell-session
nano HelloWorld.py
Now copy paste the text below, then press CTRL+O and ENTER to save, CTRL+X to exit.
.. code:: shell-session
from __future__ import print_function
import qwiic_oled_display
import sys
import time
def runExample():
# These three lines of code are all you need to initialize the
# OLED and print the splash screen.
# Before you can start using the OLED, call begin() to init
# all of the pins and configure the OLED.
print("\nSparkFun OLED Display - Hello World Example\n")
# Create instance with parameters for Qwiic OLED Display
myOLED = qwiic_oled_display.QwiicOledDisplay(0x3C)
if not myOLED.connected:
print("The Qwiic OLED Display isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myOLED.begin()
# clear(ALL) will clear out the OLED's graphic memory.
myOLED.clear(myOLED.ALL) # Clear the display's memory (gets rid of artifacts)
# To actually draw anything on the display, you must call the display() function.
myOLED.display() # Display buffer contents
time.sleep(1)
# clear(PAGE) will clear the SBC display buffer.
myOLED.clear(myOLED.PAGE) # Clear the display's buffer
# Display buffer contents
myOLED.display()
time.sleep(1)
# Print "Hello World"
# ---------------------------------------------------------------------------
# Add text
myOLED.print("Hello World!")
myOLED.set_cursor(0, 10) # Set cursor to top-left
myOLED.print("I'm BeaglePlay!")
# Display buffer contents
myOLED.display()
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding OLED Hello Example")
sys.exit(0)
Now run it. After executing following command, "Hello World!" in first line and "I'm BeaglePlay!"
in second line will be printed on OLED display.
.. code:: shell-session
python HelloWorld.py
.. figure:: images/qwiic/beagleplay-qwiic-oled-helloworld.*
:width: 600
:align: center
:alt: BeaglePlay QWIIC OLED HelloWorld.py Output
BeaglePlay QWIIC OLED HelloWorld.py Output
Now, lets display our current IP Address.
Shout out out to `this <https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib>`_ StackOverflow one-liner which gets our IP Address cleanly so we can
display it as a string:
.. code:: shell-session
ipAddr = ((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0]
[1]]) + ["no IP found"])[0])
Additionaly in above text we can display our current IP Address using below script.
You can create a new file then copy paste it and run.
.. code:: shell-session
from __future__ import print_function
import qwiic_oled_display
import sys
import time
import socket
def runExample():
IPAddr=(([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0]
# These three lines of code are all you need to initialize the
# OLED and print the splash screen.
# Before you can start using the OLED, call begin() to init
# all of the pins and configure the OLED.
print("\nSparkFun OLED Display - Hello World Example\n")
# Create instance with parameters for Qwiic OLED Display
myOLED = qwiic_oled_display.QwiicOledDisplay(0x3C)
if not myOLED.connected:
print("The Qwiic OLED Display isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myOLED.begin()
# clear(ALL) will clear out the OLED's graphic memory.
myOLED.clear(myOLED.ALL) # Clear the display's memory (gets rid of artifacts)
# To actually draw anything on the display, you must call the display() function.
myOLED.display() # Display buffer contents
time.sleep(1)
# clear(PAGE) will clear the SBC display buffer.
myOLED.clear(myOLED.PAGE) # Clear the display's buffer
# Display buffer contents
myOLED.display()
time.sleep(1)
# Print "Hello World"
# ---------------------------------------------------------------------------
# Add text
myOLED.print("Hello World!")
myOLED.set_cursor(0, 10) # Set cursor to top-left
myOLED.print("I'm BeaglePlay!")
myOLED.set_cursor(0, 25) # Set cursor to top-left
myOLED.print("My IP Is:")
myOLED.print(IPAddr)
# Display buffer contents
myOLED.display()
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding OLED Hello Example")
sys.exit(0)
You will now see current IP Address as well on OLED display.
.. figure:: images/qwiic/beagleplay-qwiic-oled-ipaddress.*
:width: 600
:align: center
:alt: IP Address on QWIIC OLED Display
Credits: `Andrei Aldea, Nishka Rao, Brian Berner <https://www.hackster.io/506688/beagleplay-qwiic-oled-hello-world-ee7270>`_