ESP32

How to make Digital & Analog Watch using SquareLine Studio and LVGL Arduino

Description:

How to make Digital & Analog Watch using SquareLine Studio and LVGL Arduino- This is my second article on the MaTouch ESP32-S3 AMOLED with a 1.8” FT3168 touch display. And today, you guys are going to learn a lot of new things.

SquareLine Studio LVGL Watch Tutorial

For example, how to display time and date on the screen in a simple way.

allpcb circuit

You will also learn how to work with multiple screens and switch between them.

I will show you how to create custom fonts, and then use those fonts to make a digital watch.

And not just a digital watch; we will also build an analog watch, which will teach you even more new concepts, like how to use image widgets and animate them. The hour, minute, and second hands you see; along with the watch background; are all image files.




Amazon Links:

MaTouch ESP32-S3 AMOLED

Other Tools and Components:

ESP32 WiFi + Bluetooth Module (Recommended)

Arduino Nano USB C type (Recommended)

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Now, you might be wondering, this development board doesn’t even have an RTC, so how are we accessing and displaying the real-time clock and date?

Well, for that, I am using pool.ntp.org, and I will explain it in more detail later in the video.

So guys, here is my template code.

I have already discussed this template code in Part1 in detail.

Now, to access and display the time and date, I will need to modify this code.

Note: I highly recommend to download the complete Template folder from my Patreon page. Because there are so many other files, libraries, example codes, etc. You can also download the Digital and Analog watch project folders.



Accessing Time and Date through Internet:

I have added these three header files

And I have also included the Wi-Fi credentials

SSID and password. Make sure to replace them with your own Wi-Fi details.

To make sure my ESP32 gets the accurate time, first, I need to tell it where to find it on the internet.

For that, I am using “pool.ntp.org”, which is a standard address for Network Time Protocol servers; that’s what this ntpServer line sets up. Now, the time we get from the internet is usually standard UTC time, but I want the display to show the local time for Pakistan (PKT), which is 5 hours ahead. So, I need to tell the system that offset in seconds; 5 hours works out to 18,000 seconds, which is the value I have assigned to utcOffsetInSeconds. Lastly, since Pakistan doesn’t currently observe Daylight Saving Time, I have set the daylightOffset_sec to 0, ensuring the time doesn’t automatically adjust for summer hours.



Some global variables for time and date.

After synchronizing time from the NTP server

I break down the time information into individual components for easier use throughout the project.  I extract the current hour, minute, and second, as well as the current day, month, and year.

After that, I have saved the time and date information into timeStr and dateStr. And then I am printing it to the Serial Monitor.

SquareLine Studio LVGL Watch Tutorial

Great! We have successfully accessed the current time and date.



Now we need to display it on the screen. For that, let’s open the SquareLine Studio project.

SquareLine Studio LVGL Watch Tutorial

In Part 1, we ended the article at this point. Now, to display the time and date, we need two labels. I will use this label to display the time. So first, let’s rename this label to lblTime.

SquareLine Studio LVGL Watch Tutorial

Next, add another label and name it lblDate.

Then, update the texts of the labels to Time and Date.

SquareLine Studio LVGL Watch Tutorial

Now, save the project and export the UI files.

After that, go to the UI Files folder, copy all the generated files, and paste them into the same folder where your Arduino .ino file is located. I have already explained this in the previous video and article.




Anyway, now open the main Arduino .ino file.

If you open the ui_Screen1.c file, you will see a lot of styling code has been generated for both the Time and Date labels.

SquareLine Studio LVGL Watch Tutorial

We will use these two labels to display the actual time and date.

Add the following two lines of codes in the Arduino loop().



Complete Code:

All the other files are already generated for you by the SquareLine Studio. If you still face any issues, simply go to my Patreon page and download the entire project folder.



Let’s upload the code.

SquareLine Studio LVGL Watch Tutorial

This was the simplest method to display the time and date. Next, we will build a Digital Watch, and after that, an Analog Watch.

To make the Digital Watch, we will use custom fonts. I have already downloaded seven-segment font. You can download and use any font you like. Personally, I prefer the segment-style font, so I will use this.



