接著需下載兩個 Library 如下連結
https://github.com/adafruit/Adafruit_SSD1306
https://github.com/adafruit/Adafruit-GFX-Library
解壓縮後丟到 C:\Program Files (x86)\Arduino\libraries 裡面即可
需注意我的 OLED 為 128*64 若你不是的話可能需要修改
Adafruit_SSD1306.h 中的第 #27-33 行 (假設你的是 96_16則要修改成以下)
// ONE of the following three lines must be #defined:
//#define SSD1306_128_64
//#define SSD1306_128_32
#define SSD1306_96_16 ///< DEPRECATED: old way to specify 96x16 screen
// This establishes the screen dimensions in old Adafruit_SSD1306 sketches
// (NEW CODE SHOULD IGNORE THIS, USE THE CONSTRUCTORS THAT ACCEPT WIDTH
// AND HEIGHT ARGUMENTS).
若你跟我一樣是128_64,那這個部份就可以不用動了
程式碼如下
#include "spi.h"
#include "wire.h"
#include "adafruit_ssd1306.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define ADJ_PIN A0
Adafruit_SSD1306 Display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
int r = 0;
void setup() {
Display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Display.setTextColor(WHITE);
Display.display();
delay (1000);
Display.clearDisplay();
}
void loop() {
r = analogRead(ADJ_PIN);
Display.setTextSize(1);
Display.setCursor(0, 0);
Display.println("Voltage(mV)");
Display.setTextSize(3);
Display.setCursor(0, 32);
Display.println(Format(r, 3, 1));
Display.display();
Display.clearDisplay();
}
String Format(double val, int dec, int dig ) {
// this is my simple way of formatting a number
// data = Format(number, digits, decimals) when needed
int addpad = 0;
char sbuf[20];
String fdata = (dtostrf(val, dec, dig, sbuf));
int slen = fdata.length();
for ( addpad = 1; addpad <= dec + dig - slen; addpad++) {
fdata = " " + fdata;
}
return (fdata);
}
結果如下
我將定期推出程式語言的新手教學影片