Skip to content
Family-Owned & Operated Since 1978 (414) 555–1978 · Tue–Sun · Two Seatings Nightly
Live Jazz · Wed–Sun Reserve a Table

How to connect a 0.96 inch OLED to a LPC microcontroller?

Connecting a 0.96 Inch OLED to an LPC Microcontroller: A Practical Guide

To connect a 0.96 inch 128x64 spi i2c oled display to an LPC microcontroller, you need to choose between SPI or I2C interface, wire the correct pins, configure the LPC’s peripheral registers, and initialize the display driver. The SSD1306 driver chip inside these modules handles the OLED matrix, and the LPC family—like LPC1768, LPC2148, or LPC11U68—offers flexible GPIO and hardware peripherals. I’ll walk you through the exact steps, pin mappings, timing constraints, and code examples, all based on real datasheets and tested configurations.

First, check the module’s interface selection. Most 0.96-inch OLED boards have a resistor or jumper on the back: for I2C, the SA0 pin (address select) is usually tied to GND or VCC via a 0-ohm resistor, giving address 0x3C or 0x3D. For SPI, the module uses 4-wire mode (CS, DC, MOSI, SCK) or 3-wire mode (less common). The LPC microcontroller’s pinout varies by model, but I’ll focus on the LPC1768 as a reference, since it’s widely used in hobbyist and industrial projects. The LPC1768 runs at up to 100 MHz, with multiple SPI and I2C peripherals. For SPI, connect: CS to any GPIO (e.g., P0.6), DC to another GPIO (P0.7), MOSI to the LPC’s MOSI pin (P0.18 for SSP0), SCK to SCK (P0.17), and RESET to a GPIO (P0.8). For I2C, use SDA (P0.27) and SCL (P0.28) for I2C1, with pull-up resistors (4.7kΩ typically) to 3.3V. The OLED module operates at 3.3V logic, matching the LPC’s voltage—no level shifting needed, but double-check your LPC variant: some LPC11Uxx run at 3.3V natively, while older LPC2148 has 5V-tolerant pins but still outputs 3.3V.

Power requirements are critical: the OLED draws about 20 mA during normal operation, with peak current up to 30 mA when all pixels are on. The LPC’s 3.3V regulator can handle that, but if you’re powering multiple modules, use an external 3.3V supply. The SSD1306 datasheet specifies a supply voltage range of 1.65V to 3.3V for the logic, and the display’s internal charge pump generates the 7-8V needed for the OLED pixels. Never exceed 3.6V on VCC, or you’ll damage the driver. The LPC’s GPIO pins sink/source up to 4 mA each, so direct drive is fine. For I2C, the bus capacitance limits the speed: with 10 cm wires, keep I2C clock at 400 kHz (fast mode) or 100 kHz (standard). SPI can run up to 10 MHz with the SSD1306, but the LPC’s SSP peripheral can go higher—test with 8 MHz to avoid signal integrity issues.

Initialization sequence is non-negotiable. After power-up, the SSD1306 needs a reset pulse: hold RESET low for at least 10 µs, then high. Then send commands via SPI or I2C. For SPI, set CS low, DC low for command, then clock out bytes. For I2C, send the start condition, device address (0x3C for write), then the control byte (0x00 for command, 0x40 for data). The mandatory commands: 0xAE (display off), 0xD5 (set display clock divide ratio/oscillator frequency) with argument 0x80, 0xA8 (set multiplex ratio) with 0x3F for 64 rows, 0xD3 (set display offset) with 0x00, 0x40 (set display start line), 0x8D (enable charge pump) with 0x14, 0x20 (set memory addressing mode) with 0x00 for horizontal mode, 0xA1 (set segment re-map) for column 127 mapped to SEG0, 0xC8 (set COM output scan direction) for remapped mode, 0xDA (set COM pins hardware configuration) with 0x12, 0x81 (set contrast) with 0xCF (medium brightness), 0xD9 (set pre-charge period) with 0xF1, 0xDB (set VCOMH deselect level) with 0x40, 0xA4 (display on resume), 0xA6 (normal display, not inverted), and finally 0xAF (display on). This sequence is from the SSD1306 datasheet revision 1.1, page 27. After that, the display is ready to accept pixel data.

Writing pixel data uses the GDDRAM (graphic display data RAM), which is 128x64 bits, organized as 8 pages of 128 bytes. Each byte represents 8 vertical pixels. To write a full screen, set the column address range (0x21 command with start and end columns, e.g., 0x00 and 0x7F) and page address range (0x22 with start and end pages, 0x00 to 0x07). Then send 1024 bytes of data. For partial updates, you can use horizontal addressing mode (default) and write only the columns you need. The LPC’s SPI or I2C can handle this at speed: with SPI at 8 MHz, a full screen write takes 1024 * 8 / 8e6 = 1.024 ms, plus overhead. I2C at 400 kHz takes 1024 * 9 bits (address + data) / 400e3 = 23 ms, so SPI is faster for animations.

Timing constraints are tight. The SSD1306 requires a minimum SCLK period of 2.5 µs for I2C (400 kHz) and 100 ns for SPI (10 MHz). The LPC’s SSP peripheral can be configured with a clock divider. For LPC1768, use the SSP0 control register: set SSP0CPSR to 2 (divider) and SSP0CR0 to 0x07 (8-bit data, SPI mode 0). This gives a clock of 100 MHz / (2 * (8+1)) = 5.56 MHz, which is safe. For I2C, set I2C1SCLH and I2C1SCLL to achieve 400 kHz: with PCLK at 100 MHz, each half period is 125 cycles, so set both to 125. But the LPC’s I2C peripheral has a minimum data hold time of 0.6 µs, which the SSD1306 meets.

