Arduino Màn hình OLED SSD1309 128x64 | Hướng dẫn OLED I2C 2,42 inch

Màn hình OLED (diode phát quang hữu cơ) cung cấp các pixel tự phát sáng mang lại màu đen sâu, độ tương phản cao và góc nhìn rộng — làm cho nó trở thành một sự nâng cấp tuyệt vời so với LCD truyền thống. SSD1309 là IC điều khiển phổ biến được tìm thấy trên các module OLED I2C 128×64 có kích thước 2.42-inch (đôi khi được ghi là 2.4-inch).

màn hình oLED ssd1309 cho Arduino

Trong hướng dẫn từng bước này, bạn sẽ học cách kết nối và lập trình SSD1309 OLED 128×64 với một bo mạch Arduino bằng thư viện DIYables_OLED_SSD1309. Cụ thể, chúng ta sẽ đề cập đến:

Phần cứng cần chuẩn bị

1×Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×SSD1309 I2C OLED Display 128x64 (2.42 inch)
1×dây jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno
1×(Khuyến nghị) Breadboard Shield for Arduino Uno
1×(Khuyến nghị) Enclosure for Arduino Uno
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về màn hình OLED SSD1309 2,42 inch

SSD1309 là một IC driver OLED CMOS tích hợp một chip được thiết kế cho bảng ma trận điểm 128×64. Nó tương thích theo thanh ghi với SSD1306 được sử dụng rộng rãi, nên nhiều ví dụ mã hiện có có thể được áp dụng lại với tối thiểu các thay đổi. Các khác biệt phần cứng chủ yếu là:

  • Không có bộ tăng áp tích hợp — SSD1309 yêu cầu một nguồn VCC bên ngoài, mặc dù hầu hết các bo mạch breakout (bao gồm các module 2,42 inch và 2,4 inch) đều được trang bị bộ chuyển đổi tăng áp tích hợp, nên điều này sẽ không gây trở ngại cho bạn.
  • Dung sai điện áp cao hơn — SSD1309 có thể chấp nhận lên tới 16 V VCC, trong khi SSD1306 bị giới hạn ở khoảng 4,2 V.

Module OLED 2,42 inch (2,4 inch) thường sử dụng trình điều khiển SSD1309 và có độ phân giải 128×64 pixel với giao diện I2C. Màu của bảng hiển thị (trắng, xanh lam, vàng, xanh lá hoặc hai vùng) được xác định bởi vật liệu OLED thực tế và không thể điều khiển bằng phần mềm.

Hướng dẫn này giao tiếp với màn hình qua I2C bus, chỉ yêu cầu hai dây tín hiệu (SDA và SCL) và có thể chia sẻ bus với các thiết bị ngoại vi I2C khác.

Bố trí chân OLED SSD1309 (Mô-đun I2C)

Mô-đun OLED I2C SSD1309 2,42 inch điển hình có bốn chân:

  • GND — Kết nối với đường đất của Arduino.
  • VCC — Nguồn cấp. Kết nối tới 5 V của Arduino (hoặc 3.3 V, tùy thuộc vào bộ điều chỉnh trên bo mạch của module).
  • SCL — Dòng xung đồng hồ I2C.
  • SDA — Dòng dữ liệu I2C.
sơ đồ chân ssd1309 oLED

※ Lưu ý:

  • Thứ tự chân pin khác nhau giữa các nhà sản xuất. Hãy luôn xác nhận các nhãn in silkscreen trên mô-đun của bạn trước khi nối dây.
  • Hướng dẫn này đã được xác minh với màn hình OLED SSD1309 2,42 inch từ DIYables. Các mô-đun dựa trên SSD1309 có kích thước 2,4/2,42 inch khác nhau sẽ hoạt động tương tự.

Sơ đồ nối dây — Arduino và SSD1309 OLED 128×64

  • Sơ đồ nối dây giữa Arduino và OLED SSD1309 2,42 inch 128×64
sơ đồ kết nối Arduino với oLED ssd1309 128x64

This image is created using Fritzing. Click to enlarge image

  • Ảnh chụp dây nối thực tế giữa Arduino và OLED SSD1309 128×64
