DIY Projects

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

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.

 

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.

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.

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

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.

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.

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.

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.

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.

Then

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 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:

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

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.

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!

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

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

Only for the T-Block

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.

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

Automatically activated devices

Unsupported devices: no restrictions specified

See more offers

LILYGO_WATCH_2019_WITH_TOUCH, T-Watch Touch

Automatically activated devices

Unsupported devices

See more offers

LILYGO_WATCH_2019_NO_TOUCH, T-Watch-N without touchscreen

Automatically activated devices

Unsupported devices

See more offers

LILYGO_WATCH_2020_V1, 2020 version, Apple Watch design watch

Automatically activated devices

Unsupported devices: all other options

See more offers

LILYGO_WATCH_2020_V2, T-Watch 2020 under development

Automatically activated devices

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]
Exit mobile version