Showing posts with label SPI ST7735. Show all posts
Showing posts with label SPI ST7735. Show all posts

Tuesday, November 16, 2021

Arduino Nano RP2040 Connect generate QR Code and display on ST7735 TFT - software SPI

This post show how to generate QR Code with Nano RP2040 Connect (Arduino Framework) using QRCode library by Richard Moore, also display on ST7735 SPI TFT, and finally adapt to WiFiNINA > AP_SimpleWebServer example to control onboard LED.

Library

Install QRCode library by Richard Moore in Arduino IDE's Libraries Manager.


And, "Adafruit ST7735 and ST7789 Library" and "Adafruit GFX Library" are needed to display with ST7735 SPI TFT.

Connection

The hardware SPI of Arduino Nano RP2040 Connect are:
- (CIPO/MISO) - D12
- (COPI/MOSI) - D11
- (SCK) - D13
- (CS/SS) - Any GPIO (except for A6/A7)


In this exercise, we have to control the onboard LED connected to D13. it's conflict with SCK. So the exercise code here use software SPI.
/************************************************
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        12
 * LED        3V3
 * **********************************************/
Exercise code

nanoRP2040_QRCode.ino
/**
 *  Arduino nano RP2040 Connect exercise to generate QR Code,
 *  with display on Serial Monitor and ST7735 SPI TFT.
 *  Modified from QRCode example.
 *  
 *  Library: QRCode by Richard  Moore
 *  https://github.com/ricmoo/qrcode/
 *  
 */
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include "qrcode.h"

/************************************************
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        12
 * LED        3V3
 * **********************************************/
// TFT display use software SPI interface.
#define TFT_MOSI 11  // Data out
#define TFT_SCLK 12  // Clock out

#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  9  // Reset line for TFT (or connect to VCC)
Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS, TFT_DC, 
                                  TFT_MOSI, TFT_SCLK, TFT_RST);

void setup() {
    delay(1000);
    Serial.begin(115200);
    delay(1000);
    Serial.println("- setup() started -");
    tft_ST7735.initR(INITR_BLACKTAB);
    tft_ST7735.setRotation(2);
    
    // tft display RGB to make sure it's work properly
    tft_ST7735.fillScreen(ST7735_BLACK);
    delay(300);
    tft_ST7735.fillScreen(ST7735_RED);
    delay(300);
    tft_ST7735.fillScreen(ST7735_GREEN);
    delay(300);
    tft_ST7735.fillScreen(ST7735_BLUE);
    delay(300);
    tft_ST7735.fillScreen(ST7735_WHITE);
    delay(300);

    // Start time
    uint32_t dt = millis();
  
    // Create the QR code
    QRCode qrcode;

    const char *data = "http://arduino-er.blogspot.com/";
    const uint8_t ecc = 0;  //lowest level of error correction
    const uint8_t version = 3;

    uint8_t qrcodeData[qrcode_getBufferSize(version)];
    qrcode_initText(&qrcode, 
                    qrcodeData, 
                    version, ecc, 
                    data);
  
    // Delta time
    dt = millis() - dt;
    Serial.print("QR Code Generation Time: ");
    Serial.print(dt);
    Serial.print("\n\n");
    
    Serial.println(data);
    Serial.print("qrcode.version: ");
    Serial.println(qrcode.version);
    Serial.print("qrcode.ecc: ");
    Serial.println(qrcode.ecc);
    Serial.print("qrcode.size: ");
    Serial.println(qrcode.size);
    Serial.print("qrcode.mode: ");
    Serial.println(qrcode.mode);
    Serial.print("qrcode.mask: ");
    Serial.println(qrcode.mask);
    Serial.println();

    const int xy_scale = 3;
    const int x_offset = (tft_ST7735.width() - xy_scale*qrcode.size)/2;
    const int y_offset = (tft_ST7735.height() - xy_scale*qrcode.size)/2;
    
    
    // Top quiet zone
    Serial.print("\n\n\n\n");
    for (uint8_t y = 0; y < qrcode.size; y++) {

        // Left quiet zone
        Serial.print("        ");

        // Each horizontal module
        for (uint8_t x = 0; x < qrcode.size; x++) {

            // Print each module (UTF-8 \u2588 is a solid block)
            bool mod = qrcode_getModule(&qrcode, x, y);
            //Serial.print(mod ? "\u2588\u2588": "  ");
            if(mod){
              Serial.print("██"); //same as "\u2588\u2588"
                                  //direct paste "██" copied from Serial Monitor
              int px = x_offset + (x * xy_scale);
              int py = y_offset + (y * xy_scale);
              tft_ST7735.fillRect(px, py, xy_scale, xy_scale, ST7735_BLACK);
              
            }else{
              Serial.print("  ");
            }
        }

        Serial.print("\n");
    }

    // Bottom quiet zone
    Serial.print("\n\n\n\n");
}

void loop() {

}


nanoRP2040_QRCode_web.ino
/**
 *  Arduino nano RP2040 Connect exercise to generate QR Code,
 *  WiFiWebServer with QRCode on ST7735 SPI TFT
 *  
 *  Library: QRCode by Richard  Moore
 *  https://github.com/ricmoo/qrcode/
 *  
 */
#include <WiFiNINA.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include "qrcode.h"

/************************************************
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        12
 * LED        3V3
 * **********************************************/
