จอแสดงผล OLED 128x64 แบบ I2C SPI ขนาด 1.54นิ้ว สีน้ำเงิน
- ขนาดจอ 1.54นิ้ว
- สีจอแสดงผล สีน้ำเงิน
- การส่งข้อมูล I2C SPI
- ไดเวอร์ SSD1309
วิธีการต่ออุปกรณ์ สอนใช้งาน Arduino จอแสดงผล OLED 128x64 แบบ I2C SPI ขนาด 1.54นิ้ว
1. เชื่อมต่ออุปกรณ์ตามด้านล่าง
Arduino UNO R3 -> จอแสดงผล OLED 128x64 แบบ I2C SPI ขนาด 1.54นิ้ว
• 5V -> VCC
• GND -> GND
• A4 -> SDA
• A5 -> SCL

2. เมื่อเชื่อมต่ออุปกรณ์เรียบร้อยแล้ว ให้ทำการดาวน์โหลด Library จากลิ้งค์ข้างล่าง
วิธีลง Library ให้ดูตัวอย่างในบทความนี้
1
/**************************************************************************
2
This is an example for our Monochrome OLEDs based on SSD1306 drivers
4
Pick one up today in the adafruit shop!
5
------> http://www.adafruit.com/category/63_98
7
This example is for a 128x64 pixel display using I2C to communicate
8
3 pins are required to interface (two I2C and one reset).
10
Adafruit invests time and resources providing this open
11
source code, please support Adafruit and open-source
12
hardware by purchasing products from Adafruit!
14
Written by Limor Fried/Ladyada for Adafruit Industries,
15
with contributions from the open source community.
16
BSD license, check license.txt for more information
17
All text above, and the splash screen below must be
18
included in any redistribution.
19
**************************************************************************/
23
#include <Adafruit_GFX.h>
24
#include <Adafruit_SSD1306.h>
26
#define SCREEN_WIDTH 128 // OLED display width, in pixels
27
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
29
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
30
// The pins for I2C are defined by the Wire-library.
31
// On an arduino UNO: A4(SDA), A5(SCL)
32
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
33
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
34
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
35
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
36
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
38
#define NUMFLAKES 10 // Number of snowflakes in the animation example
40
#define LOGO_HEIGHT 16
42
static const unsigned char PROGMEM logo_bmp[] =
43
{ 0b00000000, 0b11000000,
44
0b00000001, 0b11000000,
45
0b00000001, 0b11000000,
46
0b00000011, 0b11100000,
47
0b11110011, 0b11100000,
48
0b11111110, 0b11111000,
49
0b01111110, 0b11111111,
50
0b00110011, 0b10011111,
51
0b00011111, 0b11111100,
52
0b00001101, 0b01110000,
53
0b00011011, 0b10100000,
54
0b00111111, 0b11100000,
55
0b00111111, 0b11110000,
56
0b01111100, 0b11110000,
57
0b01110000, 0b01110000,
58
0b00000000, 0b00110000 };
63
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
64
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
65
Serial.println(F("SSD1306 allocation failed"));
66
for(;;); // Don't proceed, loop forever
69
// Show initial display buffer contents on the screen --
70
// the library initializes this with an Adafruit splash screen.
72
delay(2000); // Pause for 2 seconds
75
display.clearDisplay();
77
// Draw a single pixel in white
78
display.drawPixel(10, 10, SSD1306_WHITE);
80
// Show the display buffer on the screen. You MUST call display() after
81
// drawing commands to make them visible on screen!
84
// display.display() is NOT necessary after every single drawing command,
85
// unless that's what you want...rather, you can batch up a bunch of
86
// drawing operations and then update the screen all at once by calling
87
// display.display(). These examples demonstrate both approaches...
89
testdrawline(); // Draw many lines
91
testdrawrect(); // Draw rectangles (outlines)
93
testfillrect(); // Draw rectangles (filled)
95
testdrawcircle(); // Draw circles (outlines)
97
testfillcircle(); // Draw circles (filled)
99
testdrawroundrect(); // Draw rounded rectangles (outlines)
101
testfillroundrect(); // Draw rounded rectangles (filled)
103
testdrawtriangle(); // Draw triangles (outlines)
105
testfilltriangle(); // Draw triangles (filled)
107
testdrawchar(); // Draw characters of the default font
109
testdrawstyles(); // Draw 'stylized' characters
111
testscrolltext(); // Draw scrolling text
113
testdrawbitmap(); // Draw a small bitmap image
115
// Invert and restore display, pausing in-between
116
display.invertDisplay(true);
118
display.invertDisplay(false);
121
testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
127
void testdrawline() {
130
display.clearDisplay(); // Clear display buffer
132
for(i=0; i<display.width(); i+=4) {
133
display.drawLine(0, 0, i, display.height()-1, SSD1306_WHITE);
134
display.display(); // Update screen with each newly-drawn line
137
for(i=0; i<display.height(); i+=4) {
138
display.drawLine(0, 0, display.width()-1, i, SSD1306_WHITE);
144
display.clearDisplay();
146
for(i=0; i<display.width(); i+=4) {
147
display.drawLine(0, display.height()-1, i, 0, SSD1306_WHITE);
151
for(i=display.height()-1; i>=0; i-=4) {
152
display.drawLine(0, display.height()-1, display.width()-1, i, SSD1306_WHITE);
158
display.clearDisplay();
160
for(i=display.width()-1; i>=0; i-=4) {
161
display.drawLine(display.width()-1, display.height()-1, i, 0, SSD1306_WHITE);
165
for(i=display.height()-1; i>=0; i-=4) {
166
display.drawLine(display.width()-1, display.height()-1, 0, i, SSD1306_WHITE);
172
display.clearDisplay();
174
for(i=0; i<display.height(); i+=4) {
175
display.drawLine(display.width()-1, 0, 0, i, SSD1306_WHITE);
179
for(i=0; i<display.width(); i+=4) {
180
display.drawLine(display.width()-1, 0, i, display.height()-1, SSD1306_WHITE);
185
delay(2000); // Pause for 2 seconds
188
void testdrawrect(void) {
189
display.clearDisplay();
191
for(int16_t i=0; i<display.height()/2; i+=2) {
192
display.drawRect(i, i, display.width()-2*i, display.height()-2*i, SSD1306_WHITE);
193
display.display(); // Update screen with each newly-drawn rectangle
200
void testfillrect(void) {
201
display.clearDisplay();
203
for(int16_t i=0; i<display.height()/2; i+=3) {
204
// The INVERSE color is used so rectangles alternate white/black
205
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE);
206
display.display(); // Update screen with each newly-drawn rectangle
213
void testdrawcircle(void) {
214
display.clearDisplay();
216
for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) {
217
display.drawCircle(display.width()/2, display.height()/2, i, SSD1306_WHITE);
225
void testfillcircle(void) {
226
display.clearDisplay();
228
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
229
// The INVERSE color is used so circles alternate white/black
230
display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE);
231
display.display(); // Update screen with each newly-drawn circle
238
void testdrawroundrect(void) {
239
display.clearDisplay();
241
for(int16_t i=0; i<display.height()/2-2; i+=2) {
242
display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i,
243
display.height()/4, SSD1306_WHITE);
251
void testfillroundrect(void) {
252
display.clearDisplay();
254
for(int16_t i=0; i<display.height()/2-2; i+=2) {
255
// The INVERSE color is used so round-rects alternate white/black
256
display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i,
257
display.height()/4, SSD1306_INVERSE);
265
void testdrawtriangle(void) {
266
display.clearDisplay();
268
for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
269
display.drawTriangle(
270
display.width()/2 , display.height()/2-i,
271
display.width()/2-i, display.height()/2+i,
272
display.width()/2+i, display.height()/2+i, SSD1306_WHITE);
280
void testfilltriangle(void) {
281
display.clearDisplay();
283
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) {
284
// The INVERSE color is used so triangles alternate white/black
285
display.fillTriangle(
286
display.width()/2 , display.height()/2-i,
287
display.width()/2-i, display.height()/2+i,
288
display.width()/2+i, display.height()/2+i, SSD1306_INVERSE);
296
void testdrawchar(void) {
297
display.clearDisplay();
299
display.setTextSize(1); // Normal 1:1 pixel scale
300
display.setTextColor(SSD1306_WHITE); // Draw white text
301
display.setCursor(0, 0); // Start at top-left corner
302
display.cp437(true); // Use full 256 char 'Code Page 437' font
304
// Not all the characters will fit on the display. This is normal.
305
// Library will draw what it can and the rest will be clipped.
306
for(int16_t i=0; i<256; i++) {
307
if(i == '\n') display.write(' ');
308
else display.write(i);
315
void testdrawstyles(void) {
316
display.clearDisplay();
318
display.setTextSize(1); // Normal 1:1 pixel scale
319
display.setTextColor(SSD1306_WHITE); // Draw white text
320
display.setCursor(0,0); // Start at top-left corner
321
display.println(F("Arduitronics!"));
323
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
324
display.println(3.141592);
326
display.setTextSize(2); // Draw 2X-scale text
327
display.setTextColor(SSD1306_WHITE);
328
display.print(F("0x")); display.println(0xDEADBEEF, HEX);
334
void testscrolltext(void) {
335
display.clearDisplay();
337
display.setTextSize(2); // Draw 2X-scale text
338
display.setTextColor(SSD1306_WHITE);
339
display.setCursor(10, 0);
340
display.println(F("scroll"));
341
display.display(); // Show initial text
344
// Scroll in various directions, pausing in-between:
345
display.startscrollright(0x00, 0x0F);
347
display.stopscroll();
349
display.startscrollleft(0x00, 0x0F);
351
display.stopscroll();
353
display.startscrolldiagright(0x00, 0x07);
355
display.startscrolldiagleft(0x00, 0x07);
357
display.stopscroll();
361
void testdrawbitmap(void) {
362
display.clearDisplay();
365
(display.width() - LOGO_WIDTH ) / 2,
366
(display.height() - LOGO_HEIGHT) / 2,
367
logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
372
#define XPOS 0 // Indexes into the 'icons' array in function below
376
void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
377
int8_t f, icons[NUMFLAKES][3];
379
// Initialize 'snowflake' positions
380
for(f=0; f< NUMFLAKES; f++) {
381
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
382
icons[f][YPOS] = -LOGO_HEIGHT;
383
icons[f][DELTAY] = random(1, 6);
384
Serial.print(F("x: "));
385
Serial.print(icons[f][XPOS], DEC);
386
Serial.print(F(" y: "));
387
Serial.print(icons[f][YPOS], DEC);
388
Serial.print(F(" dy: "));
389
Serial.println(icons[f][DELTAY], DEC);
392
for(;;) { // Loop forever...
393
display.clearDisplay(); // Clear the display buffer
395
// Draw each snowflake:
396
for(f=0; f< NUMFLAKES; f++) {
397
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
400
display.display(); // Show the display buffer on the screen
401
delay(200); // Pause for 1/10 second
403
// Then update coordinates of each flake...
404
for(f=0; f< NUMFLAKES; f++) {
405
icons[f][YPOS] += icons[f][DELTAY];
406
// If snowflake is off the bottom of the screen...
407
if (icons[f][YPOS] >= display.height()) {
408
// Reinitialize to a random position, just off the top
409
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
410
icons[f][YPOS] = -LOGO_HEIGHT;
411
icons[f][DELTAY] = random(1, 6);