kết nối thực tế của Arduino ssd1309 oLED 128x64

This image is created using Fritzing. Click to enlarge image

Khi sử dụng một biến thể Arduino khác, các chân I2C sẽ thay đổi. Bảng dưới đây liệt kê các kết nối đúng:

OLED Module Arduino Uno, Nano Arduino Mega
Vin 5V 5V
GND GND GND
SDA A4 20
SCL A5 21

Hướng dẫn bắt đầu — SSD1309 OLED với Arduino

Bước 1: Cài đặt thư viện DIYables_OLED_SSD1309

  • Mở Arduino IDE và nhấp vào biểu tượng Thư viện ở thanh bên trái.
  • "DIYables_OLED_SSD1309" vào hộp tìm kiếm và tìm thư viện được phát hành bởi DIYables.
  • Nhấp vào nút Cài đặt.
thư viện oLED ssd1309 cho Arduino
  • IDE sẽ nhắc bạn cài đặt phụ thuộc bắt buộc (Adafruit GFX Library). Nhấp vào Cài đặt tất cả để tiếp tục.
thư viện gfx của adafruit cho Arduino

Bước 2: Mẫu Lập trình Cơ bản

Mọi bản phác thảo điều khiển SSD1309 đều theo cùng một mẫu: bao gồm các header, tạo một đối tượng hiển thị, khởi tạo nó trong setup(), vẽ vào bộ đệm ngoài màn hình, sau đó đẩy bộ đệm lên màn hình bằng display().

  • Bao gồm các header bắt buộc:
#include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h>
  • Đặt kích thước màn hình (128×64 cho mô-đun 2,42 inch):
#define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  • Tạo đối tượng hiển thị (truyền -1 khi không có chân reset được kết nối):
// Create an SSD1309 display instance on the default I2C bus DIYables_OLED_SSD1309 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  • Khởi tạo màn hình trong hàm setup():
// Initialise the SSD1309 — use address 0x3C (default for most modules) if (!oled.begin(SSD1309_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1309 allocation failed")); while (true); }
  • Từ bây giờ, bạn có thể tự do gọi bất kỳ hàm vẽ nào (clearDisplay(), drawPixel(), print(), v.v.), sau đó gọi oled.display() để cập nhật màn hình.

※ Lưu ý:

Toàn bộ mã ví dụ trong hướng dẫn này nhắm tới OLED SSD1309 128×64 (2,42 inch) và sử dụng thư viện DIYables_OLED_SSD1309.

Mã Arduino — Xin chào thế giới trên OLED SSD1309

Điểm khởi đầu đơn giản nhất: in một vài dòng văn bản với các kích thước khác nhau.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Hello World * Prints text in two sizes on the 2.42 inch 128x64 I2C OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(1); // 6x8 pixels per character display.setTextColor(SSD1309_PIXEL_ON); // turn pixels on display.setCursor(0, 0); display.println(F("Hello, World!")); display.println(); display.setTextSize(2); // 12x16 pixels per character display.println(F("DIYables")); display.setTextSize(1); display.println(); display.println(F("SSD1309 OLED 128x64")); display.display(); // push buffer to screen } void loop() { }

Mã Arduino — Hiển thị văn bản trên OLED SSD1309

Ví dụ sau đây trình diễn thêm các tính năng văn bản — nhiều kích thước chữ, định dạng số và macro F() để tiết kiệm RAM.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Text Demo * Demonstrates text display features on 2.42" SSD1309 128x64 I2C OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } delay(2000); // wait for initializing display.clearDisplay(); // clear display display.setTextSize(1); // text size = 1 display.setTextColor(SSD1309_PIXEL_ON); // set text color display.setCursor(0, 10); // set position to display display.println(F("Text size = 1")); // display text display.display(); // update display delay(2000); display.setTextSize(2); // text size = 2 display.setCursor(0, 30); // set position to display display.println(F("Size = 2")); // display text display.display(); // update display delay(2000); display.clearDisplay(); // clear display display.setTextSize(1); display.setCursor(0, 0); display.println(F("Display integer:")); display.println(12345); display.println(); display.println(F("Display float:")); display.println(1.2345); display.println(); display.println(F("Display HEX:")); display.println(0xABCD, HEX); display.display(); // update display } void loop() { }

