Adafruit GFX Graphics Library
Adafruit GFX Graphics Library
Guide Contents 2
Overview 3
Coordinate System and Units 5
Graphics Primitives 7
Drawing pixels (points) 7
Drawing lines 7
Rectangles 9
Circles 10
Rounded rectangles 11
Triangles 12
Characters and text 13
Bitmaps 15
Clearing or filling the screen 16
Rotating the Display 17
The Adafruit_GFX library for Arduino provides a common syntax and set of graphics
functions for all of our LCD and OLED displays. This allows Arduino sketches to easily be
adapted between display types with minimal fuss…and any new features, performance
improvements and bug fixes will immediately apply across our complete offering of color
displays.
The Adafruit_GFX library always works together with a second library provided for each
specific display type — for example, the ST7735 1.8" color LCD requires installing both the
Adafruit_GFX and Adafruit_ST7735 libraries. The following libraries now operate in this
manner:
For information how to use and install libraries, see our tutorial (http://adafru.it/aYG)!
The libraries are written in C++ for Arduino but could easily be ported to any microcontroller
by rewriting the low-level pin access functions.
Also unlike the mathematical Cartesian coordinate system, points here have dimension —
they are always one full integer pixel wide and tall.
Coordinates are always expressed in pixel units; there is no implicit scale to a real-world
measure like millimeters or inches, and the size of a displayed graphic will be a function of
that specific display’s dot pitch or pixel density. If you’re aiming for a real-world dimension,
you’ll need to scale your coordinates to suit. Dot pitch can often be found in the device
datasheet, or by measuring the screen width and dividing the number of pixels across by
this measurement.
For color-capable displays, colors are represented as unsigned 16-bit values. Some
displays may physically be capable of more or fewer bits than this, but the library operates
with 16-bit values…these are easy for the Arduino to work with while also providing a
consistent data type across all the different displays. The primary color components — red,
green and blue — are all “packed” into a single 16-bit variable, with the most significant 5
For the most common primary and secondary colors, we have this handy cheat-sheet that
you can include in your own code. Of course, you can pick any of 65,536 different colors, but
this basic list may be easiest when starting out:
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
For monochrome (single-color) displays, colors are always specified as simply 1 (set)
or 0 (clear). The semantics of set/clear are specific to the type of display: with something
like a luminous OLED display, a “set” pixel is lighted, whereas with a reflective LCD display, a
“set” pixel is typically dark. There may be exceptions, but generally you can count
on 0 (clear) representing the default background state for a freshly-initialized display,
whatever that works out to be.
The function descriptions below are merely prototypes — there’s an assumption that a
display object is declared and initialized as needed by the device-specific library. Look at
the example code with each library to see it in actual use. For example, where we
show print(1234.56), your actual code would place the object name before this, e.g. it might
read screen.print(1234.56) (if you have declared your display object with the name screen).
Drawing lines
You can also draw lines, with a starting and end point and color:
For horizontal or vertical lines, there are optimized line-drawing functions that avoid the
angular calculations:
Rectangles
Next up, rectangles and squares can be drawn and filled using the following procedures.
Each accepts an X, Y pair for the top-left corner of the rectangle, a width and height (in
pixels), and a color. drawRect() renders just the frame (outline) of the rectangle — the
interior is unaffected — while fillRect() fills the entire area with a given color:
Circles
Likewise, for circles, you can draw and fill. Each function accepts an X, Y pair for the center
point, a radius in pixels, and a color:
Here’s an added bonus trick: because the circle functions are always drawn relative to a
center pixel, the resulting circle diameter will always be an odd number of pixels. If an even-
sized circle is required (which would place the center point between pixels), this can be
achieved using one of the rounded rectangle functions: pass an identical width and height
that are even values, and a corner radius that’s exactly half this value.
Triangles
With triangles, once again there are the draw and fill functions. Each requires a full seven
parameters: the X, Y coordinates for three corner points defining the triangle, followed by a
color:
void drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color
void fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color
void drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
Begin with setCursor(x, y), which will place the top left corner of the text wherever you
please. Initially this is set to (0,0) (the top-left corner of the screen). Then set the text color
with setTextColor(color) — by default this is white. Text is normally drawn “clear” — the
open parts of each character show the original background contents, but if you want the text
to block out what’s underneath, a background color can be specified as an optional second
parameter tosetTextColor(). Finally, setTextSize(size) will multiply the scale of the text by a
given integer factor. Below you can see scales of 1 (the default), 2 and 3. It appears blocky
at larger sizes because we only ship the library with a single simple font, to save space.
By default, long lines of text are set to automatically “wrap” back to the leftmost column. To
override this behavior (so text will run off the right side of the display — useful for scrolling
marquee effects), use setTextWrap(false). The normal wrapping behavior is restored with
setTextWrap(true).
Bitmaps
You can draw small monochrome (single color) bitmaps, good for sprites and other mini-
animations or icons:
This issues a contiguous block of bits to the display, where each '1' bit sets the
The bitmap data must be located in program memory using the PROGMEM directive. This is a
somewhat advanced function and beginners are best advised to come back to this later. For
an introduction, see the Arduino tutorial on PROGMEM usage (http://adafru.it/aMw).
The fillScreen() function will set the entire display to a given color, erasing any existing
content:
We can only rotate 0, 90, 180 or 270 degrees - anything else is not possible in hardware and
is too taxing for an Arduino to calculate in software
The rotation parameter can be 0, 1, 2 or 3. For displays that are part of an Arduino shield,
rotation value 0 sets the display to a portrait (tall) mode, with the USB jack at the top right.
Rotation value 2 is also a portrait mode, with the USB jack at the bottom left. Rotation 1 is
landscape (wide) mode, with the USB jack at the bottom right, while rotation 3 is also
landscape, but with the USB jack at the top left.
When rotating, the origin point (0,0) changes — the idea is that it should be arranged at the
top-left of the display for the other graphics functions to make consistent sense (and match
all the function descriptions above).
If you need to reference the size of the screen (which will change between portrait and
landscape modes), use width() and height().
uint16_t width();
uint16_t height();
Each returns the dimension (in pixels) of the corresponding axis, adjusted for the display’s
current rotation setting.