简介:本文将介绍如何使用树莓派和OLED显示屏来显示文字和图片。我们将通过安装必要的软件库和配置I2C接口来设置OLED显示屏,并使用Python代码来控制显示内容。
在使用树莓派和OLED显示屏之前,你需要准备以下工具和材料:
sudo apt-get install -y python-smbussudo apt-get install -y i2c-toolssudo apt-get install -y build-essential
在配置菜单中,选择“Advanced Options”,然后选择“I2C”,最后选择“Yes”。保存并退出配置菜单。
sudo raspi-config
安装完成后,你可以使用以下代码来控制OLED显示屏的显示内容:
sudo git clone https://github.com/adafruit/Adafruit_Python_SSD1306.gitcd Adafruit_Python_SSD1306sudo python setup.py install
import Adafruit_SSD1306from PIL import Image, ImageDraw, ImageFontimport smbus
bus = smbus.SMBus(1) # 0=I2C1, 1=I2C2, 2=SPI bus, 3=OHCI USB Bus (Windows only)address = 0x3C # OLED屏幕的I2C地址是0x3C,也可以尝试使用0x3A地址。
disp = Adafruit_SSD1306.SSD1306_128_64(address, bus)disp.begin() # 初始化OLED显示屏,并设置显示模式为128×64像素。disp.display(Image.new('RGB', (disp.width, disp.height))) # 清空屏幕。