Tham khảo các hàm hiển thị hữu ích

Dưới đây là danh sách tham khảo nhanh các hàm được sử dụng nhiều nhất khi làm việc với OLED SSD1309 thông qua thư viện DIYables:

  • oled.clearDisplay() — xóa bộ đệm khung hình (tất cả các điểm ảnh đều tắt).
  • oled.display() — chuyển bộ đệm lên OLED để các thay đổi được hiển thị.
  • oled.drawPixel(x, y, color) — thiết lập hoặc xóa một điểm ảnh riêng lẻ.
  • oled.setTextSize(n) — phóng chữ theo hệ số *n* (1 = 6×8, 2 = 12×16, …, tối đa 8).
  • oled.setCursor(x, y) — di chuyển con trỏ chữ tới tọa độ pixel *(x, y)*.
  • oled.setTextColor(SSD1309_PIXEL_ON) — màu chữ chỉ (nền trong suốt).
  • oled.setTextColor(SSD1309_PIXEL_OFF, SSD1309_PIXEL_ON) — chữ có nền màu được chỉ định.
  • oled.println("message") — in một chuỗi và xuống dòng tiếp theo.
  • oled.println(number) — in một số nguyên ở hệ thập phân.
  • oled.println(number, HEX) — in một số nguyên ở hệ thập lục phân.
  • oled.startscrollright(start, stop) — bắt đầu cuộn phải theo phần cứng từ trang *start* đến trang *stop*.
  • oled.startscrollleft(start, stop) — bắt đầu cuộn trái theo phần cứng từ trang *start* đến trang *stop*.
  • oled.startscrolldiagright(start, stop) — bắt đầu cuộn chéo phải theo phần cứng từ trang *start* đến trang *stop*.
  • oled.startscrolldiagleft(start, stop) — bắt đầu cuộn chéo trái theo phần cứng từ trang *start* đến trang *stop*.
  • oled.stopscroll() — dừng mọi cuộn theo phần cứng đang hoạt động.
  • oled.setContrast(value) — điều chỉnh độ sáng hiển thị (0–255).
  • oled.dim(true/false) — tối nhanh màn hình về mức tối thiểu hoặc khôi phục độ tương phản trước đó.
  • oled.invertDisplay(true/false) — đảo ngược màu ở cấp độ phần cứng (điểm ảnh sáng ↔ tối).

Cách căn giữa chữ theo chiều dọc và chiều ngang trên OLED SSD1309

Xem Cách căn giữa theo chiều dọc và chiều ngang cho văn bản và số trên OLED

Mã Arduino — Vẽ các hình trên OLED SSD1309

