一、配置清单

1、硬件

  • 树莓派 4B x 1 台
  • 四针脚 0.96 寸 12468 屏幕 x 1 块
  • 母对母杜邦线 x 4 条

2、硬件链接

树莓派4B搭配124*68屏幕 - 图1

树莓派4B搭配124*68屏幕 - 图2

二、引脚定义及连接方法

屏幕针脚 含义 连接树莓派针脚位置
VCC 电源 (3.3V~5V) 左一
GND 左五
SCL IIC 时钟信号 左三
SDA IIC 数据信号 左二

二、树莓派配置

通过命令我们来安装屏幕所需要的驱动:

1、开启 i2C 功能

  1. sudo apt-get install -y python-smbus
  2. sudo apt-get install -y i2c-tools
  3. sudo raspi-config

树莓派4B搭配124*68屏幕 - 图3

树莓派4B搭配124*68屏幕 - 图4

2、查看 i2c 地址

  1. sudo apt-get install -y python-smbus
  2. sudo apt-get install -y i2c-tools
  3. sudo raspi-config


树莓派4B搭配124*68屏幕 - 图5

3、安装 Adafruit_Python_SSD1306 库

  1. sudo apt-get update
  2. sudo apt-get install build-essential python-dev python-pip
  3. sudo pip install RPi.GPIO
  4. sudo apt-get install python-imaging python-smbus
  5. sudo apt-get install git
  6. git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
  7. cd Adafruit_Python_SSD1306
  8. sudo python setup.py install

python 如果是 3.x 版本,pip 需要改成 pip3

屏幕显示

安装好 Adafruit_Python_SSD1306 库后,cd examples 进入例程目录,ls 查看文件。
下面以 shapes.py 例程说明。

  1. import time
  2. import Adafruit_GPIO.SPI as SPI
  3. import Adafruit_SSD1306
  4. import Image
  5. import ImageDraw
  6. import ImageFont
  7. # Raspberry Pi pin configuration:
  8. RST = 24
  9. # Note the following are only used with SPI:
  10. DC = 23
  11. SPI_PORT = 0
  12. SPI_DEVICE = 0
  13. # Beaglebone Black pin configuration:
  14. # RST = 'P9_12'
  15. # Note the following are only used with SPI:
  16. # DC = 'P9_15'
  17. # SPI_PORT = 1
  18. # SPI_DEVICE = 0
  19. # 128x32 display with hardware I2C:
  20. disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
  21. # 128x64 display with hardware I2C:
  22. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
  23. # Alternatively you can specify an explicit I2C bus number, for example
  24. # with the 128x32 display you would use:
  25. # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=2)
  26. # 128x32 display with hardware SPI:
  27. # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
  28. # 128x64 display with hardware SPI:
  29. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
  30. # Alternatively you can specify a software SPI implementation by providing
  31. # digital GPIO pin numbers for all the required display pins. For example
  32. # on a Raspberry Pi with the 128x32 display you might use:
  33. # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, sclk=18, din=25, cs=22)
  34. # Initialize library.
  35. disp.begin()
  36. # Clear display.
  37. disp.clear()
  38. disp.display()
  39. # Create blank image for drawing.
  40. # Make sure to create image with mode '1' for 1-bit color.
  41. width = disp.width
  42. height = disp.height
  43. image = Image.new('1', (width, height))
  44. # Get drawing object to draw on image.
  45. draw = ImageDraw.Draw(image)
  46. # Draw a black filled box to clear the image.
  47. draw.rectangle((0,0,width,height), outline=0, fill=0)
  48. # Draw some shapes.
  49. # First define some constants to allow easy resizing of shapes.
  50. padding = 2
  51. shape_width = 20
  52. top = padding
  53. bottom = height-padding
  54. # Move left to right keeping track of the current x position for drawing shapes.
  55. x = padding
  56. # Draw an ellipse.
  57. draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
  58. x += shape_width+padding
  59. # Draw a rectangle.
  60. draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
  61. x += shape_width+padding
  62. # Draw a triangle.
  63. draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0)
  64. x += shape_width+padding
  65. # Draw an X.
  66. draw.line((x, bottom, x+shape_width, top), fill=255)
  67. draw.line((x, top, x+shape_width, bottom), fill=255)
  68. x += shape_width+padding
  69. # Load default font.
  70. font = ImageFont.load_default()
  71. # Alternatively load a TTF font.
  72. # Some other nice fonts to try: http://www.dafont.com/bitmap.php
  73. #font = ImageFont.truetype('Minecraftia.ttf', 8)
  74. # Write two lines of text.
  75. draw.text((x, top), 'Hello', font=font, fill=255)
  76. draw.text((x, top+20), 'World!', font=font, fill=255)
  77. # Display image.
  78. disp.image(image)
  79. disp.display()

按照你的 oled 屏修改代码,程序默认是 12832 的,你的 oled 屏是这个就不用改直接运行就 OK。
如果是 12864 的 I2C 就像下面那样修改,把 12832 加 #注释,12864# 注释去掉保存。

  1. # 128x32 display with hardware I2C:
  2. #disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
  3. # 128x64 display with hardware I2C:
  4. disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

终端输入 sudo python shapes.py 或用 Python 打开文件运行就能看到 OLED 屏有显示了。

三、运行程序

树莓派4B搭配124*68屏幕 - 图6


参考资料