{"id":159220,"date":"2024-06-26T15:25:15","date_gmt":"2024-06-26T15:25:15","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=159220"},"modified":"2024-09-03T15:07:27","modified_gmt":"2024-09-03T15:07:27","slug":"esp32-cyd-lvgl-temperature-ds18b20","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-cyd-lvgl-temperature-ds18b20\/","title":{"rendered":"ESP32 CYD with LVGL: Display Temperature with DS18B20 Sensor (Text and Arc)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to display temperature from the DS18B20 sensor on an ESP32 Cheap Yellow Display (CYD) using LVGL (Light Versatile Graphics Library). We&#8217;ll display the temperature using a text label and an arc (curved gauge) object. You&#8217;ll also learn how to style LVGL objects. The ESP32 will be programmed using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 CYD with LVGL Display Temperature with DS18B20 Sensor Text and Arc\" class=\"wp-image-159257\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\"><strong>New to the ESP32 Cheap Yellow Display?<\/strong> Start here: <a href=\"https:\/\/randomnerdtutorials.com\/cheap-yellow-display-esp32-2432s028r\/\">Getting Started with ESP32 Cheap Yellow Display Board \u2013 CYD (ESP32-2432S028R)<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>In this project, we\u2019ll display the temperature from the <a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds18b20-temperature-arduino-ide\/\" title=\"\">DS18B20 sensor<\/a> on the ESP32 CYD screen. The temperature will be displayed on a text label and represented on a curved gauge (arc).<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-2 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"460\" data-id=\"159232\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Min-f.png?resize=750%2C460&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display LVGL DS18B20 Demonstration Temperature Minimum\" class=\"wp-image-159232\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Min-f.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Min-f.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"460\" data-id=\"159233\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Max-f.png?resize=750%2C460&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display LVGL DS18B20 Demonstration Temperature Max\" class=\"wp-image-159233\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Max-f.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Max-f.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"460\" data-id=\"159234\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Medium-f.png?resize=750%2C460&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display LVGL DS18B20 Demonstration Temperature Medium\" class=\"wp-image-159234\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Medium-f.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-LVGL-DS18B20-Demonstration-Temperature-Medium-f.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>The temperature text will be displayed in different colors depending on the following ranges:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&lt; 10 \u00b0C (50 \u00b0F): the temperature will be displayed in <span class=\"rnthl rntcblue\">blue<\/span> color;<\/li>\n\n\n\n<li>10 \u00b0C (50 \u00b0F) &lt; temperature &lt; 29 \u00b0C (84\u00b0F): the temperature will be displayed in <span class=\"rnthl rntcgreen\">green<\/span> color;<\/li>\n\n\n\n<li>29 \u00b0C (84\u00b0F) &lt; temperature: the temperature will be displayed in <span class=\"rnthl rntcred\">red<\/span> color.<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclgray\">If you don&#8217;t have a DS18B20 sensor, you can use a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\" title=\"\">BME280<\/a>, a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/\" title=\"\">DHT22, <\/a>or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-guides-sensors-modules\/\" title=\"\">any other sensor<\/a> (that requires a maximum of two GPIOs to connect)&#8230; Or you can use random values to test the project.<\/p>\n\n\n\n<p>With this tutorial, you&#8217;ll learn how to use LVGL to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Display data on text labels;<\/li>\n\n\n\n<li>Change the text color and size;<\/li>\n\n\n\n<li>Create a curved gauge;<\/li>\n\n\n\n<li>Change the colors of the gauge;<\/li>\n\n\n\n<li>Set the gauge value.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding, make sure you follow the next prerequisites.<strong> You must follow all steps, otherwise, your project will not work.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Parts Required<\/h3>\n\n\n\n<p>For this project, you need the following parts<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32-2432S028R \u2013 2.8 inch 240\u00d7320 Smart Display TFT with Touchscreen<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noopener\" title=\"\">DS18B20 Onewire Temperature Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">4.7k Ohm resistor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2) Install ESP32 Boards in Arduino IDE<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"204\" height=\"204\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/arduino-ide-2.jpg?resize=204%2C204&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE 2 Logo\" class=\"wp-image-159109\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/arduino-ide-2.jpg?w=204&amp;quality=100&amp;strip=all&amp;ssl=1 204w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/arduino-ide-2.jpg?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 204px) 100vw, 204px\" \/><\/figure><\/div>\n\n\n<p>We&#8217;ll program the ESP32 using Arduino IDE. Make sure you have the ESP32 boards installed. Follow the next tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" title=\"\">Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3) Get familiar with the ESP32 Cheap Yellow Display<\/h3>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32-2432S028R<\/a>&nbsp;development board has become known in the maker community as the \u201c<em>Cheap Yellow Display<\/em>\u201d or CYD for short. This development board, whose main chip is an ESP32-WROOM-32 module, comes with a 2.8-inch TFT touchscreen LCD, a microSD card interface, an RGB LED, and all the required circuitry to program and apply power to the board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Cheap-Yellow-Display-CYD-Board-ESP32-2432S028R-front.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display CYD Board ESP32-2432S028R front\" class=\"wp-image-149592\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Cheap-Yellow-Display-CYD-Board-ESP32-2432S028R-front.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Cheap-Yellow-Display-CYD-Board-ESP32-2432S028R-front.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>If this is your first time using the ESP32 Cheap Yellow Display, make sure to follow our getting started guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/lvgl-cheap-yellow-display-esp32-2432s028r\/\">Getting Started with ESP32 Cheap Yellow Display Board and LVGL<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4) Install TFT and LVGL Libraries<\/h3>\n\n\n\n<p><a href=\"https:\/\/docs.lvgl.io\/master\/\" target=\"_blank\" rel=\"noreferrer noopener\">LVGL<\/a>&nbsp;(Light and Versatile Graphics Library) is a free and open-source graphics library that provides a wide range of easy-to-use graphical elements for your microcontroller projects that require a graphical user interface (GUI).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"194\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/08\/LVGL-new-logo.png?resize=622%2C194&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"LVGL new logo\" class=\"wp-image-161339\" style=\"width:340px;height:auto\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/08\/LVGL-new-logo.png?w=622&amp;quality=100&amp;strip=all&amp;ssl=1 622w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/08\/LVGL-new-logo.png?resize=300%2C94&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/><\/figure><\/div>\n\n\n<p>Follow the next tutorial to install and configure the required libraries to use LVGL for the ESP32 Cheap Yellow Display using Arduino IDE.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/lvgl-cheap-yellow-display-esp32-2432s028r\/\" title=\"\"><strong>Get started with LVGL using the ESP32 Cheap Yellow Display Board (ESP32-2432S028R)<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5) Install DS18B20 Libraries<\/h3>\n\n\n\n<p>In this tutorial, we&#8217;ll use the DS18B20 sensor to get temperature readings. Follow the next tutorial to get familiar with the sensor and install the required libraries.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds18b20-temperature-arduino-ide\/\" title=\"\">ESP32 DS18B20 Temperature Sensor with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>You&#8217;ll need to install the \u201c<strong>OneWire<\/strong>\u201d library by Paul Stoffregen and &#8220;<strong>DallasTemperature<\/strong>&#8221; library by Miles Burton.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wiring the DS18B20 Temperature Sensor to the ESP32 CYD Board<\/h2>\n\n\n\n<p>We\u2019ll use OneWire communication protocol to get data from the DS18B20 sensor. This communication protocol only requires one GPIO to connect with a microcontroller.<\/p>\n\n\n\n<p>On the Cheap Yellow Display, there is an extended IO socket that allows you to connect external peripherals. We\u2019ll connect the sensor to the CN1 connector, and we&#8217;ll use GPIO 27.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-CYD-Extended-IO-pins.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display Extended IO Connectors\" class=\"wp-image-159211\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-CYD-Extended-IO-pins.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-CYD-Extended-IO-pins.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Your board should have come with a&nbsp;little JST connector to access those GPIOs. You can check the diagram below to see how to connect the sensor to the board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"651\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/DS18B20_ESP32_CYD_circuit.png?resize=748%2C651&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring the DS18B20 Temperature Sensor to the ESP32 CYD Board\" class=\"wp-image-159252\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/DS18B20_ESP32_CYD_circuit.png?w=748&amp;quality=100&amp;strip=all&amp;ssl=1 748w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/DS18B20_ESP32_CYD_circuit.png?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 748px) 100vw, 748px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"423\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Circuit-Wiring-1.jpg?resize=750%2C423&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Cheap Yellow Display LVGL DS18B20 Circuit Wiring\" class=\"wp-image-159236\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Circuit-Wiring-1.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Circuit-Wiring-1.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">Learn more about the CYD Pinout: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cheap-yellow-display-cyd-pinout-esp32-2432s028r\/\">ESP32 Cheap Yellow Display (CYD) Pinout (ESP32-2432S028R)<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Displaying Temperature on ESP32 CYD using LVGL &#8211; Arduino Code<\/h2>\n\n\n\n<p>The following code will get the temperature from the DS18B20 sensor and display it on a text label and curved gauge on the screen. The text color will vary depending on the temperature value.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n    CYD Project Details: https:\/\/RandomNerdTutorials.com\/esp32-cyd-lvgl-temperature-ds18b20\/\n    TFT Project Details: https:\/\/RandomNerdTutorials.com\/esp32-lvgl-ds18b20-tempreature-tft-display\/\n    \n    THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:\n    1) ESP32-2432S028R 2.8 inch 240\u00d7320 also known as the Cheap Yellow Display (CYD): https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\n      SET UP INSTRUCTIONS: https:\/\/RandomNerdTutorials.com\/cyd-lvgl\/\n    2) REGULAR ESP32 Dev Board + 2.8 inch 240x320 TFT Display: https:\/\/makeradvisor.com\/tools\/2-8-inch-ili9341-tft-240x320\/ and https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\n      SET UP INSTRUCTIONS: https:\/\/RandomNerdTutorials.com\/esp32-tft-lvgl\/\n    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\/\/  *** YOU MUST USE THE lv_conf.h AND User_Setup.h FILES PROVIDED IN THE NEXT LINKS IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS: https:\/\/RandomNerdTutorials.com\/cyd-lvgl\/ or https:\/\/RandomNerdTutorials.com\/esp32-tft-lvgl\/ \n\/\/  Install the &quot;lvgl&quot; library version 9.2 by kisvegabor to interface with the TFT Display - https:\/\/lvgl.io\/ - IMPORTANT: lv_conf.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials\n#include &lt;lvgl.h&gt;\n\n\/\/  Install the &quot;TFT_eSPI&quot; library by Bodmer to interface with the TFT Display - https:\/\/github.com\/Bodmer\/TFT_eSPI - IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials\n#include &lt;TFT_eSPI.h&gt;\n\n\/\/ Install OneWire and DallasTemperature libraries\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 27;     \n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ SET VARIABLE TO 0 FOR TEMPERATURE IN FAHRENHEIT DEGREES\n#define TEMP_CELSIUS 1    \n\n#if TEMP_CELSIUS\n  #define TEMP_ARC_MIN -20\n  #define TEMP_ARC_MAX 40\n#else\n  #define TEMP_ARC_MIN -4\n  #define TEMP_ARC_MAX 104\n#endif\n\n#define SCREEN_WIDTH 240\n#define SCREEN_HEIGHT 320\n\n#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT \/ 10 * (LV_COLOR_DEPTH \/ 8))\nuint32_t draw_buf[DRAW_BUF_SIZE \/ 4];\n\n\/\/ If logging is enabled, it will inform the user about what is happening in the library\nvoid log_print(lv_log_level_t level, const char * buf) {\n  LV_UNUSED(level);\n  Serial.println(buf);\n  Serial.flush();\n}\n\nlv_obj_t * arc;\n\n\/\/ Set the temperature value in the arc and text label\nstatic void set_temp(void * text_label_temp_value, int32_t v) {\n  sensors.requestTemperatures();\n  \/\/ Get the latest temperature reading in Celsius or Fahrenheit\n  #if TEMP_CELSIUS\n    float ds18b20_temp = sensors.getTempCByIndex(0);\n    if(ds18b20_temp &lt;= 10.0) {\n      lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_BLUE), 0);\n    }\n    else if (ds18b20_temp &gt; 10.0 &amp;&amp; ds18b20_temp &lt;= 29.0) {\n      lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_GREEN), 0);\n    }\n    else {\n      lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_RED), 0);\n    }\n    const char degree_symbol[] = &quot;\\u00B0C&quot;;\n  #else\n    float ds18b20_temp = sensors.getTempFByIndex(0);\n    if(ds18b20_temp &lt;= 50.0) {\n      lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_BLUE), 0);\n    }\n    else if (ds18b20_temp &gt; 50.0 &amp;&amp; ds18b20_temp &lt;= 84.2) {\n      lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_GREEN), 0);\n    }\n    else {\n      lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_RED), 0);\n    }\n    const char degree_symbol[] = &quot;\\u00B0F&quot;;\n  #endif\n\n  lv_arc_set_value(arc, map(int(ds18b20_temp), TEMP_ARC_MIN, TEMP_ARC_MAX, 0, 100));\n\n  String ds18b20_temp_text = String(ds18b20_temp) + degree_symbol;\n  lv_label_set_text((lv_obj_t*) text_label_temp_value, ds18b20_temp_text.c_str());\n  Serial.print(&quot;Temperature: &quot;);\n  Serial.println(ds18b20_temp_text);\n}\n\nvoid lv_create_main_gui(void) {\n  \/\/ Create an Arc\n  arc = lv_arc_create(lv_screen_active());\n  lv_obj_set_size(arc, 210, 210);\n  lv_arc_set_rotation(arc, 135);\n  lv_arc_set_bg_angles(arc, 0, 270);\n  lv_obj_set_style_arc_color(arc, lv_color_hex(0x666666), LV_PART_INDICATOR);\n  lv_obj_set_style_bg_color(arc, lv_color_hex(0x333333), LV_PART_KNOB);\n  lv_obj_align(arc, LV_ALIGN_CENTER, 0, 10);\n\n  \/\/ Create a text label in font size 32 to display the latest temperature reading\n  lv_obj_t * text_label_temp_value = lv_label_create(lv_screen_active());\n  lv_label_set_text(text_label_temp_value, &quot;--.--&quot;);\n  lv_obj_align(text_label_temp_value, LV_ALIGN_CENTER, 0, 10);\n  static lv_style_t style_temp;\n  lv_style_init(&amp;style_temp);\n  lv_style_set_text_font(&amp;style_temp, &amp;lv_font_montserrat_32);\n  lv_obj_add_style(text_label_temp_value, &amp;style_temp, 0);\n\n  \/\/ Create an animation to update with the latest temperature value every 10 seconds\n  lv_anim_t a_temp;\n  lv_anim_init(&amp;a_temp);\n  lv_anim_set_exec_cb(&amp;a_temp, set_temp);\n  lv_anim_set_duration(&amp;a_temp, 1000000);\n  lv_anim_set_playback_duration(&amp;a_temp, 1000000);\n  lv_anim_set_var(&amp;a_temp, text_label_temp_value);\n  lv_anim_set_values(&amp;a_temp, 0, 100);\n  lv_anim_set_repeat_count(&amp;a_temp, LV_ANIM_REPEAT_INFINITE);\n  lv_anim_start(&amp;a_temp);\n}\n\nvoid setup() {\n  String LVGL_Arduino = String(&quot;LVGL Library Version: &quot;) + lv_version_major() + &quot;.&quot; + lv_version_minor() + &quot;.&quot; + lv_version_patch();\n  Serial.begin(115200);\n  Serial.println(LVGL_Arduino);\n\n  \/\/ Start the DS18B20 sensor\n  sensors.begin();\n  \n  \/\/ Start LVGL\n  lv_init();\n  \/\/ Register print function for debugging\n  lv_log_register_print_cb(log_print);\n\n  \/\/ Create a display object\n  lv_display_t * disp;\n  \/\/ Initialize the TFT display using the TFT_eSPI library\n  disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));\n  lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270);\n    \n  \/\/ Function to draw the GUI\n  lv_create_main_gui();\n}\n\nvoid loop() {\n  lv_task_handler();  \/\/ let the GUI do its work\n  lv_tick_inc(5);     \/\/ tell LVGL how much time has passed\n  delay(5);           \/\/ let this time pass\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/raw\/main\/examples\/ESP32_LVGL_DS18B20_Temperature\/ESP32_LVGL_DS18B20_Temperature.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does the Code Work?<\/h2>\n\n\n\n<p>Let\u2019s take a look at how to get temperature from the DS18B20 sensor and update the text label and gauge periodically with the latest reading.<\/p>\n\n\n\n<p>Alternatively, you can skip to the <a href=\"#demonstration\" title=\"\">Demonstration <\/a>section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Including Libraries<\/h3>\n\n\n\n<p>In all your sketches, you need to include the <span class=\"rnthl rntliteral\">lvgl.h<\/span> and the <span class=\"rnthl rntliteral\">TFT_eSPI.h<\/span> libraries to display on the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;lvgl.h&gt;\n#include &lt;TFT_eSPI.h&gt;<\/code><\/pre>\n\n\n\n<p>You need to include the <span class=\"rnthl rntliteral\">OneWire<\/span> and the <span class=\"rnthl rntliteral\">DallasTemperature<\/span> libraries to interface with the DS18B20 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Declaring the DS18B20 Sensor<\/h3>\n\n\n\n<p>The data pin of the DS18B20 sensor is connected to GPIO 27.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 27;    <\/code><\/pre>\n\n\n\n<p>We create a <span class=\"rnthl rntliteral\">OneWire<\/span> instance on that GPIO.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);<\/code><\/pre>\n\n\n\n<p>Finally, we create a <span class=\"rnthl rntliteral\">DallasTemperature<\/span> object called <span class=\"rnthl rntliteral\">sensors<\/span> to refer to our sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Celsius or Fahrenheit<\/h3>\n\n\n\n<p>Our code is prepared to display the temperature in Celsius or Fahrenheit degrees. To choose your desired unit, you can set the value of the <span class=\"rnthl rntliteral\">TEMP_CELSIUS<\/span> variable. It is set to <span class=\"rnthl rntliteral\">1<\/span> by default to display the temperature in Celsius degrees.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define TEMP_CELSIUS 1 &nbsp; &nbsp;<\/code><\/pre>\n\n\n\n<p>If you want to display in Fahrenheit degrees instead, set it to <span class=\"rnthl rntliteral\">0<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define TEMP_CELSIUS 0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Gauge Minimum and Maximum Values<\/h3>\n\n\n\n<p>On the following variables we define the minimum and maximum values for the gauge. For the temperature in Celsius, we&#8217;re defining a minimum of -20\u00baC and a maximum of 40\u00baC. For the temperature in Fahrenheit, we&#8217;re defining the minimum for -4\u00baF and the maximum for 104\u00baF.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#if TEMP_CELSIUS\n  #define TEMP_ARC_MIN -20\n  #define TEMP_ARC_MAX 40\n#else\n  #define TEMP_ARC_MIN -4\n  #define TEMP_ARC_MAX 104\n#endif<\/code><\/pre>\n\n\n\n<p>You can adjust those values depending on the temperature range you&#8217;ll be reading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining the Display Width and Height<\/h3>\n\n\n\n<p>You need to define your display width and height in all your sketches that use LVGL. If you\u2019re using the recommended display, the size is 240&#215;320.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 241\n#define SCREEN_HEIGHT 320<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Drawing Buffer<\/h3>\n\n\n\n<p>You need to create a buffer to draw on the display as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT \/ 10 * (LV_COLOR_DEPTH \/ 8))\nuint32_t draw_buf&#091;DRAW_BUF_SIZE \/ 4];<\/code><\/pre>\n\n\n\n<p>You should also include this in all LVGL examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Debugging Function<\/h3>\n\n\n\n<p>For debugging with the LVGL library, you should use the <span class=\"rnthl rntliteral\">log_print()<\/span> function. It is defined below. Include it in all your sketches before the <span class=\"rnthl rntliteral\">setup()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ If logging is enabled, it will inform the user about what is happening in the library\nvoid log_print(lv_log_level_t level, const char * buf) {\n  LV_UNUSED(level);\n  Serial.println(buf);\n  Serial.flush();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Global Gauge\/Arc Object<\/h3>\n\n\n\n<p>We create a global LVGL object <span class=\"rnthl rntliteral\">arc<\/span> (that&#8217;s the curved gauge) so that we can access it inside all functions later on.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_obj_t * arc;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, include the following lines for debugging. These will print the version of LVGL that you\u2019re using. You must be using version 9.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String LVGL_Arduino = String(\"LVGL Library Version: \") + lv_version_major() + \".\" + lv_version_minor() + \".\" + lv_version_patch();\nSerial.begin(115200);\nSerial.println(LVGL_Arduino);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize the DS18B20 Sensor<\/h4>\n\n\n\n<p>Intialize the DS18B20 sensor in the <span class=\"rnthl rntliteral\">setup()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Start the DS18B20 sensor\nsensors.begin();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize the LVGL Library<\/h4>\n\n\n\n<p>Initialize the LVGL Library by calling the <span class=\"rnthl rntliteral\">lv_init()<\/span> function in the <span class=\"rnthl rntliteral\">setup()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Start LVGL\nlv_init();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Register Debugging Function<\/h4>\n\n\n\n<p>Register your <span class=\"rnthl rntliteral\">log_print()<\/span> function declared previously as a function associated with debugging LVGL.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Register print function for debugging\nlv_log_register_print_cb(log_print);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create a Display Object<\/h4>\n\n\n\n<p>To write to the display, you must create a display object first. You need to do this in all your LVGL sketches. The following lines will create an LVGL display object called <span class=\"rnthl rntliteral\">disp<\/span> with the screen width, screen height, and drawing buffer defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Create a display object\nlv_display_t * disp;\n\/\/ Initialize the TFT display using the TFT_eSPI library\ndisp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));\nlv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Drawing the GUI<\/h3>\n\n\n\n<p>The LVGL library works asynchronously. You must call the function to draw on the display in the <span class=\"rnthl rntliteral\">setup()<\/span>. Then, everything works with events and callbacks. The code will always be listening for events in the background. When something happens, it will run the callback function associated with the event. You don\u2019t need to check for any events in the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<p>Throughout most of our examples, the function that will draw to the screen will be called <span class=\"rnthl rntliteral\">lv_create_main_gui()<\/span>. Then, inside that function, we\u2019ll add the instructions to build the interface.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Function to draw the GUI\nlv_create_main_gui();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating an Arc (Curved Gauge)<\/h4>\n\n\n\n<p>To create an arc, we can call the LVGL function <span class=\"rnthl rntliteral\">lv_arc_create()<\/span> and pass as argument where we want to display the arc. We want to add it to the current screen (<span class=\"rnthl rntliteral\">lv_screen_active()<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>arc = lv_arc_create(lv_screen_active());<\/code><\/pre>\n\n\n\n<p>Set the arc size by using the <span class=\"rnthl rntliteral\">lv_obj_set_size()<\/span> function. This function is used to set the size of LVGL objects. Pass as arguments: the object you&#8217;re referring to, the width and the height.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_obj_set_size(arc, 210, 210);<\/code><\/pre>\n\n\n\n<p>Set the arc rotation (<span class=\"rnthl rntliteral\">lv_arc_set_rotation()<\/span>) and maximum and minimum angles for the indicator that will be drawn in the arc (<span class=\"rnthl rntliteral\">lv_arc_set_bg_angle()<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_arc_set_rotation(arc, 135);\nlv_arc_set_bg_angles(arc, 0, 270);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Styling the Arc<\/h4>\n\n\n\n<p>We can set the color of the indicator arc using the <span class=\"rnthl rntliteral\">lv_object_set_style_arc_color()<\/span> that is used to define the color of LVGL arches. Pass as arguments: the LVGL arc, the color, and the part of the arc that you want to set the color, in our case, it&#8217;s the indicator (<span class=\"rnthl rntliteral\">LV_PART_INDICATOR<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code> lv_obj_set_style_arc_color(arc, lv_color_hex(0x666666), LV_PART_INDICATOR);<\/code><\/pre>\n\n\n\n<p>To apply any color, you can use the <span class=\"rnthl rntliteral\">lv_color_hex()<\/span> function and pass as argument the color\u2019s hex code.<\/p>\n\n\n\n<p>You can also set the color of the knob as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_obj_set_style_bg_color(arc, lv_color_hex(0x333333), LV_PART_KNOB);<\/code><\/pre>\n\n\n\n<p>Finally, set the alignment of the arc. To do that, you can use the <span class=\"rnthl rntliteral\">lv_obj_align()<\/span> function. Pass as arguments, the LVGL object, the alignment and x and y offsets in pixels.<\/p>\n\n\n\n<pre id=\"language-c\" class=\"wp-block-code language-c\"><code>lv_obj_align(arc, LV_ALIGN_CENTER, 0, 10);<\/code><\/pre>\n\n\n\n<p>You can use the following image as a reference to place your object on the screen.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"419\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/LVGL_object_alignment.png?resize=760%2C419&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"LVGL Set Object's alignment\" class=\"wp-image-159248\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/LVGL_object_alignment.png?w=760&amp;quality=100&amp;strip=all&amp;ssl=1 760w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/LVGL_object_alignment.png?resize=300%2C165&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><\/figure><\/div>\n\n\n<p>In our case, we\u2019re aligning it at the center, so we use LV_ALIGN_<strong>CENTER<\/strong>.<\/p>\n\n\n\n<p>If you wanted to place it at the top right corner, you would use <strong>LV_ALIGN_TOP_RIGHT<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Displaying Text on the Display<\/h4>\n\n\n\n<p>Inside the <span class=\"rnthl rntliteral\">lv_create_main_gui()<\/span> function, we also add the instructions to write text on the screen.<\/p>\n\n\n\n<p>A text is a <strong>label<\/strong> object. The following lines create an LVGL label object called <span class=\"rnthl rntliteral\">text_label_temp_value<\/span>. We use the <span class=\"rnthl rntliteral\">lv_label_create()<\/span> function and we pass as argument <span class=\"rnthl rntliteral\">lv_screen_active()<\/span> to add the label to the current screen.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_obj_t * text_label_temp_value = lv_label_create(lv_screen_active());<\/code><\/pre>\n\n\n\n<p>After creating the text label, we can set its text by using the <span class=\"rnthl rntliteral\">lv_label_set_text()<\/span> function that accepts as arguments the text label we\u2019re referring to and the text we want to add to that label. In our case, we\u2019re setting the text to <span class=\"rnthl rntliteral\">&#8211;.&#8211;<\/span> because when the display first starts, there isn&#8217;t a reading from the sensor yet. You can display any other text.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_label_set_text(text_label_temp_value, \"--.--\");<\/code><\/pre>\n\n\n\n<p>The following lines align the text label.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_obj_align(text_label_temp_value, LV_ALIGN_CENTER, 0, 10);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Styling the Label Object<\/h4>\n\n\n\n<p>To style our text label, we\u2019ll start by creating an object of type <span class=\"rnthl rntliteral\">lv_style_t<\/span> called <span class=\"rnthl rntliteral\">style_temp<\/span>. This kind of object is used to apply styles to LVGL objects.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>static lv_style_t style_temp;<\/code><\/pre>\n\n\n\n<p>After creating the style, we initialize it using the <span class=\"rnthl rntliteral\">lv_style_init()<\/span> function and pass as argument our text label style (<span class=\"rnthl rntliteral\">style_temp<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_style_init(&amp;style_temp);<\/code><\/pre>\n\n\n\n<p>Then, we can set the font type and size using the <span class=\"rnthl rntliteral\">lv_style_set_text_font()<\/span> function. We pass as argument the style object we\u2019re referring to and the font type.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_style_set_text_font(&amp;style_temp, &amp;lv_font_montserrat_32);<\/code><\/pre>\n\n\n\n<p>The font type name already includes the font size. We\u2019re using <span class=\"rnthl rntliteral\">lv_font_montserrat_32<\/span>, which refers to Montserrat font in size 32. A list of built-in font types can be found in the link below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.lvgl.io\/master\/overview\/font.html#built-in-fonts\" target=\"_blank\" rel=\"noopener\" title=\"\">LVGL built-in fonts<\/a><\/li>\n<\/ul>\n\n\n\n<p>Once we\u2019ve defined all the properties of our style, we can finally apply it to our <span class=\"rnthl rntliteral\">text_label_temp_value<\/span> using the <span class=\"rnthl rntliteral\">lv_obj_add_style()<\/span> function as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_obj_add_style(text_label_temp_value, &amp;style_temp, 0);<\/code><\/pre>\n\n\n\n<p>&nbsp;The <span class=\"rnthl rntliteral\">0<\/span> indicates that we want to add the style to the <span class=\"rnthl rntliteral\">text_label_temp_value<\/span> object as a whole and not just some parts of the <span class=\"rnthl rntliteral\">text_label_temp_value<\/span>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Creating an Animation &#8211; Updating the Temperature<\/h4>\n\n\n\n<p>To update the temperature on the screen both on the text label and arc, we can create an animation that will run a specific function periodically. In this case, we\u2019ll update it every 10 seconds.<\/p>\n\n\n\n<p>First, create an animation called <span class=\"rnthl rntliteral\">a_temp<\/span> and associate a callback function to that animation. In this case, the <span class=\"rnthl rntliteral\">set_temp()<\/span> callback function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_anim_t a_temp;\nlv_anim_init(&amp;a_temp);\nlv_anim_set_exec_cb(&amp;a_temp, set_temp);<\/code><\/pre>\n\n\n\n<p>Set how frequently you want to run the <span class=\"rnthl rntliteral\">set_temp()<\/span> function. In our case, we\u2019re setting it to 10 seconds (this function will update the screen with the newest data).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_anim_set_duration(&amp;a_temp, 1000000);\nlv_anim_set_playback_duration(&amp;a_temp, 1000000);<\/code><\/pre>\n\n\n\n<p>You can pass a variable to the callback function by using the <span class=\"rnthl rntliteral\">lv_anim_set_var()<\/span> function. In our case, we want to access the <span class=\"rnthl rntliteral\">text_label_temp_value<\/span>, which is the variable we want to update.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_anim_set_var(&amp;a_temp, text_label_temp_value);<\/code><\/pre>\n\n\n\n<p>Then, you need to add the following lines to make the animation work and run forever as long as the program is running.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_anim_set_var(&amp;a_temp, text_label_temp_value);\nlv_anim_set_values(&amp;a_temp, 0, 100);\nlv_anim_set_repeat_count(&amp;a_temp, LV_ANIM_REPEAT_INFINITE);\nlv_anim_start(&amp;a_temp);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Updating the Temperature Label \u2013 The Callback Function<\/h3>\n\n\n\n<p>Let\u2019s now take a look at the <span class=\"rnthl rntliteral\">set_temp()<\/span> callback function that will run every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>static void set_temp(void * text_label_temp_value, int32_t v) {<\/code><\/pre>\n\n\n\n<p>Frist, we check if we need to display the temperature in Celsius or Fahrenheit degrees depending on the value of the <span class=\"rnthl rntliteral\">TEMP_CELSIUS<\/span> variable.<\/p>\n\n\n\n<p>We get the temperature from the DS18B20 and save it in the <span class=\"rnthl rntliteral\">ds18b20_temp<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float ds18b20_temp = sensors.getTempCByIndex(0);<\/code><\/pre>\n\n\n\n<p>We then, check in which range the temperature value is to set its color accordingly.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.requestTemperatures();\n\/\/ Get the latest temperature reading in Celsius or Fahrenheit\n#if TEMP_CELSIUS\n  float ds18b20_temp = sensors.getTempCByIndex(0);\n  if(ds18b20_temp &lt;= 10.0) {\n    lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_BLUE), 0);\n  }\n  else if (ds18b20_temp &gt; 10.0 &amp;&amp; ds18b20_temp &lt;= 29.0) {\n    lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_GREEN), 0);\n  }\n  else {\n    lv_obj_set_style_text_color((lv_obj_t*) text_label_temp_value, lv_palette_main(LV_PALETTE_RED), 0);\n  }<\/code><\/pre>\n\n\n\n<p>We want to display the degree symbol on the screen. It is an unicode character and we can get it as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char degree_symbol&#091;] = \"\\u00B0F\";<\/code><\/pre>\n\n\n\n<p>More information about <a href=\"https:\/\/www.compart.com\/en\/unicode\/U+00B0\" target=\"_blank\" rel=\"noopener\" title=\"\">unicode characters<\/a>.<\/p>\n\n\n\n<p>We update the value displayed on the arc by calling the <span class=\"rnthl rntliteral\">lv_arc_set_value()<\/span> function. Pass as an argument, the <span class=\"rnthl rntliteral\">arc<\/span> object, and the value. It accepts a value from 0 to 100%. So, we use the <span class=\"rnthl rntliteral\">map()<\/span> function to map the current temperature to that range taking into account the minimum and maximum values we defined at the beginning of the code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_arc_set_value(arc, map(int(ds18b20_temp), TEMP_ARC_MIN, TEMP_ARC_MAX, 0, 100));<\/code><\/pre>\n\n\n\n<p>The arc will now be updated with the latest temperature reading. Now, we need to update the text label.<\/p>\n\n\n\n<p>We create a string with the current temperature value concatenated with the degree symbol.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String ds18b20_temp_text = String(ds18b20_temp) + degree_symbol;<\/code><\/pre>\n\n\n\n<p>Finally, set the <span class=\"rnthl rntliteral\">text_label_temp_value<\/span> text to that string using the <span class=\"rnthl rntliteral\">lv_label_set_text()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_label_set_text((lv_obj_t*) text_label_temp_value, ds18b20_temp_text.c_str());<\/code><\/pre>\n\n\n\n<p>We also print in the Serial Monitor for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Temperature: \");\nSerial.println(ds18b20_temp_text);<\/code><\/pre>\n\n\n\n<p>And that\u2019s how you update the arc value and a text label using an LVGL animation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, you can add any other tasks that you need your ESP32 to do like in any regular Arduino sketch. In our case, we won\u2019t add any tasks to the <span class=\"rnthl rntliteral\">loop()<\/span>, but to keep LVGL running and detecting events, you always need to add the following lines to your <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() {\n  lv_task_handler();  \/\/ let the GUI do its work\n  lv_tick_inc(5);         \/\/ tell LVGL how much time has passed\n  delay(5);                 \/\/ let this time pass\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your board. Go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select <strong>ESP32 <\/strong>&gt; <strong>ESP32 Dev Module<\/strong>. Then, select the right COM port in <strong>Tools <\/strong>&gt; <strong>Port<\/strong>. Finally, click the upload button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"36\" height=\"39\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-ide-2-upload-button.png?resize=36%2C39&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE 2 Upload Button\" class=\"wp-image-146269\" style=\"width:36px;height:auto\"\/><\/figure><\/div>\n\n\n<p>After a few seconds, the temperature will be displayed on the screen as shown in the picture below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Temperature-Arduino-IDE.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Cheap Yellow Display LVGL DS18B20 Temperature Arduino IDE\" class=\"wp-image-159222\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Temperature-Arduino-IDE.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Temperature-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The color of the text will change depending on the temperature range.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Temperature-High-Demonstration.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Cheap Yellow Display LVGL DS18B20 Temperature High Demonstration\" class=\"wp-image-159223\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Temperature-High-Demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-TFT-Cheap-Yellow-Display-LVGL-DS18B20-Temperature-High-Demonstration.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>At the same time, the temperature will be displayed on the Serial Monitor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"320\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-LVGL-Display-DS18B20-Temperature.png?resize=666%2C320&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Display DS18B20 Temperature on ESP32 CYD Display Serial Monitor\" class=\"wp-image-159247\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-LVGL-Display-DS18B20-Temperature.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-LVGL-Display-DS18B20-Temperature.png?resize=300%2C144&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned to display sensor data on the Cheap Yellow display screen using the LVGL library. You learned how to display text and create a curved gauge.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. We&#8217;re preparing more guides about this board, so stay tuned. If you would like to learn more about creating graphical user interfaces using the LVGL library with the ESP32, check out our latest eBook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/learn-lvgl-esp32-ebook\/\">Learn LVGL: Build GUIs for ESP32 Projects (eBook)<\/a><\/strong><\/li>\n<\/ul>\n\n\n\n<p>Other guides you might like reading:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/cheap-yellow-display-esp32-2432s028r\/\">Getting Started with ESP32 Cheap Yellow Display Board \u2013 CYD (ESP32-2432S028R)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/touchscreen-on-off-button-cheap-yellow-display-esp32-2432s028r\/\">ESP32 Touchscreen On\/Off Button \u2013 Cheap Yellow Display (ESP32-2432S028R)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cheap-yellow-display-cyd-pinout-esp32-2432s028r\/\">ESP32 Cheap Yellow Display (CYD) Pinout (ESP32-2432S028R)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/lvgl-cheap-yellow-display-esp32-2432s028r\/\">LVGL with ESP32 Cheap Yellow Display Board (ESP32-2432S028R)<\/a><\/li>\n<\/ul>\n\n\n\n<p>To learn more about the ESP32, make sure to take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\">SMART HOME with Raspberry Pi, ESP32, and ESP8266 eBook<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\" title=\"\"><strong>Free ESP32 Guides and Tutorials<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to display temperature from the DS18B20 sensor on an ESP32 Cheap Yellow Display (CYD) using LVGL (Light Versatile Graphics Library). We&#8217;ll display the temperature &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 CYD with LVGL: Display Temperature with DS18B20 Sensor (Text and Arc)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-cyd-lvgl-temperature-ds18b20\/#more-159220\" aria-label=\"Read more about ESP32 CYD with LVGL: Display Temperature with DS18B20 Sensor (Text and Arc)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":159257,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-159220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Cheap-Yellow-Display-Temperature-DS18B20.png?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/159220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=159220"}],"version-history":[{"count":25,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/159220\/revisions"}],"predecessor-version":[{"id":162166,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/159220\/revisions\/162166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/159257"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=159220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=159220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=159220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}