ESP32

LVGL ESP32 & SquareLine Tutorial using Arduino IDE

LVGL ESP32 & SquareLine:

LVGL ESP32 & SquareLine Tutorial using Arduino IDE- From today, I am starting a complete series on Smartwatch programming. Throughout this series, we will be using the CrowPanel ESP32-C3 1.28-inch IPS Capacitive Touch Display. I have already written a getting started tutorial on this display, so before reading this article, I highly recommend reading my previous article; you can also watch its video available on my YouTube Channel “Electronic Clinic”.

In that article, I talked in detail about its hardware, how to install the ESP32-C3 board in the Arduino IDE, which libraries you need to install, and at the end, I also shared 5 examples. These examples demonstrate how to control the Vibration Motor and Buzzer, check if a button is pressed, access the date and time from the RTC (Real-Time Clock/Calendar), and read the x, y values along with gestures.

LVGL ESP32 squreline tutorials using arduino

allpcb circuit

In this first article, we are going to make this simple counter because I don’t want to make it complicated for you. For now, your only focus should be on how to create a simple graphical user interface “GUI” in SquareLine Studio and then generate UI Files to use them in the Arduino IDE with the LVGL library. It took me two days to figure out how to use SquareLine Studio and LVGL together. Anyway, after fixing all the errors, I finally made a basic template folder for myself. You can also download this template folder from my Patreon page, this will make your work a lot easier.




If you open the basic template folder, you will find the Arduino project folder “first_project”.

LVGL ESP32 arduino project

Inside this folder, you will find the Arduino “first_project.ino” file, it’s name should match the folder name. Along with the Arduino main file, you will also need to place these other files in the same project folder.

CST816D.cpp

CST816D.h

do_main.cpp

do_mian.h

I2C_BM8563.cpp

I2C_BM8563.h

Lv_confi.h

You can download these files from the ESP32_1.28_Arduino_Demo folder. This is the same folder we downloaded from the product’s official page, in the previous article. Open this folder, go to the libraries folder, and copy this file.

LVGL ESP32 confi file

Then go to your Arduino project folder and paste it.



Next, go to the factory_demo folder, open the ESP_Watch_Demo folder and copy these files.

LVGL ESP32 libraries required for arduino

And paste them in your Arduino project folder as well.

LVGL ESP32 and arduino first project

Next, create two folders, in the “squareline project files” folder we will keep the SquareLine Project files and inside the “ui files” folder we will keep the UI files. Right now these folders are empty.

When your basic template folder is ready then you can go ahead and open the SquareLine Studio.



SquareLine Studio First Project:

I am using the SquareLine Studio 1.5.0.

Click the + Create button, select Arduino, and then click on the Arduino with TFT_eSPI.

LVGL ESP32 squareline studio first project

On the Right side you can see the BOARD DESCRIPTION. TFT_eSPI supports Raspberry pi Pico, ESP32, ESP32-S2, ESP32-C3, ESP32-S3, and STM controllers.

LVGL ESP32 crowpanel squareline studio project setting

Under the project settings,

  • you can set the project name. I named it my_first_counter_Project.
  • Next, we have to select a folder, where we have to save the SquareLine project files. If you remember, we created a folder for this; inside the first_project folder with the name squareline project files. Simply select that folder.
  • Next, we have to select the display resolution. The CrowPanel resolution is 240*240.
  • We are going to leave Rotation to its default value, but if you want you can change it.
  • Let’s also keep the Offset to its default values.
  • Next, we have to select the display shape, the one I am using is round, so I am going to select Circle.
  • Select the color depth as 16 bit.
  • You can see the LVGL version 8.3.11. We will install the same version of the LVGL library in the Arduino IDE, otherwise it will generate an error.
  • Next, you can select a Light or Dark theme as per your preference. But I am going to proceed with the Dark theme.
  • Finally, you can enable or disable the Multilanguage support, but in my case I am going to leave it as it is.



So, that’s all about the project settings, finally you can go ahead and click the CREATE button.

LVGL ESP32 squareline studio

By default, Screen1 will be added.

On the left side, you can see Screens, and on the Widgets tab, you can see the Basic Widgets, CONTROLLER, VISUALISER, and SCREEN.

On the right side, you can see the Inspector tab. When this tab is selected, we can configure the SCREEN settings, STYLE settings, and add Events.

On the History tab, we can go back in time.

On the Font tab, we can create our own custom fonts. This one is my favorite, and I will explain it in upcoming videos and articles.

On the Animation tab, you can create amazing, eye-catching animations. Finally, on the Themes tab, you can create themes. We will cover everything step by step; but in this article, we will only be using the Inspector tab.

Anyway, go to the File menu and click on Project Settings.

LVGL ESP32 project setting in squreline studio

Double-check the properties.