Digital Watch SquareLine Studio:

So, let’s start by adding a second screen.

SquareLine Studio LVGL Watch Tutorial

On this screen, we will display the hours, minutes, and seconds in a slightly different style.

So, let’s go ahead and import the Seven Segment font by clicking on “ADD FILE INTO ASSETS.”

As you can see, our Seven Segment.ttf font has been successfully added to the assets.

Now, using this font, we will create three different fonts.

To do this, go to the Font tab and let’s name it font_hour.

SquareLine Studio LVGL Watch Tutorial

After that, we need to select the font size; you can choose any size you prefer.

For hours, I want to use a larger font size.

250 should be fine for now. Then click on the Create button.

As you can see, our first font has been successfully created. When the font is created you will see two more font files will appear in the Assets along with the Seven Segment.ttf file.

Later on, if you want, you can change the font size.




In the same way, you need to create two more fonts for the minutes and seconds. In my case, I selected different sizes. For the seconds I selected 45 and for the minutes I selected 150.

SquareLine Studio LVGL Watch Tutorial

Now all three fonts are ready.

Let’s go ahead and add three labels and assign each one the font we created. For the quick setup please watch video tutorial given at the end of this article.

After assigning the fonts and setting the positions. I also added a 4th label for displaying the Date.

SquareLine Studio LVGL Watch Tutorial

For this label, I used a a built-in font “as you can see on the right side” so that you get an idea of how to use different fonts at the same time.

Now, let’s save the project and export the UI files.

Go to the ui_files folder, copy all the generated files, and paste them into the same folder where your main Arduino .ino file is located.

SquareLine Studio LVGL Watch Tutorial

This time, you can see that it has also loaded all three fonts that we created for hours, minutes, and seconds.



Digital Watch Complete Code:

This code converts integer time values (hour, minute, second) into two-digit strings using sprintf.

It then updates three LVGL label widgets (ui_lblhour, ui_lblminutes, ui_lblseconds) to display the formatted time values.

And this instruction prints the Date.

And I just remembered; I haven’t applied screen gestures yet.



So let’s go back to SquareLine Studio and apply the screen gestures; otherwise, we won’t be able to switch between the two screens.

SquareLine Studio LVGL Watch Tutorial

  • Select Screen1, then click on “Add Event.”
  • Set the Trigger Type to “Gesture Left.”
  • Set the Action Type to “Change Screen.”
  • Click the “Add” button.
  • Select Screen2.

Now select Screen2 and follow the same steps. This time, choose “Gesture Right” as the trigger and set Screen1 as the target.

Now, let’s save the project and export the UI files.

After that, let’s go ahead and upload the program.

SquareLine Studio LVGL Watch Tutorial

Our digital watch is ready, and it looks so cool!

You saw how, even without an RTC module, you can easily access and use the date and time from the internet.



Analog Watch:

To create an analog watch, I have designed a simple watch face in Photoshop.

SquareLine Studio LVGL Watch Tutorial

Along with that, I have also created the hour hand, minute hand, and second hand.

I have saved all four elements as separate .png images. You can download the watch face and project folder from the Patreon page.

So, let’s open SquareLine Studio and start building our analog watch.

Analog Watch in SquareLine Studio:

For the step-by-step explanation, watch the video tutorial given at the end of this article.

Analog Watch Complete code:

You only need these three lines of code; and that’s it,

Your analog watch is ready!

You may have noticed that I made a small change:

I have removed the seconds hand. Instead, I am now displaying the seconds inside the red rectangle. By the way, you can also display date in that Red rectangle.

SquareLine Studio LVGL Watch Tutorial

So that’s all for now.

Support me on Patreon for more videos and articles.




Watch Video Tutorial:

 

Engr. Shahzada Fahad

Engr. Shahzada Fahad is an Electrical Engineer with over 15 years of hands-on experience in electronics design, programming, and PCB development. He specializes in microcontrollers (Arduino, ESP32, STM32, Raspberry Pi), robotics, and IoT systems. He is the founder and lead author at Electronic Clinic, dedicated to sharing practical knowledge.

Related Articles

Leave a Reply

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

Back to top button
Electronic Clinic
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.