BPI-Leaf-S3 OLED显示电位器电压与实时进度条,ESP32-S3 & MicroPython

BPI-Leaf-S3 全部可用GPIO引脚都可用作I2C 接口,默认的使用GPIO15作为SDA信号线,GPIO16作为SCL时钟线,同时板载一个JST SH 1mm 4-Pin连接座对应连接了这两个GPIO,使用此连接座可以更稳固的连接I2C设备。

SSD1306 OLED屏幕模块是一个很常见的可以使用I2C通信协议的屏幕模块,最大输出128*64 bit的图像,无灰阶,单颗像素仅有亮灭两个状态,控制逻辑相对简单,很适合入门学习单片机驱动屏幕显示的项目。

继续使用上一贴 BPI-Leaf-S3 用电位器无极调控彩灯亮度 中用ADC检测电位器电压的方法,可以设计一个让OLED屏幕显示电位器电压与实时进度条的程序。

接线参考

Potentiometer Board
GND GND
VCC 3V3
S GPIO1
SSD1306 OLED Board
GND GND
VCC 3V3
SCL 16
SDA 15

MicroPython

Micropython 运行环境搭建 - Banana Pi Wiki (banana-pi.org)

MicroPython ADC 文档

MicroPython framebuf 文档

micropython/ssd1306.py 驱动

from machine import Pin,ADC,I2C
from ssd1306 import SSD1306_I2C
import time

adc1 = ADC(Pin(1),atten=ADC.ATTN_11DB)

sda_pin=Pin(15,Pin.PULL_UP)
scl_pin=Pin(16,Pin.PULL_UP)

i2c = I2C(1,sda=sda_pin, scl=scl_pin, freq=800_000)
print(i2c.scan())
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

#Init, white background
oled.fill(1)
oled.rect(0,32,128,10,0)

while True:
    #Read ADC
    adc1_read = adc1.read() # 12bit
    adc1_read_mv = adc1.read_uv()//1000
    adc1_read_u16 = adc1.read_u16() # 16bit
    
    #Set progress bar
    bar_width = round (adc1_read / 4095 * 128)
    oled.fill_rect(bar_width,33,128-bar_width,8,0)
    oled.fill_rect(0,33,bar_width,8,1)
    
    #Set ADC text, centered
    text_adc1 = str(adc1_read_mv) + " mV"
    start_x_text_adc1 = 64 - len(text_adc1)*4
    oled.fill_rect(36,24,56,8,1)
    oled.text(text_adc1,start_x_text_adc1,24,0)
    
    #Show
    oled.show()
    
    print(adc1_read,adc1_read_u16,adc1_read_mv,"mv",bar_width,"width")
    time.sleep(0.05)

视频演示

BPI-Leaf-S3 开发板 - Banana Pi Wiki (banana-pi.org)