T-Watch. Create first ESP32 project with the LilyGoWatch library (Arduino IDE or PlatformIO)

Get started with the lilygoatch library for Lilygo TTGO T-Watch watches. Example code with ide arduino platformio
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

The LilyGoWatch library for the Arduino IDE is delivered with a set of drivers necessary to operate the peripherals of T-Watch watches and cases. In this article we will learn how to create a project from scratch on the Arduino IDE and PlatformIO.

GitHub logo

PlatformIO is much better at compiling firmware than the Arduino IDE, except for the first compilation. If you are new to programming, getting started with PlatformIO can be confusing. I therefore advise you to start by reading these few articles.

Create a project for the T-Watch 2020 on the Arduino IDE

Before you can start developing a project for your T-Watch, T-Block, or T-Bot, you need to install the ESP-IDF SDK. This is the official development kit that contains all the libraries and tools necessary to be able to develop Arduino code for ESP32 microcontrollers.

Added repository to ESP-IDF for ESP32 cards

Once the SDK is installed, you must manually install the TTGO library for T-Watch from GitHub.

Install LilyGo’s TTGO library

LilyGo does not (yet) offer its library on the official Arduino IDE repository. We will therefore have to go and retrieve it on GitHub by going to this page.

1 Click on Code then Download ZIP to download the library. Do not compress the ZIP archive.

ide arduino install ttgo twatch library lilygo

2 Go to the Sketch menu of the Arduino IDE then Add a library and finally Add .ZIP library.

Add ZIP library from the Arduino IDE

3 Indicate the path to the library ZIP file that you have just downloaded, then validate to install the library.

Select the ESP32 TTGO T-Watch board (any model)

Once installed, go to the Tools -> Development board menu

Select the “TTGO T-Watch” board in the ESP32 section.

ide arduino select esp32 ttgo t-watch

If the board does not appear in the list, update the ESP32 SDK from the Tools -> Board -> Board Manager menu. Select the version then Install to update.

ide arduino install update esp32 sdk esp-idf board manager

Create a new T-Watch project on the Arduino IDE

Create a new project, paste the following Arduino code and save the file

#include 
// Must be defined before include LilyGoWatch librarie 
// Uncomment the line corresponding your T-Watch 
#define LILYGO_WATCH_2019_WITH_TOUCH     // To use T-Watch2019 with touchscreen, please uncomment this line
// #define LILYGO_WATCH_2019_NO_TOUCH    // To use T-Watch2019 Not touchscreen , please uncomment this line
// #define LILYGO_WATCH_2020_V1          //To use T-Watch2020, please uncomment this line

#include 

// C++ object which will allow access to the functions of the Watch 
TTGOClass *watch;

void setup() {
    // Get Watch object and set up the display 
    watch = TTGOClass::getWatch();
    watch->begin();     
    watch->openBL();
    
    // Use eTFT library to display text on screen 
    watch->tft->fillScreen(TFT_BLACK);
    watch->tft->setTextFont(2);
    watch->tft->setTextSize(2);
    watch->tft->setTextColor(TFT_WHITE);
    watch->tft->setCursor(0, 0);
    watch->tft->println(F("Hello T-Watch"));
}

void loop() {}

Arduino ESP32 code explanation

When the LilyGoWatch library is initialized, it loads the drivers (libraries) of the devices embedded on the Core PCB (accelerometer, RTC clock, TFT screen or e-Paper …)

The LilyGoWatch library uses a system of constants to determine which libraries should be loaded at initialization.

To avoid having to search for the constants to be activated manually according to the on-board peripherals, we have a constant (detailed in this paragraph ) for each T-Watch model.

  • LILYGO_WATCH_BLOCK T-Block with eInk display
  • LILYGO_WATCH_2019_WITH_TOUCH Box with color touch screen
  • LILYGO_WATCH_2019_NO_TOUCH T-Watch-N without touchscreen
  • LILYGO_WATCH_2020_V1 T-Watch 2020
  • LILYGO_WATCH_2020_V2  T-Watch 2020 v2 (not yet released)

The constant defining the T-Watch model must be placed before including the LilyGoWatch library like this

#define LILYGO_WATCH_2019_WITH_TOUCH 
#include <LilyGoWatch.h>

The LilyGoWatch library provides access to the methods offered (at least those exposed by the developers of LilyGo) of the various devices of the watch. To do this, we declare a C ++ object of type TTGOClass.

It is possible to use the standard libraries for ESP32 instead of the integrated ones

TTGOClass *watch;

Then we initialize the watch object by calling the getWatch() method.

watch = TTGOClass::getWatch();

Now we can access the functions of all peripherals.