// TFT display use software SPI interface.
//
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.
//
// In this exercise, we are going to implement a web server to
// turn ON/OFF on board LED, which is connected to D13. It's
// conflict with hardware SPI SCK.
// So we cannot use hardware SPI to controll ST7735.
#define TFT_MOSI 11  // Data out
#define TFT_SCLK 12  // Clock out

#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  9  // Reset line for TFT (or connect to VCC)
Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS, TFT_DC,
                                  TFT_MOSI, TFT_SCLK, TFT_RST);

char ssid[] = "ssid";       // your network SSID (name)
char pass[] = "password";   // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
    
    delay(1000);
    Serial.begin(115200);
    delay(1000);
    Serial.println("- setup() started -");
    Serial.print("LED_BUILTIN: ");
    Serial.println(LED_BUILTIN);
    pinMode(LED_BUILTIN, OUTPUT);      // set the LED pin mode
    tft_ST7735.initR(INITR_BLACKTAB);
    tft_ST7735.setRotation(2);
    tft_ST7735.setTextWrap(true);
    tft_ST7735.setTextColor(ST77XX_BLACK);
    
    // tft display RGB to make sure it's work properly
    digitalWrite(LED_BUILTIN, HIGH);
    tft_ST7735.fillScreen(ST7735_BLACK);
    delay(300);
    digitalWrite(LED_BUILTIN, LOW);
    tft_ST7735.fillScreen(ST7735_RED);
    delay(300);
    digitalWrite(LED_BUILTIN, HIGH);
    tft_ST7735.fillScreen(ST7735_GREEN);
    delay(300);
    digitalWrite(LED_BUILTIN, LOW);
    tft_ST7735.fillScreen(ST7735_BLUE);
    delay(300);
    digitalWrite(LED_BUILTIN, HIGH);
    tft_ST7735.fillScreen(ST7735_WHITE);
    delay(300);

    // check for the WiFi module:
    if (WiFi.status() == WL_NO_MODULE) {
      Serial.println("Communication with WiFi module failed!");
      tft_ST7735.setCursor(0, 0);
      tft_ST7735.print("Communication with WiFi module failed!");
      // don't continue
      while (true);
    }

    Serial.print("WiFi.firmwareVersion(): ");
    Serial.println(WiFi.firmwareVersion());

    // attempt to connect to WiFi network:
    while (status != WL_CONNECTED) {
      Serial.print("Attempting to connect to SSID: ");
      Serial.println(ssid);
      // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
      status = WiFi.begin(ssid, pass);
      
      // wait 10 seconds for connection:
      delay(10000);
    }
    server.begin();
    // you're connected now, so print out the status:
    printWifiStatus();

    // Start time
    uint32_t dt = millis();
  
    // Create the QR code
    QRCode qrcode;

    char myIpQrData[30] ="https://";
    prepareIpQrData(myIpQrData);
    
    const uint8_t ecc = 0;  //lowest level of error correction
    const uint8_t version = 3;

    uint8_t qrcodeData[qrcode_getBufferSize(version)];
    qrcode_initText(&qrcode, 
                    qrcodeData, 
                    version, ecc, 
                    myIpQrData);
  
    // Delta time
    dt = millis() - dt;
    Serial.print("QR Code Generation Time: ");
    Serial.print(dt);
    Serial.print("\n\n");
    
    Serial.println(myIpQrData);
    Serial.print("qrcode.version: ");
    Serial.println(qrcode.version);
    Serial.print("qrcode.ecc: ");
    Serial.println(qrcode.ecc);
    Serial.print("qrcode.size: ");
    Serial.println(qrcode.size);
    Serial.print("qrcode.mode: ");
    Serial.println(qrcode.mode);
    Serial.print("qrcode.mask: ");
    Serial.println(qrcode.mask);
    Serial.println();

    const int xy_scale = 3;
    const int x_offset = (tft_ST7735.width() - xy_scale*qrcode.size)/2;
    const int y_offset = (tft_ST7735.height() - xy_scale*qrcode.size)/2;
    
    // Top quiet zone
    Serial.print("\n\n\n\n");
    for (uint8_t y = 0; y < qrcode.size; y++) {

        // Left quiet zone
        Serial.print("        ");

        // Each horizontal module
        for (uint8_t x = 0; x < qrcode.size; x++) {

            // Print each module (UTF-8 \u2588 is a solid block)
            bool mod = qrcode_getModule(&qrcode, x, y);
            //Serial.print(mod ? "\u2588\u2588": "  ");
            if(mod){
              Serial.print("██"); //same as "\u2588\u2588"
                                  //direct paste "██" copied from Serial Monitor
              int px = x_offset + (x * xy_scale);
              int py = y_offset + (y * xy_scale);
              tft_ST7735.fillRect(px, py, xy_scale, xy_scale, ST7735_BLACK);
              
            }else{
              Serial.print("  ");
            }
        }

        Serial.print("\n");
    }

    // Bottom quiet zone
    Serial.print("\n\n\n\n");
    digitalWrite(LED_BUILTIN, LOW);
}

