Arduino OLED显示屏是一种非常流行的显示模块,它具有低功耗、高分辨率的特点,非常适合用于各种电子项目中。无论是初学者还是有一定基础的爱好者,都能通过简单的步骤轻松地将Arduino与OLED显示屏连接起来。下面,我们就来一步步揭秘Arduino OLED显示屏的连接教程,让你从入门到精通,小白也能快速上手!
一、准备工作
在开始连接之前,我们需要准备以下材料:
- Arduino开发板(如Arduino Uno、Arduino Nano等)
- OLED显示屏(如0.96英寸、1.3英寸等)
- 杜邦线(用于连接Arduino和OLED显示屏)
- USB线(用于给Arduino供电)
- 电源适配器(可选,用于给OLED显示屏供电)
二、硬件连接
连接OLED显示屏与Arduino:
- 将OLED显示屏的VCC引脚连接到Arduino的5V引脚。
- 将OLED显示屏的GND引脚连接到Arduino的GND引脚。
- 将OLED显示屏的SCL引脚连接到Arduino的A5引脚。
- 将OLED显示屏的SDA引脚连接到Arduino的A4引脚。
连接电源:
- 使用USB线将Arduino连接到电脑,通过USB供电。
- 如果OLED显示屏需要外部供电,可以使用电源适配器将OLED显示屏的VCC引脚连接到5V电源。
三、软件配置
安装Arduino IDE:
- 如果还没有安装Arduino IDE,请从官方网站下载并安装。
安装OLED显示屏库:
- 打开Arduino IDE,选择“文件”>“首选项”>“管理板”。
- 在“板管理器”中搜索“Adafruit OLED”库,并安装。
编写代码:
- 在Arduino IDE中创建一个新的草图。
- 将以下代码复制到草图编辑器中:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Initialize OLED display with I2C address 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes the display buffer, and prints
// the initial text. This will be overwritten, however, by
// the text() function below.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer.
display.clearDisplay();
// Draw a single pixel
display.drawPixel(10, 10, WHITE);
display.display();
delay(2000);
}
void loop() {
// Display initial message
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Hello, World!");
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
// Draw a rectangle
display.drawRect(0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1, WHITE);
display.display();
delay(2000);
// Draw a circle
display.drawCircle(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, SCREEN_WIDTH/4, WHITE);
display.display();
delay(2000);
// Fill the screen with a solid color
display.fillScreen(WHITE);
display.display();
delay(2000);
display.fillScreen(BLACK);
display.display();
delay(2000);
}
上传代码:
- 将Arduino连接到电脑,选择正确的板子和端口。
- 点击“上传”按钮,将代码上传到Arduino。
四、总结
通过以上步骤,你已经成功地将Arduino与OLED显示屏连接起来,并运行了一个简单的示例程序。接下来,你可以根据自己的需求,编写更复杂的程序,实现更多的功能。希望这篇教程能帮助你从入门到精通,轻松掌握Arduino OLED显示屏的连接和使用!