For example, to use the TFT screen of the watch, we start by initializing the library with the begin() method, then we turn on the backlight with the openBL() method.

watch->begin();  // Init TFT screen
watch->openBL(); // Switch ON backlight

A fork of the library SPI_eTFT of Bodmer is integrated into the library LiLyGOWatch. Since the screen is already pre-configured, you can directly use the proposed functions.

Here we will put the background of the screen in black and write a simple white text in the upper left corner of the screen.

watch->tft->fillScreen(TFT_BLACK); 
watch->tft->setTextFont(2); 
watch->tft->setTextSize(2); 
watch->tft->setTextColor(TFT_WHITE); 
watch->tft->setCursor(0, 0); 
watch->tft->println(F("Hello T-Watch"));

Upload the project to a 2020 T-Watch

There you have it, all you have to do is upload the project like any other ESP32 project.

hello world ttgo t-watch lilygo ide arduino

Create a T-Watch project on PlatformIO (VSCode)

Unlike the Arduino IDE, PlatformIO is able to go and find everything it needs on the Internet (SDK, libraries …). We will therefore go directly to the creation of the project!

Read this article to quickly install and get started with PlatformIO

From the PIO menu, click on + New project.

Create a new project with PlatformIO under VSCode

Then

  • Name give a name to your project
  • Board select TTGO T-Watch from the list (enter the first letters to search faster)
  • Framework leave Arduino
  • Location indicate the save path of the project or keep that of PlatformIO by default (recommended for beginners)
  • Click on Continue

pio project wizard ttgo lilygo t-watch get started

It is possible to import (convert) a project (or an example) developed with the Arduino IDE. Read this article to learn more

Unlike a classic Arduino project, a PlatformIO project adopts a particular structure as well as a platformio.ini configuration file located at the root

project_dir
├── .pio
|     └─ buid
|     └─ libdeps
├── lib
|     └─ ...
├── includes
|     └─ ...
├── src
|     └─ main.cpp
└── test
|     └─ ...
└── platformio.ini

The .pio hidden folder will contain

  • The build(s), compilation files and firmware that is uploaded to the development board.
  • A local copy of the libraries needed for the project. Here the LilyGoWatch bookstore

The src folder will contain the source code of the project. At least one C ++ file named main.cpp. The equivalent of the ino file on the Arduino IDE.

Read this article to quickly get started with PlatformIO on VSCode.

Edit the main.cpp source code file

The creation wizard creates a main.cpp file in the scr (source) folder. Open it then paste the following code

#include Arduino.h
// Select you T-Watch in the platformio.ini file 
#include LilyGoWatch.h

// C++ object which will allow access to the functions of the Watch 
TTGOClass *watch;

void setup() {
    // Get Watch object and set up the display 
    watch = TTGOClass::getWatch();
    watch->begin();     
    watch->openBL();
    
    // Use SPI_eTFT library to display text on screen 
    watch->tft->fillScreen(TFT_BLACK);
    watch->tft->setTextFont(2);
    watch->tft->setTextSize(2);
    watch->tft->setTextColor(TFT_WHITE);
    watch->tft->setCursor(0, 0);
    watch->tft->println(F("Hello T-Watch"));
}

void loop() {}

You can notice two differences from the Arduino project:

  • Arduino.h required for compilation from the Arduino framework
  • The declaration of the type of watch is moved to the platformio.ini configuration file

Edit the platformio.ini configuration file

The configuration proposed by the PIO project creation wizard does not (yet) declare the libraries to be installed.

We will therefore have to do it manually. To do this, open the platformio.ini file located at the root of the project and replace the configuration with this one:

[env:ttgo-t-watch]
platform = espressif32
board = ttgo-t-watch
framework = arduino
build_flags =
    ;-D LILYGO_WATCH_2019_WITH_TOUCH=1
    ;-D LILYGO_WATCH_2019_NO_TOUCH=1
    -D LILYGO_WATCH_2020_V1=1
lib_deps =
    TTGO TWatch Library
upload_speed = 2000000
monitor_speed = 115200

Save the file. The installation of the libraries starts automatically!

Explanations

  • build_flags automatically adds the constant corresponding to the watch model to the compilation! Uncomment the line that corresponds to your watch
  • lib_deps declares the libraries to integrate into the project. They are downloaded and installed automatically in the .pio / libdeps folder
  • upload_speed speed to upload firmware
  • monitor_speed serial monitor speed

Read this article to learn more about the platformio.ini configuration file

Upload the project from PlatformIO

Plug the T-Watch into a USB port and click on the arrow icon in the PIO menu. The project is compiled and directly uploaded.

palette pio vscode upload

