Skip to content
Mundología Explora cada capa del mundo — y tu lugar en él.

How to change font size on a 1.54 inch 128x64 OLED?

Un reportaje de admin para la revista Mundología.

admin

How to change font size on a 1.54 inch 128x64 OLED

To change font size on a 1.54 inch 128x64 oled display, you need to modify the font data in your microcontroller firmware, typically using libraries like Adafruit_GFX or U8g2. The display itself has a fixed resolution of 128x64 pixels, so font size is determined by the pixel dimensions of the character set you load. For example, a 5x7 font (like the default in Adafruit_GFX) will render characters 5 pixels wide and 7 pixels tall, while a 12x16 font will be larger. You can switch between built-in fonts by calling functions like setFont(&FreeMono12pt7b) in U8g2, or by defining custom fonts using a bitmap array. The key is that the font size is not a hardware setting—it’s purely software-controlled. The display module uses SPI or I2C communication, so you can upload new font data via your microcontroller’s flash memory. For instance, the 1.54 inch 128x64 oled display supports multiple font sizes, but you must ensure the total character width doesn’t exceed 128 pixels horizontally or 64 pixels vertically. A common mistake is using a 24x32 font, which only fits 5 characters per line, leading to clipping. To avoid this, calculate the maximum characters per line: for a 6x8 font, you get 21 characters (128/6 ≈ 21), but for a 12x16 font, you get only 10 characters. This is critical for UI design. Also, the OLED driver (SSD1306 or SH1106) does not handle font scaling—it only accepts pixel data. So, changing font size means replacing the entire font table in your code. Many libraries include pre-defined fonts like FreeSans9pt, FreeSans12pt, or FreeSans18pt, which are optimized for readability. For example, in Arduino IDE, you can use u8g2.setFont(u8g2_font_ncenB08_tr) for a 8-point bold font, or u8g2.setFont(u8g2_font_10x20_tf) for a 10x20 pixel font. The actual size in points depends on the display’s DPI, but for a 1.54-inch OLED with 128x64 pixels, the pixel density is about 128/1.54 ≈ 83 PPI horizontally, so a 10-point font is roughly 10/72*83 ≈ 11.5 pixels tall. This is a rough estimate—you should test on the actual hardware. Another approach is to use a custom font generator like GLCD Font Creator or Online Font Converter. You can create a font with specific pixel dimensions, e.g., 8x8, 8x16, or 16x32, and then export it as a byte array. For the 1.54 inch 128x64 oled display, the maximum font height is 64 pixels, but that would only allow one line of text. Practical fonts are between 8 and 24 pixels tall. If you need multiple font sizes in the same project, you can load multiple font tables in memory, but this consumes flash space. For example, the Adafruit_GFX library has a setFont() function that switches between fonts, but you must include the font header files. The U8g2 library, on the other hand, supports over 1000 fonts, including proportional and monospaced variants. You can also rotate the display using setDisplayRotation() to change orientation, which affects how text wraps. For a 1.54-inch OLED, typical rotation values are 0°, 90°, 180°, or 270°. If you rotate 90°, the width becomes 64 pixels and height 128 pixels, so you can fit 64/6 ≈ 10 characters of a 6x8 font per line. This is useful for vertical text layouts. Additionally, the font size can be adjusted by scaling pixel data manually, but this is inefficient. For example, you can double the size of a 5x7 font to 10x14 by repeating each pixel, but this creates a blocky appearance. A better method is to use anti-aliased fonts, but the OLED’s monochrome nature limits this—you’d need dithering. The 1.54 inch 128x64 oled display uses a 1-bit color depth, so only black and white pixels are supported. This means font size changes are binary: you either use a larger font or a smaller one. There’s no smooth scaling. For data-heavy applications, consider using a font like u8g2_font_tom_thumb_4x6_tf (4x6 pixels) to fit 32 characters per line, or u8g2_font_6x10_tf for 21 characters. For labels, use u8g2_font_10x20_tf for 6 characters. The choice depends on the information density. For example, a weather station might use a 12x16 font for temperature and a 6x8 font for humidity. You can also mix fonts in the same display by calling setFont() before each text block. But be careful: changing fonts frequently can cause flickering if the display is updated too fast. The OLED’s response time is about 10-20 microseconds per pixel, so a full screen refresh takes about 128*64*20µs ≈ 164ms. To reduce flicker, use page-mode updates instead of horizontal scrolling. Another factor is the SPI clock speed. For the 1.54 inch 128x64 oled display, the maximum SPI speed is typically 10 MHz, but you can increase it to 20 MHz if the microcontroller supports it. Faster SPI reduces the time to send font data. For example, sending a 128x64 bitmap at 10 MHz takes 128*64/10,000,000 ≈ 0.82ms, but with overhead, it’s about 1-2ms. This is fast enough for real-time font changes. However, if you’re using I2C, the speed is limited to 400 kHz, so a full screen update takes 128*64/400,000 ≈ 20ms, which is slower. For font size changes, I2C is still usable, but avoid frequent updates. In terms of memory, the font data for a 12x16 ASCII set (95 characters) takes 12*16*95/8 ≈ 2280 bytes. For a 5x7 font, it’s 5*7*95/8 ≈ 416 bytes. The 1.54 inch 128x64 oled display has a 128-byte display buffer (if using page mode), but the font data is stored in the microcontroller’s flash. For an Arduino Uno with 32 KB flash, you can fit about 14 such fonts. For a ESP32 with 4 MB flash, you can store hundreds. The font size also affects the display’s power consumption. Larger fonts mean fewer characters, so the OLED pixels are on for longer periods, but the current draw is constant (20-30 mA for the display). The difference is negligible. For battery-powered devices, use smaller fonts to reduce the number of pixel transitions, which saves power during updates. For example, a 4x6 font updates 24 pixels per character, while a 12x16 font updates 192 pixels. Over 100 updates, the larger font uses 8x more power. This is a key consideration for IoT devices. Finally, the font size can be changed dynamically based on input. For instance, you can use a button to cycle through font sizes: if (buttonPressed) { currentFont = (currentFont + 1) % numFonts; setFont(fonts[currentFont]); }. This is common in menu systems. The 1.54 inch 128x64 oled display is ideal for such applications because of its compact size and low power. To implement this, you need to store the font pointers in an array. For the U8g2 library, you can use const uint8_t *fontList[] = {u8g2_font_5x7_tf, u8g2_font_8x13_tf, u8g2_font_12x20_tf};. Then call u8g2.setFont(fontList[index]). The font index can be stored in EEPROM to persist across power cycles. For the Adafruit_GFX library, you need to include separate font header files like #include and then use display.setFont(&FreeMono9pt7b). The font size is determined by the point size in the font name. For example, FreeMono9pt7b is 9 points, which is about 9 pixels tall on a 83 PPI display. But note that these fonts are proportional, so character widths vary. For fixed-width fonts, use FreeMono variants. For the 1.54 inch 128x64 oled display, proportional fonts can cause alignment issues if you’re building tables. To fix this, use monospaced fonts like u8g2_font_6x12_tf (6x12 pixels) for consistent spacing. Another trick is to use the setCursor() function to position text precisely. For example, to center text, calculate the width of the string: int16_t x = (128 - u8g2.getStrWidth("Hello")) / 2;. This works for any font size. The font size also affects the vertical spacing. For a 12-pixel font, you need at least 14 pixels between lines to avoid overlap. Use setCursor(x, y + 14) for the next line. For a 20-pixel font, use 22 pixels. This is important for multi-line text. The 1.54 inch 128x64 oled display has a 64-pixel height, so you can fit 64/14 ≈ 4 lines of a 12-pixel font, or 64/22 ≈ 2 lines of a 20-pixel font. For a 4x6 font, you can fit 10 lines. This is useful for data logs. In terms of code examples, here’s a simple Arduino sketch for changing font size on the 1.54 inch 128x64 oled display:

