Manage RAM and Refresh on a Graphic OLED: Full, Page and Text Modes
A small graphic OLED can make a sensor project much easier to use, but a display example that works alone can become unreliable when logging, communications and menus are added. The problem is often the way the program stores and redraws the screen. Choosing a suitable drawing strategy early saves both memory and debugging time.
This guide uses a monochrome 128 × 64 display as a practical example. PMD Way's graphic OLED displays include different interfaces and formats. The 0.96-inch 128 × 64 OLED range offers I2C and SPI versions, so confirm the selected version, controller and electrical requirements before choosing a library configuration.
Start with the memory arithmetic
A monochrome image needs one bit per pixel. A complete 128 × 64 image therefore contains 8,192 bits, or 1,024 bytes. That calculation describes the pixel buffer alone: your program also needs variables, communication buffers, function stack space and library state. Successful compilation does not prove that enough working memory remains during the busiest part of the application.
Make a memory budget before adding decorative graphics. List the display buffer, a sensor history array, incoming messages and temporary formatting strings. For example, storing 120 two-byte measurements uses another 240 bytes. Decide whether the history must remain in RAM, whether fewer samples are sufficient, or whether a larger microcontroller is appropriate. Preserve useful information before spending memory on animation.
Choose a drawing mode deliberately
| Approach | Useful when | Main trade-off |
|---|---|---|
| Full pixel buffer | The program has memory and needs flexible graphics | A complete screen occupies RAM |
| Page buffer | Graphics are needed but RAM is limited | Drawing operations are repeated for successive portions |
| Character-oriented output | Status text is sufficient | Layout and graphics are more restricted |
The maintained U8g2 setup tutorial explains its full-buffer, page-buffer and U8x8 character modes. Full-buffer drawing clears a RAM image, draws into it and sends it to the display. Page mode instead builds the picture through a repeated drawing loop. U8x8 communicates character output without a full graphic framebuffer. These are programming choices, separate from whether the module uses I2C or SPI.
Match the constructor to the drawing code
U8g2 constructor names identify the controller, geometry, buffer choice and interface. Its constructor reference distinguishes F, 1 and 2 buffer options. A full-buffer sketch uses clearBuffer() and sendBuffer(); a page-buffer sketch uses the firstPage()/nextPage() loop. Changing only the constructor can leave an otherwise plausible program drawing incorrectly.
Select the controller from documentation for the actual module. Similar dimensions and an acknowledging I2C address do not prove that two displays use the same controller. Keep a known-working configuration in the project notes, including the library version, interface, rotation and reset arrangement. This makes a replacement-module test much more informative than trying random constructors.
Keep each frame internally consistent
A page renderer may execute your drawing routine more than once to construct one visible screen. The U8g2 API reference requires consistent content throughout that picture loop. Read sensors and calculate the display values before entering it. Drawing should reproduce that snapshot rather than perform fresh measurements or advance counters.
Consider a temperature logger with a value, trend arrow and sample number. If each drawing pass reads the sensor again, different portions can describe different samples. Instead, prepare a small record containing the temperature, arrow direction, sample number and alarm state. Render only that record until the entire picture is complete. Process the next measurement afterwards.
The same separation helps with button presses. A button handler changes the requested menu or setting once; the renderer displays the resulting state. Do not increment a setting inside a drawing function. Otherwise the increment can happen several times during one apparent screen update, and changing buffer mode may unexpectedly change how far the menu moves.
Budget transfer time as well as RAM
A framebuffer is not instantaneously visible. For ordinary acknowledged I2C transfers, each data byte consumes nine clock pulses including acknowledgement, as described in NXP's I2C specification. Sending 1,024 pixel bytes therefore takes at least 9,216 clock pulses, before addresses, commands and software overhead.
At a hypothetical 100 kHz bus clock that payload alone takes 92.16 milliseconds; at 400 kHz it takes 23.04 milliseconds. These are calculated lower bounds, not guaranteed module refresh rates or permission to increase the clock. The display, controller, wiring and other bus devices must support the selected timing. Measure the complete redraw in the finished application.
A room-temperature display seldom needs continuous full-speed updates. Try sampling on the application's required schedule while refreshing the screen a few times per second, or when the visible state changes. Keep urgent control and communication work independent of cosmetic redraws. If a redraw blocks for too long, simplify the interface or reconsider the hardware and library.
Design a useful screen before adding decoration
Sketch a layout with a clear primary value, units, status and a small indication of data age. Reserve enough width for negative values and the longest expected label. Use an explicit unavailable marker when a sensor fails; leaving an old reading on screen without explanation can be more misleading than a blank display.
The listed yellow/blue module has fixed colour regions: its upper quarter is yellow and the remainder blue, with a small separation. Treat that boundary as a layout constraint rather than attempting to assign colours in software. Put a heading or status line in the upper region and keep important digits clear of the boundary.
Test the combined application
- Verify the module's supply, signal levels and pin labels against its own documentation before connecting it.
- Run a minimal display example with the selected interface and controller.
- For I2C, discover the actual address. PMD Way notes that the rear address marking can be incorrect on some units.
- Add the sensor and communications features individually, checking available memory and redraw duration.
- Exercise the longest text, alarm state, menu changes and missing-sensor condition.
- Leave the complete project running through its normal operating cycle and confirm that display activity does not interrupt its main job.
A dependable OLED interface comes from predictable data flow: gather information, make one coherent snapshot, render it with the correct buffer strategy and update only as often as the user needs.