The compilation time is similar to that of the Arduino IDE … the first time, then it’s much faster because PIO only re-compiles the project. Test for yourself, you will not be able to do without it!

hello world ttgo t-watch 2020 lilygo ide arduino

Where can I find the examples installed with the TTGO library?

The advantage of the Arduino IDE is that it offers an Examples menu where it is very easy to find the example codes delivered with the libraries.

With PIO, just go to the .pio folder -> libdeps -> ttgo-t-watch -> TTGO T-Watch Library -> examples

examples ttgo t-watch platformio vscode

Declare accessories and expansion boards for T-Watch

Depending on the model, the Core PCB (the equivalent of a computer motherboard) of a T-Watch embeds peripherals (RTC clock, accelerometer, power supply controller, eInk or LCD screen) and possibly an extension board (GPS, GPRS, Lora …).

Read this article to learn more about the different T-Watch models available.

To simplify development, we have constants to load the drivers from the LilyGoWatch library. So no need to know the pins of each device. Everything is pre-configured.

Strikethrough lines correspond to obsolete expansion boards

  • LILYGO_WATCH_HAS_SIM800L, modem GSM/GPRS quad-bandes
  • LILYGO_WATCH_HAS_SIM868, modem GSM/GPRS quad-bandes
  • LILYGO_WATCH_HAS_NFC, NFC reader
  • LILYGO_WATCH_HAS_GPRS, modem GPRS
  • LILYGO_WATCH_HAS_GPS, module GPS u-blox NEO M8N
  • LILYGO_WATCH_HAS_LORA, module Lora Semtec 1276 ou 1278
  • LILYGO_WATCH_HAS_BUZZER, buzzer
  • LILYGO_WATCH_HAS_MOTOR, motor
  • LILYGO_WATCH_HAS_BMA423, 3-axis accelerometer
  • LILYGO_WATCH_HAS_MPU6050, 6-axis accelerometer
  • LILYGO_WATCH_HAS_MAX301XX, heart rate measurement (just fixed on the bottom cover)
  • LILYGO_WATCH_HAS_PCF8563, RTC clock
  • LILYGO_WATCH_HAS_SDCARD, micro SD card reader
  • LILYGO_WATCH_HAS_BUTTON User Button
  • LILYGO_WATCH_HAS_MPR121, external touch interface
  • LILYGO_WATCH_HAS_BBQ_KEYBOARD, mini clavier externe !
  • LILYGO_WATCH_HAS_S76_S78G, expansion board embedding the SiP AcSIP S76GXB or AcSIP S78GXB
  • LILYGO_WATCH_LVGL, activates the LVGL graphics library. Please note, only for ST7789 TFT screens
  • LILYGO_WATCH_HAS_TOUCH, driver tactile FT6236
  • LILYGO_WATCH_HAS_BACKLIGHT, TFT screen backlight ST7789
  • LILYGO_WATCH_HAS_GAMEPAD, gamepad with joystick and 4 buttons
  • LILYGO_WATCH_HAS_AXP202, I2C power controller AXP202
  • LILYGO_WATCH_HAS_DISPLAY, activate screen
  • LILYGO_WATCH_HAS_IRREMOTE, IR transmitter (only for T-Watch 2020)
  • LILYGO_WATCH_HAS_EINK, activates the eInk screen
  • LILYGO_WATCH_DRV2605, haptic controller

Only for the T-Block

  • LILYGO_EINK_TOUCHSCREEN, ePaper touch screen
  • LILYGO_BLOCK_ST7796S_MODULE, driver for LCD screen ST7796S
  • LILYGO_BLOCK_ILI9488_MODULE, driver for LCD screen ILI9488

The drivers are already pre-installed and are located in the src / divers folder of the library. No need therefore to manually install the Arduino libraries of the peripherals, the developers of LilyGo take care of everything. You can however use the standard libraries if some functions are missing.

To avoid having to search for constants to be activated manually, we have a constant for each T-Watch. Everything is explained in the next paragraph.

Remember that some options are activated automatically when choosing the T-Watch. However, a double declaration does not lead to any compilation and execution error of the Arduino project.

Declare the target T-Watch type

To avoid having to search for the constants to be activated manually according to the on-board peripherals, we have a constant for each T-Watch model.

  • LILYGO_WATCH_BLOCK T-Block with eInk display
  • LILYGO_WATCH_2019_WITH_TOUCH Box with color touch screen
  • LILYGO_WATCH_2019_NO_TOUCH T-Watch-N without touchscreen
  • LILYGO_WATCH_2020_V1 T-Watch 2020
  • LILYGO_WATCH_2020_V2  T-Watch 2020 v2 (not yet released)

