Character LCD and OLED displays
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.
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 bothRead the lesson step by step
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 bothA 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 screenSix 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 nowA 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 0x40A 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-D7Then 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 safeThe 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)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 % 8Two 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 timeOne 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 byteWhich 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
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01On an HD44780 LCD in 4-bit mode, what does the RS pin tell the controller?
RS selects the register: 0 is the instruction register (commands such as set address), 1 is the data register (characters). The E pulse is what tells the controller to read the data pins.
02lcd.print('A') sends 0x41 in 4-bit mode. What appears on D7 to D4, in order?
0x41 is 0100 0001. The HD44780 takes the high four bits first, then the low four, each latched by its own E pulse. 1000 then 0000 is the setCursor(0, 0) command, 0x80.
03Why can the Uno show 'A' on the character LCD by sending just one byte?
The HD44780 stores character codes in its display memory and looks each one up in its built-in character ROM (208 shapes of 5 × 8 dots), plus 8 shapes you can define yourself.
04An SSD1306 OLED receives the data byte 0x7C (0111 1100) at column 0 of page 0. Which dots light?
Each data byte fills one column of 8 dots in the current page, with bit 0 at the top. 0111 1100 has bits 2 to 6 set, so rows 2 to 6 of that column light: the left leg of the A.
05The Adafruit library keeps a 128 × 64 frame buffer in the Uno's SRAM. How much of the Uno's 2,048 bytes does it take?
128 × 64 = 8,192 dots at 1 bit each = 1,024 bytes, half of the ATmega328P's 2,048 bytes of SRAM. That is why a small OLED can be tight on an Uno, while a character LCD needs no buffer at all.
Glossary
- OLED
- Organic light-emitting diode display: every dot gives off its own light and any shape can be drawn.
- Display controller
- A chip on the display module (HD44780, SSD1306) that holds what is shown in its own memory and keeps the screen refreshed; the microcontroller only sends it bytes.
- RS and E
- Two HD44780 pins. RS (register select) marks a byte as a command (0) or a character to display (1); a short high pulse on E (enable) tells the controller to read the data pins.
- Nibble
- Half a byte: four bits. In 4-bit mode the LCD gets each byte as two nibbles, high half first.
- Font ROM (CGROM)
- Read-only memory inside a character LCD controller that holds the dot pattern of each character code.
- SRAM
- The working memory inside a microcontroller. It loses its contents when the power goes off; the Uno's ATmega328P has 2,048 bytes.
- I2C
- A two-wire bus: SDA carries the data bits and SCL the clock. Each message starts with the address of the chip it is for.
- Frame buffer
- A copy of the whole screen in memory, one bit per dot. For a 128 × 64 OLED it is 1,024 bytes, sent in 8 pages: bands of 8 dot rows, one byte per column.
Notes and sources
- The card shows the five lines that put A on both screens. The whole sketch also creates lcd (pins 12, 11, 5, 4, 3, 2) and oled (128 x 64, I2C address 0x3C), calls lcd.begin(16, 2) and oled.begin(), clears the OLED and sets its text colour to white.
- The LCD also has an R/W pin. Tied to ground it means "write only", so the Uno cannot read the busy flag and simply waits: LiquidCrystal pauses 100 µs after each pulse, more than the 37 µs a write can take.
- The 37 µs is the HD44780 datasheet maximum at a 270 kHz internal clock; a slower clock takes longer. Many modules use compatible controllers with similar timing.
- Many 128 x 64 modules answer at 0x3C; others answer at 0x3D because their SA0 pin is tied high. The Wire library sends at most 32 bytes at a time on an Uno, so display() splits the 1,024 bytes into chunks, each starting with 0x40. On a logic analyser the first byte of each message reads 0x78, not 0x3C: the 7-bit address shifted left one place, plus a 0 bit that means write.
- The OLED in the figure shows only its top-left 16 x 16 dots (pages 0 and 1), enlarged; the real screen has 128 x 64. The A uses the Adafruit GFX built-in 5 x 7 font, so its eighth row is empty.
- Hitachi HD44780U (LCD-II) dot matrix LCD controller datasheet
- Solomon Systech SSD1306 OLED controller datasheet, rev 1.1
- Arduino docs: Liquid Crystal Displays (LCD) with Arduino (source)
- Arduino LiquidCrystal library: LiquidCrystal.cpp
- Arduino LiquidCrystal library: LiquidCrystal.h
- Adafruit SSD1306 library: Adafruit_SSD1306.cpp
- Adafruit GFX library: glcdfont.c (the built-in 5 × 7 font)
- Arduino language reference: Wire (source)
- Arduino UNO R3 product datasheet (A000066)
Quiz Unlocks after the last step.