OLED-SSD1306顯示圖片和字串實作

之前黑白的LCD玩過了,也在成品上用上了,趁年假還有幾天,研究一下OLED。

OLED有很多種款式,常用的有0.91和0.96吋,目前我手上有的是0.96吋,所以
先實作這款來試試效果。
可以看到下圖只有個接點,它是用I2C為介面。



接線方式


須用到的庫

Adafruit SSD1306


Adafruit GFX Library

假如有出現Adafruit_I2CDevice.h的相關錯誤訊息,請在庫裡安裝

程式碼

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_I2CDevice.h>

#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTHSCREEN_HEIGHT, &Wire, OLED_RESET);
const unsigned char bitmap [] PROGMEM = {
  0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00
0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00
0x000x000x000x000x000x000x000x1e0x000x000x000x000x000x000x000x00
0x000x000x000x000x000x000x000x7f0x800x000x000x000x000x000x000x00
0x000x000x000x000x000x000x000xff0xc00x000x000x000x000x000x000x00
0x000x000x000x000x000x000x000xff0xc00x000x000x000x000x000x000x00
0x000x000x000x000x000x000x010xff0xc00x000x000x000x000x000x000x00
0x000x000x000x000x000x000x010xff0xc00x000x000x000x000x000x000x00
0x000x000x000x000x000x000x010xff0xc10xe00x000x000x000x000x000x00
0x000x000x000x000x000x000x000xff0xc70xf80x000x000x000x000x000x00
0x000x000x000x000x000x000x000xff0x870xf80x000x000x000x000x000x00
0x000x000x000x000x000x000x000x7f0x8f0xf80x000x000x000x000x000x00
0x000x000x000x000x000x000x000x7f0xef0xfc0x000x000x000x000x000x00
0x000x000x000x000x000x000x000xdf0xff0xfc0x000x000x000x000x000x00
0x000x000x000x000x000x000x010x0f0xff0xfc0x000x000x000x000x000x00
0x000x000x000x000x000x000x030x070xff0xf80x000x000x000x000x000x00
0x000x000x000x000x000x000x020x070xff0xf80x000x000x000x000x000x00
0x000x000x000x000x000x000x040x870xff0xf00x000x000x000x000x000x00
0x000x000x000x000x000x000x040x430xff0xe00x000x000x000x000x000x00
0x000x000x000x000x000x000x060x030x060x000x000x000x000x000x000x00
0x000x000x000x000x000x000x070x620x060x000x000x000x000x000x000x00
0x000x000x000x000x000x000x3f0xe00x420x000x000x000x000x000x000x00
0x000x000x000x000x000x000x7f0xc00x440x000x000x000x000x000x000x00
0x000x000x000x000x000x000x7e0x000x440x3e0x000x000x000x000x000x00
0x000x000x000x000x000x000x7e0x000xc00xc20x000x000x000x000x000x00
0x000x000x000x000x000x000x3e0x000xb90x810x000x000x000x000x000x00
0x000x000x000x000x000x000x0c0x010xbf0x010xe00x000x000x000x000x00
0x000x000x000x000x000x000x0c0x030x3e0x000xf00x000x000x000x000x00
0x000x000x000x000x000x000x030xbf0xfe0x400x700x000x000x000x000x00
0x000x000x000x000x000x000x000x7f0xfd0x800x500x000x000x000x000x00
0x000x000x000x000x000x000x000x7f0xfc0x900x300x000x000x000x000x00
0x000x000x000x000x000x000x000xfe0xee0x900x200x000x000x000x000x00
0x000x000x000x000x000x000x010xf80xee0x600x400x000x000x000x000x00
0x000x000x000x000x000x000x030xf00x4f0xf30x800x000x000x000x000x00
0x000x000x000x000x000x000x0f0xc00x4f0x1e0x000x000x000x000x000x00
0x000x000x000x000x000x000x1f0xc00x460x100x000x000x000x000x000x00
0x000x000x000x000x000x000x100x600x400x100x000x000x000x000x000x00
0x000x000x000x000x000x000x200x100x400x300x000x000x000x000x000x00
0x000x000x000x000x000x000x480x080x800x100x000x000x000x000x000x00
0x000x000x000x000x000x000x580x040x800x100x000x000x000x000x000x00
0x000x000x000x000x000x000x400x090x030xf00x000x000x000x000x000x00
0x000x000x000x000x000x000x000x190x030xc00x000x000x000x000x000x00
0x000x000x000x000x000x000x400x310xff0x800x000x000x000x000x000x00
0x000x000x000x000x000x000x400x330xff0x000x000x000x000x000x000x00
0x000x000x000x000x000x000x380x230xef0x000x000x000x000x000x000x00
0x000x000x000x000x000x000x070xef0xff0x7f0x000x000x000x000x000x00
0x000x000x000x000x000x000x000x1f0xff0x000x800x000x000x000x000x00
0x000x000x000x000x000x080x030xef0xc10x800x400x000x000x000x000x00
0x000x000x000x000x000x040x300x300xfe0x000x400x000x000x000x000x00
0x000x000x000x000x000x060x600x1f0x400x000x400x000x000x000x000x00
0x000x000x000x000x000x010xc00x000x600x000x400x000x000x000x000x00
0x000x000x000x000x000x000x800x000x400x000x800x000x000x000x000x00
0x000x000x000x000x000x000x800x000x400x010x000x000x000x000x000x00
0x000x000x000x000x000x000x400x000xc00x060x000x000x000x000x000x00
0x000x000x000x000x000x000x400x010xb00x380x000x000x000x000x000x00
0x000x000x000x000x000x000x300x030x0f0xe00x000x000x000x000x000x00
0x000x000x000x000x000x000x180x060x000x000x000x000x000x000x000x00
0x000x000x000x000x000x000x070x380x000x000x000x000x000x000x000x00
0x000x000x000x000x000x000x000x000x000x000x700x000x000x000x000x00
0x000x000x000x000x000x000x000x000x000x000x780x000x000x000x000x00
0x000x000x000x000x000x000x000x000x000x000x780x000x000x000x000x00
0x000x000x000x000x000x070x000x000x000x000x780x000x000x000x000x00
0x000x000x000x000x000x0f0xfe0x000x000x000x200x000x000x000x000x00
0x000x000x000x000x000x000x7e0x000x000x000x000x000x000x000x000x00
};