Just add the constant at the start of your project or in a separate configuration file to load the libraries and automatically start the devices when the ESP32 is powered on.

LILYGO_WATCH_BLOCK, T-Block with eInk display

ttgo lilygo t-block esp32Automatically activated devices

  • LILYGO_WATCH_HAS_PCF8563, RTC clock
  • LILYGO_WATCH_HAS_AXP202, AXP202 power controller
  • LILYGO_WATCH_HAS_BUTTON, user button on GPIO36
  • LILYGO_WATCH_HAS_MPU6050, 6-axis accelerometer

Unsupported devices: no restrictions specified

See more offers

LILYGO_WATCH_2019_WITH_TOUCH, T-Watch Touch

lilygo ttgo t-watch touch revision 211-191209Automatically activated devices

  • LILYGO_WATCH_HAS_TOUCH, drivers tactile
  • LILYGO_WATCH_HAS_DISPLAY, TFT screen
  • LILYGO_WATCH_HAS_PCF8563, RTC clock
  • LILYGO_WATCH_HAS_BMA423, 3-axis accelerometer
  • LILYGO_WATCH_HAS_AXP202, AXP202 power controller
  • LILYGO_WATCH_HAS_BACKLIGHT, backlighting of the screen controlled by the LDO2 output of the AXP202
  • LILYGO_WATCH_HAS_BUTTON, user button on GPIO36

Unsupported devices

  • LILYGO_EINK_TOUCHSCREEN, e-Paper touch screen
  • LILYGO_WATCH_HAS_IRREMOTE, IR transmitter

See more offers

LILYGO_WATCH_2019_NO_TOUCH, T-Watch-N without touchscreen

lilygo ttgo t-watch-n no touchAutomatically activated devices

  • LILYGO_WATCH_HAS_DISPLAY, TFT screen
  • LILYGO_WATCH_HAS_PCF8563, RTC clock
  • LILYGO_WATCH_HAS_BMA423, 3-axis accelerometer
  • LILYGO_WATCH_HAS_AXP202, AXP202 power controller
  • LILYGO_WATCH_HAS_BACKLIGHT, backlighting of the screen controlled by the LDO2 output of the AXP202
  • LILYGO_WATCH_HAS_BUTTON, user button on GPIO36

Unsupported devices

  • LILYGO_WATCH_HAS_TOUCH, drivers tactile
  • LILYGO_EINK_TOUCHSCREEN, e-Paper touch screen
  • LILYGO_WATCH_HAS_IRREMOTE, IR transmitter

See more offers

LILYGO_WATCH_2020_V1, 2020 version, Apple Watch design watch

lilygottgo t watch 2020 esp32 puce principale 154 pouces cran tactile 3Automatically activated devices

  • LILYGO_WATCH_HAS_TOUCH, drivers tactile
  • LILYGO_WATCH_HAS_DISPLAY, TFT screen
  • LILYGO_WATCH_HAS_BUZZER, buzzer (attention, this is not a speaker)
    LILYGO_WATCH_HAS_MOTOR, buzzer on pin 4
  • LILYGO_WATCH_HAS_PCF8563, RTC clock
  • LILYGO_WATCH_HAS_BMA423, 3-axis accelerometer
  • LILYGO_WATCH_HAS_AXP202, AXP202 power controller
  • LILYGO_WATCH_HAS_BACKLIGHT, backlighting of the screen controlled by the LDO2 output of the AXP202
  • LILYGO_WATCH_HAS_IRREMOTE, IR emitter on pin 13

Unsupported devices: all other options

See more offers

LILYGO_WATCH_2020_V2, T-Watch 2020 under development

Automatically activated devices

  • LILYGO_WATCH_HAS_TOUCH, drivers tactile
  • LILYGO_WATCH_HAS_DISPLAY, TFT screen
  • LILYGO_WATCH_HAS_PCF8563, RTC clock
  • LILYGO_WATCH_HAS_BMA423, 3-axis accelerometer
  • LILYGO_WATCH_HAS_AXP202, AXP202 power controller
  • LILYGO_WATCH_HAS_BACKLIGHT, backlighting of the screen controlled by the LDO2 output of the AXP202
  • LILYGO_WATCH_HAS_IRREMOTE, IR emitter on pin 4
  • LILYGO_WATCH_HAS_GPS, GPS Air 530
  • LILYGO_WATCH_HAS_SDCARD, micro SD card reader

Unsupported devices: all other options

Technical documentation of peripherals

Updates

20201112 Publication of the article

Version française

Click to rate this post!
[Total: 0 Average: 0]

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
Ask your question

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

We will be happy to hear your thoughts

      Leave a Reply

      DIY Projects