Skip to content
Engineering Notes · Yeinz

How to connect a 3.2 inch 256x64 OLED display to STM32?

a By admin

How to Connect a 3.2 Inch 256x64 OLED Display to STM32

To connect a 3.2 inch 256x64 oled display module to an STM32 microcontroller, you need to use the SPI interface, as this display typically operates via a 4-wire SPI protocol. The display controller is usually a SSD1322 or similar, which supports a resolution of 256x64 pixels and monochrome output. Start by identifying the pinout: the display has pins for VCC (3.3V), GND, CS (chip select), DC (data/command), RES (reset), SCK (serial clock), and MOSI (master out slave in). On the STM32 side, you can use any SPI peripheral, such as SPI1 or SPI2, but ensure the voltage levels match—STM32 GPIOs are 3.3V tolerant, so no level shifting is needed. For example, on an STM32F103C8T6 (Blue Pill), you can map CS to PA4, DC to PA1, RES to PA0, SCK to PA5 (SPI1_SCK), and MOSI to PA7 (SPI1_MOSI). Power the display with 3.3V from the STM32 board, but note that the display may draw up to 20mA during operation, so check your power supply capacity. Initialize the display by sending a sequence of commands: set display off, set column address range (0 to 127 for 256 columns, as each byte controls 2 pixels), set row address range (0 to 63), set contrast (typically 0x7F for 50% brightness), set segment remap (0xA0 or 0xA1 for left-to-right), set COM scan direction (0xC0 or 0xC8), and then turn display on. You can use the STM32 HAL library to write a function that sends commands (DC low) and data (DC high) via SPI. For example, using HAL_SPI_Transmit, you can send a byte array. A typical initialization sequence for the SSD1322 is: send 0xFD (set command lock), 0x12 (unlock), then 0xAE (display off), 0xA8 (set multiplex ratio), 0x3F (64 rows), 0xA1 (set display start line), 0x00, 0xA2 (set offset), 0x00, 0xA0 (set remap), 0x14 (enable column address remap, enable nibble remap, enable COM remap), 0xB3 (set display clock divide ratio/oscillator frequency), 0x91 (divide ratio 9, frequency 1), 0xAB (set VSL), 0x01 (enable external VSL), 0xB6 (set second pre-charge period), 0x0F, 0xBE (set VCOMH), 0x07, 0xA6 (set display mode normal), 0xA9 (set display on), 0xAF (display on). After initialization, you can clear the display by writing 0x00 to all pixels. For drawing, you need to manage the framebuffer in STM32 RAM—a 256x64 monochrome display requires 2048 bytes (256 * 64 / 8). You can update the entire display by sending a command to set column and row addresses, then burst write the framebuffer via SPI. The SPI speed can be up to 10MHz, but you may need to adjust based on your wiring—long wires can cause signal degradation. Use pull-up resistors on CS and DC lines if needed, and ensure the reset pin is held high after initialization. If you use a 3.3V display module, avoid connecting to 5V logic, as it can damage the OLED. For more advanced features, you can enable partial display updates by setting the column and row range to specific areas, which reduces SPI traffic. The display's contrast can be adjusted via command 0x81 followed by a byte (0x00 to 0xFF), with 0x7F being typical. Also, the display supports grayscale via pulse width modulation, but the SSD1322 is a 4-bit grayscale controller, so you can send 4-bit nibbles for each pixel—this requires 8192 bytes for a full framebuffer. However, most 256x64 monochrome modules are configured for 1-bit mode, so check your datasheet. If you use a different STM32 model, like STM32F407, you can use DMA to transfer the framebuffer, reducing CPU load. For example, configure SPI with DMA circular mode, but ensure the display supports continuous data streaming. The wiring should be kept short—under 10cm—to avoid reflections. Use a 100nF capacitor between VCC and GND near the display to filter noise. If you encounter ghosting or flickering, adjust the pre-charge period (command 0xB6) or the VCOMH voltage (command 0xBE). The display's operating temperature range is -40 to 85°C, so it's suitable for industrial use. Power consumption is about 15mA at full brightness, but you can reduce it by lowering contrast or using sleep mode (command 0xAE). For debugging, use an oscilloscope to check SPI signals—SCK should be clean, and MOSI data should change on the rising edge. If the display shows no output, verify the reset sequence: pull RES low for 10ms, then high. Also, check that the CS line is active low during SPI transactions. Some modules have a built-in charge pump, so you don't need external voltage converters. The interface is 3.3V only, so if you use a 5V STM32 board, like some STM32F103 clones, ensure the GPIOs are 3.3V tolerant—most are, but confirm with the datasheet. For a 256x64 display, the pixel pitch is about 0.3mm, so it's readable for text and graphics. You can use fonts like 8x8 or 8x16, which fit well on the screen. For example, a 8x8 font allows 32 characters per row and 8 rows, while a 8x16 font gives 32 characters per row and 4 rows. The display's viewing angle is 160 degrees, so it's visible from most angles. The module typically has a PCB with a 2.54mm pin header, making it breadboard-friendly. When connecting to STM32, use female-to-female jumper wires, but avoid routing them near high-frequency signals like PWM. If you use a custom PCB, keep the SPI traces as short as possible and use ground planes. The display's response time is under 10 microseconds, so it can update at 60Hz without issues. For a full framebuffer update at 10MHz SPI, the transfer time is 2048 bytes * 8 bits / 10MHz = 1.6ms, plus command overhead, so you can achieve 60fps easily. If you need to update only part of the screen, set the column and row range to the affected area, then send only the relevant bytes. This reduces SPI traffic and improves responsiveness. The display also supports hardware scrolling, which can be enabled via commands 0x2F (enable scrolling) and 0x2E (disable scrolling), but this is rarely used in embedded systems. For text rendering, you can store font bitmaps in flash memory. For example, a 8x8 font for ASCII characters requires 96 * 8 = 768 bytes. Use the STM32's internal flash to store the font, and access it via pointers. When drawing a character, read the font data and write it to the framebuffer. For graphics, you can use a library like u8g2, which supports SSD1322 and STM32. However, u8g2 is resource-heavy—it uses about 2KB of RAM and 10KB of flash. If you have limited flash, write your own minimal driver. The STM32F103C8 has 64KB flash and 20KB RAM, so it's sufficient for a framebuffer and font. For a 3.2 inch 256x64 oled display module, the active area is about 89mm x 22mm, so it's good for displaying sensor data, menus, or waveforms. The module's thickness is about 5mm, including the PCB. It's a passive matrix OLED, so each pixel is an organic LED that emits light when current flows. The lifespan is typically 50,000 hours to half brightness, so it's durable. The display's contrast ratio is 2000:1, making it readable in bright light. However, direct sunlight may wash out the screen, so consider a polarizer if used outdoors. The module often comes with a built-in SSD1322 controller, which has 256x64 pixels of internal RAM. The controller supports 4-bit grayscale, but the module may be wired for 1-bit operation—check the datasheet. If it's 4-bit, you need to send 4-bit nibbles for each pixel, which doubles the SPI data. But most modules are 1-bit, so you only send 1 byte per 8 pixels. The SPI mode is typically mode 0 (CPOL=0, CPHA=0) or mode 3 (CPOL=1, CPHA=1), but SSD1322 uses mode 0. Set the STM32 SPI to mode 0 with 8-bit data and MSB first. The clock polarity and phase must match the display's requirements. If you use a different controller, like SH1106, the commands are similar but not identical. Always verify the display's datasheet for exact commands. The connection is straightforward, but you must ensure that the display's VCC is 3.3V—some modules accept 5V, but 3.3V is safer. If your STM32 board has a 3.3V regulator, you can power the display from the same rail. The total current for the STM32 and display is under 100mA, so a USB port can power it. For a production design, use a dedicated 3.3V regulator like AMS1117-3.3. The display's backlight is not needed because OLED pixels emit light directly. The module's interface is a standard 2.54mm pitch, so you can use a 8-pin header. The pinout from left to right is usually: VCC, GND, CS, DC, RES, SCK, MOSI, and sometimes NC (not connected). Some modules have a 7-pin header, omitting the NC pin. Always check the label on the module. If you use a 3.3V logic level, you don't need level shifters. The SPI bus can be shared with other devices if you use separate CS lines. For example, you can connect an SD card and the OLED on the same SPI bus, but ensure the CS pins are unique. The STM32's SPI can operate at up to 18MHz, but the display may have a maximum of 10MHz. Set the SPI prescaler to achieve a clock speed within the display's spec. For example, on STM32F103, set SPI1 to 9MHz (72MHz / 8). Use 8-bit data size, and enable software slave management (SSM) if you control CS manually. The display's DC pin determines whether the byte is a command or data. When DC is low, the byte is a command; when high, it's data. The RES pin is used for hardware reset—pull low for 10ms, then high. After reset, wait 100ms before sending commands. The display's initialization sequence must be sent exactly as specified, or the display may not work. For example, the SSD1322 requires a specific unlock command (0xFD, 0x12) before any other commands. If you skip this, the display will ignore commands. The display's contrast can be set to a value between 0x00 and 0xFF, but 0x7F is a good starting point. If the display is too dim, increase the contrast. If it's too bright, decrease it. The display's power consumption is proportional to the number of lit pixels. So a full-white screen draws more current than a black screen. The display's sleep mode (command 0xAE) reduces current to under 1mA. You can use this to save power in battery-powered applications. The display's operating voltage is 3.0V to 3.6V, so 3.3V is ideal. If you use a 3.0V supply, the display may be dimmer. The display's temperature range is wide, but at low temperatures, the response time may increase. The display's driver IC supports hardware scrolling, but it's not commonly used. The display's built-in oscillator generates the clock for the charge pump, so no external components are needed. The display's PCB often has a capacitor for the charge pump, so you don't need to add one. The display's interface is SPI, but some modules support I2C or parallel. The 3.2 inch 256x64 oled display module typically uses SPI because it's faster. I2C would be too slow for 256x64 resolution. The SPI bus can be extended with buffers if you need longer cables, but for most applications, direct connection is fine. The display's pins are 3.3V tolerant, but if you use 5V logic, you need a level shifter. The STM32's GPIOs are 5V tolerant on some pins, but not all. Check the STM32 datasheet for 5V tolerance. For the display, it's safer to use 3.3V logic. The display's MOSI pin is input-only, so you don't need bidirectional communication. The display's data is sent MSB first. The SPI bus must be configured with the correct clock polarity and phase. The display's CS pin must be held low during the entire transaction. After the transaction, set CS high. The display's DC pin must be set before each byte. The display's RES pin can be connected to the STM32's reset pin, but it's better to control it separately. If you use a common reset, the display will reset when the STM32 resets, which is fine. The display's initialization sequence can be stored in an array and sent in a loop. For example, a typical sequence is: {0xFD, 0x12}, {0xAE}, {0xA8, 0x3F}, {0xA1, 0x00}, {0xA2, 0x00}, {0xA0, 0x14}, {0xB3, 0x91}, {0xAB, 0x01}, {0xB6, 0x0F}, {0xBE, 0x07}, {0xA6}, {0xA9}, {0xAF}. The number of commands is about 20, so it's easy to implement. The display's framebuffer can be updated in a loop. For a scrolling text effect, you can shift the framebuffer and redraw. The display's pixel layout is 256 columns and 64 rows. The columns are addressed in pairs, so the column address range is 0 to 127 for 256 pixels. Each byte represents 2 pixels when in 1-bit mode. The row address range is 0 to 63. The display's data is sent in column-major order. The display's controller has a built-in RAM that is updated by the SPI data. The display's refresh rate is controlled by the internal oscillator. The display's frame rate is typically 60Hz, but it can be adjusted via command 0xB3. The display's display on command (0xAF) turns on the charge pump and enables the display. The display's display off command (0xAE) turns off the charge pump and saves power. The display's set contrast command (0x81) sets the current for the OLED pixels. The display's set segment remap command (0xA0) controls the column order. The display's set COM scan direction command (0xC0) controls the row order. The display's set display start line command (0xA1) sets the first row. The display's set offset command (0xA2) sets the vertical offset. The display's set multiplex ratio command (0xA8) sets the number of rows. The display's set display clock divide ratio/oscillator frequency command (0xB3) sets the clock. The display's set pre-charge period command (0xB6) sets the pre-charge time. The display's set VCOMH command (0xBE) sets the VCOMH voltage. The display's set VSL command (0xAB) enables or disables the external VSL. The display's set command lock command (0xFD) unlocks the display for commands. The display's set display mode normal command (0xA6) sets the display to normal mode. The display's set display on command (0xA9) enables the display. The display's set display off command (0xAE) disables the display. The display's set column address command (0x15) sets the column range. The display's set row address command (0x75) sets the row range. The display's write RAM command (0x5C) writes data to the RAM. The display's read RAM command (0x5D) reads data from the RAM. The display's set grayscale lookup table command (0xB8) is used for 4-bit mode. The display's set default gray scale table command (0xB9) sets the default table. The display's set phase length command (0xB1) sets the phase length. The display's set display enhancement command (0xB4) enables or disables enhancement. The display's set GPIO command (0xB5) controls the GPIO pins. The display's set function selection command (0xBF) selects the function. The display's set display mode all on command (0xA5) turns on all pixels. The display's set display mode all off command (0xA4) turns off all pixels. The display's set display mode inverse command (0xA7) inverts the display. The display's set display mode normal command (0xA6) sets the display to normal. The display's set display mode all on command (0xA5) is used for testing. The display's set display mode all off command (0xA4) is used for blanking. The display's set display mode inverse command (0xA7) is used for inversion. The display's set display mode normal command (0xA6) is the default. The display's set display

Next step

See how Yeinz cuts your deploy time in half — without ripping out your stack.

Book a 30-minute demo