#include
U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* rst=*/ 8);
const uint8_t *fonts[] = {u8g2_font_5x7_tf, u8g2_font_8x13_tf, u8g2_font_12x20_tf};
int fontIndex = 0;
void setup() { u8g2.begin(); }
void loop() {
u8g2.firstPage();
do {
u8g2.setFont(fonts[fontIndex]);
u8g2.setCursor(0, 20);
u8g2.print("Font Size: ");
u8g2.print(fontIndex);
} while (u8g2.nextPage());
delay(2000);
fontIndex = (fontIndex + 1) % 3;
}

This code cycles through three font sizes. Note that the 1.54 inch 128x64 oled display uses the SSD1306 driver, so the U8G2 constructor is correct. If you’re using the SH1106 driver, replace with U8G2_SH1106_128X64_NONAME_F_4W_SW_SPI. The font size change is immediate after setFont(). For more advanced control, you can use the setFontMode() function to enable transparent or solid background. For example, u8g2.setFontMode(1) enables transparent mode, which is useful for overlaying text on graphics. The font size remains the same, but the background pixels are not overwritten. This is useful for the 1.54 inch 128x64 oled display when displaying charts or images. Another consideration is the font’s baseline. Different fonts have different baseline offsets. For example, the u8g2_font_5x7_tf has a baseline of 7 pixels, while u8g2_font_12x20_tf has a baseline of 20 pixels. To align text vertically, use the getAscent() and getDescent() functions. For instance, int16_t y = 10 + u8g2.getAscent(); ensures the text is at the top of the display. This is critical for multi-font layouts. The 1.54 inch 128x64 oled display has a resolution of 128x64, so the y-coordinate ranges from 0 to 63. If you use a font with a baseline of 20, the y-coordinate should be at least 20 to avoid clipping. For example, setCursor(0, 20) places the baseline at pixel 20, so the text extends from pixel 0 to 20. For a 20-pixel font, this is perfect. For a 5-pixel font, the text will be at the top. To center vertically, use int16_t y = (64 + u8g2.getAscent() - u8g2.getDescent()) / 2;. This works for any font size. The font size also affects the display’s refresh rate. If you change fonts frequently, the OLED may exhibit ghosting due to pixel persistence. The SSD1306 driver has a built-in charge pump that refreshes pixels at 60 Hz, but if you update the buffer too fast, you might see artifacts. To avoid this, use a frame rate of 30-60 FPS. For the 1.54 inch 128x64 oled display, the typical refresh rate is 60 Hz, but you can reduce it by using setFrequency() in the U8g2 library. For example, u8g2.setDisplayClockDiv(2) halves the refresh rate to 30 Hz, which reduces power consumption but may cause flicker. This is a trade-off. In terms of data density, the font size directly impacts how much information you can display. For a 128x64 display, the maximum number of characters for a 5x7 font is 128/5 * 64/7 ≈ 25 * 9 = 225 characters. For a 12x16 font, it’s 128/12 * 64/16 ≈ 10 * 4 = 40 characters. This is a 5.6x difference. For the 1.54 inch 128x64 oled display, this means you can show a paragraph of text with a small font, or just a few words with a large font. This is important for user interfaces. For example, a menu system might use a 12x16 font for titles and a 6x8 font for items. The font size change can be triggered by a button press, as shown earlier. Another method is to use a potentiometer to adjust font size in real time. For instance, read an analog pin, map the value to a font index, and update the display. This is useful for accessibility. The 1.54 inch 128x64 oled display is also compatible with the FontManager library, which allows dynamic font loading from an SD card. This is advanced but useful for projects with many fonts. For example, you can store 10 fonts on an SD card and load them as needed. The font size is limited by the microcontroller’s RAM. For the ESP32, you can load a 24x32 font (about 9 KB) into RAM, but for an Arduino Uno, you’re limited to 2 KB. So, the font size is also constrained by hardware resources. In summary, changing font size on the 1.54 inch 128x64 oled display is a software task that involves selecting the right font table, calculating pixel dimensions, and optimizing for memory and power. The key is to match the font size to the information density and use case. For example, a clock display might use a 16x32 font for the time, while a data logger uses a 4x6 font for logs. The font size change is straightforward with libraries like U8g2 or Adafruit_GFX, but you must account for the display’s resolution, driver type, and communication speed. The 1.54 inch 128x64 oled display is a versatile module that supports a wide range of font sizes, from 4x6 to 16x32, as long as you manage the pixel data correctly. Always test the font on the actual hardware to ensure readability and avoid clipping. The OLED’s high contrast (10,000:1) makes even small fonts legible, but the viewing angle is 160°, so font size is less critical for readability. For the best results, use a font that is at least 6 pixels tall for text, and 12 pixels for icons. The 1.54 inch 128x64 oled display is a solid choice for projects requiring

Sobre el autor

admin

Corresponsal de Mundología · Red editorial