Zonnestroompanelen in Nederland

duurzaamheid achter de meter

(14) Connecting a 240×240 TFT display with ST7789 controller with a NodeMCU ESP8266 or an Arduino Nano

by Floris Wouterlood – December 24, 2019

Summary
Newcomers in the Arduino realm consist of small form factor, high dot pitch, full color LCD TFT displays. Compared with monochrome LCDs and with small OLED displays one advantage of these full color TFT displays is that their color palette is enormous: 65,536 available colors (16 bits per pixel, RGB565). Images on these TFT displays look amazingly sharp. The displays operate under the SPI protocol with an on board ST77nn controller chip. They require four pins of the microcontroller board. Libraries are available in the Arduino community for this category of controller.
As these new TFT displays operate at 3.3V they seem to be designed primarily as companions for 3.3V microcontroller boards in IOT environments. Hence we connect here first a 1.3 inch diagonal 240×240 pixel color TFT display with ST7789 controller with a NodeMCU ESP8266 microcontroller board and we run a demo sketch, ‘radar’. Next we discuss how the display can be connected best with an Arduino Nano: an example of the older 5V generation of microcontrollers.

Introduction
The Internet of Things (IOT) revolution is based on small form factor microcontroller boards with wifi on board that connect to the world via some local area network. As these devices do their assigned jobs silently, some means of direct monitoring of what they are doing, other than a distant smartphone or tablet, can be desirable. This concern fuels the demand for small affordable displays. Manufacturers of small electronic displays have responded with exciting products. In earlier papers I discuss monochrome OLED 128×32* and 128×64** displays and in another paper a 7-segment numerical LED display*** that is wired in a highly successful IOT temperature monitoring project to a NodeMCU ESP8266 microcontroller board****. Displays with 128×32 or 128×64 pixels are often sufficient to show essential information. More luxurious are full color displays with a small form factor together with more addressable pixels. These can show more data and in different colors, anyway in a less Spartan way than 7-segment numerical displays or small OLED displays do. The traditional ‘luxury’ solution would be a relatively large TFT display mounted on a shield or breakout board. Because connecting such a device with a 3.3V IOT device can be considered a ‘tour de force’, the demand for 3.3V powered, small form factor, full color, high dot pitch displays seems justified. In addition it is desirable that a minimum number of pins on the IOT device be needed for display purposes.
Recently I acquired a 1.3 inch screen diagonal full color TFT 240×240 pixel display controlled by a ST7789 chip and addressable via the SPI protocol. Because these displays are advertised as typical 3.3V devices my intuitive choice of testing was to connect the display with a NodeMCU ESP8266 microcontroller board before attempting to connect it with a classical Arduino.

Graphical capabilities of a 1.3 inch 240×240 pixel TFT display
The <Arduino_ST7789.h> library supports graphical functions such as drawing lines, rectangles, rectangles with rounded corners, fills, triangles and circles. One library is necessary for communication: <SPI.h>.

Necessary parts:
1 x 1.4 inch 240×240 ST7789 TFT display
jumper wires
4 x 220 Ω resistor (recommended Nano setup)
microcontroller boards:
setup A: 1x Lolin NodeMCU ESP8266
setup B: 1x Arduino Nano

Figure 1. Setup A: wiring diagram including a Lolin NodeMCU and a ST7789 driven 240×240 pixel TFT display. The NodeMCU is running the ‘radar’ demo.

Setup A: 240×240 inch TFT display connected to a Lolin NodeMCU ESP8266
A wiring diagram for setup A is shown in figure 1. The wiring is straightforward: the VCC pin of the display is connected with one of the 3V pins on the NodeMCU. GND is connected to GND. Of the remaining pins only four are necessary. The pins marked SCL and SDA (on other SPI displays sometimes labeled as SCLK and MOSI, respectively) are connected to pins D5 and D7 of the NodeMCU, respectively. Pin ‘RES’ on the TFT display is connected to pin D0 of the NodeMCU, and pin ‘DC’ of the display goes to pin D1 of the NodeMCU. Pin BLK of the TFT is not connected.

