How to Display Text on a 72x40 OLED
To display text on a 72x40 OLED, you need to send data over I2C or SPI to the SSD1306 or SH1106 driver chip that controls the 72x40 pixel matrix. The most direct method is using a microcontroller like Arduino or ESP32 with a library such as Adafruit SSD1306 or u8g2. For a 72x40 OLED, the resolution is 72 columns by 40 rows, which is a non-standard size compared to the more common 128x64 or 128x32 displays. This specific resolution is often found in the 0.42 inch 72x40 oled display, which uses the SSD1306 driver. The display is monochrome, with each pixel being either on or off, and it supports both text and graphics. You’ll need to initialize the display with the correct dimensions in your code, set the text size, and then call functions like display.print() or u8g2.drawStr() to render characters. The I2C address is typically 0x3C or 0x3D, depending on the module’s configuration. Power consumption is low, around 20mA during operation, making it suitable for battery-powered projects. The refresh rate can reach up to 60Hz for static text, but complex graphics may require a lower frame rate due to the limited memory and processing speed of the microcontroller.
The physical dimensions of the 72x40 OLED are small, typically 0.42 inches diagonally, which translates to an active area of about 10.8mm by 6.0mm. Each pixel is roughly 0.15mm square, so text must be at least 5 pixels tall to be readable. A common font size is 5x7 pixels, which allows up to 14 characters per line and 5 lines of text, but you can use larger fonts like 8x13 for better readability, which reduces the capacity to 9 characters per line and 3 lines. The SSD1306 driver has 128x64 bytes of internal RAM, but only 72x40 pixels are used, so you need to set the display offset correctly. For example, in the Adafruit library, you call display.begin(SSD1306_SWITCHCAPVCC, 0x3C) and then display.setContrast(100) to adjust the brightness. The contrast value ranges from 0 to 255, with 100 being a good starting point for indoor use. The display’s viewing angle is 160 degrees, and the response time is less than 10 microseconds, so there’s no visible lag when updating text.
When writing code, you must include the correct header files. For Arduino, use #include
For the u8g2 library, the setup is slightly different. Include #include
Power management is critical for portable applications. The 72x40 OLED consumes about 8mA when idle and 20mA when all pixels are on. To reduce power, you can use the sleep mode by calling display.ssd1306_command(SSD1306_DISPLAYOFF) or u8g2.sleep(). The wake-up time from sleep is about 100 microseconds, so you can cycle the display on and off to save battery. The operating voltage is 3.3V to 5V, but the I2C logic level is 3.3V, so a level shifter is needed if you use a 5V microcontroller like an Arduino Uno. The display’s lifetime is rated at 100,000 hours for the OLED panel, but the driver chip can last longer if operated within its temperature range of -40°C to 85°C.
Text rendering on a 72x40 OLED requires careful font selection. The 5x7 font is the most common, but it’s small and may be hard to read from a distance. For better legibility, use a 8x13 font, which gives 9 characters per line and 3 lines. The u8g2 library includes a font called u8g2_font_8x13_tf that works well. You can also rotate the display by 90 degrees using the U8G2_R1 or U8G2_R2 parameter in the constructor, which is useful for vertical text layouts. The display’s pixel pitch is 0.15mm, so the dot density is about 169 dots per inch, which is sharp for text but not for detailed graphics. For scrolling text, you can use the display.startscrollleft() or display.startscrollright() functions in the Adafruit library, which move the entire display buffer. The scroll speed is set by the command display.ssd1306_command(SSD1306_SETSCROLLSPEED) with a value from 0 to 7, where 0 is the fastest. The scroll function uses the hardware acceleration in the SSD1306, so it doesn’t load the microcontroller.
Common issues include incorrect I2C address, wrong display dimensions, and missing pull-up resistors. The I2C address can be found by scanning the bus with a sketch like Wire.begin(); for (address = 1; address < 127; address++ ). If the address is 0x3C, the display is standard; if it’s 0x3D, update the code. The 72x40 OLED may have a different memory mapping than the default 128x64, so you need to set the display offset. In the Adafruit library, you can call display.ssd1306_command(SSD1306_SETDISPLAYOFFSET, 0x00) to shift the start row. The SSD1306 has a page addressing mode, where each page is 8 pixels tall. For a 40-pixel height, you have 5 pages (0 to 4). The column range is 0 to 71. If the display shows shifted or garbled text, adjust the column offset with display.ssd1306_command(SSD1306_SETCOLUMNADDR, 0, 71) and display.ssd1306_command(SSD1306_SETPAGEADDR, 0, 4). The reset pin is optional on I2C modules, but if you have a reset pin, connect it to a digital output and pulse it low for 10 microseconds during initialization.
For advanced text effects, you can use custom fonts or bitmaps. The u8g2 library allows you to create your own font with the u8g2_font_t structure, but it’s easier to use the built-in fonts. You can also invert the text by calling display.setTextColor(SSD1306_BLACK) and then drawing a rectangle with display.fillRect() to create a highlight. The display’s contrast can be adjusted per character by using the display.setContrast() function, but it affects the entire screen. For partial updates, the SSD1306 supports a page write mode, where you can update a single 8-pixel row without rewriting the whole buffer. This is useful for fast text updates, like a clock display. The write speed for I2C is about 400 kHz, which gives a theoretical throughput of 50 kbytes per second, but the actual text update rate is limited by the library overhead.
Interfacing with the 72x40 OLED on a Raspberry Pi is also possible using the smbus or wiringPi libraries. For Python, install the Adafruit_SSD1306 package and use from PIL import Image, ImageDraw, ImageFont to render text. The resolution is set to 72x40, and you draw text with draw.text((0, 0), "Hello", font=font, fill=255). The I2C address is the same, and the display object is initialized with disp = Adafruit_SSD1306.SSD1306_128_64(rst=None, i2c_bus=1, i2c_address=0x3C). The Raspberry Pi’s I2C bus runs at 100 kHz by default, but you can increase it to 400 kHz in the config file. The power consumption is similar to Arduino, but the Pi’s 5V logic needs a level shifter for the 3.3V OLED. The display can also be used with the luma.oled library, which provides a high-level interface for text and graphics.
The 72x40 OLED’s small size makes it ideal for wearable devices, smart badges, or sensor readouts. For example, you can display temperature, humidity, and time on three lines using the 5x7 font. The code would set the cursor to (0, 0) for the first line, (0, 10) for the second, and (0, 20) for the third. The line spacing is 10 pixels for the 5x7 font, but you can adjust it by changing the y coordinate. For a scrolling ticker, you can use the hardware scroll function, but it only works in one direction. To scroll text in both directions, you need to manually shift the buffer using display.drawBitmap() and display.display() in a loop. The buffer manipulation is fast because the SSD1306 supports vertical and horizontal scrolling commands. The scroll speed is set by the command display.ssd1306_command(SSD1306_SETSCROLLSPEED, 0x00) for the fastest speed, which scrolls 1 pixel per frame.
Memory usage is minimal. The Adafruit library uses a 360-byte buffer in RAM, which is fine for most microcontrollers. The u8g2 library uses a similar buffer but also stores font data in flash memory. The fonts are stored in the program memory, so they don’t consume RAM. The total code size for a simple text display is about 10 KB on an Arduino Uno, leaving room for other functions. The display’s driver chip has a 128x64 bit internal RAM, but only 72x40 pixels are used, so the remaining memory is unused. You can access the unused RAM for custom graphics, but it’s not recommended because it can cause conflicts with the display’s addressing. The SSD1306 also supports a charge pump for the OLED voltage, which is enabled by default. The charge pump can be disabled to save power, but the display will be dimmer. The command display.ssd1306_command(SSD1306_CHARGEPUMP, 0x10) disables it, and 0x14 enables it.
For reliable operation, use 10k ohm pull-up resistors on the I2C lines. The SDA and SCL lines should be connected to VCC through these resistors. The display’s VCC pin is 3.3V, but some modules have a built-in regulator for 5V input. Check the datasheet for your specific module. The 0.42 inch 72x40 oled display typically has four pins: VCC, GND, SDA, and SCL. Some modules also have a reset pin, which you can leave unconnected or connect to a GPIO. The I2C bus can handle multiple devices, so you can connect the OLED along with a sensor like a BME280. The address conflict is rare because the OLED uses 0x3C, and most sensors use different addresses. The bus capacitance limits the number of devices, but for a few sensors, it’s fine.
Testing the display is straightforward. Upload a simple sketch that prints "Hello World" and checks the I2C address. If the display is blank, verify the wiring and the address. Use a logic analyzer to check the I2C signals. The SDA line should show a start condition, followed by the address byte, and then the data bytes. The display should acknowledge each byte. If the display shows random pixels, the initialization sequence may be wrong. The SSD1306 requires a specific sequence of commands, including setting the display on, charge pump, and contrast. The library handles this, but if you’re using a custom driver, you need to follow the datasheet. The initialization sequence is: display.ssd1306_command(SSD1306_DISPLAYOFF), SSD1306_SETDISPLAYCLOCKDIV, 0x80, SSD1306_SETMULTIPLEX, 0x27 (for 40 rows), SSD1306_SETDISPLAYOFFSET, 0x00, SSD1306_SETSTARTLINE, 0x00, SSD1306_CHARGEPUMP, 0x14, SSD1306_MEMORYMODE, 0x00, SSD1306_SEGREMAP, 0x01, SSD1306_COMSCANDEC, 0x08, SSD1306_SETCOMPINS, 0x12, SSD1306_SETCONTRAST, 0x7F, SSD1306_SETPRECHARGE, 0xF1, SSD1306_SETVCOMDETECT, 0x40, SSD1306_DISPLAYALLON_RESUME, SSD1306_NORMALDISPLAY, and SSD1306_DISPLAYON. The multiplex value is 39 for 40 rows (0 to 39). The com pins setting is 0x12 for 40 rows, which uses alternative com pin configuration.
In summary, the 72x40 OLED is a small, low-power display that works well for text output. The key steps are selecting the correct library, setting the display dimensions, choosing a font, and handling the I2C communication. The display’s resolution limits the amount of text, but it’s sufficient for simple messages. The power consumption and size make it a good choice for embedded projects. The code examples provided work with Arduino and Raspberry Pi, and the libraries handle the low-level details. The display’s driver chip is mature and well-documented, so troubleshooting is straightforward. The 0.42 inch 72x40 oled display is a specific module that matches these specifications, and it’s widely available from suppliers like DisplayModule. The I2C interface simplifies wiring, and the library support ensures quick development. The display’s performance is reliable for static and scrolling text, and the contrast can be adjusted for different lighting conditions. The operating temperature range is wide, so it can be used in outdoor projects. The display’s lifetime is long, and the power consumption is low, making it suitable for battery-powered devices. The text rendering is crisp due to the high pixel density, and the small size allows for compact designs. The display’s memory mapping is non-standard, but the libraries handle it correctly. The initialization sequence is critical, and the libraries provide the correct commands. The I2C bus speed can be increased for faster updates, but the default 100 kHz is sufficient for text. The display’s driver chip supports hardware scrolling, which is useful for ticker displays. The text can be combined with simple graphics, like icons or progress bars, using the bitmap functions. The display’s buffer is small, so graphics are limited to simple shapes. The display’s power