//prepare ip address in char *,
//to pass to qrcode_initText()
char * prepareIpQrData(char *dest){
  Serial.println("***********************");
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip[0]);
  Serial.println(ip[1]);
  Serial.println(ip[2]);
  Serial.println(ip[3]);

  Serial.println("-------------");
  char buffer0[3];
  char buffer1[3];
  char buffer2[3];
  char buffer3[3];
  itoa(ip[0], buffer0, 10);
  itoa(ip[1], buffer1, 10);
  itoa(ip[2], buffer2, 10);
  itoa(ip[3], buffer3, 10);

  char str[15] = "";
  char dot[] = ".";

  strcat(dest, buffer0);
  strcat(dest, dot);
  strcat(dest, buffer1);
  strcat(dest, dot);
  strcat(dest, buffer2);
  strcat(dest, dot);
  strcat(dest, buffer3);
  
  Serial.println(dest);
  Serial.println("***********************");
  
}

//The web page part is mainly modified 
//from WiFiNINA example > AP_SimpleWebServer
//that lets you blink an LED via the web.
void loop() {
  // compare the previous status to the current status
  if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }
  
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      delayMicroseconds(10);                // This is required for the Arduino Nano RP2040 Connect 
                                            // - otherwise it will loop so fast that SPI will 
                                            // never be served.
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            
            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          Serial.println("\n===> GET /H");
          digitalWrite(LED_BUILTIN, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          Serial.println("\n===> GET /L");
          digitalWrite(LED_BUILTIN, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Related:

Tuesday, November 9, 2021

Arduino Nano RP2040 Connect + ST7735 SPI TFT with SD Card, read and display bitmap, using hardware SPI.

Exercises run on Arduino Nano RP2040 Connect (in Arduino framework) work with 1.8" 128x160 ST7735 SPI TFT/SD Card Module, to:
- simple display something on ST7735 SPI TFT
- Display files in SD Card, and read txt file from SD Card.
- Read bmp from SD Card and display on ST7735 SPI TFT
- Try example of Adafruit_ImageReader
- Simplified version display bitmap from SD and display on TFT using Adafruit_ImageReader.

In the following exercise Hardware SPI is used for ST7735 and SD Card SPI interface. refer to last post, SPI pins in Arduino Nano RP2040 Connect is:
- MISO    - D12
- MOSI    - D11
- SCK      - D13

Connection:
 * Nano RP2040 Connect drive ST7735 SPI and SD using hardware SPI
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        13
 * LED        3V3
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * SD_CS      4
 * SD_MOSI    11
 * SD_MISO    12
 * SD_SCK     13
 ****************************************************/

Exercise code:

Please notice that  "Adafruit ST7735 and ST7789 Library" and "Adafruit GFX Library" are needed, make sure it's installed in Arduino IDE's Libraries Manager.

nanoRP2040_ST7735.ino, a simple exercise to display something on ST7735 SPI TFT.

/***************************************************
 * nano RP2040 Connect exercise
 * + ST7745/SD
 * 
 * On Arduino nanp RP2040 Connect:
 * (CIPO/MISO)  - D12
 * (COPI/MOSI)  - D11
 * (SCK)        - D13
 * (CS/SS) - Any GPIO (except for A6/A7
 * 
 * This example drive ST7735 SPI and SD using hardware SPI
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        13
 * LED        3V3
 * 
 ****************************************************/

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>

// TFT display using hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.
#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  9  // Reset line for TFT (or connect to +5V)

Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

void setup(void) {
  delay(1000);
  Serial.begin(115200);
  delay(1000);
  Serial.println("=====================");
  Serial.println("- setup() -");
  tft_ST7735.initR(INITR_BLACKTAB);
  Serial.println("tft: " 
                  + String(tft_ST7735.width()) + " : " 
                  + String(tft_ST7735.height()));

  tft_ST7735.fillScreen(ST7735_BLACK);
  tft_ST7735.setTextWrap(true);
  tft_ST7735.setTextColor(ST77XX_WHITE);
  tft_ST7735.setCursor(0, 0);
  tft_ST7735.print("Arduino nano RP2040 Connect");
  tft_ST7735.println("\n");

  //----------------------------------------
  delay(2000);

  tft_ST7735.setRotation(3);
  tft_ST7735.setCursor(0, 30);
  tft_ST7735.print("rotation: " + String(tft_ST7735.getRotation()));
  tft_ST7735.setCursor(0, 40);
  tft_ST7735.print(String(tft_ST7735.width()) + " x " + String(tft_ST7735.height()));
  
  delay(2000);

  tft_ST7735.fillScreen(ST77XX_RED);
  tft_ST7735.setCursor(50, 50);
  tft_ST7735.print("RED");
  delay(1000);
  tft_ST7735.fillScreen(ST77XX_GREEN);
  tft_ST7735.setCursor(50, 50);
  tft_ST7735.print("GREEN");
  delay(1000);
  tft_ST7735.fillScreen(ST77XX_BLUE);
  tft_ST7735.setCursor(50, 50);
  tft_ST7735.print("BLUE");
  delay(1000);

  delay(1000);
  
  //----------------------------------------

  Serial.println("\n- End of setup() -\n");
}


void loop() {

  tft_ST7735.fillScreen(ST77XX_BLUE);
  for(int offset=0; offset<tft_ST7735.height()/2; offset++){
    int col;
    if(offset%5 == 0)
      col = ST77XX_WHITE;
    else
      col = ST77XX_BLACK;
      
    tft_ST7735.drawRect(offset, offset, 
                 tft_ST7735.width()-2*offset, tft_ST7735.height()-2*offset,
                 col);
    delay(100);
  }

  delay(2000);

  tft_ST7735.fillScreen(ST77XX_BLACK);
  int cx = tft_ST7735.width()/2;
  int cy = tft_ST7735.height()/2;
  for(int r=0; r<tft_ST7735.height()/2; r=r+10){

    tft_ST7735.drawCircle(cx, cy, 
                 r,
                 ST77XX_WHITE);
    delay(200);
  }

  delay(2000);
  delay(2000);
}


nanoRP2040_ST7735_SD.ino, list files in SD card and read the text file hello.txt.
/***************************************************
 * nano RP2040 Connect exercise
 * + ST7745/SD
 * 
 * On Arduino nanp RP2040 Connect:
 * (CIPO/MISO) - D12
 * (COPI/MOSI) - D11
 * (SCK) - D13
 * (CS/SS) - Any GPIO (except for A6/A7
 * 
 * This example drive ST7735 SPI and SD using hardware SPI
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        13
 * LED        3V3
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * SD_CS      4
 * SD_MOSI    11
 * SD_MISO    12
 * SD_SCK     13
 ****************************************************/

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type
#define SD_CS    4  // Chip select line for SD card
#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  9  // Reset line for TFT (or connect to +5V)

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

File root;
File myFile;

void setup(void) {
  delay(1000);
  Serial.begin(115200);
  delay(1000);
  Serial.println("=====================");
  Serial.println("- setup() -");
  tft.initR(INITR_BLACKTAB);
  Serial.println("tft: " 
                  + String(tft.width()) + " : " 
                  + String(tft.height()));

  tft.fillScreen(ST7735_BLACK);
  tft.setTextWrap(true);
  tft.setTextColor(ST77XX_WHITE);
  tft.setCursor(0, 0);
  tft.print("Arduino nano RP2040 Connect");
  tft.println("\n");

  //----------------------------------------
  Serial.println("Initializing SD card...");
  if (!SD.begin(SD_CS)){
    Serial.println("SD.begin() failed!");
  }
  else{
    Serial.println("SD.begin() Success.");

    root = SD.open("/");
    printDirectory(root, 0);

    Serial.println("===============================");
    // open the file for reading:
    myFile = SD.open("hello.txt");
    if (myFile) {
      Serial.println("test.txt:");
      Serial.println("-------------------------------");
      // read from the file until there's nothing else in it:
      while (myFile.available()) {
      //Serial.write(myFile.read());
      byte b = myFile.read();
      Serial.write(b);
      tft.print((char)b);
      }
      // close the file:
      myFile.close();
    } else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
    }
    Serial.println("\n===============================\n");

    
  }


  //----------------------------------------

  Serial.println("\n- End of setup() -\n");
}


void loop() {

  delay(100);
}

//
// printDirectory() copy from:
// Examples > SD > listfiles
//

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}


