ส่วนบอร์ด Arduino Uno มีความนิยมมาก เพราะสามารถต่อกับ shield และมีไลบรารี่สำหรับผู้ที่เพิ่งเริ่มต้นมากมาย
ในบทความนี้จึงกล่าวถึงการใช้ Arduino uno R3 ต่อกับจอแสดงผล RGB OLED
โดยจอ OLED ที่ใช้คือตัวนี้
ต่อจอแสดงผล OLED และ Arduino uno ดังนี้
ขา SCL ของจอ OLED ต่อกับ ขา 13 ของบอร์ด
ขา SDA ของจอ OLED ต่อกับ ขา 11 ของบอร์ด
ขา CS ของจอ OLED ต่อกับ ขา 10 ของบอร์ด
ขา RES ของจอ OLED ต่อกับ ขา 9 ของบอร์ด
ขา DC ของจอ OLED ต่อกับ ขา 8 ของบอร์ด
ในที่นี้ผู้ใช้สามารถเปลี่ยนขาตามโค๊ดที่ตนเองต้องการได้ ยกเว้นขา 13, 11 และ 10 เพราะ
ขา 13 ของ Arduino ( ขา SCK ) ใช้สำหรับส่งสัญญาณนาฬิกา และขา SCL ของ OLED ใช้สำหรับรับสัญญาณนาฬิกา
ขา 11 ของ Arduino ( ขา MOSI ) ใช้สำหรับการส่งข้อมูลจาก Master ( Arduino ) ไปยัง Slave ( OLED )
ขา 10 ของ Arduino ( ขา CS ) ใช้สำหรับส่งสัญญาณ Low ไปให้ OLED
( อ้างอิงรูปภาพ pin ของ Arduino Uno จาก http://itbakery.net/ )
ต่อไปลองใส่ code เพื่อทดสอบการทำงาน โดยติดตั้งไลบรารี่เพิ่มเพื่อใช้งาน OLED คือ Adafruit_GFX และ Adafruit_SSD1331
( อ้างอิง Library จาก https://github.com/adafruit/ )
โค้ด: เลือกทั้งหมด
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1331.h"
#include "SPI.h" //มีมาให้ในโปรแกรม ArduinoIDE
#define sclk 13
#define mosi 11
#define cs 10
#define rst 9
#define dc 8
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);
float p = 3.1415926;
void setup(void) {
Serial.begin(9600);
Serial.print("hello!");
display.begin(); //เริ่มใช้งาน OLED
Serial.println("init");
uint16_t time = millis();
display.fillScreen(BLACK);
time = millis() - time;
Serial.println(time, DEC);
delay(500);
lcdTestPattern();
delay(1000);
display.fillScreen(BLACK);
display.setCursor(0,0);
display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
delay(1000);
// tft print function!
tftPrintTest();
delay(2000);
//a single pixel
display.drawPixel(display.width()/2, display.height()/2, GREEN);
delay(500);
// line draw test
testlines(YELLOW);
delay(500);
// optimized lines
testfastlines(RED, BLUE);
delay(500);
testdrawrects(GREEN);
delay(1000);
testfillrects(YELLOW, MAGENTA);
delay(1000);
display.fillScreen(BLACK);
testfillcircles(10, BLUE);
testdrawcircles(10, WHITE);
delay(1000);
testroundrects();
delay(500);
testtriangles();
delay(500);
Serial.println("done");
delay(1000);
}
void loop() {
}
void testlines(uint16_t color) {
display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(0, 0, x, display.height()-1, color);
}
for (int16_t y=0; y < display.height()-1; y+=6) {
display.drawLine(0, 0, display.width()-1, y, color);
}
display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(display.width()-1, 0, x, display.height()-1, color);
}
for (int16_t y=0; y < display.height()-1; y+=6) {
display.drawLine(display.width()-1, 0, 0, y, color);
}
display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(0, display.height()-1, x, 0, color);
}
for (int16_t y=0; y < display.height()-1; y+=6) {
display.drawLine(0, display.height()-1, display.width()-1, y, color);
}
display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(display.width()-1, display.height()-1, x, 0, color);
}
for (int16_t y=0; y < display.height()-1; y+=6) {
display.drawLine(display.width()-1, display.height()-1, 0, y, color);
}
}
void testtriangles() {
display.fillScreen(BLACK);
int color = 0xF800;
int t;
int w = display.width()/2;
int x = display.height();
int y = 0;
int z = display.width();
for(t = 0 ; t <= 15; t+=1) {
display.drawTriangle(w, y, y, x, z, x, color);
x-=4;
y+=4;
z-=4;
color+=100;
}
}
ผลที่ได้ จะมีการแสดงข้อความ และสีต่าง ๆ ที่ define ไว้ตอนต้นแบบเร็ว ๆ ( 0.5 วินาที ) และแสดงรูปสามเหลี่ยม แปลว่าจอ OLED สามารถทำงานได้แล้ว
หลังจากนี้แล้วแต่การนำไปใช้ของผู้ใช้งาน
บทความที่เกี่ยวข้อง
ส่วนประกอบ Arduino UNO R3
การติดตั้ง Library บน Arduino IDE
การติดตั้งโปรแกรม และการใช้งาน ArduinoIDE