食用指南
- 配置清单
- 硬件
- 硬件链接
- 软件
- 所需软件(Windows系统)
- 所需软件(Raspbian系统)
- 开启i2C功能
- 查看i2c地址
- 安装Adafruit_Python_SSD1306库
- 屏幕显示
- python程序运行
- 命令
- 后台运行命令
- 退出ssh,仍可运行命令
- 实际应用效果图(微博热搜)
配置清单
硬件
- 树莓派4B x 1台
- 四针脚0.96寸12468屏幕 x 1块
- 母对母杜邦线 x 4条
硬件链接
- 树莓派引脚图
- 屏幕细节尺寸图
- 针脚连接方法
二、引脚定义及连接方法
屏幕针脚 | 含义 | 连接树莓派针脚位置 |
---|---|---|
VCC | 电源(3.3V~5V) | 左一 |
GND | 地 | 左五 |
SCL | IIC时钟信号 | 左三 |
SDA | IIC数据信号 | 左二 |
软件
连接好硬件设备,我们开始软件方面的配置。
所需软件(Windows系统)
- PUTTY
- FileZilla
有了以上两个软件,我们便可以和树莓派系统实现互联互通,通过Windows来控制树莓派和文件的相互拷贝
所需软件(Raspbian系统)
通过命令我们来安装屏幕所需要的驱动:
开启i2C功能
sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo raspi-config
查看i2c地址
sudo i2cdetect -y 1
安装Adafruit_Python_SSD1306库
sudo apt-get update
sudo apt-get install build-essential python-dev python-pip
sudo pip install RPi.GPIO
sudo apt-get install python-imaging python-smbus
sudo apt-get install git
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install
python如果是3.x版本,pip需要改成pip3
屏幕显示
安装好Adafruit_Python_SSD1306库后,cd examples进入例程目录,ls查看文件。
下面以shapes.py例程说明。
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Image
import ImageDraw
import ImageFont
# Raspberry Pi pin configuration:
RST = 24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
# Beaglebone Black pin configuration:
# RST = 'P9_12'
# Note the following are only used with SPI:
# DC = 'P9_15'
# SPI_PORT = 1
# SPI_DEVICE = 0
# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# 128x64 display with hardware I2C:
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Alternatively you can specify an explicit I2C bus number, for example
# with the 128x32 display you would use:
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=2)
# 128x32 display with hardware SPI:
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
# 128x64 display with hardware SPI:
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
# Alternatively you can specify a software SPI implementation by providing
# digital GPIO pin numbers for all the required display pins. For example
# on a Raspberry Pi with the 128x32 display you might use:
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, sclk=18, din=25, cs=22)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = padding
# Draw an ellipse.
draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
x += shape_width+padding
# Draw a rectangle.
draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
x += shape_width+padding
# Draw a triangle.
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0)
x += shape_width+padding
# Draw an X.
draw.line((x, bottom, x+shape_width, top), fill=255)
draw.line((x, top, x+shape_width, bottom), fill=255)
x += shape_width+padding
# Load default font.
font = ImageFont.load_default()
# Alternatively load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
#font = ImageFont.truetype('Minecraftia.ttf', 8)
# Write two lines of text.
draw.text((x, top), 'Hello', font=font, fill=255)
draw.text((x, top+20), 'World!', font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
按照你的oled屏修改代码,程序默认是12832的,你的oled屏是这个就不用改直接运行就OK。
如果是12864的I2C就像下面那样修改,把12832加#注释,12864#注释去掉保存。
# 128x32 display with hardware I2C:
#disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
终端输入sudo python shapes.py或用Python打开文件运行就能看到OLED屏有显示了。
python程序运行
命令
python test1.py
后台运行命令
python test.py &
退出ssh,仍可运行命令
nohup python test.py >null 2>&1 &