Skip to content

A Reference Work for World History

How to scroll text on a 2.42 inch OLED display?

By admin

Scrolling text on a 2.42 inch OLED display is straightforward when you use the right hardware and software approach. The most common method involves sending commands to the display driver IC, typically the SSD1306 or SH1106, which natively supports horizontal and vertical scrolling. For a 2.42 inch 128x64 oled display, you can implement scrolling by writing to the control registers via SPI or I2C, using pre-defined scrolling commands like 0x26, 0x27, 0x29, and 0x2A. This is not a hack; it’s a built-in feature of the driver that offloads the scrolling from your microcontroller, saving CPU cycles and reducing code complexity. The key is to set the scroll direction, start page, end page, and frame frequency before enabling the scroll. For example, to scroll text left continuously, you send 0x2E (deactivate scroll), then 0x26 (continuous horizontal scroll left), followed by bytes for dummy, start page, frame frequency, and end page, then 0x2F (activate scroll). The frame frequency byte controls speed: 0x00 gives 5 frames per second, 0x01 gives 64 fps, 0x02 gives 128 fps, and 0x03 gives 256 fps. On a 2.42 inch 128x64 oled display, the pixel pitch is around 0.43 mm, so text at 8x8 font size (about 3.4 mm tall) scrolls smoothly at 64 fps without visible flicker. If you’re using a microcontroller like an Arduino Uno or ESP32, the SPI clock speed can be set to 8 MHz for reliable data transfer, but the scroll command itself only needs a few microseconds to execute. The display’s active area is 60.5 mm x 30.7 mm, so you can fit up to 16 characters per line in 8x8 font, and with 8 lines total, scrolling a single line of text is efficient. For vertical scrolling, you use command 0x29 (vertical and right horizontal scroll) or 0x2A (vertical and left horizontal scroll), which require a vertical scroll offset byte. This is useful for marquee-style text that moves upward. However, note that the SSD1306 only supports scrolling within the GDDRAM, which is 128x64 bits, so you cannot scroll beyond the display boundaries. If you need to scroll long text that exceeds the buffer, you must manually shift the data in software using a frame buffer, which requires more RAM—about 1 KB for a 128x64 monochrome display. On an ESP32 with 520 KB SRAM, this is trivial, but on an ATmega328P with 2 KB, you need to optimize by using PROGMEM for font data.

The hardware interface matters for scrolling performance. The 2.42 inch 128x64 oled display typically uses a 7-pin SPI interface (CS, DC, RST, SCLK, MOSI, VCC, GND) or a 4-pin I2C interface (VCC, GND, SCL, SDA). SPI is faster for scrolling because it supports clock speeds up to 10 MHz, while I2C is limited to 400 kHz in fast mode. For scrolling text, SPI reduces latency between command sends, which is critical when you need to update the scroll parameters dynamically. For example, if you want to change scroll speed on the fly, you must deactivate scroll (0x2E), update the frame frequency byte, then reactivate. With SPI, this cycle takes about 10 microseconds, whereas I2C takes around 50 microseconds due to the protocol overhead. The display’s driver IC also supports horizontal scrolling by page, where each page is 8 pixels tall. On a 128x64 display, there are 8 pages (0 to 7). You can scroll multiple pages simultaneously by setting the start page and end page bytes. For instance, to scroll the top half of the display (pages 0 to 3), you set start page to 0 and end page to 3. This allows partial scrolling, which is useful for UI elements where you want a static header and a scrolling body. The frame frequency byte also affects power consumption: at 5 fps, the display draws about 20 mA, but at 256 fps, it jumps to 25 mA due to more frequent refresh cycles. The OLED pixels themselves consume about 0.1 mA per lit pixel, so scrolling text with 50% fill rate adds negligible current. The display’s contrast ratio is 10,000:1, and the viewing angle is 160 degrees, so scrolling text remains readable from the side, which is important for public information displays.

Software implementation requires careful handling of the scroll commands. The SSD1306 datasheet specifies that the scroll commands must be sent in a specific sequence: first deactivate scroll (0x2E), then set the scroll parameters, then activate scroll (0x2F). If you skip the deactivation, the display may ignore the new parameters. For horizontal scrolling, the command format is: 0x26 (left) or 0x27 (right), then dummy byte (0x00), start page (0x00 to 0x07), frame frequency (0x00 to 0x07), end page (0x00 to 0x07), then dummy byte (0x00). The frame frequency byte maps to specific speeds: 0x00=5 fps, 0x01=64 fps, 0x02=128 fps, 0x03=256 fps, 0x04=3 fps, 0x05=4 fps, 0x06=25 fps, 0x07=2 fps. For vertical and horizontal scrolling, the command is 0x29 (right) or 0x2A (left), with an additional byte for vertical scroll offset (0x00 to 0x3F, representing 0 to 63 rows). This is useful for diagonal scrolling effects. On a 2.42 inch 128x64 oled display, the vertical scroll offset can shift the entire display content up or down by up to 63 pixels, but the GDDRAM content remains unchanged. This means you can create a scrolling ticker that moves diagonally, which is visually appealing for news feeds. However, the driver does not support simultaneous horizontal and vertical scrolling in different directions; you must choose one mode. If you need custom scrolling, like bouncing text, you must implement it in software by updating the frame buffer and redrawing the display. This requires a timer interrupt to refresh at a fixed rate, typically 30 to 60 fps. The display’s write time for a full frame is about 10 ms at 8 MHz SPI, so you can achieve 100 fps theoretical maximum, but the OLED response time is 0.1 ms, so 60 fps is smooth. The frame buffer is stored in the microcontroller’s RAM, and you can use a circular buffer to shift text left or right by one pixel per frame. For example, to scroll a 128-character string continuously, you allocate a 128-byte buffer for the visible area plus a 16-byte buffer for the off-screen text. Each frame, you copy the buffer to the display via SPI, then shift the buffer by one pixel. This uses more CPU but gives full control over speed and direction. The ESP32’s I2S peripheral can also be used for DMA-based SPI transfers, reducing CPU load to near zero during scrolling.

