Zonnestroompanelen in Nederland

duurzaamheid achter de meter

(18) Connecting a Wemos D1 mini and a 128*64 LCD

by Floris Wouterlood – June 3, 2020

Introduction

During the design and subsequent development of a test bench platform around an ESP8266 family microprocessor board and a graphic 128×64 LCD display the first hurdle was to establish proper connectivity between the two main components. The present communication deals exclusively with the wiring and basic software sketch necessary to get the LCD working with the microcontroller board. The design and construction of the test bench is explained in a separate project. I am interested in these particular LCDs because they can perform much more than presenting text and numbers. Their controller (ST7920) allows basic graphical functions such as lines, circles, rectangles and different fonts, while individual pixels can be switched on/off and even the possibility is provided to display bitmaps. They are also big and robust. The Wemos D1 mini is favored because this ESP8266-microcontroller board has a very small footprint and it has wireless communication functionality.


Figure 1: A Wemos D1 mini microprocessor board wired via its designated SPI pins with a graphical, ST7920 controlled 128*64 LCD breakout.

Electronic components

  • Wemos D1 mini ESP8266 microcontroller board
  • 128*64 pixel LCD with ST7920 controller 20-pin breakout board
  • 2x breadboard
  • jumper wires

Connections – SPI requirements

The 128*64 LCD breakout board is equipped with an intimidating 20 pins. Several of these are necessary to power the device and its backlight while others are involved in technology such as parallel and serial communication. We are using here the synchronous serial communication (SPI, serial peripheral interface) functionality between microprocessor board and LCD breakout. SPI requires in our configuration three pins: chip select, data and clock. Data is also called MOSI, that is, in four-wire configurations where there is more than one peripheral device (master-and-slaves configuration).
The pins on the LCD breakout board dedicated to SPI are pins 4, 5 and 6, marked RS, RW and E respectively. These pins need to be connected with the master device which presently is a Wemos D1 mini. The latter device follows the ESP8266 family convention of pin designation, however because of its small footprint the number of available pins is reduced.
The Wemos D1 mini has hardware SPI pins. Pin D5 on the D1 mini supports clock, pin D7 supports data and pin D8 is the chip select pin (CS). Figure 1 shows the wiring between both components. I have added the labeling printed on the devices next to their particular pin.

Note 1: Pins 1, 15 and 20 of the LCD breakout board should be connected to GND. Pin 15 is labeled PSB. If this pin is set LOW (that is, connected to GND), SPI functionality on the ST7920 is enabled. If this pin is not connected or set HIGH, then SPI is disabled and, conversely, parallel communication is supported.

Note 2: Pin 2 on the LCD breakout board needs 5V to power the ST7920 controller chip while pin 19 needs 3.3V for the backlight led. If both pins receive 3.3V the display will light up but won’t display anything.

LCD breakout board pin number table

ESP8266 pins and pin definition in an Arduino sketch

There is continuous confusion when it comes to addressing ESP8266-convention pins in Arduino sketches. While physical pins of the Wemos D1 are labeled D0 through D8, you must call them in their Arduino equivalent when you write a sketch in the Arduino IDE. For instance, if you want to set pin D0 of the Wemos to HIGH, then the appropriate instruction in the Arduino sketch should read ‘digitalWrite (16, HIGH)’. This can be very confusing for newbies and veterans alike. A Wemos-Arduino sketch pin translation, or pin equivalent scheme that I always keep within reach is shown in Figure 2. This scheme holds for other ESP8266 family microcontroller boards as well.


Figure 2: Pin equivalent scheme. The pins of the Wemos D1 need to be called in the sketch according to their Arduino ‘disguises’. D0 is pin 16 in the Arduino sketch, D5 is Arduino pin 14, and so forth.

Sketch

The sketch discussed here is called ‘LCD_128x64_wemos_dino_demo.ino’. In order to compile it for this specific LCD you will need the library <U8g2lib> created by Oliver Kraus. This library is available at github.com. The ‘constructor’ for this LCD is as follows:

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R2, 14, 13, 15, U8X8_PIN_NONE);


Figure 3: 128*64 ST7920 LCD wired to a Wemos D1 mini. The sketch is the demo ‘Dino’ sketch that uses the <U8g2lib> library.

The sketch uses a c-array prepared from a monochrome, 128 pixels wide and 64 pixels high ‘dino’ image. Because the Wemos has plenty of dynamic memory compared with an Arduino this c-array can reside in dynamic memory. In void setup this array is called via the instruction:

u8g2.drawBitmap (0, 0, 14, 64, dino_demo);

where 0,0 is the position on the LCD where the first pixel of the image is positioned. The ‘14’ stands for the number of bytes in horizontal direction (multiple of 8; because there are 112 pixels, the number of bytes is 112/8 = 14). The ‘64’ is the number of pixels in vertical direction. ‘dino_demo’ is the name of the c-array (a const unsigned char).
The letters ‘D’, ‘I’, ‘N’ and ‘O’ are placed vertically to the right of the bitmap representation.
Because the call for the c-array and the letters are instructions stored in void setup, void loop remains empty and the bitmap is written to screen once (and for all).

Sketch to download

LCD_128x64_wemos_dino_demo.ino (unzip and open in Arduino IDE)