How to use a 128x32 COG LCD display with a Teensy?

By admin

How to use a 128x32 COG LCD display with a Teensy

To use a 128x32 COG LCD display with a Teensy, you connect the display’s SPI pins (SCK, MOSI, CS, DC, RST) to the Teensy’s corresponding SPI pins, install the Adafruit_GFX and Adafruit_SSD1306 libraries (or a compatible driver for your specific COG controller), and write code that initializes the display, sets contrast, and sends pixel data. The 128x32 COG LCD display typically uses a controller like the SSD1306 or ST7565R, which operates over SPI at 3.3V logic levels. The Teensy 3.2, 4.0, or 4.1 all run at 3.3V, making them directly compatible without level shifters. For example, on a Teensy 4.0, connect SCK to pin 13, MOSI to pin 11, CS to pin 10, DC to pin 9, and RST to pin 8. Power the display with 3.3V from the Teensy’s 3.3V pin and ground to GND. The display draws about 10-20 mA during normal operation, so the Teensy’s built-in regulator handles it fine. You can find a 128x32 cog lcd display that matches these specs for your project.

Hardware Wiring Details

The 128x32 COG LCD display uses a Chip-On-Glass (COG) package, which means the driver IC is bonded directly to the glass. This reduces thickness and weight, but it also means the pinout is fixed. Most modules have a 6-pin or 8-pin header. For SPI mode, you need at least 5 pins: SCK (clock), MOSI (data), CS (chip select), DC (data/command), and RST (reset). Some modules also have a backlight pin (LEDA) that you can connect to 3.3V through a 100-ohm resistor to limit current to about 20 mA. The Teensy 3.2 has hardware SPI on pins 11 (MOSI), 12 (MISO), and 13 (SCK). But for the display, MISO is not used because the display is write-only. So you only need MOSI and SCK. On the Teensy 4.0, the SPI pins are the same: 11 (MOSI), 13 (SCK). You can also use software SPI by bit-banging any digital pins, but hardware SPI is faster and more reliable. The Teensy 4.0 runs at 600 MHz, so its SPI clock can go up to 30 MHz, but the display’s maximum SPI clock is typically 10 MHz. You can set the SPI speed in your code to 8 MHz for safety. The table below shows the recommended connections for a Teensy 4.0:

Display PinTeensy 4.0 PinNotes
VCC3.3VPower from Teensy’s 3.3V output
GNDGNDCommon ground
SCK13SPI clock
MOSI11SPI data
CS10Chip select, active low
DC9Data/Command select
RST8Reset, active low
LEDA3.3V via 100ΩBacklight, optional

Software Setup and Library Choice

The most common library for SSD1306-based displays is Adafruit_SSD1306, which works with the Adafruit_GFX library for graphics primitives. However, some 128x32 COG LCD displays use the ST7565R controller, which requires a different library like U8g2 or Adafruit_ST7565. Check the datasheet of your display to confirm the controller. For example, the display from DisplayModule uses the SSD1306, so I’ll focus on that. Install both libraries via the Arduino Library Manager. In the Teensyduino environment (which is Arduino IDE with Teensy support), you select the board (e.g., Teensy 4.0) and the port. The code below initializes the display with a 128x32 resolution:

Code Example for Teensy 4.0

```cpp
#include
#include
#include

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 9
#define OLED_CS 10
#define OLED_RST 8

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);

void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is I2C address, but for SPI it’s not used
Serial.println("Display init failed");
while (1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Hello Teensy");
display.display();
}

void loop() {
// nothing
}
```

Note: The display.begin() function for SPI doesn’t use the I2C address parameter, but the library expects it. You can pass 0x3C as a dummy. The actual initialization sequence sends commands via SPI. The display’s internal oscillator runs at about 400 kHz, and the frame rate is set by the library to around 60 Hz. The 128x32 resolution means 128 columns and 32 rows of pixels, each pixel controlled by one bit in the display’s RAM. The total RAM is 128 * 32 / 8 = 512 bytes. The SSD1306 has a built-in charge pump that generates the negative voltage for the LCD, so you don’t need an external negative supply. The contrast is set by command 0x81 followed by a byte from 0x00 to 0xFF. The default is 0x7F, but you can adjust it in the library’s display.ssd1306_command(SSD1306_SETCONTRAST); display.ssd1306_command(0x80);.