nanoRP2040_ST7735_SD_bmp.ino, read bmp files in SD Card, test.bmp, test2.bmp and test3.bmp, and display on ST7735 SPI TFT. It can be noted in bmpDraw(), copy from examples under "Adafruit ST7735 and ST7789 Library" > shieldtest, bmpDepth must be 24. To prepare bmp for this using GIMP, refer to the above video, ~ 6:38.
/***************************************************
 * nano RP2040 Connect exercise
 * + ST7745/SD
 * 
 * On Arduino nanp RP2040 Connect:
 * (CIPO/MISO) - D12
 * (COPI/MOSI) - D11
 * (SCK) - D13
 * (CS/SS) - Any GPIO (except for A6/A7
 * 
 * This example drive ST7735 SPI and SD using hardware SPI
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        11
 * SCK        13
 * LED        3V3
 * 
 * TFT_ST7735 nano RP2040 Connect
 * ------------------------------
 * SD_CS      4
 * SD_MOSI    11
 * SD_MISO    12
 * SD_SCK     13
 ****************************************************/

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type.
#define SD_CS    4  // Chip select line for SD card
#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  9  // Reset line for TFT (or connect to +5V)

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

File root;
File myFile;

void setup(void) {
  delay(1000);
  Serial.begin(115200);
  delay(1000);
  
  Serial.println("=====================");
  Serial.println("- setup() -");
  tft.initR(INITR_BLACKTAB);
  Serial.println("tft: " 
                  + String(tft.width()) + " : " 
                  + String(tft.height()));

  tft.fillScreen(ST7735_BLACK);
  tft.setTextWrap(true);
  tft.setTextColor(ST77XX_WHITE);
  tft.setCursor(0, 0);
  tft.print("Arduino nano RP2040 Connect");

  //----------------------------------------
  Serial.println("Initializing SD card...");
  if (!SD.begin(SD_CS)){
    Serial.println("SD.begin() failed!");
  }
  else{
    Serial.println("SD.begin() Success.");

    root = SD.open("/");
    printDirectory(root, 0);

  }


  //----------------------------------------

  Serial.println("\n- End of setup() -\n");
}

const int NO_OF_BMP = 3;
char* bmpFiles[NO_OF_BMP] = {"/test.bmp", "/test2.bmp",  "/test3.bmp"};

void loop() {
  for(int i=0; i<NO_OF_BMP; i++){
    bmpDraw(bmpFiles[i], 0, 0);
    delay(2000);
  }
}

//
// printDirectory() copy from:
// Examples > SD > listfiles
//

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

//
// bmpDraw() copy from:
// Examples under "Adafruit ST7735 and ST7789 Library" > shieldtest
//
// This function opens a Windows Bitmap (BMP) file and
// displays it at the given coordinates.  It's sped up
// by reading many pixels worth of data at a time
// (rather than pixel by pixel).  Increasing the buffer
// size takes more of the Arduino's precious RAM but
// makes loading a little faster.  20 pixels seems a
// good balance.

