OLED 128*64小屏(为 SSD1306 芯片驱动)
O1CN01DQO4Ye1mnaXP3CATb_!!2206918794999.jpg
【一、启用树莓派的I2C功能】
更新库命令: sudo apt-get install -y python-imaging python-smbus i2c-tools
开启I2C命令: sudo raspi-config
设置开启 I2C 功能
开启后,重启树莓派
命令:sudo reboot
【二、连接屏幕】
屏幕 GND 接 树莓派 GND
屏幕 VCC 接 树莓派 5V
屏幕 SDA 接 树莓派 SDA 在 IIC 通信中为数据管脚
屏幕 SCL 接 树莓派 SCL 在 IIC 通信中为时钟管脚
注意一定不要接反 VCC 和 GND,否则会烧坏屏幕。
下面通过命令检测是否识别到 I2C 设备。
命令: sudo i2cdetect -y 1
3c.png
将显示已连接设备的 I2C 地址,不同的屏幕地址可能会不一样,通过这个步骤可以确认一下。
【三 、克隆库】
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install
sudo pip install Adafruit-BBIO
【四、运行测试】
cd examples/
sudo python stats.py
image.png
【五、简单代码演示】
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# Raspberry Pi pin configuration:
RST = None # on the PiOLED this pin isnt used
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width #128
height = disp.height #64
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
#定义Y位置,也就是哪一行
y = -2
# 设置默认字体
font = ImageFont.load_default()
# 也可以输入指定位置的字体文件和字体大小
font16 = ImageFont.truetype('/usr/share/fonts/truetype/ttf-bitstream-vera/VeraBd.ttf', 16)
#程序循环开始
while True:
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
my_date=time.strftime("%Y-%m-%d",time.localtime()) #2020-09-23
my_time=time.strftime("%H:%M:%S",time.localtime()) #13:15:03
# Write two lines of text.
draw.text((28, y), my_time , font=font16, fill=255)
draw.text((16, y+16), my_date , font=font16, fill=255)
draw.text(( 0, y+32), "ABCDEFGHIJKLMNOPQRSTUVWXYZ" , font=font, fill=255)
draw.text(( 0, y+40), "abcdefghijklmnopqrstuvwxyz" , font=font, fill=255)
draw.text(( 0, y+56), "1234567890 1234567890 1234567890" , font=font, fill=255)
# 画多个点:[(点1x,点1y),(点2x,点2y)]
draw.point([(1,1),(2,2), (3, 3)],fill=255)
# 画线: 起点x,起点y,终点x,终点y
draw.line((0, 0, 50, 20), fill=255)
# 画椭圆: 左上角x,左上角y, 右下角x,右下角y
draw.ellipse((1, 1 , 50, 30), outline=255, fill=0)
# 画长方形: 左上角x,左上角y, 右下角x,右下角y
draw.rectangle((10, 10, 40, 20), outline=255, fill=0)
# 画三角形::角1x,y,角2x,y,角3x,y
draw.polygon([(0, 0), (0, 40), (30, 40)], outline=255, fill=0)
# Display image.
disp.image(image)
disp.display()
time.sleep(0.2)
我的液晶屏本身就是上黄下蓝,不是程序设置的。
image.png
【例子:用DHT11温湿度传感器获取温度湿度,显示在液晶屏上】
import RPi.GPIO as GPIO
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import dht11
# initialize GPIO
GPIO.setmode(GPIO.BCM)
#指定BCM 引脚编号 Pin 18
instance = dht11.DHT11(pin=18)
#read
#result = instance.read()
# Raspberry Pi pin configuration:
RST = None # on the PiOLED this pin isnt used
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width #128
height = disp.height #64
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Load default font.
font = ImageFont.load_default()
font16 = ImageFont.truetype('/usr/share/fonts/truetype/ttf-bitstream-vera/VeraBd.ttf', 16)
x = 0 # X坐标
y = 0 # Y坐标
i = 11 # 因为温度获取太频繁了,就会显示0度0湿度,所以设置每显示10次,才获取1次,防止出现00
while True:
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
my_date=time.strftime("%Y-%m-%d",time.localtime()) #2020-09-23
my_time=time.strftime("%H:%M:%S",time.localtime()) #13:15:03
# Write two lines of text.
draw.text((28, y), my_time , font=font16, fill=255)
draw.text((16, y+20), my_date , font=font16, fill=255)
#draw.text(( 0, y+32), "ABCDEFGHIJKLMNOPQRSTUVWXYZ" , font=font, fill=255)
#draw.text(( 0, y+40), "abcdefghijklmnopqrstuvwxyz" , font=font, fill=255)
#draw.text(( 0, y+56), "1234567890 1234567890 1234567890" , font=font, fill=255)
# 画椭圆: 左上角x,左上角y,宽,高
#draw.ellipse((30, 30 , 80, 60), outline=255, fill=0)
# 画长方形: 左上角x,左上角y,宽,高
#draw.rectangle((15, 15, 65, 45), outline=255, fill=0)
# 画多个点:[(点1x,点1y),(点2x,点2y)]
#draw.point([(1,1),(2,2), (3, 3)],fill=255)
# 画线: 起点x,起点y,终点x,终点y
#draw.line((0, 0, 50, 20), fill=255)
# 画三角形::角1x,y,角2x,y,角3x,y
#draw.polygon([(40, 15), (30, 40), (50, 40)], outline=255, fill=0)
# 每显示10次获取一次湿度和温度,防止太频繁变成00
if i>=10:
result = instance.read()
i=1
else :
i=i+1
draw.text((8, 45), "T:"+str(result.temperature)+"C" , font=font16, fill=255)
draw.text((70, 45), "H:"+str(result.humidity)+"%" , font=font16, fill=255)
# 画线: 起点x,起点y,终点x,终点y 画几条装饰线
draw.line((0, 18, 128, 18), fill=255)
draw.line((0, 40, 128, 40), fill=255)
draw.line((64, 40, 64, 64), fill=255)
draw.ellipse((46, 47 , 48, 49), outline=255, fill=0) #画温度符号上的小圆点
# Display image.
disp.image(image)
disp.display()
time.sleep(1)
image.png
网友评论