ST7920 12864 LCD on three wires, on an Arduino Uno
The blue 128x64 ST7920 LCD in serial mode on an Uno, driven by U8g2’s software SPI constructor: a box, big text and a bar that grows each second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
What you'll learn
- Displays
- Using the 12864 LCD
- Reading and changing a sketch
- Difficulty: Intermediate
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
The blue 128x64 ST7920 LCD in serial mode on an Uno, driven by U8g2’s software SPI constructor: a box, big text and a bar that grows each second.
Arduino Uno R3 runs the sketch below. The circuit around it: 12864 LCD, ST7920. delay waits 1000 milliseconds. Serial.begin opens the serial port at 9600 baud.
Parts list
4 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| 12864 LCD, ST7920 pin E | Arduino Uno R3 pin 13 |
| 12864 LCD, ST7920 pin RW | Arduino Uno R3 pin 11 |
| 12864 LCD, ST7920 pin RS | Arduino Uno R3 pin 10 |
| 12864 LCD, ST7920 pin GND | Ground |
| 12864 LCD, ST7920 pin VCC | 5 V |
| 12864 LCD, ST7920 pin PSB | Ground |
| 12864 LCD, ST7920 pin BLK | Ground |
| 12864 LCD, ST7920 pin BLA | 5 V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
/*
* The blue 12864 LCD (an ST7920) on three wires, drawn with U8g2.
*
* PSB is tied to GND, which puts the ST7920 in its serial mode: RS becomes
* chip select (active high), R/W the data line and E the clock. U8g2's
* software SPI constructor sends every byte as a sync byte and two nibbles.
* The panel is 128x64, but the chip sees it as 256x32: the bottom half of the
* screen is the right half of its graphic RAM, which U8g2 takes care of.
*
* Wiring: E to 13, R/W to 11, RS to 10, PSB to GND, VCC and BLA to 5V,
* GND and BLK to GND.
*/
#include <U8g2lib.h>
// clock (E), data (R/W), chip select (RS), no reset pin
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, 13, 11, 10, U8X8_PIN_NONE);
unsigned long seconds = 0;
void setup() {
Serial.begin(9600);
u8g2.begin();
Serial.println("ST7920 12864 LCD ready, serial mode");
}
void loop() {
u8g2.clearBuffer();
u8g2.drawFrame(0, 0, 128, 64); // a box round the edge
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(10, 22, "12864 LCD");
u8g2.drawBox(8, 30, 8 + (seconds % 12) * 9, 10); // a bar that grows each second
u8g2.setFont(u8g2_font_6x10_tr);
char line[24];
snprintf(line, sizeof line, "ST7920, up %lu s", seconds);
u8g2.drawStr(8, 56, line);
u8g2.sendBuffer(); // the whole frame, a row at a time
Serial.print("frame ");
Serial.println(seconds);
seconds++;
delay(1000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(9600);
u8g2.begin();
Serial.println("ST7920 12864 LCD ready, serial mode");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
u8g2.clearBuffer();
u8g2.drawFrame(0, 0, 128, 64); // a box round the edge
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(10, 22, "12864 LCD");
u8g2.drawBox(8, 30, 8 + (seconds % 12) * 9, 10); // a bar that grows each second
u8g2.setFont(u8g2_font_6x10_tr);
char line[24];
snprintf(line, sizeof line, "ST7920, up %lu s", seconds);
u8g2.drawStr(8, 56, line);
u8g2.sendBuffer(); // the whole frame, a row at a time
Serial.print("frame ");
Serial.println(seconds);
seconds++;
delay(1000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.