#define BUFFPIXEL 20

void bmpDraw(char *filename, uint8_t x, uint8_t y) {

  File     bmpFile;
  int      bmpWidth, bmpHeight;   // W+H in pixels
  uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  uint32_t bmpImageoffset;        // Start of image data in file
  uint32_t rowSize;               // Not always = bmpWidth; may have padding
  uint8_t  sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  boolean  goodBmp = false;       // Set to true on valid header parse
  boolean  flip    = true;        // BMP is stored bottom-to-top
  int      w, h, row, col;
  uint8_t  r, g, b;
  uint32_t pos = 0, startTime = millis();

  if((x >= tft.width()) || (y >= tft.height())) return;

  Serial.println();
  Serial.print("Loading image '");
  Serial.print(filename);
  Serial.println('\'');

  // Open requested file on SD card
  if ((bmpFile = SD.open(filename)) == NULL) {
    Serial.print("File not found");
    return;
  }

  // Parse BMP header
  if(read16(bmpFile) == 0x4D42) { // BMP signature
    Serial.print("File size: "); Serial.println(read32(bmpFile));
    (void)read32(bmpFile); // Read & ignore creator bytes
    bmpImageoffset = read32(bmpFile); // Start of image data
    Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
    // Read DIB header
    Serial.print("Header size: "); Serial.println(read32(bmpFile));
    bmpWidth  = read32(bmpFile);
    bmpHeight = read32(bmpFile);
    if(read16(bmpFile) == 1) { // # planes -- must be '1'
      bmpDepth = read16(bmpFile); // bits per pixel
      Serial.print("Bit Depth: "); Serial.println(bmpDepth);
      if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed

        goodBmp = true; // Supported BMP format -- proceed!
        Serial.print("Image size: ");
        Serial.print(bmpWidth);
        Serial.print('x');
        Serial.println(bmpHeight);

        // BMP rows are padded (if needed) to 4-byte boundary
        rowSize = (bmpWidth * 3 + 3) & ~3;

        // If bmpHeight is negative, image is in top-down order.
        // This is not canon but has been observed in the wild.
        if(bmpHeight < 0) {
          bmpHeight = -bmpHeight;
          flip      = false;
        }

        // Crop area to be loaded
        w = bmpWidth;
        h = bmpHeight;
        if((x+w-1) >= tft.width())  w = tft.width()  - x;
        if((y+h-1) >= tft.height()) h = tft.height() - y;

        // Set TFT address window to clipped image bounds
        tft.startWrite();
        tft.setAddrWindow(x, y, w, h);

        for (row=0; row<h; row++) { // For each scanline...

          // Seek to start of scan line.  It might seem labor-
          // intensive to be doing this on every line, but this
          // method covers a lot of gritty details like cropping
          // and scanline padding.  Also, the seek only takes
          // place if the file position actually needs to change
          // (avoids a lot of cluster math in SD library).
          if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
            pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
          else     // Bitmap is stored top-to-bottom
            pos = bmpImageoffset + row * rowSize;
          if(bmpFile.position() != pos) { // Need seek?
            tft.endWrite();
            bmpFile.seek(pos);
            buffidx = sizeof(sdbuffer); // Force buffer reload
          }

          for (col=0; col<w; col++) { // For each pixel...
            // Time to read more pixel data?
            if (buffidx >= sizeof(sdbuffer)) { // Indeed
              bmpFile.read(sdbuffer, sizeof(sdbuffer));
              buffidx = 0; // Set index to beginning
              tft.startWrite();
            }

            // Convert pixel from BMP to TFT format, push to display
            b = sdbuffer[buffidx++];
            g = sdbuffer[buffidx++];
            r = sdbuffer[buffidx++];
            tft.pushColor(tft.color565(r,g,b));
          } // end pixel
        } // end scanline
        tft.endWrite();
        Serial.print("Loaded in ");
        Serial.print(millis() - startTime);
        Serial.println(" ms");
      } // end goodBmp
    }
  }

  bmpFile.close();
  if(!goodBmp) Serial.println("BMP format not recognized.");
}

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t read16(File f) {
  uint16_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read(); // MSB
  return result;
}

uint32_t read32(File f) {
  uint32_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read();
  ((uint8_t *)&result)[2] = f.read();
  ((uint8_t *)&result)[3] = f.read(); // MSB
  return result;
}


nanoRP2040_ST7735_SD_ImageReader.ino, a simplified version to display bitmaps using Adafruit_ImageReader, make sure it's installed.
// Adafruit_ImageReader test for Adafruit ST7735 TFT Breakout for Arduino.
// Demonstrates loading images from SD card or flash memory to the screen,
// to RAM, and how to query image file dimensions.
// Requires three BMP files in root directory of SD card:
// test.bmp, test2.bmp and test3.bmp.
// As written, this uses the microcontroller's SPI interface for the screen
// (not 'bitbang') and must be wired to specific pins.

#include <Adafruit_GFX.h>         // Core graphics library
#include <Adafruit_ST7735.h>      // Hardware-specific library
#include <SdFat.h>                // SD card & FAT filesystem library
#include <Adafruit_ImageReader.h> // Image-reading functions

// TFT display and SD card share the hardware SPI interface, using
// 'select' pins for each to identify the active device on the bus.

