All GPIO pins of BPI-Leaf-S3 can be used as I2C interface. By default, GPIO15 is used as the SDA signal line, and GPIO16 is used as the SCL clock line. And there is a JST SH 1mm 4-Pin connector onboard used to connect the two GPIO pins. In this way, I2C devices can be fixed more firmly.
The SSD1306 is a very common OLED screen module that can use the I2C communication protocol. The maximum output image is 128*64 bit, no grayscale, and a single pixel only has two states of on and off. The control logic is not difficult. So it is suitable for the beginners to learn the project of screen display driven by microcontroller.
In the previous page, we have designed a program to control the brightness of lamp steplessly - use ADC to detect the voltage of potentiometer. On this basis, we can design a program to implement the functions of showing the voltage of potentiometer and real-time progress bar on OLED screen.
How to connect
Potentiometer | Board |
---|---|
GND | GND |
VCC | 3V3 |
S | GPIO1 |
SSD1306 OLED | Board |
---|---|
GND | GND |
VCC | 3V3 |
SCL | 16 |
SDA | 15 |
Micropython
Micropython establishment of operating environment - Banana Pi Wiki (banana-pi.org)
Micropython framebuf documents
Micropython ssd1306.py on Github
Micropython code
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)
video demo:
Banana Pi BPI-Leaf-S3 wiki page: https://wiki.banana-pi.org/BPI-Leaf-S3