Arduino UNO R4 OLED 128x32

Hướng dẫn này sẽ chỉ cho bạn cách sử dụng Arduino UNO R4 với màn hình OLED 128x32 I2C. Bạn sẽ học:

Arduino UNO R4 oLED i2c màn hình

Phần Cứng Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Màn Hình OLED SSD1306 I2C 128x32
1×Dây Cắm Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

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

Giới Thiệu Về Màn Hình OLED

Sơ Đồ Chân Màn Hình OLED I2C

  • Chân GND: nên được kết nối với mass của Arduino UNO R4
  • Chân VCC: là nguồn cung cấp cho màn hình, chúng ta kết nối với chân 5V trên Arduino UNO R4.
  • Chân SCL: là chân xung clock nối tiếp cho giao diện I2C.
  • Chân SDA: là chân dữ liệu nối tiếp cho giao diện I2C.
oLED sơ đồ chân

※ Lưu ý:

Cách sắp xếp chân trên module OLED có thể khác nhau tùy thuộc vào nhà sản xuất và model của module. Luôn kiểm tra và làm theo nhãn trên module OLED. Hãy chú ý!

Hướng dẫn này dành cho màn hình OLED sử dụng driver SSD1306 I2C. Chúng tôi đã test với màn hình OLED của DIYables. Nó hoạt động hoàn hảo không có vấn đề gì.

Sơ Đồ Đấu Nối

Arduino UNO R4 oLED 128x32 sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Nếu bạn sử dụng loại Arduino UNO R4 khác, sơ đồ chân sẽ không giống như Uno. Xem bảng dưới đây để biết thông tin về các mẫu Arduino UNO R4 khác.

Module OLED 128x32 Arduino UNO R4
Vin 5V
GND GND
SDA A4
SCL A5

Cách Sử Dụng OLED Với Arduino UNO R4

Cài Đặt Thư Viện SSD1306 OLED

  • Vào biểu tượng Libraries ở phía bên trái của Arduino IDE.
  • Gõ "SSD1306" vào ô tìm kiếm và tìm thư viện SSD1306 do Adafruit làm.
  • Nhấn nút Install để thêm thư viện.
Arduino UNO R4 oLED thư viện
  • Bạn sẽ cần cài đặt thêm một số thư viện khác.
  • Nhấn nút Install All để cài đặt tất cả các thư viện cần thiết.
Arduino UNO R4 adafruit gfx sensor thư viện

Cách lập trình cho OLED

  • Bao gồm thư viện.
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
  • Thiết lập kích thước màn hình cho OLED 128x32.
#define SCREEN_WIDTH 128 // Định nghĩa chiều rộng của màn hình OLED tính bằng pixel #define SCREEN_HEIGHT 32 // Định nghĩa chiều cao của màn hình OLED tính bằng pixel
  • Tạo đối tượng SSD1306 OLED.
// Khởi tạo đối tượng màn hình Adafruit SSD1306 cho giao tiếp I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  • Trong hàm setup(), thiết lập màn hình OLED.
// Khởi động màn hình OLED với địa chỉ I2C cụ thể (0x3C) cho độ phân giải 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); // In thông báo lỗi nếu khởi tạo thất bại while (true); // Vào vòng lặp vô tận để dừng thực thi tiếp theo }
  • Sau đó bạn có thể hiển thị văn bản, hình ảnh và vẽ đường.

Mã Arduino UNO R4 - Hiển Thị Văn Bản Trên OLED

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display oled.println("Hello World!"); // text to display oled.display(); // show on OLED } void loop() { }

Dưới đây là một số hàm bạn có thể sử dụng để hiển thị văn bản trên OLED:

  • oled.clearDisplay(): tắt tất cả pixels.
  • oled.drawPixel(x, y, color): vẽ một pixel tại tọa độ x, y.
  • oled.setTextSize(n): thay đổi kích thước văn bản, với lựa chọn từ 1 đến 8.
  • oled.setCursor(x, y): đặt điểm bắt đầu cho văn bản.
  • oled.setTextColor(WHITE): làm cho màu văn bản trở thành trắng.
  • oled.setTextColor(BLACK, WHITE): làm cho màu văn bản thành đen và nền trắng.
  • oled.println("message"): hiển thị văn bản.
  • oled.println(number): hiển thị một số.
  • oled.println(number, HEX): hiển thị một số ở định dạng thập lục phân.
  • oled.display(): cập nhật màn hình với các thay đổi.
  • oled.startscrollright(start, stop): di chuyển văn bản từ trái sang phải.
  • oled.startscrollleft(start, stop): di chuyển văn bản từ phải sang trái.
  • oled.startscrolldiagright(start, stop): di chuyển văn bản chéo từ dưới-trái lên trên-phải.
  • oled.startscrolldiagleft(start, stop): di chuyển văn bản chéo từ dưới-phải lên trên-trái.
  • oled.stopscroll(): dừng mọi văn bản cuộn.