#define SD_CS    4 // SD card select pin
#define TFT_CS  10 // TFT select pin
#define TFT_DC   8 // TFT display/command pin
#define TFT_RST  9 // Or set to -1 and connect to Arduino RESET pin

SdFat                SD;         // SD card filesystem
Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys

Adafruit_ST7735      tft    = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image       img;        // An image loaded into RAM
int32_t              width  = 0, // BMP image dimensions
                     height = 0;

void setup(void) {

  ImageReturnCode stat; // Status from image-reading functions

  Serial.begin(115200);

  tft.initR(INITR_BLACKTAB); // Initialize screen

  // The Adafruit_ImageReader constructor call (above, before setup())
  // accepts an uninitialized SdFat or FatFileSystem object. This MUST
  // BE INITIALIZED before using any of the image reader functions!
  Serial.print(F("Initializing filesystem..."));

  // SD card is pretty straightforward, a single call...
  if(!SD.begin(SD_CS, SD_SCK_MHZ(10))) { // Breakouts require 10 MHz limit due ...
    Serial.println(F("SD begin() failed"));
    for(;;); // Fatal error, do not continue
  }

  Serial.println(F("OK!"));

  // Fill screen blue. Not a required step, this just shows that we're
  // successfully communicating with the screen.
  tft.fillScreen(ST7735_BLUE);

  // Load full-screen BMP file 'test.bmp' at position (0,0) (top left).
  // Notice the 'reader' object performs this, with 'tft' as an argument.
  Serial.print(F("Loading test.bmp to screen..."));
  stat = reader.drawBMP("/test.bmp", tft, 0, 0);
  reader.printStatus(stat);   // How'd we do?

  // Query the dimensions of image 'test2.bmp' WITHOUT loading to screen:
  Serial.print(F("Querying test2.bmp image size..."));
  stat = reader.bmpDimensions("/test2.bmp", &width, &height);
  reader.printStatus(stat);   // How'd we do?
  if(stat == IMAGE_SUCCESS) { // If it worked, print image size...
    Serial.print(F("Image dimensions: "));
    Serial.print(width);
    Serial.write('x');
    Serial.println(height);
  }

  // Load small BMP 'test3.bmp' into a GFX canvas in RAM.
  Serial.print(F("Loading test3.bmp to canvas..."));
  stat = reader.loadBMP("/test3.bmp", img);
  reader.printStatus(stat); // How'd we do?

  delay(2000); // Pause 2 seconds before moving on to loop()
}

void loop() {

  for(int r=0; r<4; r++) { // For each of 4 rotations...
    tft.setRotation(r);    // Set rotation
    tft.fillScreen(0);     // and clear screen

    reader.drawBMP("/test2.bmp", tft, 0, 0);

    delay(1000); // Pause 1 sec.

    img.draw(tft,                                    // Pass in tft object
        0 ,  // Horiz pos.
        0); // Vert pos

    delay(2000); // Pause 2 sec.
  }

}

Related:


Thursday, December 31, 2020

Bi-direction Bluetooth Classic comm. between ESP32 (Arduino framework)

Last post show my exercise of "ESP-32S as Bluetooth classic Server, bi-direction communication with Raspberry Pi/Python". Here is the ESP32 implementation for the Client side, to connect to the ESP32 server in last post.

NodeMCU ESP-32S act as a server (in last post):
It echo what received from bluetooth back to sender, and display on SPI ST7735 display.

ESP32-DevKitC-V4 as client (this post):
Connect to server, forward data from serial,  to Bluetooth. Display data from Bluetooth on I2C SSD1306 OLED display.

The code start from ESP32 example of SerialToSerialBTM. But I found that the code SerialBT.connect() always return "true", and prompt "Connected Succesfully!", no matter the device name and MAC address, even no server exist. 

To solve it, I implement my bluetooth callback function to double check if ESP_SPP_OPEN_EVT raised. I don't know is it a long time solution, anyway it work in this exercise.

SPPClient_ESP32_ssd1306_20201231b.ino
/*
 * SPP Client on ESP32
 * Display on  SSD1306
 */

#include "ssd1306.h"
#include "ssd1306_console.h"
#include "BluetoothSerial.h"

Ssd1306Console  console;

BluetoothSerial SerialBT;

String ServerMACadd = "3C:71:BF:0D:DD:6A";
uint8_t ServerMAC[6]  = {0x3C, 0x71, 0xBF, 0x0D, 0xDD, 0x6A};
String ServerName = "ESP32_SPP";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
bool isSppOpened = false;

/*
.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/
BluetoothSerial/src/BluetoothSerial.cpp

 */

void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
  
  switch (event)
    {
    case ESP_SPP_INIT_EVT:
        Serial.println("ESP_SPP_INIT_EVT");
        break;

    case ESP_SPP_SRV_OPEN_EVT://Server connection open
        Serial.println("ESP_SPP_SRV_OPEN_EVT");
        break;

    case ESP_SPP_CLOSE_EVT://Client connection closed
        Serial.println("ESP_SPP_CLOSE_EVT");
        isSppOpened = false;
        break;

    case ESP_SPP_CONG_EVT://connection congestion status changed
        Serial.println("ESP_SPP_CONG_EVT");
        break;

    case ESP_SPP_WRITE_EVT://write operation completed
        Serial.println("ESP_SPP_WRITE_EVT");
        break;

    case ESP_SPP_DATA_IND_EVT://connection received data
        Serial.println("ESP_SPP_DATA_IND_EVT");
        break;

    case ESP_SPP_DISCOVERY_COMP_EVT://discovery complete
        Serial.println("ESP_SPP_DISCOVERY_COMP_EVT");
        break;

    case ESP_SPP_OPEN_EVT://Client connection open
        Serial.println("ESP_SPP_OPEN_EVT");
        isSppOpened = true;
        break;

    case ESP_SPP_START_EVT://server started
        Serial.println("ESP_SPP_START_EVT");
        break;

    case ESP_SPP_CL_INIT_EVT://client initiated a connection
        Serial.println("ESP_SPP_CL_INIT_EVT");
        break;

    default:
        Serial.println("unknown event!");
        break;
    }
}