Vì thư viện DIYables_OLED_SSD1309 mở rộng Adafruit_GFX, bạn có được một tập hợp đầy đủ các hình cơ bản để vẽ: điểm ảnh, đường thẳng, hình chữ nhật, hình chữ nhật được tô, hình tròn, hình tròn được tô, tam giác, tam giác được tô, và hình chữ nhật bo góc tròn. Bản phác thảo dưới đây sẽ luân phiên hiển thị qua tất cả chúng với các trình diễn động.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Draw Shapes * Cycles through every shape primitive on the 2.42" SSD1309 128x64 OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); } void loop() { // --- Pixels --- display.clearDisplay(); for (int16_t i = 0; i < display.width(); i += 4) { display.drawPixel(i, i * display.height() / display.width(), SSD1309_PIXEL_ON); } display.display(); delay(1500); // --- Lines --- display.clearDisplay(); for (int16_t i = 0; i < display.width(); i += 8) { display.drawLine(0, 0, i, display.height() - 1, SSD1309_PIXEL_ON); } for (int16_t i = 0; i < display.height(); i += 8) { display.drawLine(0, 0, display.width() - 1, i, SSD1309_PIXEL_ON); } display.display(); delay(1500); // --- Rectangles --- display.clearDisplay(); for (int16_t i = 0; i < display.height() / 2; i += 4) { display.drawRect(i, i, display.width() - 2 * i, display.height() - 2 * i, SSD1309_PIXEL_ON); } display.display(); delay(1500); // --- Filled rectangles (inverse) --- display.clearDisplay(); for (int16_t i = 0; i < display.height() / 2; i += 6) { display.fillRect(i, i, display.width() - 2 * i, display.height() - 2 * i, SSD1309_PIXEL_INVERSE); } display.display(); delay(1500); // --- Circles --- display.clearDisplay(); for (int16_t i = max(display.width(), display.height()) / 2; i > 0; i -= 5) { display.drawCircle(display.width() / 2, display.height() / 2, i, SSD1309_PIXEL_ON); } display.display(); delay(1500); // --- Filled circles (inverse) --- display.clearDisplay(); for (int16_t i = max(display.width(), display.height()) / 2; i > 0; i -= 6) { display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1309_PIXEL_INVERSE); } display.display(); delay(1500); // --- Rounded rectangles --- display.clearDisplay(); for (int16_t i = 0; i < display.height() / 2 - 2; i += 4) { display.drawRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i, display.height() / 4, SSD1309_PIXEL_ON); } display.display(); delay(1500); // --- Filled rounded rectangles (inverse) --- display.clearDisplay(); for (int16_t i = 0; i < display.height() / 2 - 2; i += 4) { display.fillRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i, display.height() / 4, SSD1309_PIXEL_INVERSE); } display.display(); delay(1500); // --- Triangles --- display.clearDisplay(); for (int16_t i = 0; i < max(display.width(), display.height()) / 2; i += 5) { display.drawTriangle( display.width() / 2, display.height() / 2 - i, display.width() / 2 - i, display.height() / 2 + i, display.width() / 2 + i, display.height() / 2 + i, SSD1309_PIXEL_ON); } display.display(); delay(1500); // --- Filled triangles (inverse) --- display.clearDisplay(); for (int16_t i = max(display.width(), display.height()) / 2; i > 0; i -= 6) { display.fillTriangle( display.width() / 2, display.height() / 2 - i, display.width() / 2 - i, display.height() / 2 + i, display.width() / 2 + i, display.height() / 2 + i, SSD1309_PIXEL_INVERSE); } display.display(); delay(1500); }

Mã Arduino — Cuộn phần cứng trên OLED SSD1309

SSD1309 có một động cơ cuộn tích hợp, cho phép dịch nội dung hiển thị mà không làm tăng tải cho CPU. Thư viện DIYables cung cấp bốn hướng cuộn: phải, trái, chéo phải và chéo trái. Mỗi hướng nhận tham số trang bắt đầu và trang kết thúc (các trang là dải ngang cao 8 pixel được đánh số từ 0 đến 7 trên màn hình cao 64 pixel).

※ Lưu ý:

Luôn gọi display() để đẩy nội dung của bạn lên OLED trước khi bắt đầu cuộn. Tránh vẽ nội dung mới trong khi cuộn đang hoạt động — hãy gọi stopscroll() trước.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Scroll Text * Demonstrates all four hardware scroll directions on the 2.42" OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(10, 24); display.println(F("DIYables")); display.display(); delay(2000); } void loop() { // Scroll right across all pages display.startscrollright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Scroll left display.startscrollleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll right display.startscrolldiagright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll left display.startscrolldiagleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); }

Mã Arduino — Hiển thị hình bitmap trên OLED SSD1309

Để hiển thị bitmap trên OLED SSD1309, trước hết bạn cần chuyển đổi hình ảnh của mình thành một mảng byte C. Sử dụng công cụ trực tuyến miễn phí image2cpp online tool để thực hiện việc này:

  1. Tải lên hình ảnh của bạn (PNG, JPG, BMP, v.v.).
  2. Đặt kích thước khung vẽ là 128×64 (hoặc nhỏ hơn).
  3. Chọn Mã Arduino làm định dạng đầu ra.
  4. Sao chép mảng được tạo ra vào sketch của bạn.
chuyển hình ảnh thành mảng bitmap

