OLED displays with I²C interface can be used with both Arduino and Raspberry Pi. In this chapter, we will explore an experiment where we replace a temperature LED indicator with an OLED display that shows the current and set temperatures.
This experiment is primarily intended to demonstrate how to use the display with a Raspberry Pi. The experiment's program displays the current time and a small animation (see the image below).
Components
The OLED display has four pins that should be connected directly to the Raspberry Pi using four "female-to-female" jumper wires.
Make sure your OLED display has a resolution of 128x64 pixels and uses the SSD1306 control chip. Some displays may have additional pins on the SSD1306 that are not used, so for simplicity, choose a display with four pins: GND (ground), VCC (+5V), SDA (data), and SCL (clock). The experiment can be conducted with either a monochrome or a color OLED display.
Connection to Raspberry Pi
The SSD1306 chip can operate at 3V or 5V. Since earlier models of the Raspberry Pi might not provide sufficient current at 3V, it's recommended to connect the chip to 5V.
Connect the wires as follows:
- Connect the GND wire on the OLED display to the GND on the Raspberry Pi.
- Connect the VCC wire on the OLED display to the 5V on the Raspberry Pi.
- Connect the SCL (clock) wire on the OLED display to GPIO 3 on the Raspberry Pi.
- Connect the SDA (data) wire on the OLED display to GPIO 2 on the Raspberry Pi.
Raspberry Pi Program
If your Raspberry Pi isn't already set up to work with the I²C interface, refer to the sections above and configure it as described in the "Setting Up I²C on Raspberry Pi" section.
To work with the OLED display, you can use the Python library developed by Adafruit. It works well with OLED displays controlled by the SSD1306 chip, regardless of their manufacturer. To install this library, execute the following commands:
$ git clone https://github.com/adafruit/Adafruit-SS ... rduino.git
$ cd Adafruit_Python_SSD1306
$ sudo python setup.py install
Display Coordinate System
OLED displays are graphic displays where you can draw shapes and output text. To work with them, you need to specify coordinates for elements on the screen. The Adafruit library uses a coordinate system where the top left corner of the screen has coordinates (0, 0), and the bottom right corner has (127, 63).
Here's the program for this experiment:
Code: Select all
#!/usr/bin/env python
from oled.device import ssd1306 # 1
from oled.render import canvas
from PIL import ImageFont # 2
import time
device = ssd1306(port=1, address=0x3C) # 3
large_font = ImageFont.truetype('FreeMono.ttf', 24) # 4
x = 0
while True:
with canvas(device) as draw: # 5
draw.pieslice((x, 30, x+30, 60), 45, -45, fill=255) # 6
x += 10 # 7
if x > 128:
x = 0
now = time.localtime() # 8
draw.text((0, 0), time.strftime('%H:%M:%S', now), font=large_font, fill=255)
time.sleep(0.1)
Let's break it down step by step:
1. Two modules are imported from the OLED library: `
SSD1306`, which is used for working with the device, and `
Image`, which is used to create a canvas on which shapes and text will be drawn.
2. The `
PIL` library (Python Imaging Library) is also imported.
3. A variable `
device` is created, which provides access to the OLED display. The value `0x3C` is the I²C device address of the OLED display. This address can vary depending on the display manufacturer, so it's important to check it in your display's documentation.
4. The font used, which is 24 pixels in height, is specified for displaying the time on the screen.
5. Using the `
with ... as` construct, a code block is created for displaying text on the screen.
6. Within this block, a shape representing the "Pac-Man" game animation is drawn. The shape is 30x30 pixels in size, and the angle of the open mouth ranges from -45° to 45°, creating the impression of movement. The color of the shape is set to 255, which corresponds to white.
7. The variable `
x` is incremented by
10 on each iteration, allowing the animation to move to the right.
8. The current time is obtained, converted to a string, and displayed on the screen.
Running the Program
To run the program, execute the following command:
$ sudo python oled.py
If nothing is displayed on the screen, the issue may be related to an incorrectly specified I²C device address. Make sure the address is correct.
In this example, the `
pieslice` function is used, which draws a circle with a cutout portion. You can also use other functions to draw various graphical elements. Additional examples and functions can be found on the website
http://effbot.org/imagingbook/imagedraw.htm. Try different variations to bring your OLED display to life!