static void startupScreen()
{
    ssd1306_setFixedFont(ssd1306xled_font6x8);
    ssd1306_clearScreen();
    ssd1306_printFixed(0, 0, "arduiino-er.blogspot.com", STYLE_BOLD);
    ssd1306_printFixed(0, 24, "ESP32 SPP Client", STYLE_NORMAL);
}

void setup()
{
    Serial.begin(115200);
    Serial.println("\n------ begin ----------------\n");
    
    ssd1306_128x64_i2c_init();
    ssd1306_clearScreen();
    startupScreen();
    delay(500);

    Serial.println("- to connect -");
    ssd1306_printFixed(0, 32, "...to connect", STYLE_NORMAL);
    
    SerialBT.begin("ESP32_Client", true);
    SerialBT.register_callback(btCallback);
    //connected = SerialBT.connect(ServerName);
    connected = SerialBT.connect(ServerMAC);

    /*
     * In my trial, 
     * SerialBT.connect() always return  true, even no server exist.
     * To solve it, I implemented bluetooth event callback function,
     * double varify if ESP_SPP_OPEN_EVT raised.
     */
    
    if(connected) {
      Serial.println("SerialBT.connect() == true");
    } else {
      Serial.println("Failed to connect! Reset to re-try");
      Serial.println("SerialBT.connect() == false");
      ssd1306_printFixed(0, 32, 
          "Failed to connect! Reset to re-try", STYLE_NORMAL);
      while(true){
      }
    }

    //may be there are some delay to call callback function,
    //delay before check
    delay(500);
    if(isSppOpened == false){
      Serial.println("isSppOpened == false");
      Serial.println("Reset to re-try");
      ssd1306_printFixed(0, 32, 
          "SPP_OPEN not raised! Reset to re-try", STYLE_NORMAL);
      while(true){
      }
    }
    
    Serial.println("isSppOpened == true");
    Serial.println("CONNECTED");

    ssd1306_clearScreen();
    ssd1306_setFixedFont(ssd1306xled_font6x8);
    console.println("CONNECTED:");
}

void loop()
{
    if(!isSppOpened){
      Serial.println("isSppOpened == false : DISCONNECTED");
      Serial.println("Reset to re-connect");
      console.println("DISCONNECTED");
      console.println("Reset to re-connect");
      while(true){
      }
    }
    if (Serial.available()) {
      SerialBT.write(Serial.read());
    }
    if (SerialBT.available()) {
      char c = SerialBT.read();
      //Serial.write(c);
      console.print(c);
    }
    delay(20);
}

For the setup of SPI ST7735 IPS used on server side, refer to the post "ESP32 display with 0.96" 80x160 SPI ST7735 IPS Display, using TFT_eSPI lib".

For the setup of I2C SSD1306 OLED used on client side, refer to the post "I2C SSD1306 OLED@ESP32 (ESP32-DevKitC-V4), using SSD1306 lib".

Tuesday, December 29, 2020

ESP-32S as Bluetooth classic Server, bi-direction communication with Raspberry Pi/Python


NodeMCU ESP-32S (in Arduino framework) act as a Bluetooth classical (SPP) server:
It echo what received back to sender, and display on SPI ST7735 display.
Also implement callback function to detect esp_spp_cb_event_t.

Python code run on Raspberry Pi, act as GUI Bluetooth classic client, using tkinter/pybluez. Python code refer to: Hello Raspberry Pi - Raspberry Pi/Python as Bluetooth classic client, bi-direction communication with ESP32

Arduino code on ESP32, SPPServer_ESP32.ino.

// ref: Examples > BluetoothSerial > SerialToSerialBT
//with SPI ST735 80x160 IPS Display

#include "BluetoothSerial.h"
#include "esp_bt_device.h"
#include <TFT_eSPI.h> // TFT library
#include <SPI.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

TFT_eSPI tft = TFT_eSPI();

const String deviceName = "ESP32_SPP";

String getMAC(){
  const uint8_t* point = esp_bt_dev_get_address();

  String s = "";

  for (int i = 0; i < 6; i++) {
    char str[3];
    sprintf(str, "%02X", (int)point[i]);
    s = s + str;
    if (i < 5){
      s = s+ ":";
    }
  }
  return s;
}

/*
.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/
BluetoothSerial/src/BluetoothSerial.cpp

 */