Next, under FILE EXPORT, select the Project Export Root. We need to add a path to the folder we created in the basic template, simply select the squareline project files folder.

Next, we have to select a path for the UI Files Export Path. If you remember, we also created a folder for the UI Files.

For the LVGL include Path, we can simply type lvgl.h.

Finally, make sure to check the Flat Export (exports all files to one folder). Then, go ahead and click the APPLY CHANGES button.

Now, let’s go ahead and install the LVGL library in the Arduino IDE.




Arduino LVGL Library Installation:

Open the latest version of the Arduino IDE. First of all, make sure you have installed the ESP32 board and the basic libraries needed for the CrowPanel display. I have already explained this in my previous video.

Once you are done with that, click on the LIBRARY MANAGER and search for LVGL.

LVGL ESP32 library installation in arduino

Make sure to install lvgl 8.3.11. This version of the LVGL library is fully compatible with the SquareLine Studio I am currently using.

Now, let’s go back to SquareLine Studio.



Designing a Counter in SquareLine Studio:

While the Inspector tab is selected, go to the STYLE SETTINGS and play with all these properties.

LVGL ESP32 style setting in squareline studio

I started off by setting the Background Radius to 240.

Next, I set the Bg Color to Black.

LVGL ESP32 gui properties setting in squreline

Next, I set the border color to Red, Border width to 10.

Next, I set the Outline color to Yellow, and set the Outline width to 5.

If you want, you can also play with other properties. But I think it’s ok for now.

Once styling is complete, before adding any widgets, you first need to make sure everything is working. For this;

  • First save your project.

Then, go to the Export menu and click on Export UI Files.

LVGL ESP32 squareline project

You can see the SquareLine Project files and folders. Now, let’s go to the UI files folder.

LVGL ESP32 squareline ui files

Great! The UI files are also generated. Let’s copy all these files and paste them into the Arduino Project folder.

LVGL ESP32 arduino ui file

Now, let’s open the Arduino file.

LVGL ESP32 16 swap error fixing

All the files are automatically added. Before uploading this program, first go to ui.c and change LV_COLOR_16_SWAP from 0 to 1. Otherwise, it will generate an error.



Code:

Now, to upload the program:

  • Select ESP32C3 Dev Module.
  • Select the correct communication port.

Select the Partition Scheme as Huge APP and then click on the Upload button.

LVGL ESP32 first squreline studio

You can see the red border and the outline, which means we have done everything correctly. Now, let’s go ahead and make a counter.



LVGL ESP32 gui desiging in squreline studio

It just a basic GUI, in the Hierarchy, you can see, I have added two buttons and three labels. To make the label1 child, simply drag and drop it over the Button1. If you face any issues; you can watch the video tutorial given at the end of this article.

Tip: Since we are using default fonts, make sure to keep the text size same. First I used different size fonts but it generated an error. So, later I changed the text size of all the labels to 30.

LVGL ESP32 simple gui in squareline studio

I also changed the button’s names and name of the lable1 to btnReset, btnCounter, and lblCounter. This way, I can easily keep track of all the buttons and labels. If I used default names then it would be quite difficult for me to remember.

Our GUI is complete. Next, we are going to add events to increment and reset the counter.

I want to increament the counter when I release the button, so I am going to select the trigger type as RELEASED. Under Action, I am going to select CALL FUNCTION.

LVGL ESP32 add even in squareline studio




Next, click the ADD button and type the function name.

LVGL ESP32 call function in squreline studio

Make sure to check the box next to Do not export.

Now, follow the same steps for the Reset Button.

LVGL ESP32 button function calling

Save the project and export the UI Files.

Each time you generate the UI files, you will have to go to the UI files folder, copy all those files, and paste them into the Arduino project folder, make sure to replace the exiting ui files.

Now, if you go to ui_events.h, you will see the functions.

LVGL ESP32 ui events file

If you want to check the properties of all the widgets, you can go to ui_Screen1.c. In fact, you can not only check but also modify the values.

And if you go to ui.h, you will find all the variables.



Final Counter Code:

These two functions will be called when we press the + and Reset buttons. So, the code is ready.

Before uploading the program, make sure to change LV_COLOR_16_SWAP from 0 to 1.

LVGL ESP32 squreline project

And here is our first counter!

It worked exceptionally well and I was able to increment and reset the counter.

But there was one issue: even if I would slightly touch the Reset button, the counter would reset. So, I went back to the SquareLine Studio, and changed the Reset button trigger from released to LONG_PRESSED.

Now, even if I intentionally press the Reset button, the counter is not reset unless I press it for more than 1 second. Now, the counter won’t reset if I accidently touch the Reset button.

So, that’s all about how to make a counter using SquareLine Studio and the LVGL library. In the next tutorial, we will add a second screen and make an awesome digital watch using custom fonts.

Next, read my article on how to make a digital watch.



Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button