树莓派之OLED显示
硬件资源:树莓派3B+板子一个,7针 128*64 SPI OLED屏一个,杜邦线若干
继上次做了一个车牌识别之后续,上次实现车牌识别之后,又加了一个OLED屏显示内容的
树莓派自带SPI 和IIC接口,我做的是用SPI实现的,OLED是以SSD1306为主控芯片的OLED。
附上Adafruit公司教程一份教程地址
参考
下面直接上操作,一些东西不理解SPI和IIC的可以自行百度,我就不误人子弟了,只有操作。
首先打开自己树莓派的SPI
打开树莓派命令行 输入
sudo raspi-config
点击是,保存退出
查看是否打开SPI
ls /dev
出现这个之后就说明打开了
打开之后连接树莓派和OLED屏
注意别接错线,这里说的GPIO4,5,6是指的BCM编码,我就一开始接错了,搞了半天不出字,都差点判OLED坏掉了。接好线之后打开命令行
pip3 install adafruit-circuitpython-ssd1306
如果没有pip3的可以
sudo apt-get install python3-pip
下载Pillow Library库
sudo apt-get install python3-pil
如果出现错误,看看错误提示是什么,缺啥装啥,我用的是pip3 python3.7.3
For the best performance, especially if you are doing fast animations, you’ll want to tweak the I2C core to run at 1MHz. By default it may be 100KHz or 400KHz
To do this edit the config with
sudo nano /boot/config.txt
and add to the end of the file
dtparam=i2c_baudrate=1000000
Ctrl+C 退出 Y保存 不改名退出
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
# Define the Reset Pin
oled_reset = digitalio.DigitalInOut(board.D4)
#oled_reset 16
# Change these
# to the right size for your display!
WIDTH = 128
HEIGHT = 64 # Change to 64 if needed
BORDER = 5
# Use for I2C.
#i2c = board.I2C()
#oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)
# Use for SPI
spi = board.SPI()
oled_cs = digitalio.DigitalInOut(board.D5)
oled_dc = digitalio.DigitalInOut(board.D6)
oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)
# Clear display.
oled.fill(0)
oled.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new("1", (oled.width, oled.height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a white background
draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
# Draw a smaller inner rectangle
draw.rectangle(
(BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
outline=1,
fill=0,
)
# Load default font.
font = ImageFont.load_default()
#font=ImageFont.truetype(none,15)
# Draw Some Text
text = "Hello World!"
(font_width, font_height) = font.getsize(text)
draw.text(
(oled.width // 2 - font_width // 2, oled.height // 2 - font_height // 2),
text,
font=font,
fill=255,
)
# Display image
oled.image(image)
oled.show()
详情请看最上面那个案例教程
结果
我踩过的坑
出现错误No module named "XXX’,可真是让我巨头疼,咋着都解决不掉用import XXX啥包都报错
后来发现自己的编译器是3.5.3而我下载的包都是3.7.3 之后用了笨方法。。。装了一个2020年12月份的新系统。。。编译器变成3.7.3然后还是报错。。。。。。。
之后我又重新安装了一遍包,就是缺什么安装什么
pip install XXX
还是报这个错误,然后我仔细看了一遍安装过程发现包的安装的路径都在python2.7下面然后
pip3 install XXX
重新把所有需要的包都安装一遍,错误消失。