Ví dụ dưới đây luân phiên giữa một biểu tượng hình trái tim kích thước 16×16 và một logo DIYables có chiều rộng toàn bộ:

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Image Demo * Displays bitmap images on the 2.42" SSD1309 128x64 I2C OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // 16x16 heart bitmap const unsigned char heart16x16[] PROGMEM = { 0x00, 0x00, 0x03, 0xc0, 0x0f, 0xf0, 0x1f, 0xf8, 0x3f, 0xfc, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x03, 0xc0, 0x00, 0x00 }; // 128x64 DIYables logo bitmap const unsigned char diyablesLogo[] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xf8, 0x00, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x7f, 0xf0, 0x00, 0x1f, 0xff, 0xff, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0xf0, 0x00, 0x7f, 0xff, 0xff, 0xe0, 0x00, 0x0f, 0xc0, 0x00, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x3f, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x07, 0x80, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x03, 0x80, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x1f, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x07, 0xff, 0xf0, 0x7f, 0xff, 0x00, 0x0f, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x7f, 0xff, 0x80, 0x0f, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x1f, 0xff, 0xf0, 0x7f, 0xff, 0x80, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x3f, 0xff, 0xf0, 0x7f, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x7f, 0xff, 0xf0, 0x7f, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xfe, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xfe, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x01, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xfe, 0x00, 0x00, 0xff, 0xff, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xfe, 0x00, 0x00, 0xff, 0xff, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xfe, 0x00, 0x00, 0xff, 0xff, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x01, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xff, 0xf0, 0x7f, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xff, 0xf0, 0x7f, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x7f, 0xff, 0xf0, 0x7f, 0xff, 0xc0, 0x07, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x3f, 0xff, 0xf0, 0x7f, 0xff, 0x80, 0x07, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x1f, 0xff, 0xf0, 0x7f, 0xff, 0x80, 0x07, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x0f, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xe0, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x1f, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x07, 0x80, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xe0, 0x00, 0x7f, 0xff, 0xff, 0xf0, 0x00, 0x0f, 0xc0, 0x00, 0x3f, 0xff, 0xff, 0xf8, 0x00, 0x3f, 0xf0, 0x00, 0x3f, 0xff, 0xff, 0xe0, 0x00, 0x1f, 0xe0, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x00, 0x3f, 0xf8, 0x00, 0x1f, 0xff, 0xff, 0x80, 0x00, 0x3f, 0xe0, 0x00, 0x07, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0xf8, 0x00, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x7f, 0xfc, 0x00, 0x03, 0xff, 0xf8, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } delay(2000); display.setCursor(0, 0); } void loop() { // Display 16x16 heart icon display.clearDisplay(); display.drawBitmap((SCREEN_WIDTH - 16) / 2, (SCREEN_HEIGHT - 16) / 2, heart16x16, 16, 16, SSD1309_PIXEL_ON); display.display(); delay(2000); // Display full-screen DIYables logo display.clearDisplay(); display.drawBitmap(0, 0, diyablesLogo, 128, 64, SSD1309_PIXEL_ON); display.display(); delay(2000); // Invert display display.invertDisplay(true); delay(2000); display.invertDisplay(false); delay(500); }

※ Lưu ý:

  • Kích thước bitmap không được vượt quá độ phân giải màn hình (128×64 cho mô-đun 2,42 inch).

Mã Arduino — Độ tương phản và làm mờ trên OLED SSD1309

SSD1309 hỗ trợ 256 mức độ tương phản (0–255). Thư viện DIYables cung cấp setContrast() để kiểm soát chi tiết và dim() để chuyển nhanh giữa mức sáng tối thiểu và mức đã được cấu hình trước đó.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Contrast & Dim * Sweeps contrast from 0→255→0 then toggles dim mode on the 2.42" OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } // Draw a test pattern so the contrast changes are visible display.clearDisplay(); display.fillRect(0, 0, 64, 32, SSD1309_PIXEL_ON); display.fillRect(64, 32, 64, 32, SSD1309_PIXEL_ON); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_INVERSE); display.setCursor(16, 28); display.println(F("Contrast Demo")); display.display(); delay(2000); } void loop() { // Ramp up for (int c = 0; c <= 255; c += 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Ramp down for (int c = 255; c >= 0; c -= 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Quick dim toggle display.dim(true); // minimum brightness delay(2000); display.dim(false); // restore delay(2000); }