Real-world applications for scrolling text on this display include digital signage, IoT dashboards, and wearable devices. For digital signage, the 2.42 inch size is ideal for showing stock tickers or weather updates, where scrolling text conveys more information than static text. The display’s power consumption is 20 mA typical, so it can run on a 500 mAh battery for 25 hours of continuous scrolling. In IoT dashboards, you can scroll sensor readings like temperature, humidity, and pressure in a loop. The display’s operating temperature range is -40°C to 85°C, so it works in outdoor environments. For wearables, the thin profile (2.5 mm) and low weight (8 grams) make it suitable for smart glasses or wrist devices. The scrolling speed can be adjusted based on user input, like a potentiometer reading via ADC. For example, you can map the ADC value (0 to 1023) to frame frequency bytes (0x00 to 0x07), giving 8 speed levels. The display’s pixel density is 128x64 over 60.5 mm x 30.7 mm, which is about 53 PPI, so text at 8x8 font is legible at a distance of 30 cm. For larger fonts like 16x16, you can only fit 8 characters per line, but scrolling is smoother because fewer pixels shift per frame. The contrast ratio ensures that scrolling text does not ghost, as OLEDs have zero response time. The display also supports inverse display mode (command 0xA7), which can be used for night mode scrolling. The driver IC’s internal oscillator runs at 450 kHz, so the display self-refreshes without external clock, but the scroll command uses the same oscillator for timing. If you need precise scrolling intervals, you can use an external timer to trigger scroll enable/disable, but the internal oscillator is accurate to ±10% over temperature.

Technical considerations for scrolling include avoiding artifacts like tearing. The SSD1306 does not have a double buffer, so if you update the GDDRAM while scrolling is active, you may see partial updates. The datasheet recommends deactivating scroll before writing to the GDDRAM, then reactivating. This causes a brief pause in scrolling, but it’s imperceptible at 64 fps. For continuous scrolling without interruption, you can use the vertical scroll mode with a fixed offset, which shifts the display without changing the GDDRAM. This is ideal for scrolling text that is pre-loaded into the buffer. For example, you can load a 128x64 bitmap of text into the GDDRAM, then enable vertical scroll with a 1-pixel offset per frame. This creates a smooth vertical scroll without any GDDRAM writes. The offset can be incremented in a loop, and when it reaches 63, you reset it to 0. This technique uses only 4 bytes of RAM for the scroll parameters, making it suitable for low-memory microcontrollers. The 2.42 inch 128x64 oled display supports this mode because the driver IC has a dedicated scroll register. The maximum vertical scroll offset is 63 pixels, which means you can scroll a full screen height in 63 steps. At 60 fps, this takes about 1 second for a full scroll cycle. For horizontal scrolling, the driver only supports continuous scrolling, not step-by-step, so you must use the frame buffer method for pixel-level control. The frame buffer method requires 1 KB of RAM, which is fine for most microcontrollers. On an Arduino Uno, you can store the font in PROGMEM to save RAM, and the scrolling logic uses about 200 bytes of SRAM. The display’s SPI interface can be shared with other devices if you use a separate CS pin, but for scrolling, it’s best to dedicate the bus to avoid conflicts. The display’s reset pin must be held high during operation, and a low pulse of 10 microseconds initializes the driver. After reset, you must configure the display parameters like multiplex ratio (0xA8, 0x3F for 64 rows), display offset (0xD3, 0x00), and start line (0x40). These settings affect how scrolling behaves. For example, if you set the start line to 32, the display shows the bottom half of the GDDRAM, and scrolling will shift that half. This is useful for split-screen effects.