void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){

  //tft.fillScreen(TFT_BLACK);
  //tft.setCursor(0, 0, 2);
  //tft.setTextSize(1);
  
  switch (event)
    {
    case ESP_SPP_INIT_EVT:
        Serial.println("ESP_SPP_INIT_EVT");
        //tft.println("ESP_SPP_INIT_EVT");
        break;

    case ESP_SPP_SRV_OPEN_EVT://Server connection open
        Serial.println("ESP_SPP_SRV_OPEN_EVT");
        //tft.println("ESP_SPP_SRV_OPEN_EVT");

        tft.fillScreen(TFT_BLACK);
        tft.setCursor(0, 0, 1);
        tft.setTextSize(1);
        break;

    case ESP_SPP_CLOSE_EVT://Client connection closed
        Serial.println("ESP_SPP_CLOSE_EVT");
        //tft.println("ESP_SPP_CLOSE_EVT");

        startUpScr();
        break;

    case ESP_SPP_CONG_EVT://connection congestion status changed
        Serial.println("ESP_SPP_CONG_EVT");
        //tft.println("ESP_SPP_CONG_EVT");
        break;

    case ESP_SPP_WRITE_EVT://write operation completed
        Serial.println("ESP_SPP_WRITE_EVT");
        //tft.println("ESP_SPP_WRITE_EVT");
        break;

    case ESP_SPP_DATA_IND_EVT://connection received data
        Serial.println("ESP_SPP_DATA_IND_EVT");
        //tft.println("ESP_SPP_DATA_IND_EVT");
        break;

    case ESP_SPP_DISCOVERY_COMP_EVT://discovery complete
        Serial.println("ESP_SPP_DISCOVERY_COMP_EVT");
        //tft.println("ESP_SPP_DISCOVERY_COMP_EVT");
        break;

    case ESP_SPP_OPEN_EVT://Client connection open
        Serial.println("ESP_SPP_OPEN_EVT");
        //tft.println("ESP_SPP_OPEN_EVT");
        break;

    case ESP_SPP_START_EVT://server started
        Serial.println("ESP_SPP_START_EVT");
        //tft.println("ESP_SPP_START_EVT");
        break;

    case ESP_SPP_CL_INIT_EVT://client initiated a connection
        Serial.println("ESP_SPP_CL_INIT_EVT");
        //tft.println("ESP_SPP_CL_INIT_EVT");
        break;

    default:
        Serial.println("unknown event!");
        //tft.println("unknown event!");
        break;
    }
}

void startUpScr(){
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(0, 0, 2);
  tft.setTextSize(1);
  tft.println("arduino-er.blogspot.com");
  tft.println(deviceName);
  tft.setTextFont(1);
  tft.setTextSize(2);
  tft.println(getMAC());
}

void setup() {
  Serial.begin(115200);
  Serial.println("\n---Start---");
  SerialBT.begin(deviceName); //Bluetooth device name
  
  Serial.println("The device started, now you can pair it with bluetooth!");
  Serial.println("Device Name: " + deviceName);
  Serial.print("BT MAC: ");
  Serial.print(getMAC());
  Serial.println();
  SerialBT.register_callback(btCallback);

  tft.init();
  tft.setRotation(3);
  startUpScr();

}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    char c = SerialBT.read();
    SerialBT.write(c);
    String s = String(c);
    if(tft.getCursorY() >= 80){
      tft.setCursor(0, 0);
      tft.fillScreen(TFT_BLACK);
    }
    tft.print(s);
  }
  delay(20);
}

To install TFT_eSPI library in Arduino IDE, and prepare custom setup file, refer ESP32 display with 0.96" 80x160 SPI ST7735 IPS Display, using TFT_eSPI lib.

Next:

Sunday, December 27, 2020

ESP32 display with 0.96" 80x160 SPI ST7735 IPS Display, using TFT_eSPI lib

Steps to install TFT_eSPI library in Arduino IDE, and prepare custom setup file. To make ESP32 (in Arduino framework) display on 0.96" 80x160 IPS Display with ST7735 SPI Driver.


ESP32 board used is NodeMCU ESP-32S with ESP32-WROOM-32 module:



0.96" 80x160 IPS Display with ST7735 SPI Driver:



Install TFT_eSPI:

In Arduino IDE, install TFT_eSPI library.

From the TFT_eSPI GitHub page, we know that if you load a new copy of TFT_eSPI then it will over-write your setups if they are kept within the TFT_eSPI folder. It's suggested to create a new folder in your Arduino library folder called "TFT_eSPI_Setups". You then place your custom setup.h files in there. After an upgrade simply edit the User_Setup_Select.h file to point to your custom setup file.


Copy the selected setup file to "TFT_eSPI_Setups" folder:


You custom setup file. No need to change in our case. Just connect ESP32 board to ST7735 display board match with the your custom setup file.


Edit the User_Setup_Select.h file to point to the custom setup file.


Then you can load examples of TFT_eSPI to see the result.

Next:


Updated@2021-01-06, after library updated.

You see, TFT_eSPI/User_Setup_Select.h changed back to original.


TFT_eSPI_Setups/Setup43_ST7735_ESP32_80x160.h still here.


You simply needed to edit the User_Setup_Select.h file to point to your custom setup file.


Sunday, August 16, 2020

NodeMCU (ESP8266) + 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2)





This video show how to driver 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2) with NodeMCU (ESP8266) using ssd1306 library. Using Arduino IDE.

- In Arduino IDE, open library manager, search ST7735, and install ssd1306 library.

- Open Example > ssd1306 > demos > st7735_demo

- Connect ESP8266 to LCD
ESP8266 LCD
===================
3V3 VCC
GND GND
D1 A0 (D/C)
D2 CS (CS)
RX RESET (RES)
D7 SDA (DIN)
D5 SCK (CLK)
LED (Open in my test)