Most simple sketch for the NodeMCU
A sketch with only the bare essentials to print something on screen is the following. In addition to the <SPI.h> library this sketch needs <Arduino_ST7789.h> for correct compilation.

// ST_7789_240x240TFT_ESP8266_most simple
// Floris Wouterlood 21 December 2019
// public domain
// sketch for NodeMCU ESP8266
// displays HELLO WORLD on a 1.3 inch 3.3V TFT_display with ST7789 controller
// 240 x 240 pixels in this confguration

#include <Arduino_ST7789.h> // hardware-specific library for ST7789
#include <SPI.h>

#define TFT_DC D1 // pin of your choice
#define TFT_RST D0 // pin of your choice
#define TFT_MOSI D7 // fixed pin
#define TFT_SCLK D5 // fixed pin

Arduino_ST7789 tft = Arduino_ST7789(TFT_DC, TFT_RST);

void setup(void) {

Serial.begin (9600);
Serial.println (); // cut the gibberish first line Serial Monitor
Serial.println ();
Serial.print (“Hello World”); // test Serial Monitor

tft.init (240, 240); // initialize a ST7789 chip, 240×240 pixels
tft.fillScreen (BLACK);
tft.setCursor (20, 50);
tft.setTextColor (YELLOW);
tft.setTextSize (3);
tft.println (“HELLO WORLD”); // initialization message on the TFT display
tft.println (“………….”);
}

void loop (){

}

Graphical sketch
To demonstrate the graphical performance of this display (the graphical engine performs quite impressive) I have added the demo sketch ‘ST7789_240x240TFT_ESP8266_radar.ino’. This sketch creates on the display an old fashioned circular radar scope with a continuously sweeping beam (see display in figure 1). Functions included in ‘radar’ are ‘tft.drawRect’, ‘tft.drawCircle’, ‘tft.fillCircle’ and ‘tft.drawLine’. The x and y coordinates of the scale segments embellishing the edge of the scope are created with a cosinus function. Note that just before the end of the loop there is a call to the ‘yield ( )’ function. Yield is a specific instruction call for ESP8266 based microcontroller boards because it resets an internal hardware timer. If ‘yield ( )’ is not included the sweeping beam will freeze at some angle because the use of the ‘delay ( )’ instruction blocks that internal timer.

Setup B: 240×240 inch TFT display connected to an Arduino Nano
A wiring diagram for setup B is shown in figure 2. Also with an Arduino the wiring is straightforward: the VCC pin of the display is connected with the 3.3V pin on the Nano. GND is connected to GND. Also in this configuration only four of the remaining pins on the TFT display board are connected. Pins marked ‘SCL’ and ‘SDA’ must be connected to pins D13 and D11 of the Nano. These Nano pins are hardware SPI pins and do not have to be defined in software in the sketch. Pin ‘RES’ of the TFT display is connected to D8 of the Nano, and pin ‘DC’ on the display goes to pin D9 of the Nano. Pin BLK of the TFT is not connected.

The most simple sketch for the Nano
Compared with the ‘most simple’ sketch for the NodeMCU the only difference is the definitions for the pin assignments. Two definitions (MOSI and SCLK) are unnecessary because in the Nano design these pins are hardware mapped. Pins 8 and 9 are not set in concrete; one can use other pins provided that the #defines in the sketch for these pins are changed in concert. This supports flexibility in all kinds of projects.

// ST_7789_240x240TFT_nano_most simple
// Floris Wouterlood 21 December 2019
// public domain
// sketch for Arduino Nano
// displays HELLO WORLD on a 1.3 inch 3.3V TFT_display with ST7789 controller
// 240 x 240 pixels in this confguration

#include <Arduino_ST7789.h> // hardware-specific library for ST7789
#include <SPI.h>

#define TFT_DC 9 // pin of your choice
#define TFT_RST 8 // pin of your choice

Arduino_ST7789 tft = Arduino_ST7789(TFT_DC, TFT_RST);

void setup(void) {

Serial.begin (9600);
Serial.println (); // cut the gibberish first line Serial Monitor
Serial.println ();
Serial.print (“Hello World”); // test Serial Monitor

tft.init (240, 240); // initialize a ST7789 chip, 240×240 pixels
tft.fillScreen (BLACK);
tft.setCursor (20, 50);
tft.setTextColor (YELLOW);
tft.setTextSize (3);
tft.println (“HELLO WORLD”); // initialization message on the TFT display
tft.println (“………….”);
}