Data from user forums and manufacturer documentation shows that the most common issue with scrolling is incorrect command sequence. For instance, sending 0x26 without the dummy byte causes the display to ignore the command. The SSD1306 datasheet specifies that the dummy byte must be 0x00, but some clones use different values. If you use a generic 2.42 inch 128x64 oled display, check the driver IC version. The SH1106 is similar but has a slightly different command set for scrolling; it does not support vertical scroll offset. For the SH1106, horizontal scrolling commands are 0x26 and 0x27, but the frame frequency byte is different: 0x00=4 fps, 0x01=64 fps, 0x02=128 fps, 0x03=256 fps. The SH1106 also has a 132x64 GDDRAM, but the display only shows 128x64, so the extra 4 columns are hidden. This affects scrolling because the hidden columns may cause visual artifacts if you scroll horizontally. To avoid this, you can set the display start column to 2 (command 0x21, 0x02). This aligns the visible area correctly. For the SSD1306, the GDDRAM is exactly 128x64, so no adjustment is needed. The display’s driver IC also supports charge pump settings (0x8D, 0x14) for internal DC-DC conversion, which is required for OLED operation. The charge pump draws 10 mA, so total power consumption is 30 mA during scrolling. If you use a battery, you can reduce power by disabling the charge pump (0x8D, 0x10) and using an external 3.3V supply, but the display brightness drops by 50%. For scrolling text, brightness is less critical because the eye tracks motion, so you can use lower brightness to save power. The display’s contrast register (0x81) can be set from 0x00 to 0xFF, with 0x7F being default. At 0x3F, the display draws 15 mA, and scrolling text is still readable. This is useful for battery-powered applications where runtime is important.

Practical implementation steps for scrolling text on a 2.42 inch 128x64 oled display start with wiring. Connect VCC to 3.3V, GND to ground, SCLK to pin 13 (Arduino), MOSI to pin 11, CS to pin 10, DC to pin 9, and RST to pin 8. For I2C, connect SCL to A5 and SDA to A4, with CS and DC tied to VCC. In code, you initialize the display with the Adafruit SSD1306 library or a custom driver. For scrolling, you do not need the library; you can send raw commands via SPI. The sequence is: send 0xAE (display off), then 0x20 (memory mode), 0x00 (horizontal mode), then 0x21 (column address), 0x00, 0x7F, then 0x22 (page address), 0x00, 0x07, then 0x8D (charge pump), 0x14, then 0xA4 (display on resume), 0xA6 (normal display), then 0x81 (contrast), 0xCF, then 0xAF (display on). Then for scrolling, send 0x2E, 0x26, 0x00, 0x00, 0x00, 0x07, 0x00, 0x2F. This scrolls page 0 to 7 left at 5 fps. To change speed, modify the frame frequency byte. To stop scrolling, send 0x2E. To scroll right, use 0x27 instead of 0x26. For vertical scroll, use 0x29 with an additional byte for offset. The offset byte determines how many rows the display shifts per frame. For example, 0x01 shifts by 1 row per frame, creating a smooth vertical scroll. The total scroll cycle time is 64 frames (64 rows) at the frame frequency. At 64 fps, one cycle takes 1 second. The display’s internal oscillator ensures that the scroll timing is consistent regardless of the microcontroller’s clock speed. This is a key advantage of hardware scrolling: it is deterministic. For software scrolling, you must use a timer interrupt to maintain consistent frame rate. The Arduino’s millis() function is not accurate enough for smooth scrolling; you need a hardware timer like Timer1 for 16-bit microcontrollers. On the ESP32, you can use the ESP32 timer API with a 1 ms resolution. The frame buffer update rate should be at least 30 fps to avoid flicker. At 60 fps, the human eye perceives smooth motion. The display’s write time for a full frame is 10 ms, so you have 6.7 ms of CPU time left for other tasks at 60 fps. This is enough to read sensors or update a network connection. The scrolling text can be stored in a string array, and you can use a pointer to index the current position. For example, to scroll a string of 100 characters, you allocate a 128-byte buffer for the visible area, and each frame, you copy 16 characters from the string starting at the current index, then increment the index. When the index reaches 84, you reset to 0. This creates a continuous loop. The font data is stored in PROGMEM as a 5x7 or 8x8 bitmap. The 8x8 font is easier to handle because it aligns with the page boundaries. The display’s page address mode allows you to write to specific pages, so you can update only the scrolling line without redrawing the entire screen. This reduces SPI traffic and improves performance. For example, if you scroll only the bottom line (page 7), you send data only for that page, which is 128 bytes per frame. At 60 fps, this is 7.68 KB/s, well within the SPI bandwidth of 8 MHz (1 MB/s). The total data rate for scrolling one line is negligible. The display’s GDDRAM is write-only, so you cannot read back the current content. This means you must maintain a software copy of the buffer if you need to modify it. For scrolling, this is not necessary because you always write new data. The display’s hardware scrolling feature is the most efficient way to scroll text, but it is limited to continuous motion. For interactive scrolling, like a user-controlled scroll, you need software scrolling. In that case, you can use a rotary encoder to change the scroll position. The encoder’s pulses can be mapped to the frame buffer index, and each click moves the text by one character. This is common in menu systems. The display’s fast response time ensures that the text updates instantly without ghosting. The 2.42 inch size is large enough to show 4 lines of 16x16 Chinese characters, which is useful