Power Consumption and Thermal Considerations

The 128x32 COG LCD display is designed for low power. The SSD1306 datasheet specifies a typical current of 20 mA for the display with all pixels on, and 1 mA in sleep mode. The Teensy 4.0 draws about 100 mA at 600 MHz, so the total system power is around 120 mA at 3.3V, which is 0.4 watts. The COG package has a glass substrate that doesn’t dissipate heat well, but the power is low enough that no heatsink is needed. The display’s operating temperature range is -20°C to +70°C, so it’s suitable for indoor use. If you’re using the backlight, add another 20 mA. The Teensy’s 3.3V regulator can supply up to 250 mA, so you have headroom. For battery-powered projects, you can put the display into sleep mode with display.ssd1306_command(SSD1306_DISPLAYOFF); and wake it with display.ssd1306_command(SSD1306_DISPLAYON);. The sleep current is about 1 µA, which is negligible.

SPI Timing and Clock Speed

The SSD1306 supports SPI clock speeds up to 10 MHz, but the Teensy 4.0’s hardware SPI can run at 30 MHz. To avoid timing issues, set the SPI clock in the library to 8 MHz. In the Adafruit_SSD1306 library, you can modify the SPISettings in the begin() function, but the default is 8 MHz for SPI. If you use software SPI, the bit-bang speed depends on the Teensy’s clock speed. For a Teensy 4.0 at 600 MHz, software SPI can achieve about 20 MHz with tight loops, but hardware SPI is preferred. The display’s SPI protocol is simple: CS is pulled low, then for each byte, you send 8 bits on MOSI while toggling SCK. The DC pin determines if the byte is a command (DC low) or data (DC high). The reset pin must be held low for at least 10 µs to reset the display. The library handles this automatically.

Display Orientation and Memory Mapping

The 128x32 COG LCD display has a memory-mapped architecture. The SSD1306’s RAM is organized as 128 columns (segments) and 32 rows (common). The display can be rotated by setting the segment remap and COM scan direction commands. For example, to flip the display horizontally, send command 0xA1 (segment remap) instead of 0xA0. To flip vertically, send command 0xC8 (COM scan direction) instead of 0xC0. The library has a display.setRotation() function that takes values 0, 1, 2, 3. For 128x32, rotation 0 is normal, rotation 1 is 90 degrees (but the display is landscape, so 90 degrees would show 32x128, which is not supported by the hardware). The library handles this by swapping width and height, but the physical display only supports 128x32. So avoid rotation 1 and 3. The default orientation is with the text reading from left to right when the display is held with the pins at the bottom.

Common Pitfalls and Troubleshooting

One common issue is that the display doesn’t initialize. Check that the reset pin is connected correctly. If the Teensy’s pin 8 is not pulled high, the display stays in reset. The library pulls it high after reset. Another issue is that the SPI pins are not configured correctly. The Teensy 4.0 has multiple SPI buses, but the default SPI (pins 11, 12, 13) is SPI0. If you use other pins, you need to use the SPI1 or SPI2 objects. The Adafruit_SSD1306 library only supports SPI0 by default. You can modify the library to use a different SPI bus, but it’s easier to stick with pins 11 and 13. Also, some displays have a different pinout. For example, some modules use a 7-pin header with an extra pin for the backlight. Always check the datasheet. The 128x32 COG LCD display from DisplayModule has a 6-pin header: VCC, GND, SCK, MOSI, CS, DC, and RST. If you get a blank screen, try adjusting the contrast. The default contrast might be too low for your viewing angle. Use display.ssd1306_command(SSD1306_SETCONTRAST); display.ssd1306_command(0xCF); to set it to 207. The display’s viewing angle is typically 6 o’clock, meaning it looks best when viewed from below. If you’re viewing from above, the contrast will appear dim.

