Translate

Monday, March 3, 2025

A TFT display for STM32F103

 

How I solved the functioning problems of the 1.8” display with ST7735 controller

Giovanni Carrera, 03/03/2025


I had already used, with success, this type of display with the Raspberry PI Pico board using MicroPython, now I wanted to use it with a STM32F103C8T6 mounted on the board, now known as bluepill.

The display is a cheap 1.8-inch 160×128 pixel TFT with 18-bit for 262k colors with 6-bit encoding per color. The control chip used for this display is a Sitronix ST7735S and the interface is SPI with a few more control pins.

To develop with the Arduino IDE with STM32 MCUs you need to download the STM32duino package, an operation that I won't describe here because you can easily find it online.

As libraries for the display I initially used the Adafruit_ST7735.h library together with the Adafruit_GFX.h, I had already used these with the ESP32. I preferred to use STMs because they consume less and the boards have good LDO regulators and can also be powered by a lithium ion cell.

As soon as I wrote the program with version 2.3.3 of Arduino IDE, numerous warnings appeared due to the fact that the new version prefers to indicate boolean variables with the simple ‘bool’, but these do not prevent compilation. The program, once transferred to the board via serial, did not run. The display was on but appeared white. I wasted a lot of time trying to understand the cause: was it the fault of the SPI bus initialization or the pins used?

In the end I tried to use the Arduino TFT library and I started to see something good on the screen, so the problem was not the hardware but the software. I do not know why but the Adafruit libraries do not work with ST processors.

 

The schematic of my system

Figure 1 shows the circuit I used. To power the display, 3.3V and not 5V are needed and, since the current is not negligible, I preferred to use an external regulator with a very low drop-out, discarding the classic LM1117 that would require a power supply of at least 4.5V. I preferred not to load the 3.3V output of the bluepill.

Also to reduce consumption, I control the backlight LED with a transistor so as to turn it on and off only when needed. I put both the display and the SD slot on the same SPI bus.

If you want a higher brightness (but also consumption) of the display you can short-circuit the resistor R1.

Figure 1

The example program

In my case I use the display only for text writing, but the library also has graphic functions being derived from those of Adafruit, but a little more basic. The colors are expressed with BGR triplets and not RGB and on the net you can find programs to obtain the desired colors, I have obtained some.

With small characters (size 1) you can print lines of 26 characters with a line spacing of 10 pixels, which are reduced to 13 with size 2.

 

/*   STM32 1.8” TFT example

 G. Carrera, 02/03/2025 */

 

#include <TFT.h>  // Arduino LCD library

#include <SPI.h>

// pin definition for the STM32F103

#define cs   PA2

#define dc   PB1

#define rst  PA1

#define TFT_BL PA3

TFT TFTscreen = TFT(cs, dc, rst); // create an instance of the library

 

void setup() {

  pinMode(TFT_BL, OUTPUT);

  digitalWrite(TFT_BL, LOW); // TFT backlight ON

  // Put this line at the beginning of every sketch that uses the GLCD:

  TFTscreen.begin();

  TFTscreen.background(0, 0, 0); // clear the screen with a black background

  TFTscreen.stroke(255, 255, 255);// set the font color to white

  TFTscreen.setTextSize(1);

  TFTscreen.text("01234567890123456789012345\n ", 0, 0);

  TFTscreen.stroke(0, 0, 255);// red color

  TFTscreen.text("01234567890123456789012345\n ", 0, 10);

  TFTscreen.stroke(255, 0, 0);// blue color (BGR)

  TFTscreen.text("01234567890123456789012345\n ", 0, 20);

  TFTscreen.stroke(255, 255, 0);// cyan color (BGR)

  TFTscreen.text("01234567890123456789012345\n ", 0, 30);

  TFTscreen.stroke(0, 255, 255);// yellow color (BGR)

  TFTscreen.text("01234567890123456789012345\n ", 0, 40);

  TFTscreen.stroke(0, 255, 0);// green color

  TFTscreen.setTextSize(2);// set the font size

  TFTscreen.text("0123456789012\n ", 0, 55);

}

 

void loop() {

}

 




No comments:

Post a Comment