Mã Arduino — Phông chữ bên ngoài tùy chỉnh trên OLED SSD1309

Thư viện Adafruit GFX đi kèm với hàng chục kiểu chữ FreeFont có thể phóng to thu nhỏ (Serif, Sans, Mono — mỗi loại có Regular, Bold, Italic và bốn kích thước). Bạn có thể kích hoạt bất kỳ kiểu chữ nào trong số chúng trên màn hình SSD1309 bằng cách bao gồm header tương ứng và gọi setFont().

※ Lưu ý:

  • Khi một phông chữ bên ngoài đang được kích hoạt, tọa độ Y của con trỏ tham chiếu đến đường cơ sở của chữ, không phải góc trên-trái. Điều này khác với phông chữ 5×7 tích hợp sẵn.
  • Các phông chữ bên ngoài được lưu trong flash (PROGMEM). Trên các bo mạch AVR có bộ nhớ giới hạn (Arduino Uno = 32 KB flash), hãy sử dụng chúng một cách tiết kiệm.
/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – External Fonts * Cycles through three FreeFont typefaces on the 2.42" SSD1309 OLED. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #include <Fonts/FreeSerif9pt7b.h> #include <Fonts/FreeSansBold12pt7b.h> #include <Fonts/FreeMono9pt7b.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // ── Built-in 5×7 font ── display.clearDisplay(); display.setFont(NULL); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Built-in 5x7 font")); display.println(); display.setTextSize(2); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeSerif 9pt ── display.clearDisplay(); display.setFont(&FreeSerif9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); // Y = baseline display.println(F("FreeSerif 9pt")); display.setCursor(0, 38); display.println(F("DIYables OLED")); display.setCursor(0, 58); display.println(F("Hello World!")); display.display(); delay(3000); // ── FreeSansBold 12pt ── display.clearDisplay(); display.setFont(&FreeSansBold12pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 20); display.println(F("SansBold")); display.setCursor(0, 52); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeMono 9pt ── display.clearDisplay(); display.setFont(&FreeMono9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); display.println(F("FreeMono 9pt")); display.setCursor(0, 34); display.println(F("0123456789")); display.setCursor(0, 54); display.println(F("!@#$%^&*()")); display.display(); delay(3000); }

Khắc phục sự cố OLED SSD1309

Nếu không thấy gì trên màn hình OLED SSD1309 2,42 inch sau khi tải lên bản phác thảo của bạn, hãy thực hiện các kiểm tra sau:

  • Kiểm tra dây dẫn — xác nhận SDA, SCL, VCCGND được kết nối đúng với các chân trên Arduino.
  • Xác nhận chip điều khiển — thư viện này được thiết kế cho SSD1309. Nếu mô-đun của bạn sử dụng một bộ điều khiển khác (ví dụ SH1106), nó sẽ không đáp ứng đúng.
  • Kiểm tra địa chỉ I2C — hầu hết các module SSD1309 có địa chỉ mặc định là 0x3C, nhưng một số dùng 0x3D. Chạy đoạn mã quét I2C dưới đây để phát hiện địa chỉ thực:
// I2C address scanner — prints every detected device to the Serial Monitor #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices = 0; Serial.println("Scanning..."); for (address = 1; address < 127; address++) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address, HEX); Serial.println(" !"); nDevices++; } else if (error == 4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (nDevices == 0) Serial.println("No I2C devices found"); else Serial.println("done"); delay(5000); }

Kết quả mong đợi trên Serial Monitor khi phát hiện SSD1309:

COM6
Send
Scanning... I2C device found at address 0x3C ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Đảm bảo gọi hàm display() — SSD1309 sử dụng một bộ đệm khung hình. Các hàm vẽ chỉ chỉnh sửa bộ đệm trong RAM; không có gì hiển thị trên màn hình cho đến khi bạn gọi oled.display().
  • Kiểm tra nguồn cấp điện — mô-đun 2,42 inch tiêu thụ nhiều dòng điện hơn so với các OLED nhỏ hơn. Đảm bảo nguồn cung cấp của bạn có thể cung cấp đủ dòng điện (thông thường từ 20–40 mA ở độ sáng tối đa).