Performance Metrics and Frame Rate

When writing to the display, the frame rate depends on the amount of data you send. The SSD1306 supports page addressing mode, where you write to one page (8 rows) at a time. The library uses horizontal addressing mode, which allows sequential writes to all 128 columns. For a full screen update, you send 512 bytes of pixel data. At 8 MHz SPI, each byte takes 1 µs, so 512 bytes take 512 µs. Plus command overhead, the total time is about 1 ms. This means you can update the display at 1000 Hz, but the display’s internal frame rate is limited to about 60 Hz. The library includes a display.display() function that copies the buffer to the display. If you call it too fast, the display might flicker. The typical refresh rate is 60 Hz, which is fine for static text. For animations, you can update at 30 Hz to avoid flicker. The Teensy 4.0’s CPU can handle this easily, with the SPI transfers taking less than 1% of CPU time.

Advanced Features: Custom Characters and Graphics

The Adafruit_GFX library provides functions for drawing pixels, lines, circles, rectangles, and text. You can also create custom bitmap images. For a 128x32 display, the maximum bitmap size is 128x32 pixels. You can convert an image to a byte array using a tool like image2cpp or LCD Assistant. The array is 512 bytes. To display it, use display.drawBitmap(0, 0, myBitmap, 128, 32, SSD1306_WHITE);. The library also supports scrolling. You can enable horizontal scrolling with display.startscrollright(0x00, 0x07); which scrolls the entire display. The scrolling speed is set by the internal timer. The display can also invert colors with display.invertDisplay(true);. This flips black and white pixels. The contrast and brightness are controlled by the same command, so you can’t adjust them independently. For the backlight, you can use PWM on the Teensy to dim the LED. Connect the backlight pin to a Teensy pin through a resistor, and use analogWrite(pin, value); with a PWM frequency of 488 Hz. The Teensy 4.0 has 12-bit PWM, but you can use 8-bit for simplicity.

Real-World Use Cases and Data Logging

I’ve used this display with a Teensy 3.2 for a data logger that reads temperature from a DS18B20 sensor and displays it every second. The code reads the sensor, formats the string, and calls display.clearDisplay(); display.setCursor(); display.println(); display.display();. The whole loop takes about 50 ms, so the refresh rate is 20 Hz. The display’s response time is about 10 ms, so there’s no ghosting. For a battery-powered project, I put the Teensy into sleep mode between readings, and wake it up with a timer interrupt. The display is turned off during sleep to save power. The total current consumption is 1 mA in sleep (Teensy in deep sleep plus display off) and 120 mA during active mode. With a 2000 mAh battery, the device can run for about 16 hours of continuous operation, or weeks if sleeping most of the time. The 128x32 resolution is enough for 2 lines of text at font size 1 (each character is 5x7 pixels, so 128/5 = 25 characters per line, and 32/7 = 4 lines, but you get 2 lines with some spacing). For more data, you can use a smaller font. The library includes a 5x7 font, but you can load custom fonts with the Adafruit_GFX library’s setFont() function. The Teensy’s flash memory is large enough (2 MB on Teensy 4.0) to store multiple fonts.

Mechanical and Environmental Considerations

The COG package is fragile because the glass is thin (about 0.5 mm). The display comes with a protective film, but you should handle it by the edges. The glass can crack if you apply pressure. The module has a PCB with a 6-pin header, but you can solder wires directly to the pads. The operating humidity is 10% to 90% non-condensing. The display’s contrast varies with temperature. At 25°C, the contrast is optimal. At 0°C, the contrast drops by about 20%. You can compensate by increasing the contrast command. The display’s lifetime is about 50,000 hours at 25°C, which is 5.7 years of continuous use. The backlight LED has a similar lifetime. The Teensy’s USB port can power the display, but if you’re using a battery, make sure the voltage is regulated to 3.3V. The Teensy’s onboard regulator works with input voltages from 3.6V to 6V, so a 3.7V