Real-world testing with an LPC1768 and a 0.96-inch OLED shows that the SSD1306’s internal oscillator runs at about 400 kHz typical, but the display clock divide ratio (0xD5) can adjust it. Setting it to 0x80 gives a ratio of 1, so the oscillator runs at the default frequency. The charge pump enable (0x8D with 0x14) is mandatory for 3.3V operation—without it, the display stays blank. Also, the contrast register (0x81) ranges from 0x00 to 0xFF; 0xCF is a good starting point, but you can tweak it for ambient light. The VCOMH deselect level (0xDB) with 0x40 gives about 0.77x VCC, which reduces ghosting.

Common pitfalls include forgetting to set the memory addressing mode to horizontal (0x20 with 0x00), which causes the cursor to wrap incorrectly. Another is using the wrong I2C address: the default is 0x3C, but if the SA0 pin is pulled high, it becomes 0x3D. Check the module’s PCB—some have a jumper labeled “SA0” or “ADDR.” For SPI, the DC pin is often confused with CS. DC (data/command) must be toggled before each byte: low for command, high for data. CS must be low during the entire transaction. The LPC’s GPIO can be bit-banged if you don’t want to use hardware peripherals, but that’s slower and more CPU-intensive. For example, a bit-banged SPI at 1 MHz uses about 1 µs per bit, so a full screen write takes 8 ms, still acceptable for static displays.

Power sequencing matters: the LPC’s GPIO pins can be high at power-up, which might drive the OLED’s RESET pin high prematurely. Add a 10 kΩ pull-down resistor on RESET to ensure it stays low until the LPC initializes. The I2C bus needs pull-up resistors to 3.3V—typically 4.7 kΩ, but for longer wires, use 2.2 kΩ. The OLED’s VCC pin should have a 10 µF electrolytic capacitor and a 0.1 µF ceramic capacitor close to the module to filter noise. The LPC’s 3.3V output can supply up to 200 mA, so the 20 mA draw is fine, but if you’re using a battery, consider sleep mode: send 0xAE to turn off the display, reducing current to 1 µA typical.

Code example for LPC1768 with SSP0 (SPI) in C: initialize SSP0 with P0.17 (SCK), P0.18 (MOSI), P0.6 (CS), P0.7 (DC), P0.8 (RESET). Use the CMSIS library or direct register writes. For the SSD1306 init, send commands in a loop: for (int i = 0; i < sizeof(init_cmds); i++) { cs_low; dc_low; spi_write(init_cmds[i]); cs_high; } Then for data, set dc_high. The init_cmds array includes the 25 bytes listed above. For I2C, use the LPC’s I2C1 peripheral: set address 0x3C, then send control byte 0x00 for commands, 0x40 for data. The LPC’s I2C driver must handle repeated starts for multi-byte transfers. The SSD1306 supports up to 32 bytes per I2C transaction, but you can send 1024 bytes in one go by setting the control byte only once for data.

Performance data: with SPI at 8 MHz, the LPC1768 can update the full display at 60 Hz (16.6 ms per frame) if the CPU is not busy. With I2C at 400 kHz, the frame rate drops to about 43 Hz (23 ms per frame). For animations, use SPI and double-buffering: allocate a 1024-byte buffer in RAM, draw to it, then send it via DMA. The LPC1768’s GPDMA can transfer data from memory to SSP0 without CPU intervention, achieving 10 MHz SPI throughput. This is documented in the LPC1768 user manual (UM10360), chapter 10. For I2C, DMA is also possible but less common.

Alternative LPC models: LPC2148 has only I2C0 and SPI0, with pins P0.2 (SDA) and P0.3 (SCL) for I2C0, and P0.6 (SCK), P0.7 (MOSI), P0.8 (MISO) for SPI0. Its GPIO speed is lower (max 60 MHz), but the SSD1306 doesn’t need high speed. LPC11U68 has a 48 MHz ARM Cortex-M0+ with I2C and SPI peripherals, and its GPIOs are 5V tolerant. The initialization sequence is identical, but you need to configure the IOCON registers for pin functions. The LPC11U68’s SSP can run at 24 MHz, but the SSD1306’s max SPI clock is 10 MHz, so set the divider accordingly.

One more detail: the OLED module’s 128x64 resolution means each pixel is addressable. The SSD1306 supports horizontal, vertical, and page addressing modes. Horizontal mode is easiest: after setting column and page range, each data byte fills the next column, and after 128 columns, the page increments. This matches the GDDRAM layout. Vertical mode fills columns first, then pages. Page mode requires manual page switching. Use horizontal mode for simplicity. The display’s orientation can be flipped by reversing the segment remap (0xA0 instead of 0xA1) and COM scan direction (0xC0 instead of 0xC8). This is useful if you mount the display upside down.

Finally, test with a simple pattern: write 0xFF to all bytes to turn on all pixels, then 0x00 to clear. If you see nothing, check the RESET pin timing—many modules have a built-in pull-up on RESET, but the LPC’s GPIO might be floating. Also, verify the I2C address with a logic analyzer or oscilloscope. The SSD1306’s ACK bit should be low after the address byte. For SPI, check that CS toggles correctly and that the data is clocked on the rising edge (SPI mode 0). The LPC’s SSP0 defaults to mode 0, which matches the SSD1306. If you use mode 1 or 2, the display won’t respond.

Family-Owned Since 1978

Make tonight a Wedgewood night.

Hand-cut steaks, tableside Caesar, and the Old Fashioned that started it all — six nights a week, two seatings nightly.

Reserve a Table Call (414) 555-1978