Mã Arduino UNO R4 - Vẽ Trên OLED

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.setCursor(0, 0); } void loop() { // draw rectangle oled.clearDisplay(); oled.drawRect(0, 15, 60, 40, WHITE); oled.display(); delay(2000); // fill rectangle oled.clearDisplay(); oled.fillRect(0, 15, 60, 40, WHITE); oled.display(); delay(2000); // draw the round rectangle oled.clearDisplay(); oled.drawRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(2000); // fill the round rectangle oled.clearDisplay(); oled.fillRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(2000); // draw circle oled.clearDisplay(); oled.drawCircle(20, 35, 20, WHITE); oled.display(); delay(2000); // fill circle oled.clearDisplay(); oled.fillCircle(20, 35, 20, WHITE); oled.display(); delay(2000); // draw triangle oled.clearDisplay(); oled.drawTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(2000); // fill triangle oled.clearDisplay(); oled.fillTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(2000); }

Mã Arduino UNO R4 – Hiển Thị Hình Ảnh

Để hiển thị hình ảnh trên màn hình OLED, trước tiên hãy chuyển đổi hình ảnh (bất kỳ định dạng nào) thành mảng bitmap. Bạn có thể sử dụng công cụ Image to Bitmap Converter này để chuyển đổi. Xem hình bên dưới để thấy cách chuyển một hình ảnh thành mảng bitmap. Tôi đã chuyển biểu tượng Arduino thành mảng bitmap.

image to bitmap array

Sao chép mã mảng mới và cập nhật nó trong mảng biểu tượng Arduino trong code bên dưới.

Video dưới đây hiển thị cách thực hiện với màn hình OLED 128x64 và Arduino Uno và biểu tượng Arduino

Chúng ta có thể làm tương tự để nó hoạt động với Arduino Uno R4 và OLED 128x32. Code dưới đây hiển thị biểu tượng DIYables trên OLED 128x32

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // bitmap of DIYable-icon image int bitmap_width = 72; // MUST match to bitmap image size int bitmap_height = 32; // MUST match to bitmap image size const unsigned char bitmap_DIYables [] PROGMEM = { // 'DIYables Icon', 72x32 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xf8, 0x07, 0x38, 0x07, 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xfe, 0x07, 0x1c, 0x0e, 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xff, 0x07, 0x1c, 0x1c, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x07, 0x87, 0x0e, 0x1c, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x03, 0xc7, 0x0f, 0x38, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x07, 0x38, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x03, 0xf0, 0xf0, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x03, 0xe0, 0xfc, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x01, 0xe0, 0xfe, 0x0f, 0xff, 0xff, 0x8e, 0x03, 0xc7, 0x01, 0xc0, 0xff, 0x8f, 0xff, 0xff, 0x8e, 0x03, 0x87, 0x01, 0xc0, 0xff, 0x8f, 0xff, 0xff, 0x8e, 0x0f, 0x87, 0x01, 0xc0, 0xff, 0xcf, 0xff, 0xff, 0x8f, 0xff, 0x07, 0x01, 0xc0, 0xff, 0xef, 0xff, 0xff, 0x8f, 0xfc, 0x07, 0x01, 0xc0, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0x0e, 0x0c, 0x0c, 0xc3, 0x07, 0xff, 0xef, 0xff, 0xfe, 0x0f, 0xec, 0xec, 0x99, 0x7f, 0xff, 0xef, 0xff, 0xfe, 0x0f, 0x04, 0xe4, 0x81, 0x0f, 0xff, 0xcf, 0xff, 0xfc, 0x0e, 0x32, 0xe4, 0x9f, 0xc7, 0xff, 0x8f, 0xff, 0xf8, 0x0e, 0x32, 0x4c, 0x9b, 0x67, 0xff, 0x0f, 0xff, 0xf0, 0x0e, 0x04, 0x0c, 0xc3, 0x0f, 0xfe, 0x0f, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xfc, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing } void loop() { oled.clearDisplay(); // display bitmap to the center int x = (SCREEN_WIDTH - bitmap_width) / 2; int y = (SCREEN_HEIGHT - bitmap_height) / 2; oled.drawBitmap(x, y, bitmap_DIYables, bitmap_width, bitmap_height, WHITE); oled.display(); delay(2000); }

※ Lưu ý:

  • Kích thước hình ảnh phải nhỏ hơn hoặc bằng kích thước màn hình.
  • Để sử dụng code đã cho cho OLED 128x32, bạn phải thay đổi kích thước hình ảnh và điều chỉnh chiều rộng và chiều cao trong hàm oled.drawBitmap();.

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

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display } void loop() { // display string String text = "DIYables"; oledDisplayCenter(text); delay(2000); // display number int number = 21; String str = String(number); oledDisplayCenter(str); delay(2000); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Khắc Phục Sự Cố OLED

Nếu màn hình OLED không hiển thị gì, vui lòng làm theo các bước sau:

  • Đảm bảo đấu nối được thực hiện đúng cách.
  • Xác nhận rằng OLED I2C của bạn được trang bị driver SSD1306.
  • Xác minh địa chỉ I2C của OLED bằng cách sử dụng code quét địa chỉ I2C sau trên Arduino UNO R4.
// I2C address scanner program #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; 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); // wait 5 seconds for next scan }

Kết quả trên Serial Monitor:

COM6
Send
Scanning... I2C device found at address 0x3C ! done Scanning... I2C device found at address 0x3C ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline