ENع
←LCD and OLED
Lesson S2 · Special topics

Character LCD and OLED displays

14 min11 steps5 questionsIntermediate
About this lesson

Put one letter, A, on two common small screens from an Arduino Uno R3. A 16 × 2 character LCD (HD44780 controller) takes it as one byte, 0x41, sent in two halves over RS, E and D4 to D7, and draws it from its own font ROM. A 128 × 64 OLED (SSD1306 controller) has no font: the Uno draws A into a 1,024-byte frame buffer in its own SRAM and sends it over I2C, where every byte lights one column of 8 dots. The lesson “UART, I2C and SPI in firmware” covers the I2C bus itself; here we use it.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S2 · Interactive
01 / 11

Two kinds of small screen

A character LCD shows text in fixed cells: 16 columns, 2 rows, each cell 5 by 8 dots. An OLED is a free grid of 128 by 64 dots, each giving off its own light. We will put the letter A on both.

character LCD  16 x 2 cells, 5 x 8 dots each
               text only, in fixed places
OLED           128 x 64 dots, each its own light
               any shape, anywhere
goal           the letter A in the top-left corner of both
Loading figure…
or ← →
Read the lesson step by step
  1. Two kinds of small screen

    A character LCD shows text in fixed cells: 16 columns, 2 rows, each cell 5 by 8 dots. An OLED is a free grid of 128 by 64 dots, each giving off its own light. We will put the letter A on both.

    character LCD  16 x 2 cells, 5 x 8 dots each
                   text only, in fixed places
    OLED           128 x 64 dots, each its own light
                   any shape, anywhere
    goal           the letter A in the top-left corner of both
  2. A controller on each screen

    The Uno never drives single dots. Each screen has its own controller chip with memory: an HD44780 on the LCD, an SSD1306 on the OLED. The Uno sends bytes; the controller keeps the picture lit.

    HD44780   LCD controller
              display memory: 80 character codes
              font ROM: 208 built-in 5 x 8 shapes
    SSD1306   OLED controller
              dot memory: 128 x 64 bits = 1,024 bytes
    the Uno only sends bytes; each controller refreshes its screen
  3. Six wires to the LCD

    In 4-bit mode the Uno uses six pins. RS picks the register: 0 means command, 1 means character. D4 to D7 carry four bits at a time. E is the strobe: a short pulse tells the controller to read them.

    RS   <- Uno pin 12
    E    <- Uno pin 11
    D7   <- Uno pin 2
    D6   <- Uno pin 3
    D5   <- Uno pin 4
    D4   <- Uno pin 5
    R/W  tied to ground: we only write
    RS   0 = command, 1 = character
    E    pulse high then low: read D4-D7 now
  4. A command: go to cell 0

    setCursor(0, 0) sends a command, so RS is 0. The byte is 0x80: "set address" plus address 0x00, the first cell of row 1. Row 2 starts at 0x40. It goes as two halves: 1000, then 0000.

    line 1   lcd.setCursor(0, 0);
    command  0x80 | address 0x00 = 0x80  (1000 0000)
    RS       0  (instruction register)
    D7-D4    1000, E pulse   then   0000, E pulse
    row 1 starts at 0x00, row 2 at 0x40
  5. A is 0x41: high half first

    'A' is the byte 0x41, or 0100 0001 in binary. RS goes to 1: this is a character. The high half goes first: D7 to D4 show 0100, and E pulses once. The controller holds those four bits.

    line 2   lcd.print('A');
    'A'      0x41 = 0100 0001
    RS       1  (data register: a character)
    D7-D4    0100   high half
    E        pulse: the controller reads D4-D7
  6. Then the low half

    Then the low half: 0001, and E pulses again. Now the controller has the whole byte 0x41. It stores it in display memory at address 0x00 and moves on to 0x01. Each write takes up to 37 µs.

    D7-D4    0001   low half, E pulse
    byte     0100 + 0001 = 0x41
    DDRAM    [0x00] = 0x41, cursor -> 0x01
    time     up to 37 µs per write (fOSC 270 kHz)
    LiquidCrystal waits 100 µs after each pulse to be safe
  7. The font is built in

    The controller looks up 0x41 in its font ROM and lights the 5 by 8 pattern of A in cell 0. The Uno sent one byte, not 40 dots. The catch: only shapes in that ROM, plus 8 of your own.

    font ROM  0x41  ->
       .###.
       #...#
       #...#
       #...#
       #####
       #...#
       #...#
       .....
    one byte sent, 40 dots lit
    custom shapes: 8 (CGRAM)
  8. The Uno draws the letter

    The OLED has no font ROM. The library keeps a copy of the screen in the Uno's SRAM: 128 × 64 ÷ 8 = 1,024 bytes, half of its 2,048. print('A') sets the dots of A in that copy.

    line 4   oled.print('A');
    buffer   128 x 64 dots / 8 = 1,024 bytes of SRAM
             the Uno has 2,048: 50 % gone
    font     A = 0x7C 0x12 0x11 0x12 0x7C (in the library)
    dot x,y  -> byte x + (y / 8) * 128, bit y % 8
  9. Two wires: I2C

    display() sends the buffer over I2C: SDA carries bits, SCL the clock. Each message starts with the address 0x3C, then a control byte: 0x00 means commands follow, 0x40 means dot data follows.

    line 5   oled.display();
    I2C      SDA = data (Uno A4), SCL = clock (Uno A5)
    address  0x3C  (0111100)
    control  0x00 = commands follow
             0x40 = data for the dot memory follows
    then     1,024 data bytes, a few at a time
  10. One byte, eight dots

    The SSD1306 memory is 8 pages of 128 columns. Each data byte fills one column of 8 dots in a page, bit 0 at the top. A is five bytes: 7C 12 11 12 7C.

    memory   8 pages x 128 columns x 8 dots
    a byte   one column of 8 dots, D0 at the top
    col 0    0x7C = 0111 1100
    col 1    0x12 = 0001 0010
    col 2    0x11 = 0001 0001
    col 3    0x12 = 0001 0010
    col 4    0x7C = 0111 1100
    the column pointer moves on after every byte
  11. Which one to pick

    Character LCD: one byte per letter and almost no RAM, but only fixed cells and its built-in font. OLED: any shape, but 1,024 bytes of RAM for the buffer and 1,024 data bytes (plus addressing) on the bus for every full refresh.

    LCD   A = 1 byte (2 halves of 4 bits)
          no buffer needed in the Uno
          letters in fixed cells, built-in font
    OLED  A = 5 bytes; full screen = 1,024 bytes
          1,024 of 2,048 bytes of SRAM for the buffer
          any shape, any place

Quiz Unlocks after the last step.