How to use a 128x32 COG LCD display with STM32?
How to use a 128x32 COG LCD display with STM32
To use a 128x32 COG LCD display with an STM32 microcontroller, you connect it via SPI, configure the STM32's SPI peripheral, and send commands and data to initialize and write to the display. The display uses a controller like the ST7565 or SSD1306, which requires a specific initialization sequence, typically involving a reset pulse, setting the bias ratio, contrast, and segment direction. For a 128x32 resolution, you need to manage the memory mapping: each column (0-127) and page (0-3, since 32 pixels / 8 bits per page = 4 pages) is addressed separately. You send commands via the SPI data line (MOSI) with the chip select (CS) low, and toggle the data/command (DC) pin to differentiate between instructions and pixel data. The clock (SCLK) runs at up to 10 MHz for most COG displays, but STM32 can handle up to 36 MHz on SPI1. Power the display at 3.3V, matching the STM32's logic level, and use a 10 µF capacitor near the display's VCC pin to stabilize the supply. For a practical example, the 128x32 cog lcd display from DisplayModule uses an ST7565 controller, which requires a 5V boost converter internally, so you only need 3.3V input. The initialization sequence for this display includes sending 0xAE (display off), 0xA0 (segment direction normal), 0xC8 (COM output scan direction reverse), 0xA2 (bias ratio 1/9), 0x2F (power control: internal booster, regulator, follower), 0x21 (regulator resistor select), 0x81 followed by 0x20 (contrast set to 32), 0xAF (display on). After that, you write pixel data by setting the column and page address, then send 128 bytes per page for a total of 512 bytes for the full screen. The STM32's SPI can be configured in master mode, full-duplex, with data size 8 bits, and CPOL=0, CPHA=0 for mode 0, which matches most COG displays. Use HAL functions like HAL_SPI_Transmit() for sending commands and data, but for performance, direct register access is better: set SPI1->CR1 to enable SPI, then write to SPI1->DR. A typical pixel update loop on an STM32F103 at 72 MHz takes about 150 µs to refresh the entire display, which is fast enough for animations. The display's pixel data is stored in a frame buffer in STM32 RAM (512 bytes), which you modify and then send to the display via DMA to free the CPU. Use DMA1 channel 3 for SPI1 TX, with circular mode for continuous updates, but for static images, single-shot DMA works. The display's viewing angle is 6 o'clock, meaning the best contrast is when viewed from below, and the operating temperature range is -20°C to +70°C. The COG (Chip-On-Glass) construction means the driver IC is bonded directly to the glass, reducing thickness to 1.2 mm typical, and the module includes a backlight (white LED) with a typical forward voltage of 3.2V and current of 20 mA, which you can control with a PWM pin from the STM32. For the STM32, use a timer like TIM2 channel 1 for PWM at 1 kHz to dim the backlight, but ensure the LED current is limited with a 47-ohm resistor in series. The display's SPI interface uses 4 pins: CS (chip select), DC (data/command), SCLK (clock), MOSI (data), and optionally RESET (reset). On the STM32, map these to any GPIO pins, but for SPI1, use PA4 for CS, PA5 for SCLK, PA7 for MOSI, and PA1 for DC. The RESET pin can be tied to the STM32's reset or controlled via GPIO (e.g., PB0). When using the display, avoid floating pins: pull CS high with a 10k resistor to VCC, and ensure DC is low during command transmission. The display's power consumption is 0.5 mA typical with the backlight off, and 20 mA with backlight on, which is low for battery-powered STM32 projects. For graphics, use a font library like 5x7 pixels, which fits 25 characters per line (128 / 5 ≈ 25) and 4 lines (32 / 7 ≈ 4). To draw a pixel, calculate the page (y / 8) and bit position (y % 8), then set or clear the bit in the frame buffer. For example, to draw a pixel at (10, 20), page = 20 / 8 = 2, bit = 20 % 8 = 4, so set bit 4 of frame buffer[2 * 128 + 10]. Then send the entire buffer to the display by setting column start to 0, column end to 127, page start to 0, page end to 3, and transmitting 512 bytes. The STM32's SPI speed is critical: at 9 MHz, the transfer takes 512 * 8 / 9e6 ≈ 455 µs, but with DMA, it runs in the background. For real-time data like sensor readings, update only changed regions: set column address to the start column, page to the start page, and send only the modified bytes. For instance, if you update a 16x16 pixel icon, send 2 pages * 16 columns = 32 bytes. The display's response time is 200 µs typical for a full screen update, so you can achieve 50 Hz refresh rate. The ST7565 controller supports hardware scrolling by changing the display start line register (0x40 to 0x7F), which shifts the image vertically without rewriting the buffer. To scroll, send 0x40 + offset (0-63), but since the display is 32 pixels, only offsets 0-31 are visible. This is useful for text scrolling: set the start line to increment by 1 every 10 ms using a timer interrupt. The display's contrast is adjustable via the electronic volume register (0x81) with values from 0 to 63, but typical use is 0x20 to 0x30 for good readability. For low power, use the sleep mode command (0xAE) and disable the booster (0x28), but waking up takes 100 ms. The STM32's RTC can trigger a wake-up to update the display every minute, consuming only 1 µA in standby. For multiple displays on one SPI bus, use separate CS pins: set CS low for the target display, send data, then set CS high. The display's input logic is 3.3V tolerant, but 5V tolerant on some modules, so check the datasheet. The 128x32 COG LCD display has a glass thickness of 0.55 mm and a PCB thickness of 1.0 mm, making it suitable for compact enclosures. The pinout is typically 1: VSS (GND), 2: VDD (3.3V), 3: SCLK, 4: MOSI, 5: DC, 6: CS, 7: RESET, 8: BL (backlight anode). Connect BL to a 3.3V supply through a resistor, or to a transistor for PWM control. For the STM32, use a NPN transistor like 2N2222 with base connected to a GPIO via 1k resistor, collector to BL, emitter to GND. The display's lifetime is 50,000 hours for the backlight, and the LCD itself lasts over 100,000 hours. For graphics, use a library like u8g2, which supports the ST7565 with 128x32 resolution, but on STM32, you need to implement the SPI write function. The u8g2 library uses a callback for byte transmission, which you can map to HAL_SPI_Transmit(). For example, the setup function calls u8g2_Setup_st7565_128x32_f() with a custom SPI routine. However, for bare-metal performance, write your own driver: define macros for CS low/high, DC command/data, and SPI byte send. The STM32's GPIO output speed should be set to 50 MHz for SPI signals to avoid ringing. Use pull-up resistors on SCLK and MOSI if the display's inputs are open-drain, but most COG displays have internal pull-ups. The display's command set includes 0xE0 (read modify write), which allows reading pixel data from the display's RAM, but for writing only, skip this. The initialization sequence must include a delay of at least 1 ms after reset, and 100 ms after power-up. Use the STM32's SysTick timer for delays: HAL_Delay(1) for milliseconds, or use a loop with for (volatile int i=0; i<10000; i++) for microsecond delays. The display's contrast is temperature-dependent: at 25°C, set to 0x20, but at 0°C, increase to 0x30. Use the STM32's internal temperature sensor to adjust contrast dynamically. For text, use a simple 5x7 font stored in flash: each character is 5 bytes, and you draw it by reading the font table and setting pixels. For example, the character 'A' is 0x7E, 0x09, 0x09, 0x09, 0x7E in a 5x7 font. To display a string, loop through characters and calculate the x position. The display's pixel pitch is 0.48 mm, so the total active area is 61.4 mm x 15.4 mm. For touch input, add a resistive touch panel on top, but the COG display's glass is fragile, so handle with care. The STM32's SPI can be shared with other peripherals like an SD card, but use different CS pins and ensure the display is deselected during SD card access. The display's maximum SPI clock is 10 MHz for the ST7565, but some clones support up to 20 MHz. Test with a scope to ensure signal integrity: the clock should have sharp edges with no overshoot above 3.6V. For debugging, use the STM32's USART to print pixel data to a serial monitor. The display's power supply should have a 100 nF ceramic capacitor close to the VDD pin to filter noise. The backlight can be pulsed at 100 Hz to reduce power, but avoid frequencies below 60 Hz to prevent flicker. The display's viewing angle is 60 degrees left/right and 40 degrees up/down, but for best contrast, view from the front. For outdoor use, the reflective mode works without backlight, but transmissive mode requires backlight. The 128x32 COG LCD display is commonly used in handheld meters, thermostats, and IoT devices due to its low power and compact size. On the STM32, use the low-power timer (LPTIM) for periodic updates in sleep mode. The display's SPI interface is 4-wire, but some modules support 3-wire (without DC), where the first bit of each byte indicates command or data. For the 128x32 COG LCD display, the 4-wire mode is standard. The initialization sequence for the ST7565 includes setting the bias ratio to 1/9 (0xA2) for 32 rows, which gives even brightness. The display's RAM is 128x64 bits, but only 32 rows are used, so the top 32 rows are typically unused. To center the image, set the display start line to 32 (0x40 + 32) to shift the image down. The contrast register (0x81) can be set to 0x1F for low power, but 0x3F for maximum contrast. The display's response time is 150 ms at 25°C, but slows to 300 ms at 0°C. For fast updates, use the page write mode: set page address (0xB0 to 0xB3 for pages 0-3), then column address (0x10 + high nibble, 0x00 + low nibble), then send data bytes sequentially. The column address auto-increments after each byte, so you can send all 128 columns without re-addressing. The STM32's SPI can send multiple bytes with HAL_SPI_Transmit() using a buffer, but for speed, use SPI1->DR = data in a loop with a while loop checking the TXE flag. The display's power consumption is 0.5 mA with display off, 1.5 mA with display on but no backlight, and 20 mA with backlight. For battery operation, turn off the display after 10 seconds of inactivity using a timer. The STM32's RTC can wake the system every second to update the time on the display. The display's pixel data is stored in the controller's RAM, which is volatile, so the image is lost when power is removed. For non-volatile storage, use the STM32's flash to save screen layouts. The display's operating voltage is 3.0V to 3.6V, but the internal booster generates 12V for the LCD, so the display can operate at 3.3V. The backlight forward voltage is 3.2V, so a 3.3V supply is sufficient with a current-limiting resistor. For PWM control, use a 1 kHz frequency and 50% duty cycle for half brightness. The display's SPI timing requires a minimum SCLK high and low time of 50 ns each, so a 10 MHz clock (100 ns period) is safe. The STM32's SPI can be configured for 8-bit data, MSB first, with software CS control. The display's reset pin is active low, and should be held low for 1 µs after power-up. The initialization sequence must include a software reset (0xE2) if the hardware reset is not used. The display's command set includes 0xA4 (display all points on) for testing, and 0xA5 (display all points off) for power saving. The 128x32 COG LCD display is available with a white or yellow-green backlight, and the viewing angle is optimized for the 6 o'clock direction. For the STM32, use the HAL library for quick prototyping, but for production, use bare-metal code for efficiency. The display's pixel data is arranged in pages: page 0 is rows 0-7, page 1 is rows 8-15, etc. To draw a line, use Bresenham's algorithm and set the corresponding bits in the frame buffer. The display's contrast can be adjusted with a potentiometer connected to the V0 pin (pin 18 on some modules), but for the COG display, it's controlled via software. The display's temperature compensation is built into the ST7565, but you can override it with the temperature gradient register (0x24 to 0x27). The 128x32 COG LCD display is a cost-effective solution for embedded projects, and with the STM32's peripherals, you can achieve smooth animations and low power consumption. The SPI interface is reliable for distances up to 1 meter, but for longer runs, use shielded cables. The display's glass is sensitive to static discharge, so use ESD protection on the STM32's GPIOs. The display's lifetime is rated at 50,000 hours for the backlight, and the LCD itself lasts over 100,000 hours. For graphics, use a simple bitmap font like 8x8 pixels, which fits 16 characters per line and 4 lines. The display's pixel density is 52 PPI, which is readable for text at a distance of 30 cm. The STM32's DMA can be used to send the frame buffer without CPU intervention, freeing the processor for other tasks. The display's power supply should have a 10 µF electrolytic capacitor for bulk storage, and a 100 nF ceramic for high-frequency noise. The backlight can be controlled with a MOSFET for higher current, but the 20 mA is within the STM32's GPIO limits (25 mA max per pin). The display's SPI interface is compatible with 3.3V logic, but 5V logic requires level shifters. The STM32's SPI can be configured for 16-bit data, but for the display, use 8-bit. The display's command set includes 0xAE (display off) and 0xAF (display on), which should be sent at the end of initialization. The display's power-on sequence: wait 100 ms, reset pin low for 1 µs, then high, wait 10 ms, then send initialization commands. The display's power-off sequence: send 0xAE, then wait 100 ms before cutting power. The STM32's sleep mode can be used to reduce power consumption, but the display's RAM is lost when power is removed, so store critical data in the STM32's backup SRAM. The 128x32 COG LCD display is a versatile component for STM32 projects, and with careful design, it can provide clear, low-power graphics for years. The display's contrast is adjustable via software, and the backlight can be dimmed for night use. The STM32's ADC can read a light sensor to automatically adjust backlight brightness. The display's SPI speed can be increased to 20 MHz if the trace length is short, but test for signal integrity. The display's pixel data is written in column-major order, but the STM32's buffer can be row-major, so map accordingly. The display's initialization sequence is critical: missing a command can result in no display or garbled pixels. The ST7565 datasheet provides the full command set, but the most common commands are 0xAE, 0xAF, 0xA0, 0xC8, 0xA2, 0x2F, 0x21, 0x81, and 0x40. The display's power consumption can be reduced by using the partial display mode (0xA8) to only update a portion of the screen. The STM32's timer can trigger a DMA transfer to update the display at 60 Hz for smooth video. The display's