How to Display a Bitmap on a 3.18 Inch 128x64 COG LCD
To display a bitmap on a 3.18 inch 128x64 cog lcd display, you need to convert your image into a monochrome bitmap array (typically 1-bit per pixel) and then send it via SPI or I2C to the LCD controller, like the SSD1306 or ST7565. The display is 128 pixels wide and 64 pixels tall, so the bitmap data must be exactly 128 * 64 / 8 = 1024 bytes if using 1-bit per pixel, or 1024 * 8 = 8192 bits. For a 3.18-inch COG (Chip-on-Glass) LCD, the controller is often a ST7565R or similar, which supports 132x65 dot matrix, but you only use the 128x64 active area. The key is to generate the bitmap in a format the controller understands: each byte represents 8 vertical pixels, arranged column-by-column. For example, pixel (0,0) is the LSB of byte 0, pixel (0,1) is bit 1, and so on. You can use tools like LCD Assistant or Image2LCD to convert a BMP or PNG to a C array. Once you have the array, initialize the LCD with the correct command set (e.g., set column address 0-127, page address 0-7 for 8 pages of 8 rows), then send the data bytes sequentially. The SPI clock speed can be up to 10 MHz for fast updates, but typical microcontrollers like Arduino Uno or STM32 run at 8-16 MHz, so you can achieve 30-60 frames per second for static bitmaps. For a 3.18-inch display, the pixel pitch is about 0.6 mm (since 128 pixels / 3.18 inches ≈ 40.3 pixels per inch), which makes it readable for text and icons. The COG technology means the driver IC is bonded directly to the glass, reducing thickness to about 1.5 mm total, and the display uses a white LED backlight with typical brightness of 200-300 cd/m². The contrast ratio is around 1000:1 for the monochrome mode, and the viewing angle is 6 o'clock, meaning the best contrast is when looking from below. To avoid flicker, you need to refresh the display at least 60 Hz, but for static bitmaps, a single write is enough. The power consumption is about 0.5 mA for the LCD driver and 20-30 mA for the backlight (depending on LED count), so it's suitable for battery-powered devices.
The first step is to prepare your bitmap image. Use any image editor (like GIMP or Photoshop) to resize the image to 128x64 pixels, then convert to grayscale and threshold to pure black and white (no dithering unless you want a halftone effect). Save as a 1-bit BMP or PNG. Then, use a converter tool to generate the byte array. For example, in Image2LCD, set the output format to "C language array", select "vertical scan" (most common for ST7565), and set the byte order to "MSB first" or "LSB first" depending on your controller. The ST7565 typically uses MSB first, where bit 7 is the top pixel of the column. The array size will be 1024 bytes, but the controller expects 128 columns * 8 pages = 1024 bytes. If you use a 132x65 controller, you need to set the column offset to 2 (starting from column 2) and page offset to 0, so the actual data starts at column 2 and page 0. The 3.18-inch display has a resolution of 128x64, so you must ensure the bitmap is exactly 128x64 pixels. If you send more data, the controller will wrap around, causing visual artifacts. For example, if you send 132 columns, the extra 4 columns will show on the right side of the display, but the physical pixels are only 128, so it will be clipped.
Next, initialize the LCD with the correct command sequence. For the ST7565 controller, the initialization includes: reset the display (pull RST low for 10 ms, then high), set bias ratio (1/7 for 64 rows), set V0 voltage regulator (contrast control), set segment direction (normal or reverse), set common output scan direction (normal or reverse), set display start line (usually 0), set page address (0-7), set column address (0-127), and then turn on the display. The command set is sent via SPI or I2C. For SPI, you need 4 wires: CS (chip select), A0 (data/command select), SCLK (clock), and MOSI (data). The clock speed can be up to 10 MHz, but for reliability, use 4 MHz. The data format is 8-bit command or data, with A0 low for commands and high for data. For example, to set the display start line to 0, send 0x40 (command 0x40-0x7F). To set the column address to 0, send 0x10 (high nibble) and 0x00 (low nibble) for column 0. To set the page address to 0, send 0xB0. Then, to send the bitmap data, set A0 high and send 1024 bytes sequentially. The controller will auto-increment the column address after each byte, so you don't need to send column commands for each byte. After sending all data, the display will show the bitmap. For a 3.18-inch display, the contrast voltage (V0) is set by a resistor divider or a software command. The ST7565 has an internal voltage regulator, and you can adjust the contrast by writing to the "Electronic Volume" register (command 0x81, followed by a value from 0x00 to 0x3F). A typical value for 3.3V operation is 0x20 (32), which gives a contrast ratio of about 10:1. If the display is too faint, increase the value; if too dark, decrease it. The operating voltage is 2.7V to 5.5V, but the backlight is separate, usually 3.0V to 3.3V with a series resistor.
For a practical implementation, consider using a microcontroller like the ESP32 or STM32, which have hardware SPI. The SPI transaction for 1024 bytes at 10 MHz takes about 0.8 ms, so you can update the display at 1200 Hz, but the LCD's internal frame rate is limited to 60-100 Hz. For a static bitmap, you only need to send it once. However, if you want to animate, you can store multiple bitmaps in flash memory (e.g., 10 frames would be 10 * 1024 = 10 KB, which is fine for most microcontrollers). The 3.18-inch display has a 128x64 resolution, so each pixel is square, and the aspect ratio is 2:1. The display module typically includes a white LED backlight with a brightness of 200 cd/m², and the contrast ratio is 1000:1 in a dark room. The viewing angle is 6 o'clock, meaning the best contrast is when the display is viewed from below (like a calculator). If you view from above, the contrast drops to 50% at 30 degrees. The COG technology makes the display thin (1.5 mm) and lightweight (about 10 grams), so it's ideal for portable devices. The interface is 8-bit parallel or SPI, but SPI is preferred for fewer wires. The SPI mode is 0 (CPOL=0, CPHA=0) or 3 (CPOL=1, CPHA=1), but most controllers use mode 0. The data is sent MSB first. For example, to send a byte 0x55 (binary 01010101), the clock pulses 8 times, and the MOSI line changes accordingly. The CS line must be low during the entire transaction. After the transaction, set CS high to disable the chip.
One common issue is the bitmap orientation. The ST7565 controller expects the bitmap to be in "vertical" orientation, meaning the first byte corresponds to the top 8 pixels of column 0, the second byte to the next 8 pixels of column 0 (if using 8-page mode), but actually, the controller uses a "page" system where each page is 8 rows. So page 0 is rows 0-7, page 1 is rows 8-15, etc. So the bitmap data is arranged as: page 0, column 0-127, then page 1, column 0-127, etc. This is different from horizontal scan (like in some OLEDs). If your bitmap is generated for horizontal scan, you'll need to transpose it. For example, if you have a 128x64 bitmap in horizontal format (each byte is 8 horizontal pixels), you need to convert it to vertical format. This can be done in software by rotating the bitmap 90 degrees or by using a conversion tool. The 3.18-inch display's pixel pitch is 0.6 mm, so the total active area is 128 * 0.6 = 76.8 mm wide and 64 * 0.6 = 38.4 mm tall, which is about 3.02 inches by 1.51 inches, matching the 3.18-inch diagonal (sqrt(76.8^2 + 38.4^2) ≈ 86 mm ≈ 3.38 inches, but the actual diagonal is 3.18 inches, so the pixel pitch is slightly smaller, around 0.55 mm). The exact specifications depend on the manufacturer, but typically, the active area is 73.4 mm x 38.8 mm, giving a pixel pitch of 0.573 mm. The display module includes a COG driver IC, which is bonded to the glass with anisotropic conductive film (ACF), and the connection is via zebra strips or a flexible PCB. The operating temperature range is -20°C to +70°C, and the storage temperature is -30°C to +80°C. The backlight is a white LED with a typical forward voltage of 3.0V and current of 20 mA, so you need a current-limiting resistor. For example, if you use a 5V supply, the resistor value is (5V - 3V) / 0.02A = 100 ohms. The brightness can be adjusted by PWM, with a frequency of 100 Hz to 1 kHz to avoid flicker.
For debugging, use a logic analyzer to check the SPI signals. The CS line should go low before the first clock, and stay low for all 1024 bytes. The A0 line should be low for commands and high for data. The clock should have a clean square wave with no glitches. If the display shows garbage, check the bitmap orientation, the contrast setting, and the initialization sequence. A common mistake is setting the display start line to a non-zero value, which shifts the image vertically. For example, if you set the start line to 16, the image will be shifted down by 16 rows, and the top 16 rows will be blank. Also, check the segment direction: if the image is mirrored horizontally, you can change the segment direction command (0xA0 for normal, 0xA1 for reverse). Similarly, for vertical mirroring, change the common output scan direction (0xC0 for normal, 0xC8 for reverse). The 3.18-inch display has a 128x64 resolution, so the number of commons is 64, and the number of segments is 128. The controller can drive up to 132 segments and 65 commons, but the extra ones are not connected. The display module uses a 1/64 duty cycle, so each row is active for 1/64 of the frame time. The frame rate is typically 60 Hz, so each row is refreshed every 16.7 ms, but the LCD response time is about 100 ms, so there is no flicker. The contrast ratio is 1000:1 in a dark room, but in ambient light, it drops to about 10:1 due to reflection. The display is reflective, so it works well in bright light without the backlight. The COG technology reduces the thickness to 1.5 mm, making it suitable for thin devices like smartwatches or calculators. The interface is 8-bit parallel or SPI, but SPI is simpler with only 4 wires. The SPI speed can be up to 10 MHz, but for long wires, use 1 MHz to avoid signal degradation. The power consumption of the LCD driver is 0.5 mA at 3.3V, and the backlight is 20 mA, so total power is about 70 mW. For battery-powered devices, you can turn off the backlight when not in use, and the display retains the image without refresh (since it's a passive LCD, not OLED). The display is also available with a yellow-green backlight or a blue backlight, but the white version is most common. The viewing angle is 6 o'clock, meaning the best contrast is when the display is tilted 30 degrees away from the viewer. The contrast ratio is 10:1 at 30 degrees, and 5:1 at 60 degrees. The display is also available with a transflective mode, which combines reflective and transmissive properties for better visibility in all lighting conditions.
For a real-world example, let's say you want to display a 128x64 bitmap of a logo. You would use an image editing tool to create a 1-bit BMP, then convert it to a C array using a tool like LCD Assistant. The tool will output an array like: const unsigned char logo[] = {0x00, 0x00, 0x00, 0x00, ...}; with 1024 entries. Then, in your microcontroller code, you initialize the LCD with the command sequence, then send the array using SPI. For an Arduino, the code would look like: digitalWrite(CS, LOW); digitalWrite(A0, HIGH); for (int i = 0; i < 1024; i++) { SPI.transfer(logo[i]); } digitalWrite(CS, HIGH);. The SPI library in Arduino uses mode 0 by default. If you use an STM32, you can use the HAL library with SPI_Transmit. The 3.18-inch display has a 128x64 resolution, so the bitmap must be exactly 128x64 pixels. If you have a larger image, you need to crop or scale it. The display's pixel pitch is 0.6 mm, so text at 8x8 pixels is about 4.8 mm tall, which is readable at 30 cm distance. For a 3.18-inch display, the typical viewing distance is 20-50 cm, so 8x8 font is fine. You can also use larger fonts like 16x16 for better readability. The display supports multiple fonts by storing them in flash memory. For example, a 16x16 font character takes 32 bytes, so you can store 256 characters in 8 KB. The display can also show graphs by plotting pixels individually. For a bar graph, you can set the pixel at (x, y) by writing to the corresponding byte and bit. The ST7565 controller allows you to set individual pixels by reading the current page and column, modifying the byte, and writing it back. However, this is slow because you need to read-modify-write. A faster method is to use a frame buffer in RAM, update the buffer, and then send the entire buffer to the display. For a 128x64 display, the frame buffer is 1024 bytes, which is small enough for most microcontrollers. For example, an ESP32 has 520 KB of SRAM, so you can easily allocate a frame buffer. The frame buffer allows you to draw lines, circles, and text using standard graphics libraries like Adafruit GFX. The Adafruit library supports the ST7565 controller, and you can use it with the 3.18-inch display by setting the correct pins. The library includes functions for drawing pixels, lines, rectangles, and text. For example, to draw a circle, you use display.drawCircle(64, 32, 10, WHITE). The library also supports bitmap images by using the drawBitmap function, which expects the array in the correct format. The 3.18-inch display has a 128x64 resolution, so the bitmap must be 128x64 pixels. The library handles the conversion automatically if you use the correct format. The Adafruit library uses the "vertical" format, so if your bitmap is in horizontal format, you need to convert it. The library also includes a font file for 5x7 characters, which is stored in program memory. The display can show up to 21 characters per line (128/6 ≈ 21) and 8 lines (64/8 = 8) for 5x7 font, or 16 characters per line for 8x8 font. The 3.18-inch display is also available with a touch panel, but the standard version is without touch. The COG technology makes the display thin and lightweight, but the glass is fragile, so you need to handle it with care. The display module includes a polarizer, which can be damaged by solvents. The operating voltage is 2.7V to 5.5V, but the backlight is separate. The display is also available with a 3.3V or 5V logic level, but the SPI signals must be within the specified range. The 3.18-inch display is commonly used in medical devices, industrial equipment, and consumer electronics. The bitmap display method is straightforward, but you need to ensure the data format matches the controller. The ST7565 controller is widely used, and the initialization sequence is standard. The display can also be used in sleep mode to save power, with a current consumption of 0.1 mA. The sleep mode is activated by writing 0xAE to the display, and it can be woken up by writing 0xAF. The display retains the image in sleep mode because it's a passive LCD. The 3.18-inch display has a 128x64 resolution, so the bitmap is 1024 bytes. The display can also be used with a 4-wire SPI interface, which is the most common. The SPI pins are: CS (chip select), A0 (data/command), SCLK (clock), and MOSI (data). The MISO pin is