Utilisation de l’écran ST7735B avec l’environemnt arduino

<aside> ⚠️ Composant nécessaires :

Schémas de connection avec un arduino Mega

Schémas Fritzing pour connecter un arduino Mega à un ST7735B - 128x160 TFT LCD

Schémas Fritzing pour connecter un arduino Mega à un ST7735B - 128x160 TFT LCD

// include TFT and SPI libraries
#include <TFT.h>  
#include <SPI.h>

// pin definition for Arduino MEGA
#define cs   53
#define dc   9
#define rst  8

// pin definition for Arduino UNO See : <https://randomnerdtutorials.com/guide-to-1-8-tft-display-with-arduino/>
//#define cs   10
//#define dc   9
//#define rst  8

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

void setup() {
  Serial.begin(9600);

  //initialize the library
  TFTscreen.begin();

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
  //set the text size
  TFTscreen.setTextSize(2);
}

void loop() {
  //generate a random color
  int redRandom = random(0, 255);
  int greenRandom = random (0, 255);
  int blueRandom = random (0, 255);
  
  // set a random font color
  TFTscreen.stroke(redRandom, greenRandom, blueRandom);
  
  // print Hello, World! in the middle of the screen
  TFTscreen.text("Hello, World!", 6, 20);
  
  // wait 200 miliseconds until change to next color
  delay(200);
}

Utilisation de deux écrans

Schémat 2 écran SPI_bb.svg

Code

// include TFT and SPI libraries
#include <TFT.h>
#include <SPI.h>

// pin definition for Arduino MEGA
#define cs 53
#define dc 9
#define rst 8

#define cs1 43
#define dc1 42
#define rst1 41

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

TFT TFTscreen1 = TFT(cs1, dc1, rst1);

// <https://forum.arduino.cc/t/display-a-variable-in-tft-screen-without-blink/259686>
void setup() {
  Serial.begin(9600);
  Serial.print(millis());
  Serial.println(" Serial.begin");

  //initialize the library
  TFTscreen.begin();
  Serial.print(millis());
  Serial.println(" TFTscreen.begin();");
  TFTscreen1.begin();
  Serial.print(millis());
  Serial.println(" TFTscreen1.begin();");

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
  TFTscreen1.background(0, 0, 0);
  Serial.print(millis());
  Serial.println(" TFTscreen.background(0, 0, 0);");
  //set the text size
  TFTscreen.setTextSize(2);
  TFTscreen1.setTextSize(2);
  Serial.print(millis());
  Serial.println(" TFTscreen1.setTextSize(2);");

  TFTscreen.stroke(255, 255, 255);
  TFTscreen1.stroke(255, 255, 255);

  TFTscreen.text("Score J1", 6, 20);
  TFTscreen1.text("Score J2", 6, 20);
}

void loop() {
  delay(200);
  updateScreen(random(100, 200), random(1, 8));
}

void updateScreen(int j1, int j2) {
  char scorej1[4];
  char scorej2[4];

  TFTscreen.stroke(0, 0, 0);
  TFTscreen1.stroke(0, 0, 0);
  TFTscreen.text(scorej1, 6, 70);
  TFTscreen1.text(scorej2, 6, 70);

  TFTscreen.stroke(255, 255, 255);
  TFTscreen1.stroke(255, 255, 255);

  String displayScorej1 = String(j1);
  String displayScorej2 = String(j2);

  displayScorej1.toCharArray(scorej1, 4);
  displayScorej2.toCharArray(scorej2, 4);
  TFTscreen.text(scorej1, 6, 70);
  TFTscreen1.text(scorej2, 6, 70);
  //prevScorej1 = scorej1;
  //prevScorej2 = scorej2;
}

Composant pour Fritzing

TFT 1.8 inch 160x128 w SD.fzpz