Datasheet

First a few modules are imported, including the Adafruit_SSD1306 module which contains the OLED display driver
classes. You can also see some of the Python Imaging Library modules like Image, ImageDraw, and ImageFont being
imported. Those are, as you can imagine, are for drawing images, shapes and text/fonts!
Pin Setup
Next up we define the pins that are used for the joystick and buttons. The Joystick has Left, Right, Center (press in), Up
and Down. There's also the A and B buttons on the right. Each one should be set as an input with pull-up resistor.
Display Setup
Below the configuration values is the display class setup. There are 4 variants of OLED displays, with 128x32 pixels or
128x64 pixels, and with I2C or with SPI.
However since the OLED Bonnet is a 128x64 I2C display
only
you should only use the
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
# Input pins:
L_pin = 27
R_pin = 23
C_pin = 4
U_pin = 17
D_pin = 22
A_pin = 5
B_pin = 6
GPIO.setmode(GPIO.BCM)
GPIO.setup(A_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(B_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(L_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(R_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(U_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(D_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(C_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
# Raspberry Pi pin configuration:
RST = None
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
© Adafruit Industries https://learn.adafruit.com/adafruit-128x64-oled-bonnet-for-raspberry-pi Page 9 of 15