Sub topic 9
Drawing Text, Lines, and Shapes with Adafruit GFX
This chapter assumes that you can upload an Arduino sketch, install libraries with the Library Manager, and initialize an SSD1306 OLED over I2C. It also assumes that you understand the OLED's display() call sends an in-memory drawing to the physical screen.
Adafruit GFX is a graphics library that provides common drawing functions. The SSD1306 library uses these functions for text, pixels, lines, rectangles, circles, and other shapes. This lets you learn one drawing interface that also works with several other display controllers.
The OLED coordinate system
An SSD1306 display uses a pixel coordinate system. The top-left pixel is coordinate (0, 0). The x coordinate increases toward the right. The y coordinate increases downward.
A 128x64 display has valid coordinates from (0, 0) through (127, 63).
| Item | Meaning |
|---|---|
| `(0, 0)` | Top-left corner |
| `x` | Horizontal position |
| `y` | Vertical position |
| Width | Number of columns, such as 128 |
| Height | Number of rows, such as 64 |
Most drawing functions use integer pixel coordinates. For example, drawLine(0, 0, 127, 63, SSD1306_WHITE) draws a diagonal line from one corner to the other.
Drawing text
Text drawing uses three steps:
- Choose the text size and color.
- Set the cursor position.
- Print the text.
setTextSize(1) uses the default character size. Larger values scale the characters. setTextColor(SSD1306_WHITE) selects the lit pixel color on a monochrome OLED.
The display has a RAM buffer. Functions such as print() and drawLine() change that buffer. The OLED does not show those changes until you call display().
Drawing basic shapes
Adafruit GFX includes several useful shape functions:
drawPixel()lights one pixel.drawLine()draws a line between two points.drawRect()draws an outline rectangle.fillRect()draws a solid rectangle.drawCircle()draws a circle outline.fillCircle()draws a solid circle.drawRoundRect()draws a rectangle with rounded corners.
The final color argument is normally SSD1306_WHITE. On a monochrome display, SSD1306_BLACK clears pixels while drawing.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) {
// Stop if the OLED is not detected.
}
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("GFX drawing"));
display.drawLine(0, 12, 127, 12, SSD1306_WHITE);
display.drawRect(4, 18, 42, 22, SSD1306_WHITE);
display.fillRect(52, 18, 24, 22, SSD1306_WHITE);
display.drawCircle(94, 29, 11, SSD1306_WHITE);
display.fillCircle(119, 29, 5, SSD1306_WHITE);
display.drawRoundRect(10, 47, 108, 14, 4, SSD1306_WHITE);
display.display();
}
void loop() {
}The rectangle arguments are x, y, width, height, and color. The circle arguments are centerX, centerY, radius, and color. A shape can extend beyond the display, but pixels outside the display area are clipped.
A frame-based drawing pattern
A changing screen usually follows this sequence:
- Read the current values.
- Clear the old frame.
- Draw all text and shapes for the new frame.
- Send the complete frame with
display().
This pattern prevents leftover pixels and makes the screen state predictable.
For a bar graph, calculate the bar width before calling fillRect(). The map() function can convert a sensor range of 0 through 1023 into a screen range of 0 through 120. Constrain the result when input values might exceed the expected range.
Exercise
What happens if a sketch draws several shapes but never calls display()?
Show answer
The shapes remain only in the RAM buffer, so the OLED does not visibly update.