How to connect a 3.18 inch 128x64 COG LCD to Raspberry Pi?
How to Connect a 3.18 inch 128x64 COG LCD to Raspberry Pi
You connect a 3.18 inch 128x64 COG LCD to a Raspberry Pi by wiring the SPI interface pins directly to the Pi’s GPIO header, enabling the SPI kernel module, and using a library like Python’s spidev or the Adafruit CircuitPython framebuffer to drive the display. This specific display, a 3.18 inch 128x64 cog lcd display, uses the ST7565R or similar controller, which operates over a 4-wire SPI bus at 3.3V logic levels. The Raspberry Pi’s GPIO pins (BCM numbering) for SPI0 are: MOSI on pin 19, MISO on pin 21, SCLK on pin 23, and CE0 on pin 24. You’ll also need to connect VCC (3.3V), GND, and the display’s reset and data/command pins. For the ST7565R, the typical pinout includes: CS (chip select), RST (reset), RS (register select, also called DC), SCLK (serial clock), and SID (serial data in). On the 3.18 inch model, the 8-pin header is usually arranged as: pin 1 (CS), pin 2 (RST), pin 3 (RS), pin 4 (SCLK), pin 5 (MOSI), pin 6 (VCC), pin 7 (GND), and pin 8 (BL or backlight). Connect CS to CE0, RST to any free GPIO (say GPIO 25), RS to GPIO 24, SCLK to SCLK, MOSI to MOSI, VCC to 3.3V, GND to GND, and BL to 3.3V through a 100-ohm resistor for backlight control. Enable SPI via raspi-config: run “sudo raspi-config”, go to “Interface Options”, select “SPI”, and enable it. Reboot. Then install the Python library: “sudo pip3 install spidev” and “sudo pip3 install adafruit-circuitpython-framebuf”. Write a Python script that initializes the SPI device (spi = spidev.SpiDev(0, 0)), sets the speed to 4 MHz, and sends commands to the ST7565R controller—like 0xAF for display on, 0xA4 for normal display, 0x40 for start line, and 0xC0 for COM scan direction. The display is 128x64 pixels, monochrome, and requires a framebuffer of 1024 bytes (128 * 64 / 8). You can use the Adafruit_SSD1306 library as a base but modify the initialization sequence for the ST7565R, which differs in command set. For example, the ST7565R needs a bias setting of 0xA2 (1/9 bias) and a contrast register (0x81 followed by a value from 0x00 to 0x3F). The 3.18 inch COG (chip-on-glass) LCD uses a reflective or transflective mode, so it doesn’t need a backlight in bright environments, but the optional backlight helps in low light. The display’s pixel pitch is about 0.56 mm, giving a clear viewable area of 71.7 mm x 38.8 mm. The SPI clock speed can go up to 10 MHz, but 4 MHz is safe for long wires. The Raspberry Pi’s 3.3V logic matches the display’s level, but the backlight LED (if present) typically draws 20-30 mA at 3.3V, so a resistor is mandatory to avoid exceeding the Pi’s GPIO current limit of 16 mA per pin. For the reset pin, you can tie it to a GPIO and pulse it low for 1 microsecond during initialization. The data/command pin (RS) selects between command mode (low) and data mode (high). The ST7565R supports vertical and horizontal scrolling, but for basic graphics, you just write pixel data to the framebuffer and send it page by page. Each page is 8 pixels tall, so you have 8 pages (64 / 8). The display’s column address range is 0 to 127, and page address is 0 to 7. You set the column start and end with commands 0x10 (high nibble) and 0x00 (low nibble), then set the page with 0xB0 to 0xB7. After that, send 128 bytes of pixel data per page. The Raspberry Pi’s SPI bus is full-duplex, but the display only receives data, so you can ignore MISO. The 3.18 inch COG LCD’s glass substrate is thin—about 1.1 mm—and the COG bonding method reduces the footprint, making it suitable for compact enclosures. The operating temperature range is typically -20°C to +70°C, and the storage range is -30°C to +80°C. The display’s driver IC is mounted directly on the glass, so handling is delicate; avoid bending the flex cable. The flex cable has a 0.5 mm pitch, so a breakout board or careful soldering is needed for breadboard use. For a permanent connection, use a 0.5 mm FPC connector on a custom PCB. The Raspberry Pi’s GPIO 2 and 3 (I2C) are not used here, but you can repurpose them if needed. The SPI interface uses only 5 wires (CS, RST, RS, SCLK, MOSI) plus power, so it’s efficient. The display’s power consumption is about 1.5 mA at 3.3V without backlight, and 20 mA with backlight on. The Raspberry Pi 4B’s 3.3V rail can supply up to 600 mA, so this is negligible. To test the connection, write a simple Python script that clears the framebuffer, draws a line from (0,0) to (127,63), and sends it to the display. Use the framebuf module: “import framebuf; fb = framebuf.FrameBuffer(bytearray(1024), 128, 64, framebuf.MONO_VLSB)”. The MONO_VLSB format means each byte’s LSB corresponds to the top pixel of the page. The ST7565R expects the data in this format. If the display shows static or no image, check the SPI wiring: use a multimeter to verify continuity, and ensure the CS pin is pulled low during transactions. The Raspberry Pi’s SPI0 CE0 is active low, so the display’s CS must be connected to CE0. If you use a different GPIO for CS, you must toggle it manually. The speed of the SPI bus affects refresh rate: at 4 MHz, sending 1024 bytes takes about 256 microseconds, so you can refresh at 60 Hz without issue. The display’s response time is 100-200 microseconds, so it’s fine for static images or slow animations. The 3.18 inch 128x64 COG LCD has a contrast ratio of about 6:1 (typical for STN LCDs), and the viewing angle is 60 degrees in the horizontal and vertical directions. The pixel arrangement is 128 columns by 64 rows, with a dot size of 0.50 mm x 0.50 mm and a dot pitch of 0.56 mm. The active area is 71.7 mm x 38.8 mm, and the module size is 80.0 mm x 48.0 mm x 6.0 mm (including the flex cable). The weight is around 12 grams. The display’s controller, the ST7565R, has a built-in charge pump for the LCD drive voltage, so you don’t need an external negative voltage. The charge pump uses capacitors connected to pins C1+, C1-, C2+, C2-, and VOUT. On the 3.18 inch module, these are pre-configured, so you just provide 3.3V. The contrast can be adjusted via software by sending 0x81 followed by a byte. A typical value for 3.3V is 0x20 (32 decimal). If the display is too faint, increase it to 0x30; if too dark, decrease to 0x10. The display’s power-on sequence is: apply VCC, wait 10 ms, pulse RST low for 1 us, then send initialization commands: 0xAE (display off), 0xA2 (bias set 1/9), 0xA0 (segment direction normal), 0xC8 (COM scan direction reverse), 0x40 (start line 0), 0x2F (power control: internal power), 0x81 (contrast set), 0x20 (contrast value), 0xA4 (display normal), 0xAF (display on). The Raspberry Pi’s GPIO library, like RPi.GPIO or gpiozero, can be used to control the RST and RS pins. For example, “import RPi.GPIO as GPIO; GPIO.setmode(GPIO.BCM); GPIO.setup(25, GPIO.OUT); GPIO.output(25, GPIO.HIGH); GPIO.output(25, GPIO.LOW); time.sleep(0.001); GPIO.output(25, GPIO.HIGH)”. The RS pin toggles between command and data: “GPIO.output(24, GPIO.LOW)” for command, “GPIO.output(24, GPIO.HIGH)” for data. The SPI transaction is done with “spi.xfer2([command])” or “spi.xfer2(data_list)”. The spidev library returns the received bytes, but the display doesn’t send data, so you can ignore the return. The 3.18 inch COG LCD is often used in industrial or medical equipment because of its wide temperature range and low power. The Raspberry Pi’s 5V pin can power the display through a 3.3V regulator, but the Pi’s 3.3V output is sufficient. If you use a long cable (over 10 cm), add a 0.1 uF capacitor between VCC and GND near the display to filter noise. The SPI clock line should be kept away from power lines to avoid coupling. The display’s backlight can be PWM-controlled via a GPIO: connect the backlight pin to a transistor (like 2N2222) base, collector to backlight, emitter to GND, and use a 1 kHz PWM signal from the Pi. The duty cycle controls brightness. For example, “p = GPIO.PWM(18, 1000); p.start(50)” for 50% brightness. The 3.18 inch 128x64 COG LCD’s contrast can also be adjusted by varying the voltage on the V0 pin, but on the module, it’s internally set. The display’s viewing angle is optimized for 6:00 (bottom view), meaning the best contrast is when looking upward. If you need top view, you can change the COM scan direction command to 0xC0. The pixel data is stored in the display’s RAM in a column-major order, but the framebuffer library handles the mapping. The ST7565R has a built-in 128x64-bit RAM, so you can write to any pixel individually. The display’s sleep mode (0xAE) reduces power to 0.1 mA, useful for battery-powered projects. The Raspberry Pi’s SPI bus can be shared with other devices if they have separate chip selects. The 3.18 inch COG LCD’s controller supports 68-series and 80-series parallel interfaces, but the module is wired for SPI only. The SPI interface is 4-wire (no MISO), so it’s half-duplex. The display’s maximum SPI clock is 10 MHz, but the Raspberry Pi’s SPI speed is limited to 32 MHz on the 4B. At 10 MHz, the refresh time is 1024 bytes * 8 bits / 10 MHz = 819 microseconds, giving a 120 Hz theoretical refresh. In practice, Python overhead reduces it to 30-40 Hz. For faster updates, use C or the pigpio library. The display’s flex cable has a 0.5 mm pitch, 8 pins, and is 20 mm long. The cable’s bend radius is 1 mm, so avoid sharp bends. The COG (chip-on-glass) technology means the IC is bonded directly to the glass, reducing the module thickness to 6 mm (including the PCB). The PCB on the back of the display has a 2.54 mm pitch header, but some modules come with a 1.0 mm pitch. The 3.18 inch model from the link uses a 2.54 mm pitch, so it fits breadboards. The display’s operating voltage is 2.7V to 3.6V, so 3.3V is ideal. The backlight LED forward voltage is 3.0V to 3.2V, so a 100-ohm resistor limits current to (3.3-3.0)/100 = 3 mA, which is safe. If the backlight is too dim, use a lower resistor like 47 ohms, but check the current with a multimeter. The Raspberry Pi’s GPIO can source 16 mA, but the backlight pin on the display is often connected directly to the LED, so use a transistor for higher current. The display’s contrast is temperature-dependent; at low temperatures, the contrast decreases, so you may need to increase the contrast register value. The ST7565R has a temperature compensation register (0x24 to 0x27), but it’s rarely used. The 3.18 inch 128x64 COG LCD’s pixel shape is square, so text is crisp. You can use the Python Imaging Library (PIL) to render fonts: “from PIL import Image, ImageDraw, ImageFont; img = Image.new(‘1’, (128, 64)); draw = ImageDraw.Draw(img); draw.text((0,0), ‘Hello’, font=ImageFont.load_default()); fb = framebuf.FrameBuffer(img.tobytes(), 128, 64, framebuf.MONO_VLSB)”. Then send the framebuffer to the display. The display’s SPI protocol requires the CS to be low during the entire transaction. The spidev library handles this automatically for CE0 and CE1. If you use a custom CS, you must set it low before the transfer and high after. The display’s initialization sequence must be sent exactly as per the datasheet. A common mistake is to forget the bias setting (0xA2), which causes the display to be blank. Another is to set the start line to non-zero (0x40 is default). The 3.18 inch COG LCD’s driver IC is the ST7565R, which is compatible with the ST7565P and ST7565G. The command set is identical. The display’s RAM is 128x64 bits, but the ST7565R also supports 132x64, so the extra columns are ignored. The display’s pinout is standard, but always verify with a multimeter: the CS pin should be pulled high when not selected. The Raspberry Pi’s SPI0 CE0 is GPIO 8 (BCM), and CE1 is GPIO 7. The 3.18 inch display uses CE0 by default. The SPI clock polarity (CPOL) and phase (CPHA) are both 0 (mode 0). The spidev library defaults to mode 0, so no change is needed. The display’s data is shifted in on the rising edge of SCLK. The maximum bit rate is 10 MHz, but the Raspberry Pi’s 4 MHz is reliable. The 3.18 inch 128x64 COG LCD’s power consumption is 1.5 mA typical, 2.5 mA max. The backlight adds 20 mA. The Raspberry Pi’s total current draw is about 600 mA for the 4B, so the display is negligible. The display’s operating temperature range is -20°C to +70°C, so it’s suitable for outdoor projects. The storage temperature is -30°C to +80°C. The display’s glass is 1.1 mm thick, so it’s fragile. The COG bonding is robust, but mechanical shock can crack the glass. The display’s viewing angle is 60 degrees, meaning you can see the image from 60 degrees off-axis. The contrast ratio is 6:1, typical for STN displays. The pixel colors are black on gray (reflective) or black on white (transflective with backlight). The 3.18 inch model is reflective, so it works best in bright light. The backlight is optional. The display’s module size is 80.0 mm x 48.0 mm x 6.0 mm, and the active area is 71.7 mm x 38.8 mm. The weight is 12 grams. The display’s flex cable is 20 mm long, and the pitch is 0.5 mm. The breakout board has a 2.54 mm pitch header, so it’s breadboard-friendly. The display’s controller is the ST7565R, which has a 128x64-bit RAM. The RAM is organized as 8 pages of 128 bytes. The page address is set with 0xB0 to 0xB7, and the column address is set with 0x10 (high nibble) and 0x00 (low nibble). The display’s data is written in bursts of up to 128 bytes. The SPI transaction is: CS low, send command byte, CS high. For data, CS low, send data bytes, CS high. The display’s reset pin must be pulsed low for 1 microsecond after power-up. The display’s power-on sequence is: apply VCC, wait 10 ms, pulse RST, wait 10 ms, send initialization commands. The initialization commands are: 0xAE (display off), 0xA2 (bias 1/9), 0xA0 (segment normal), 0xC8 (COM reverse), 0x40 (start line 0), 0x2F (power control), 0x81 (contrast), 0x20 (contrast value), 0xA4 (display normal), 0xAF (display on). The contrast value can be adjusted from 0x00 to 0