目录
一、实验准备
- Wemos D1 R32 ESP32开发板
- OLED液晶屏
- 实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
二、实验代码
1.OLED液晶屏显示“Hello World”
/*
Wemos D1 R32 ESP32开发板
OLED液晶屏显示“Hello World”
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
void setup() {
//如果需要使用 128x32 显示,请将下面的行替换为 ssd1306_128x32_i2c_init()
ssd1306_128x64_i2c_init();
ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(ssd1306xled_font6x8);
ssd1306_printFixed (0, 16, " Hello World", STYLE_NORMAL);
}
void loop() {
}
2. OLED液晶满屏显示字符
/*
Wemos D1 R32 ESP32开发板
OLED液晶满屏显示字符
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
void setup(){
/* Replace the line below with ssd1306_128x32_i2c_init() if you need to use 128x32 display */
ssd1306_128x64_i2c_init();
ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(ssd1306xled_font6x8);
ssd1306_printFixed (0, 8, "Line 1. Normal text", STYLE_NORMAL);
ssd1306_printFixed (0, 16, "Line 2. Bold text", STYLE_BOLD);
ssd1306_printFixed (0, 24, "Line 3. Italic text", STYLE_ITALIC);
ssd1306_printFixedN (0, 32, "Line 4. Double size", STYLE_BOLD, FONT_SIZE_2X);
}
void loop(){
}
3.OLED屏显示时钟
/*
Wemos D1 R32 ESP32开发板
OLED屏显示时钟
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
uint32_t lastMillis;
uint8_t hours = 10;
uint8_t minutes = 36;
uint8_t seconds = 0;
void printSeconds() {
if (seconds & 1) {
ssd1306_printFixed(54, 8, ":", STYLE_NORMAL);
}
else {
ssd1306_printFixed(54, 8, " ", STYLE_NORMAL);
}
}
void printMinutes() {
char minutesStr[3] = "00";
minutesStr[0] = '0' + minutes / 10;
minutesStr[1] = '0' + minutes % 10;
ssd1306_printFixed(78, 8, minutesStr, STYLE_NORMAL);
}
void printHours() {
char hoursStr[3] = "00";
hoursStr[0] = '0' + hours / 10;
hoursStr[1] = '0' + hours % 10;
ssd1306_printFixed(6, 8, hoursStr, STYLE_NORMAL);
}
void setup() {
//如果您需要使用 128x32 显示,请将下面的行替换为 ssd1306_128x32_i2c_init()
ssd1306_128x64_i2c_init();
ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(comic_sans_font24x32_123);
lastMillis = millis();
printHours();
printMinutes();
}
void loop() {
if ((uint32_t)(millis() - lastMillis) >= 1000) {
lastMillis += 1000;
if (++seconds > 59)
{
seconds = 0;
if (++minutes > 59)
{
minutes = 0;
if (++hours > 23)
{
hours = 0;
}
printHours();
}
printMinutes();
}
printSeconds();
}
}
4.OLED屏显示矢量图片
/*
Wemos D1 R32 ESP32开发板
OLED屏显示矢量图片
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
const uint8_t Owl [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
0x0A, 0x05, 0x0D, 0x01, 0x01, 0x03, 0x87, 0xFE, 0xFE, 0xFC, 0xF8, 0xF0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0E,
0x08, 0x0C, 0x0C, 0x0C, 0x0E, 0x0F, 0x0F, 0x07, 0x07, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xE0, 0x30,
0x98, 0xDE, 0xE6, 0xE7, 0xF7, 0xD7, 0xD6, 0x56, 0x56, 0xD7, 0xD7, 0x5F, 0xDF, 0x3F, 0x3F, 0x2F,
0x9F, 0xD7, 0xDF, 0x6F, 0x6B, 0x6B, 0x7F, 0xF7, 0xF3, 0xF3, 0xE0, 0xEC, 0x98, 0x30, 0xE0, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x60, 0x70, 0x70, 0x60,
0x40, 0x60, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xC0, 0xC0, 0xE0, 0xF0, 0xE0, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFB, 0xE0,
0xDF, 0xB1, 0xEF, 0x5F, 0xB9, 0xB0, 0xA0, 0xE6, 0x6E, 0x2E, 0xB6, 0xB9, 0x9F, 0xAF, 0xA0, 0xA7,
0xBF, 0x99, 0xB6, 0xB6, 0xA6, 0xA6, 0xB0, 0xB0, 0xA9, 0xDF, 0xCF, 0xF0, 0x7F, 0x77, 0xFD, 0x01,
0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x08, 0xEE, 0xB3, 0x7D, 0xBE, 0x7F, 0xC7, 0x87, 0xB7,
0xB7, 0xB7, 0xCD, 0x7D, 0x83, 0x93, 0xFB, 0xCD, 0xB5, 0x35, 0xA5, 0x87, 0xCE, 0xFE, 0x1C, 0xF9,
0xC3, 0x1C, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1F, 0x3F, 0x3C, 0x73,
0xEF, 0x9E, 0x7E, 0xFD, 0xFD, 0xED, 0xAD, 0xFD, 0xDD, 0xFF, 0xBF, 0xFF, 0x5F, 0xDF, 0xEF, 0xFF,
0xFF, 0xFF, 0x6F, 0xFF, 0xDF, 0xEF, 0xFD, 0xDD, 0xFD, 0xBC, 0xFE, 0x7E, 0xBF, 0xEF, 0x7B, 0x3E,
0x1F, 0x0F, 0x07, 0x00, 0x00, 0x0E, 0x1F, 0x3C, 0x77, 0x5F, 0x3D, 0x7D, 0xFB, 0xFB, 0x7A, 0xFA,
0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0x7E, 0xFE, 0xBF, 0xFD, 0xF5, 0xF5, 0xF6, 0xFA, 0xFB, 0xDF,
0x67, 0x78, 0x3C, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xC0, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x40, 0x00, 0x10, 0x38, 0x80, 0x00, 0x00, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x81, 0x83, 0x83, 0x87, 0xE7, 0xEF, 0xEF, 0xEB, 0xFF, 0xF7, 0xDF,
0xFA, 0xFE, 0xFF, 0xEB, 0xEE, 0xEE, 0xE7, 0x67, 0x63, 0x61, 0x60, 0x60, 0x60, 0xE0, 0xE0, 0xE0,
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x71, 0x73, 0x7B,
0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7E, 0x7F, 0x77, 0x76, 0x73, 0x73, 0x71, 0x71, 0x70, 0x71, 0x70,
0x70, 0x70, 0x70, 0x70, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF1, 0xF1, 0xF1, 0xE0, 0xE0,
0xE8, 0xEC, 0xEE, 0xE7, 0xE2, 0xE4, 0xE8, 0xD0, 0xE0, 0xE0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0,
0x10, 0x10, 0x10, 0x10, 0x1F, 0x13, 0xA1, 0xAD, 0xC8, 0x49, 0x47, 0x42, 0x40, 0xC0, 0xDC, 0x78,
0x60, 0x60, 0x20, 0x21, 0x31, 0x30, 0x11, 0x1A, 0x1B, 0x0B, 0x0D, 0x0C, 0x04, 0x06, 0x06, 0x06,
0x03, 0x03, 0x03, 0x13, 0x31, 0x71, 0x71, 0x61, 0x81, 0x81, 0x41, 0x20, 0x26, 0x0C, 0x1C, 0x30,
0x78, 0x00, 0x00, 0x84, 0xC4, 0x84, 0x84, 0x0C, 0x04, 0x02, 0x02, 0x01, 0x01, 0x07, 0x0E, 0x0E,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xE0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xE0, 0xC0, 0xC0, 0x00, 0x00, 0x81, 0x81, 0x81, 0x41, 0x41,
0x21, 0x21, 0x13, 0x13, 0x13, 0x03, 0x0B, 0x0B, 0x0B, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x07, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x00, 0x00, 0x06, 0x07, 0x03, 0x03, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0C, 0x40, 0xEE, 0x79,
0x35, 0x02, 0x08, 0x08, 0x04, 0x04, 0x02, 0x1F, 0x3D, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x04, 0x24, 0x00, 0x0D, 0x30, 0x31, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0xC6, 0xC6, 0x36, 0x68, 0x44,
0xB4, 0xA0, 0x52, 0x62, 0x02, 0x02, 0x02, 0x00, 0x01, 0x03, 0x32, 0x7A, 0x3E, 0x1A, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x1D, 0x3D, 0x1D, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const uint8_t Soba [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x80,
0x09, 0x00, 0x00, 0x00, 0xc0, 0x0c, 0x00, 0x00,
0x00, 0x60, 0x06, 0x00, 0x00, 0x00, 0x30, 0x03,
0x00, 0x00, 0x00, 0x98, 0x01, 0x00, 0xf8, 0x00,
0xcc, 0x00, 0x00, 0xde, 0x03, 0x66, 0x00, 0x80,
0x07, 0x0f, 0x33, 0x00, 0xc0, 0x01, 0x9c, 0x19,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff,
0xff, 0xff, 0x07, 0xe0, 0xff, 0xff, 0xff, 0x07,
0xe0, 0xff, 0xff, 0xff, 0x07, 0xe0, 0xff, 0xff,
0xff, 0x07, 0xe0, 0xff, 0xff, 0xff, 0x07, 0xc0,
0xff, 0xff, 0xff, 0x03, 0xc0, 0xff, 0xff, 0xff,
0x03, 0x80, 0xff, 0xff, 0xff, 0x01, 0x80, 0xff,
0xff, 0xff, 0x01, 0x00, 0xff, 0xff, 0xff, 0x00,
0x00, 0xfe, 0xff, 0x7f, 0x00, 0x00, 0xfc, 0xff,
0x3f, 0x00, 0x00, 0xf8, 0xff, 0x1f, 0x00, 0x00,
0xf0, 0xff, 0x0f, 0x00, 0x00, 0xc0, 0xff, 0x03,
0x00, 0x00, 0x80, 0xff, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup(){
ssd1306_128x64_i2c_init();
}
void loop(){
ssd1306_clearScreen( );
ssd1306_drawBitmap(0, 0, 128, 64, Owl);
delay(2000);
ssd1306_clearScreen( );
ssd1306_drawXBitmap(0, 0, 40, 32, Soba);
delay(2000);
}
5 OLED屏显示自动翻滚文字字符
/*
OLED屏显示自动翻滚文字字符
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
#include "ssd1306_console.h"
void setup(){
/* Replace the line below with the display initialization function, you want to use */
ssd1306_128x64_i2c_init();
ssd1306_clearScreen();
/* Set font to use with console */
ssd1306_setFixedFont(ssd1306xled_font6x8);
}
void loop(){
ssd1306_print( "This is console output: " );
ssd1306_print( "go to the next line\n" );
delay(500);
}
6 OLED屏显示进度条
/*
OLED屏显示进度条
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
void setup(){
/* Replace the line below with ssd1306_128x32_i2c_init() if you need to use 128x32 display */
ssd1306_128x64_i2c_init();
ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(ssd1306xled_font6x8);
}
int progress = 0;
void loop(){
ssd1306_drawProgressBar( progress );
progress++;
if ( progress > 100 )
{
progress = 0;
delay( 1000 );
}
else
{
delay( 30 );
}
}
7.自动上滚计数器
/*
自动上滚计数器
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
#include "ssd1306_console.h"
Ssd1306Console console;
void setup(){
/* Replace the line below with the display initialization function, you want to use */
ssd1306_128x64_i2c_init();
ssd1306_clearScreen();
/* Set font to use with console */
ssd1306_setFixedFont(ssd1306xled_font6x8);
}
void loop(){
static uint8_t i = 0;
/* Here use any methods, provided by Arduino Print class */
console.print("Line ");
console.print( i );
i++;
delay(500);
console.println("");
}
8.弹来弹去的小心脏
/*
弹来弹去的小心脏
实验接脚: 0.96寸OLED液晶屏SDA接22, SCL接21
*/
#include "ssd1306.h"
/*
* Heart image below is defined directly in flash memory.
* This reduces SRAM consumption.
* The image is defined from bottom to top (bits), from left to
* right (bytes).
*/
const PROGMEM uint8_t heartImage[8] ={
0B00001110,
0B00011111,
0B00111111,
0B01111110,
0B01111110,
0B00111101,
0B00011001,
0B00001110
};
/*
* Define sprite width. The width can be of any size.
* But sprite height is always assumed to be 8 pixels
* (number of bits in single byte).
*/
const int spriteWidth = sizeof(heartImage);
/* Declare variable that represents our sprite */
SPRITE sprite;
int speedX = 1;
int speedY = 1;
void setup(){
ssd1306_128x64_i2c_init();
ssd1306_fillScreen(0x00);
/* Create sprite at 0,0 position. The function initializes sprite structure. */
sprite = ssd1306_createSprite( 0, 0, spriteWidth, heartImage );
/* Draw sprite on the display */
sprite.draw();
}
void loop(){
/* Move sprite every 40 milliseconds */
delay(15);
sprite.x += speedX;
sprite.y += speedY;
/* If right boundary is reached, reverse X direction */
if (sprite.x == (128 - spriteWidth)) speedX = -speedX;
/* If left boundary is reached, reverse X direction */
if (sprite.x == 0) speedX = -speedX;
/* Sprite height is always 8 pixels. Reverse Y direction if bottom boundary is reached. */
if (sprite.y == (64 - 8)) speedY = -speedY;
/* If top boundary is reached, reverse Y direction */
if (sprite.y == 0) speedY = -speedY;
/* Erase sprite on old place. The library knows old position of the sprite. */
sprite.eraseTrace();
/* Draw sprite on new place */
sprite.draw();
}
摘自最像Arduino Uno的ESP32开发板之WeMos D1 R32 - DF创客社区 - 分享创造的喜悦 (dfrobot.com.cn)
仅用于自身学习