void setup(){
  display.begin(SSD1306_SWITCHCAPVCC0x3C);
  display.clearDisplay();
  delay(2000);
}

void loop(){
  display.drawBitmap(00, bitmap, 12864WHITE);
  // display.setTextSize(2);
  // display.setTextColor(1);
  // display.setCursor(1, 0);
  // display.print("hello OLED");
  // display.display();

}

#0x3C是OLED的位址
#128,64 是0.96吋的寛和高
#其中一大堆二進位碼是圖片轉換過來的,請參考下面解說

圖片轉二進位碼

這時可以用這個方便的網站來轉碼






將二進位編碼複制下來貼到程式碼Adafruit_SSD1306 display(SCREEN_WIDTHSCREEN_HEIGHT, &Wire, OLED_RESET);的下方

如果要顯現字型?

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_I2CDevice.h>

#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTHSCREEN_HEIGHT, &Wire, OLED_RESET);

void setup(){
  display.begin(SSD1306_SWITCHCAPVCC0x3C);
  display.clearDisplay();
  delay(2000);
}

void loop(){
  // display.drawBitmap(0, 0, bitmap, 128, 64, WHITE);
  display.setTextSize(2);
  display.setTextColor(1);
  display.setCursor(10);
  display.print("hello OLED");
  display.display();

}











留言

這個網誌中的熱門文章

平衡小車(balance-Robot)-基本平衡-Arduino