Interfacing a 1.8in TFT Display, Rotary Encoder, and Switch Module with ESP32-C3
Summary: This project demonstrates how to effectively integrate a 1.8-inch TFT display, rotary encoder, and switch module with an ESP32-C3 microcontroller. It tackles the common challenge of housing disparate electronic components by designing and 3D printing a custom, cost-effective enclosure. The article provides a comprehensive guide from assembly and wiring to programming using the Arduino IDE.
Key Takeaways
- Custom Enclosure Solution: Learn to design and 3D print a functional case for under $15, solving common mounting issues for multi-component modules.
- ESP32-C3 Integration: Understand the process of interfacing a 1.8-inch TFT display, rotary encoder, and push-button switch with an ESP32-C3 development board.
- Arduino IDE for Embedded Development: Gain insights into setting up the Arduino IDE for ESP32-C3, including board installation and code uploading procedures.
Detailed Overview
Introduction: Solving the Mounting Challenge
Integrating off-the-shelf electronic modules, such as a 1.8" TFT/Encoder/Switch module, into a project case often presents a challenge. Due to varying component heights and form factors, standard enclosures rarely provide a perfect fit. This project offers a practical solution by designing and 3D printing a custom case specifically tailored for this module and an ESP32-C3 microcontroller, ensuring a seamless and professional integration.
Components Utilized
This project leverages readily available and affordable components to create a functional device:
- 1.8in TFT Display Module: A compact 1.8-inch TFT screen, EC11 rotary encoder, and push-button switch, commonly sourced from online retailers.
- ESP32-C3 Module: A mini WiFi and Bluetooth development board featuring a 32-bit single-core processor, ideal for various IoT applications.
- Arduino IDE: The primary software environment for programming the ESP32-C3 microcontroller.
- 3D Printer (generic): Essential for fabricating the custom enclosure parts.
- Soldering Iron (generic): Used for making the necessary electrical connections between components.
1.8" TFT Module ST7735S Driver / EC11 Encoder
ESP32-C3 development module
Deep Dive & Technical Insights
3D Printed Enclosure Assembly
The custom 3D printed case is central to this project's physical integration. The design consists of three STL files: "TFT 1.8in - Front", "TFT 1.8in - Back.stl", and "TFT 1.8in - ESP32C3.stl". These parts are optimized for a 0.2 mm layer height and require no support structures during printing, simplifying the fabrication process.
Assembly involves securely screwing the TFT/Encoder/Switch module onto the front panel using four 4 mm M3 screws. The ESP32-C3 holder is integrated during this step, ensuring both modules are precisely positioned and held within the custom enclosure.
Screw on PCB to the front panel with the ESP32-C3 holder.
Wiring the Modules
Connecting the 1.8" TFT/Encoder/Switch module to the ESP32-C3 requires careful attention to the pin assignments. The provided wiring diagram and ESP32-C3 pinout facilitate accurate electrical connections.
Wiring Diagram
ESP32-C3 Pinout
Programming the ESP32-C3 Microprocessor
The ESP32-C3 is programmed using the Arduino IDE. Follow these steps for setup and code upload:
- Install ESP32 Boards: Add the ESP32 board manager URL to your Arduino IDE preferences and install the "esp32" boards package.
Install the ESP32 boards in the Arduino IDE - Select the ESP32-C3 Dev Module: Navigate to the Tools > Board menu and choose the specific "ESP32-C3 Dev Module".
Select the ESP32-C3 Dev Module - Enter Boot Mode: To upload code, hold down the BOOT button while pressing and releasing the RESET button on the ESP32-C3 module. This puts the board into bootloader mode.
BOOT and RESET button locations - Upload Code: Compile and upload the provided sample code to test the display, rotary encoder, and switch functionalities. After a successful upload, press the RESET button on the module to run the sketch.
Sketch to test each feature of the module
The project includes two versions of the Arduino code. IPS_1.8in_Module_V2.ino is the recommended version, offering optimized rotary interrupt handling and reduced serial output compared to IPS_1.8in_Module_V1.ino. The V2 code is provided below:
/*---------------------------------------------------------------------------------------
1.8in IPS + EC11 Rotary Encoder + 1 push button module test
Version 1 - Initial code for ESP32-C3 module
MISO, GPI05 +------\_/------+ 5 V
MOSI, GPIO6 + + GND
SS, GPI07 + + 3V3
SDA, GPIO8 + ESP32-C3 + GPIO4, ADC1_4, SCK
SCL, GPI09 + + GPIO3, ADC1_3
GPIO10 + + GPIO2, ADC1_2
TXD, GPIO20 + + GPIO1, ADC1_1
RXD, GPIO21 +---------------+ GPIO0, ADC1_0
Version 2
- Modified rotary interrupt to sum movement rather than once per main loop
- Removed Serial.print code
----------------------------------------------------------------------------------------*/
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
#define ENC_A 0
#define ENC_B 1
#define ENC_S 2
#define SWITCH 3
//Hardware SPI for ESP32-C3
#define TFT_SCK 4 //SCL
#define TFT_MISO 5
#define TFT_MOSI 6 //SDA
#define TFT_CS 7
#define TFT_DC 8
#define TFT_RST 9
Adafruit_ST7735 display = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// 1.8in IPS Display settings
#define SCREEN_WIDTH 160
#define SCREEN_HEIGHT 128
//Position of each screen lines
#define TEXT_SIZE 5
#define LINE_HEIGHT 26
#define LINE_1_Y 10
#define LINE_2_Y LINE_1_Y+LINE_HEIGHT
#define LINE_3_Y LINE_2_Y+LINE_HEIGHT
#define LINE_4_Y LINE_3_Y+LINE_HEIGHT
#define TEXT_X 10
#define COUNT_X TEXT_X+103
#define ENC_X TEXT_X+70
#define SWITCH_X TEXT_X+123
//Global variables
volatile int8_t rotaryMovement = 0;
volatile bool lastRotA = false;
volatile bool rotaryPressed = false;
volatile int rotaryCounter = 50;
volatile long timeout = 0;
volatile bool switchPressed = false;
volatile bool update = false;
char buf[80]; // Used to format strings
#define ON_TIMEOUT 500
//---------------------------------------------------------------------
// Setup Hardware
//---------------------------------------------------------------------
void setup()
{
pinMode(ENC_A,INPUT);