Arduino的顯示方式最常見的是用1602 LCD,第二順位應該就是OLED了。OLED的優點在於顯示效果非常漂亮,還能顯示圖片,尺寸雖然不大,但完全不影響閱讀效果,所以相當受開發者的喜愛。
市面上OLED常見的有0.96吋、0.91吋,還有1.3吋。0.96和0.91吋都屬於SSD1306晶片規格,可用相同的函式庫,1.3吋是SH1106,傑森會再寫另一篇來介紹。
OLED接線很簡單,它是用I2C介面,所以只有4條線,SDA、SCL接到Arduino Uno板上的A4、A5,VCC接到3.3V,GND接GND,就搞定了。
再來就是安裝函式庫了,這裡傑森選的是Adafruit提供的函式庫,大家也可以選擇u8glib,但傑森發現u8glib對於圖形文字檔要求比較嚴格,所以現在都改用Adafruit SSD1306函式庫;而這個函式庫還要Adafruit GFX Library函式庫來配合,所以請一併安裝。
再來就要寫程式囉!基本上0.96吋和0.91吋的程式都相同,只有在設定尺寸和座標時有差別。
- 0.96吋:128x64
- 0.91吋:128x32
因為我們要在畫面中顯示圖形,所以要先把圖形轉成文字陣列,傑森認為Image2LCD這套最好用,大家可以試試。
Image2LCD下載網址:
http://www.e-paper-display.com/download_detail/downloadsId=625.html
先在繪圖軟體中畫好圖,存成JPG格式,在Image2LCD打開,基本上只有「輸出灰度」改成「單色」,寬度和高度設成128、64,然後就可以進行「保存」了。然後把得到的陣列COPY到我們的程式中即可。
我們先以0.96吋為例,傑森簡化了範例程式ssd1306_128x64_i2c,只留下顯示文字和顯示圖形。
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
// 設定OLED
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//自訂圖形,從Image2LCD轉過來的,尺寸是128x48,少掉的16個像素是上方黃色區塊(假設是用藍黃雙色的OLED)
//本範例為了在上方區塊顯示文字,所以圖形就沒有涵蓋整個高度
static const unsigned char PROGMEM logo_bmp[] =
{
0XFF,0XFF,0XFF,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XF8,0X7F,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XF8,0X3F,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFE,0XFF,0XF8,0X3F,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFE,0X1F,0XFC,0X7F,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFC,0X03,0XFD,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XE4,0X00,0XFB,0XFF,0XFF,0X83,0X00,0X18,0X00,0X60,0X00,0X38,0X02,0X00,0X00,0X00,
0XE4,0X00,0X1B,0XFF,0XFF,0X83,0X7E,0X18,0X3F,0XFF,0XC0,0XFE,0X03,0X0F,0XFF,0XFC,
0XCC,0X00,0X03,0XFF,0XFF,0X83,0X7F,0XFE,0X7F,0XFF,0XC3,0XC7,0XB3,0X0F,0XFF,0XFC,
0XC8,0X7C,0X00,0XFF,0XFF,0X83,0X46,0XFC,0X01,0XF8,0X07,0XFE,0XB3,0X00,0X0C,0X00,
0XC8,0XFE,0X00,0X1F,0XFF,0X86,0XE6,0XD8,0X03,0XFE,0X00,0XFE,0X33,0X00,0X0C,0X00,
0X99,0XFF,0X00,0X07,0XFF,0X87,0XF4,0XD8,0X1F,0X67,0X80,0X00,0X33,0X00,0X0C,0X00,
0X91,0XFF,0X00,0X00,0XFF,0X8E,0X1C,0XD8,0X7C,0X61,0XE3,0XFF,0X33,0X00,0X0C,0X00,
0X91,0XFF,0X00,0X00,0X1F,0X8E,0X1B,0XFE,0X64,0X62,0X23,0XFF,0XB3,0X00,0X0C,0X00,
0XE1,0XFF,0X00,0X00,0X1F,0X9E,0X31,0XFE,0X0C,0X26,0X03,0XFF,0XB3,0X00,0X0C,0X00,
0XE0,0XFE,0X01,0X80,0X1F,0X9E,0XE1,0X18,0X7F,0X9F,0XE3,0XFF,0XB3,0X00,0X0C,0X00,
0XE0,0X7C,0X03,0XE0,0X3F,0X86,0X43,0X08,0XFF,0X9F,0XE3,0X01,0XB3,0X00,0X0C,0X00,
0XC0,0X38,0X07,0XE0,0X3F,0X86,0XFF,0XFC,0X0C,0X07,0X03,0XFF,0XB3,0X00,0X0C,0X00,
0XF0,0X00,0X07,0XF0,0X4F,0X86,0XFF,0XFC,0X1E,0X0F,0X03,0XFF,0X33,0X00,0X0C,0X00,
0XFE,0X00,0X07,0XE0,0X4F,0X86,0X07,0X80,0X3F,0X9F,0X83,0X00,0X33,0X00,0X0C,0X00,
0XC7,0XC0,0X07,0XE0,0X9F,0X86,0X0F,0XC0,0X3D,0X9E,0XC2,0X7F,0X33,0X00,0X0C,0X00,
0X81,0XF0,0X03,0XC0,0X9F,0X86,0X3F,0X70,0X6C,0X36,0XE6,0X7F,0X83,0X00,0X0C,0X00,
0X80,0X3E,0X00,0X01,0X9F,0X86,0XF3,0X3C,0XCC,0X66,0X66,0X61,0X83,0X00,0X0C,0X00,
0XC0,0X07,0X80,0X01,0X3F,0X86,0XE3,0X0C,0X0C,0X06,0X06,0X7F,0X83,0X1F,0XFF,0XFE,
0XF8,0X01,0XF0,0X01,0X31,0X86,0X03,0X00,0X0C,0X06,0X06,0X7F,0XBE,0X1F,0XFF,0XFE,
0XBF,0X00,0X3E,0X03,0XF1,0X86,0X03,0X00,0X04,0X02,0X00,0X41,0X1C,0X00,0X00,0X00,
0X8F,0XC0,0X0F,0X83,0XF1,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XCC,0XE0,0X01,0XF7,0XF3,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XCC,0X1C,0X00,0X3F,0XF1,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X8D,0XC7,0XC0,0X0F,0XF0,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X8E,0X38,0XF8,0X0F,0XF8,0X83,0X00,0X00,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X83,0X8E,0X1E,0X0F,0XFF,0X82,0X00,0X00,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X80,0X71,0XC7,0XDF,0XF3,0X80,0X00,0X00,0X30,0X00,0X00,0X00,0X00,0X00,0X30,0X00,
0X80,0X1C,0X67,0XFF,0XF9,0X83,0X7D,0XCF,0X33,0X39,0XE0,0XE3,0X9F,0X70,0X7A,0X26,
0X81,0XE3,0X8C,0X7F,0XC7,0X83,0X6E,0XC9,0XB6,0X6D,0XE1,0X86,0XDD,0XD8,0X33,0X66,
0X81,0XFC,0XF8,0X1F,0X83,0X83,0X66,0X61,0XBC,0X4D,0X81,0X0C,0X59,0X98,0X33,0X74,
0X80,0XFE,0X13,0X1C,0X47,0X83,0X66,0X6F,0XBC,0XFD,0X83,0X0C,0X79,0X98,0X33,0X54,
0X80,0X3E,0X00,0X24,0X6F,0X83,0X66,0X69,0XB6,0XC1,0X81,0X0C,0X59,0X98,0X31,0XDC,
0X83,0X3E,0X00,0X64,0X7F,0X83,0X66,0X79,0XB6,0X61,0X8D,0X86,0XD9,0X9B,0X31,0X98,
0X83,0X70,0X00,0X47,0XFF,0X83,0X66,0X6F,0XB3,0X39,0X8C,0XE3,0X99,0X9B,0X19,0X98,
0X87,0XF0,0X00,0XC7,0XFF,0X83,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X87,0XF0,0X00,0X8F,0XFF,0X86,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X81,0XE0,0X01,0XFF,0XFF,0X84,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X80,0X60,0X03,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X80,0X00,0X03,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X80,0X00,0X67,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, };
void setup() {
Serial.begin(9600);
// 偵測是否安裝好OLED了
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 一般1306 OLED的位址都是0x3C
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// 顯示Adafruit的LOGO,算是開機畫面
display.display();
delay(1000); // 停1秒
// 清除畫面
display.clearDisplay();
testdrawstyles(); // 測試文字
testdrawbitmap(); // 顯示圖形
}
void loop() {
}
void testdrawstyles(void) {
display.clearDisplay();
display.setTextSize(2); // 設定文字大小
display.setTextColor(1); // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.setCursor(0,0); // 設定起始座標
display.print("Hello OLED"); // 要顯示的字串
display.display(); // 要有這行才會把文字顯示出來
delay(1000);
}
void testdrawbitmap(void) {
//display.clearDisplay();
//顯示圖形,x,y,圖形文字,寬,高,1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.drawBitmap(0,16,logo_bmp, 128, 48, 1);
display.display(); // 要有這行才會把文字顯示出來
delay(1000);
}
程式中傑森都有簡單做了註解,感該不難懂的。
如果要用的是0.91吋的OLED,#define SCREEN_HEIGHT 64 這行改成#define SCREEN_HEIGHT 32,就可以了。當然圖形尺寸要注意一下就是了。
0.96吋OLED範例程式下載:
http://jmaker.banner.tw/doc/ssd1306_128x64_i2c.zip
0.91吋OLED範例程式下載:
http://jmaker.banner.tw/doc/ssd1306_128x32_i2c.zip