Skip to content
Brief Us on a Project

How to scroll text on a 0.96 inch 128x64 OLED?

How to Scroll Text on a 0.96 Inch 128x64 OLED

To scroll text on a 0.96 inch 128x64 OLED, you need to use the built-in hardware scrolling feature of the SSD1306 driver chip, which is the most common controller for these displays. The SSD1306 supports horizontal, vertical, and diagonal scrolling through dedicated commands sent via I2C or SPI. For example, you can send command 0x26 for continuous horizontal scrolling left or right, with parameters defining the start page, end page, and frame rate. The frame rate is set by a 3-bit value in the command, allowing speeds from 2 frames per second (0x07) to 64 frames per second (0x00). This is a hardware-level scroll, meaning the display updates its internal RAM buffer automatically, without needing to rewrite pixel data from the microcontroller. This saves CPU cycles and reduces power consumption, which is critical for battery-powered projects. The OLED module itself, like a 0.96 inch 128x64 spi i2c oled display, typically uses the SSD1306 with a resolution of 128 columns by 64 rows, organized into 8 pages of 8 pixels each. The scrolling is page-based, meaning you can only scroll entire rows of 8 pixels, not individual pixel lines. To scroll a single line of text, you need to position it within one or two pages, then trigger the scroll command. For instance, if you want to scroll text left continuously, you send: 0x2E (stop scroll, just in case), 0x26 (horizontal scroll left), 0x00 (dummy byte), 0x02 (start page 2), 0x00 (frame rate, 2 fps), 0x07 (end page 7), 0x00 (dummy), 0x2F (start scroll). The start and end pages define the vertical range of the scroll. If you set start page 0 and end page 7, the entire display scrolls. For a single line of text at the top, set start page 0 and end page 0, and the text will scroll only within that 8-pixel-tall strip. The frame rate directly affects the smoothness: 2 fps is very slow, 64 fps is fast but may cause flickering due to the OLED's response time. In practice, 4 to 8 fps works well for readable text. You can also use vertical scrolling with command 0x29 for right and 0x2A for left, but these require additional setup for vertical offset. The SSD1306 datasheet specifies that vertical scrolling uses a 6-bit vertical offset register, which shifts the display content up or down before scrolling horizontally. This allows for diagonal scrolling by combining vertical offset with horizontal scroll. However, the hardware scroll has a limitation: it only works on the entire page range you set, and you cannot scroll individual pixels. For pixel-level scrolling, you must use software scrolling, where you manually shift the buffer and redraw the display. This is slower and uses more memory but gives finer control. The I2C interface runs at 400 kHz max, while SPI can go up to 10 MHz, so SPI is faster for software scrolling. But for hardware scrolling, the interface speed doesn't matter because the scroll is executed by the SSD1306 internally. The display's power consumption during hardware scrolling is about 20 mA typical, compared to 15 mA idle, due to the constant RAM refresh. The OLED's contrast is set via command 0x81, with a value from 0 to 255, but scrolling doesn't affect contrast unless you change it. The memory mapping is column-major, with columns 0 to 127 and pages 0 to 7. Each page is a row of 8 pixels, with the least significant bit representing the top pixel. When scrolling, the SSD1306 shifts the entire column data within the specified pages. For example, horizontal left scroll moves column 0 to column 127, and all other columns shift left by one. This is a circular scroll, so the data wraps around. If you want non-circular scrolling, you need to clear the buffer after the scroll starts, but that requires software intervention. The frame rate is set by the 3-bit value in the scroll command: 0x00 = 64 fps, 0x01 = 32 fps, 0x02 = 16 fps, 0x03 = 8 fps, 0x04 = 4 fps, 0x05 = 2 fps, 0x06 = 1 fps, 0x07 = 0.5 fps. Note that the actual frame rate depends on the internal oscillator frequency, which is typically 4 MHz for the SSD1306. The datasheet states that the scroll step interval is (frame rate value + 1) * 2 * 8 / (frequency in MHz), so for 4 MHz, 0x00 gives 64 fps, 0x01 gives 32 fps, etc. This is consistent across most modules, but some clones may have different oscillator frequencies, causing slight variations. To test, you can measure the time for one full scroll cycle by counting the number of steps (128 columns for horizontal scroll) and dividing by the frame rate. For 64 fps, one cycle takes 2 seconds; for 2 fps, it takes 64 seconds. This is important for long text messages: if you have a 20-character line, each character is 6 pixels wide in a typical 5x7 font, so the total width is 120 pixels. At 64 fps, the text moves across the screen in 1.875 seconds, which may be too fast to read. At 8 fps, it takes 15 seconds, which is more comfortable. You can also adjust the start and end pages to scroll only part of the display, leaving the rest static. For example, to scroll a status bar at the top, set start page 0 and end page 0, and display static content on pages 1 to 7. This is commonly done in data dashboards where you need to show scrolling alerts without disturbing the main data. The hardware scroll also supports vertical scrolling with a vertical offset, which shifts the display content up or down by a number of pixels before scrolling horizontally. The vertical offset is set by command 0xA3, with a value from 0 to 63. For example, if you set vertical offset to 10, the display content shifts up by 10 pixels, and the bottom 10 pixels become blank. Then, when you start horizontal scroll, the content scrolls horizontally while maintaining the vertical offset. This is useful for creating a parallax effect or for scrolling text that is not aligned to page boundaries. However, the vertical offset affects the entire display, not just the scrolling region. So if you have static content, it will also shift. To avoid this, you can use the vertical scroll command (0x29 or 0x2A) which includes a vertical scroll direction and a vertical offset. The vertical scroll command moves the entire display content vertically, and then you can combine it with horizontal scroll for diagonal movement. But this is complex and rarely used in practice. Most projects use simple horizontal scrolling for text. The SSD1306 also supports a "scroll stop" command (0x2E) which stops all scrolling immediately. You should always send this command before changing scroll parameters to avoid glitches. After stopping, you can clear the display or change the buffer. One common issue is that after scrolling, the display may show artifacts if the buffer is not updated. This is because the scroll is circular, so the data wraps around. For example, if you scroll left, the leftmost column appears on the right side. To prevent this, you can clear the buffer after each scroll cycle, but that requires software timing. Alternatively, you can use a non-circular scroll by writing new data to the buffer while scrolling, but this is tricky because the SSD1306 updates its internal RAM during scrolling, and writing to the RAM while scrolling can cause corruption. The datasheet recommends stopping the scroll before writing to the RAM. So for dynamic text, you typically stop the scroll, update the buffer, then restart the scroll. This creates a brief pause, which is acceptable for most applications. The I2C address of the SSD1306 is typically 0x3C or 0x3D, depending on the module. For SPI, the CS pin is usually active low. The display's resolution of 128x64 means you can display up to 21 characters per line (if using 6x8 font) and 8 lines (if using 8-pixel tall font). For scrolling text, you usually use a larger font, like 16x32, to make it readable, but that reduces the number of characters per line. The hardware scroll works at the page level, so if you use a 16-pixel tall font, it spans two pages. You can still scroll it by setting the start and end pages to cover those two pages. For example, if the text is on pages 2 and 3, set start page 2 and end page 3. The scroll will move both pages simultaneously, keeping the text intact. The frame rate affects the perceived speed of the text. For a 16-pixel tall font, the text is thicker, so it may appear to move slower because the eye tracks the center of the text. In practice, a frame rate of 4 fps (0x04) works well for large fonts. The OLED's response time is about 100 microseconds, which is much faster than the scroll rate, so there is no motion blur. However, the OLED's persistence of vision can cause flickering if the frame rate is too low. At 2 fps, the text appears to jump, which is distracting. At 8 fps, it's smooth. The display's brightness is also a factor: at lower brightness, the flickering is less noticeable. You can adjust the contrast via command 0x81, but this doesn't affect the scroll speed. The SSD1306 also has a "charge pump" setting (command 0x8D) that enables the internal DC-DC converter for the OLED voltage. This is required for most modules, and it's set to 0x14 for enable. Without it, the display may be dim or not work. The charge pump draws about 10 mA additional current, so total power consumption during scrolling is around 30 mA. This is important for battery life. For example, a 2000 mAh battery can run the display for about 66 hours continuous. But if you use software scrolling, the microcontroller also draws current, reducing battery life. Hardware scrolling is more efficient because the microcontroller can sleep while the display scrolls. For instance, if you use an ESP32, you can put it into deep sleep mode during scrolling, waking only to update the text. This reduces average current to less than 1 mA, extending battery life to months. The I2C interface uses two wires (SDA and SCL) with pull-up resistors, typically 4.7 kOhm. The SPI interface uses four wires (CS, DC, MOSI, SCK) plus a reset pin. The SPI speed can be up to 10 MHz, but for hardware scrolling, the speed doesn't matter because the scroll is internal. However, for initializing the display and setting up the scroll, faster SPI reduces setup time. The initialization sequence for the SSD1306 includes: 0xAE (display off), 0xD5 (set display clock divide ratio/oscillator frequency), 0x80 (default), 0xA8 (set multiplex ratio), 0x3F (64 rows), 0xD3 (set display offset), 0x00 (no offset), 0x40 (set start line to 0), 0x8D (charge pump), 0x14 (enable), 0x20 (set memory addressing mode), 0x00 (horizontal addressing), 0xA1 (set segment remap, column 127 is mapped to SEG0), 0xC8 (COM output scan direction, remapped mode), 0xDA (set COM pins hardware configuration), 0x12 (alternative pin configuration), 0x81 (set contrast), 0xCF (default contrast), 0xD9 (set pre-charge period), 0xF1 (default), 0xDB (set VCOMH deselect level), 0x40 (default), 0xA4 (display on resume), 0xA6 (normal display, not inverted), 0xAF (display on). After this, you can send the scroll commands. The scroll commands must be sent after the display is on, and they take effect immediately. To stop scrolling, send 0x2E. You can also use the "scroll" command with a vertical scroll for text that moves up or down. For vertical scrolling, the command is 0x29 for vertical scroll right and 0x2A for vertical scroll left, followed by 5 bytes: dummy, start page, end page, vertical offset, and frame rate. The vertical offset is a 6-bit value that determines how many pixels the content shifts vertically before scrolling horizontally. This is useful for creating a marquee effect where text moves diagonally. However, the vertical offset affects the entire display, so you need to account for it in your design. For example, if you set vertical offset to 10, the top 10 pixels of the display become blank, and the bottom 10 pixels are filled with the top 10 pixels of the original content. This can be used to create a scrolling banner that moves from bottom to top. But for text scrolling, horizontal scrolling is more common. The hardware scroll also supports "continuous" and "one-shot" modes. The commands 0x26 and 0x27 are for continuous horizontal scroll, while 0x28 and 0x29 are for continuous vertical and horizontal scroll. There is no one-shot scroll command in the SSD1306; you have to stop the scroll manually after a certain time. This is typically done with a timer in the microcontroller. For example, you can start the scroll, then after 10 seconds, send 0x2E to stop it. The scroll stops immediately, and the display shows the last frame. If you want to reset the display to the original content, you need to rewrite the buffer. One common trick is to use the "display start line" command (0x40) to shift the display content vertically without scrolling. This is not a true scroll, but it can be used to move text up or down. For example, you can set the start line to 0, then increment it to 63, and the display will shift up by one pixel each time. This is a software scroll, but it's simple and works for small movements. However, it's not as smooth as hardware scroll because it requires rewriting the buffer. The hardware scroll is the best option for continuous text scrolling. The display's viewing angle is 160 degrees, typical for OLEDs, so the text is readable from most angles. The contrast ratio is 10000:1, which makes the text sharp. The display's lifetime is about 10000 hours for full brightness, but at lower brightness, it can last longer. The operating temperature range is -40°C to 85°C, so it's suitable for outdoor use. The display's weight is about 5 grams, making it ideal for wearable projects. The module often includes a 4-pin or 7-pin interface, depending on whether it's I2C or SPI. The I2C version has VCC, GND, SCL, SDA. The SPI version has VCC, GND, SCK, MOSI, CS, DC, RES. Some modules also have a 3.3V regulator, but most are 3.3V only. The logic level is 3.3V, but some modules are 5V tolerant on the I2C pins. The display's resolution of 128x64 is standard for 0.96 inch OLEDs, and the pixel size is about 0.17 mm, so the text is small but readable. For scrolling text, you typically use a font size of at least 8 pixels tall, but 16 pixels is better for readability. The font data is stored in the microcontroller's flash memory, and you can use a library like Adafruit SSD1306 or U8g2 to simplify the process. These libraries have built-in functions for hardware scrolling. For example, in the Adafruit library, you can call display.startscrollright(0x00, 0x07) to start horizontal scroll right on all pages. The library handles the command sequence for you. However, the library's scroll function only supports horizontal scroll, not vertical or diagonal. For that, you need to send raw commands. The U8g2 library supports more scroll modes, including vertical and diagonal. It also has a function for setting the vertical offset. In U8g2, you can call u8x8_SetScrollMode(u8x8, 1) to enable horizontal scroll, and then set the scroll interval. The library uses the hardware scroll of the SSD1306, so it's efficient. The library also supports software scrolling for displays that don't have hardware scroll, but for the SSD1306, hardware scroll is preferred. The software scroll in U8g2 works by shifting the buffer and redrawing, which is slower but gives pixel-level control. For example, you can scroll text pixel by pixel, which is smoother than the page-based hardware scroll. However, the software scroll requires more memory (a 128x64 buffer is 1024 bytes) and more CPU time. For a microcontroller with limited RAM, like an Arduino Uno (2 KB), the buffer takes half of the available RAM, leaving little for other tasks. So hardware scroll is better for memory-constrained devices. The ESP32 has 520 KB of RAM, so software scroll is fine. But for battery life, hardware scroll is still better because the CPU can sleep. The display's I2C speed is limited to 400 kHz, so writing the buffer takes about 2.5 ms for 1024 bytes (assuming 8-bit writes). For SPI, writing the buffer takes about 0.1 ms at 10 MHz. So for software scrolling, SPI is faster. But for hardware scrolling, the interface speed doesn't matter. The scroll speed is determined by the frame rate, not the interface. The frame rate also affects the smoothness of the text. At 64 fps, the text moves smoothly but quickly. At 2 fps, it's jerky. For most applications, 8 fps is a good balance. You can also change the scroll direction dynamically by sending a new scroll command. For example, you can start with left scroll, then after 5 seconds, stop and start right scroll. This creates a bouncing effect. The SSD1306 supports this without any issues, as long as you stop the scroll first. The scroll commands are non-blocking, so you can send them and then do other tasks. The display will continue scrolling until you stop it. This is useful for multi-tasking. The display's