sh1106

OLED的尺寸還不少,常見的0.96吋和0.91吋都屬於SSD1106系列,但常見的就有一款1.3吋的,這就麻煩了,它是SH1106模組,函式庫無法共用,傑森習慣上用wonho-maker改寫自Adafruit SSD1106的函式庫,因為用法和Adafruit SSD1106幾乎相同,很方便。

只要是I2C介面的OLED接線都一樣,只有4條線,SDA、SCL接到Arduino Uno板上的A4、A5,VCC接到3.3V,GND接GND,就搞定了。

oled-illu-1

先下載wonho-maker的SH1106函式庫,下庫網址:
https://github.com/wonho-maker/Adafruit_SH1106

而這個函式庫還要Adafruit GFX Library函式庫來配合,所以請一併安裝。

oled-lib2-1

因為要做圖形,會用到Image2LCD,Image2LCD下載網址:
http://www.e-paper-display.com/download_detail/downloadsId=625.html

程式和SSD1106幾乎一樣,比較細節的說明,請參考之前傑森寫的SSD1106的教學,這裡就不多說囉!

Arduino 運用SSD1306 OLED 0.96吋 、0.91吋

SH1106範例程式下載:
http://jmaker.banner.tw/doc/sh1106_128x64_13.zip

/*
 * 本範程式為Arduino結合1.3吋OLED的範例程式,由傑森創工提供
 * 本程式由wonho-maker改寫的Adafruit函式庫範例進行簡化,若需要進一步學習,建議參考sh1306_128x64_i2c範例
 * 
 * 1,請先安裝Adafruit GFX Library函式庫,可由Arduino IDE裡的「程式管理員」進行安裝
 * 2,請先安裝wonho-maker SH1306函式庫,下載網址:
 *    https://github.com/wonho-maker/Adafruit_SH1106
 * 
 * 粉絲團:https://www.facebook.com/jasonshow
 * 傑森創工購物網:https://www.jmaker.com.tw/
 * 傑森創工部落格:https://blog.jmaker.com.tw/
 */

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

// 設定OLED
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);

//自訂圖形,從Image2LCD轉過來的,尺寸是128x48
//本範例為了在上方區塊顯示文字,所以圖形就沒有涵蓋整個高度
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, };

#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);  // 一般1306 OLED的位址都是0x3C

  // 顯示Adafruit的LOGO,算是開機畫面
  display.display();
  delay(1000); // 停1秒

  // 清除畫面
  display.clearDisplay();

  // 測試文字
  display.clearDisplay();
  display.setTextSize(2);             // 設定文字大小
  display.setTextColor(1);        // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
  display.setCursor(0,0);             // 設定起始座標
  display.print("Hello OLED");        // 要顯示的字串
  display.display();                  // 要有這行才會把文字顯示出來
  delay(1000);
  
  //display.clearDisplay();
  //顯示圖形,x,y,圖形文字,寬,高,1:OLED預設的顏色(這個會依該OLED的顏色來決定)
  display.drawBitmap(0,16,logo_bmp, 128, 48, 1);
  display.display();  // 要有這行才會把文字顯示出來
  delay(1000);
}


void loop() {
  
}