基于ESP-WROOM-32的双串口通信并显示到OLED显示屏上

本文详细介绍了如何在ESP32开发板上配置Arduino环境,包括驱动库安装,以及如何使用SSD1306OLED模块进行串口通信,展示了从串口监视器接收数据并在OLED上显示的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发板

ESP-WROOM-32

引脚图

在这里插入图片描述

Arduino环境配置

1.ESP32开发版下载

选择 esp32 by Espressif Systems

2.Arduino开发板选择 -> ESP32 Dev Module

Flash Mode选择DIO
连接ESP32并选择指定端口

3.安装驱动库

ESP8266 and ESP32 OLED driver for SSD1306 displays by ThingPulse, Fabrice Weinberg

版本:4.4.0以上

接线图

ESP32开发版OLED模块串口1串口2
GNDGND
3V3VCC
GPIO22SCL
GPIO21SDA
TypeC接口TypeC
GPIO17RX
GPIO16TX

Arduino代码


#include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h"        // legacy: #include "SSD1306.h"


SSD1306Wire display(0x3c, SDA, SCL);   // ADDRESS, SDA, SCL  -  SDA and SCL 

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  Serial.println();

  // Initialising the UI will init the display too.
  display.init();

  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);

}

void loop() {
  // clear the display
  
  //从串口监视器读取输入数据
    if(Serial.available()){
      char data = Serial.read();
      display.clear();
      display.flipScreenVertically();
      display.setTextAlignment(TEXT_ALIGN_LEFT);
      display.setFont(ArialMT_Plain_10);
      display.drawString(0, 0, "Serial.read:");
      display.drawString(0, 12, "Serial2.read:");
      display.drawString(65, 0, &data);
      display.display();
      //将数据发送到 UART2
      Serial2.write(data);
    }

    //从UART2读取输入数据
    if(Serial2.available()){
      char data = Serial2.read();
      display.clear();
      display.flipScreenVertically();
      display.setTextAlignment(TEXT_ALIGN_LEFT);
      display.setFont(ArialMT_Plain_10);
      display.drawString(0, 0, "Serial.read:");
      display.drawString(0, 12, "Serial2.read:");
      display.drawString(70, 12, &data);
      display.display();
      //将数据发送到 UART2
      Serial.write(data);
    }

}

现象演示

将代码烧录到ESP中,打开Arduino中的端口监视器,另外再打开一个串口调试工具
清空两端的显示区域,并相互发送字符
在这里插入图片描述
在OLED中看到对应刚发送的内容,如下图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值