void loop (){

}

Figure 2. Setup B: wiring diagram including an Arduino Nano and a ST7789 driven 240×240 pixel TFT display. The Nano is running the ‘radar’ demo.

‘Radar’ sketch for the Nano
With a few modifications the sketch ‘ST_7789_240x240TFT_ESP8266_radar.ino’ works with a Nano. The wiring diagram is shown in figure 2. Because in the Arduino Uno and Nano pins 11 and 13 are hardware SPI pins it is not necessary to declare these pins. The following is sufficient:

#include <Arduino_ST7789.h> // hardware-specific library for ST7789
#include <SPI.h>

#define TFT_DC 9 // pin of your choice
#define TFT_RST 8 // pin of your choice

Arduino_ST7789 tft = Arduino_ST7789 (TFT_DC, TFT_RST);

In the Nano sketch there is no call to function ‘yield ( )’ at the end of the loop. All else is similar to the sketch for the NodeMCU.

Discussion
The 1.3 inch diagonal 240×240 pixel TFT display essentially is a unit operating at 3.3V. With a NodeMCU there is no voltage mismatch between devices because the ESP8266 family, including the Lolin NodeMCU, is designed as 3.3V devices. The Arduino is an older design that is primarily a 5V device wherein 3.3V is supplied from a special pin. The signals delivered by the on board Atmel processor chip however are 5V DC and here a voltage mismatch problem with external devices may occur. For instance, 5V may damage dedicated 3.3V display units. As can be seen in figure 2, the Nano sketch runs fine with the TFT display with only VCC connected to the 3.3V pin of the Nano. Should one position voltage reducing resistors (e.g., 220 Ω) in the wires leading to pins SCL, SDA,RES and DC? For this purpose I placed, in one wire after another, a 220 Ω resistor in these connections (figure 3) and tested the ‘radar’ sketch. The results are shown in the summary below:

Figure 3. Setup B with voltage-reducing 220 Ω resistors applied in SCL, SDA, RES and DC. The Nano works with 5V logics while the TFT display is designed for 3V platforms.

• 220 Ω resistor in wire between Nano D9 and display pin DC: no change, radar sketch runs normally.
• 220 Ω resistor in wire Nano D9 – display DC and a 220 Ω resistor in wire Nano D8 – display RES: no change, radar sketch runs normally.
• 220 Ω resistor in wire Nano D9 – display DC, a 220 Ω resistor in wire Nano D8 – display RES and a 220 Ω resistor in wire Nano D11 and display SDA: no change, radar sketch runs normally.
• Finally: 220 Ω resistor in each of the wires to the TFT’s SCLK, SDA, RST and DC pins: no change, radar sketch runs normally.

While the TFT display breakout board at my disposal performed the ‘radar’ sketch (and additional test sketches) well with the Nano, application of ‘safety’ 220 Ω resistors can be considered a potentially life-extending precautionary measure. That’s why these resistors are indicated in figure 3’s wiring scheme.

References

*Floris Wouterlood – How to wire and run a 128×32 OLED display with SSD1306 driver with an Arduino. Thesolaruniverse, October 28, 2019.

** Floris Wouterlood – Connecting a 128×64 OLED display with SSD1306 driver to an Arduino with a SPI interface. Thesolaruniverse, August 20, 2019.

*** Floris Wouterlood – Four digit, 7-segment led display for Arduino based on the TM1637 driver. Thesolaruniverse, August 8, 2018.

*** Floris Wouterlood – Constant monitoring/logging via a NodeMCU ESP8266 and the internet of central heating water flow temperature – Thesolaruniverse, August 17, 2018.

Sketches
The zip file ‘240x240_ST7789_display_sketches.zip’ contains:
ST_7789_240x240TFT_ESP8266_most simple sketch.ino
ST_7789_240x240TFT_ESP8266_radar.ino
ST_7789_240x240TFT_nano_most simple sketch.ino
ST_7789_240x240TFT_nano_radar.ino