# Lessons Learned with Arduino

**URL:** <http://forum.squareline.io/t/lessons-learned-with-arduino/1577>\
**Category:** Uncategorized\
**Created:** [July 28, 2023, 3:44pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577 "2023-07-28T15:44:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcuervo](http://forum.squareline.io/user_avatar/forum.squareline.io/bcuervo/32/772_2.png) [@bcuervo](http://forum.squareline.io/u/bcuervo)\
**Post date:** [July 28, 2023, 3:44pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577/1 "2023-07-28T15:44:52Z")

</div>

I’m excited to use to this relatively new display editor. I’ve used Nextion boards and their editor in the past but it limited me to only the Nextion displays and their editor is not as advanced.  
My current setup is an Adafruit ESP32 Feather board and a Waveshare round display using the GC9A01 driver.  
It did take me hours and a lot of head scratching/research to get my first display uploaded properly so I want to share some lessons learned to save others the hassle.

1. **Don’t include spaces in your project name!!** I had named my project “Round Display” and kept getting odd compile errors in Arduino. I renamed my project “Round\_Display” and that resolved the issue.
2. The SLS basic tutorial video #6 shows how to export a project to Arduino but it doesn’t fully apply to the latest version of SLS. The video shows the UI folder (with the UI.ino file) containing multiple .h and .c files but when I create a template project those files don’t appear in that folder. I thought this could have been the cause of the compile errors but I now know the file structure has been changed in these later releases and the .ino file is supposed to be in a folder by itself.
3. The default color depth is 32 bit. When I finally got a successful compile and download, the display image was a mess. Changing the depth to 16 bit yielded the proper image. **You also have to change it in the lv\_conf.h file, line 30.**
4. To update your display with any changes, click on “Export” and “Export UI files”. Then go back to your Arduino UI.ino sketch and upload it again. No need to close and reopen the sketch.

Hope this helps others to get over that initial learning curve with Arduino.

 ![round display](http://forum.squareline.io/uploads/default/original/1X/f461936031d56386ffc498ff7c7b258b49acd95b.jpeg)

---

<div class="post-metadata">

**Author:** ![bcuervo](http://forum.squareline.io/user_avatar/forum.squareline.io/bcuervo/32/772_2.png) [@bcuervo](http://forum.squareline.io/u/bcuervo)\
**Post date:** [August 1, 2023, 3:54pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577/2 "2023-08-01T15:54:05Z")

</div>

Now I’ve got a 2.8", ILI9341 TFT display connected to my ESP32 and learning how to get data from the microcontroller. After searching different methods, the only one I’ve gotten to work is shown below.  
I’ve declared the “temp” variable above void setup() and have a random number generator in the loop.

 ![UI code pic](http://forum.squareline.io/uploads/default/original/1X/cde8e728df3b6941033a57898aa0341a01e74c5b.jpeg)

You can compare the code to the SLS UI to see how to incorporate the name of the label.

 ![SLS pic](http://forum.squareline.io/uploads/default/original/1X/68f7b3ce915668fed72a785d72ed0cd13c874c19.jpeg)

Here is the physical setup:

 ![Display](http://forum.squareline.io/uploads/default/original/1X/8bd03d3db6a1fbd380dc9809a7a231553097e8c4.jpeg)

---

<div class="post-metadata">

**Author:** ![bcuervo](http://forum.squareline.io/user_avatar/forum.squareline.io/bcuervo/32/772_2.png) [@bcuervo](http://forum.squareline.io/u/bcuervo)\
**Post date:** [August 3, 2023, 12:11pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577/3 "2023-08-03T12:11:04Z")

</div>

**Touchscreen integration**

It looks like this is the biggest hurdle to using SLS with a display and now I’m stuck. My display has an FT6206 touchscreen driver. SLS uses the TFT\_eSPI library and this library only supports the XPT2046 touchscreen driver. To use a “non-native” touchscreen driver (like mine) I’d have to add the driver library to the SLS folder and somehow link it to the program. My programming skills are pretty basic and I’ve not found any examples on how to do this. So, long story short…  
The choice of touchscreen displays that work with the libraries included with SLS is very limited 😔

~Bryan

---

<div class="post-metadata">

**Author:** ![bcuervo](http://forum.squareline.io/user_avatar/forum.squareline.io/bcuervo/32/772_2.png) [@bcuervo](http://forum.squareline.io/u/bcuervo)\
**Post date:** [August 8, 2023, 6:23pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577/4 "2023-08-08T18:23:38Z")

</div>

I’m taking a step backward to understand and get a touch screen working. I bought a cheap, basic touch screen that is known to work well with the TFT\_eSPI library. The display driver is ILI9341 and the touch driver is XPT 2046. There is a lot of documentation on these and they cost less than $20.00.  
The SLS ui.ino sketch was slightly modified to show the live X and Y touch locations while the SLS display is running on the screen so I can see the touch values corresponding to the button locations.  
The next step will be to use the TFT\_eSPI on-off button example as a guide for bounding the X,Y touch positions of the SLS buttons to tell when a button has been pushed. Not an elegant way to get functionality with the SLS software but I’ve found limited documentation/examples for incorporating a touch screen with SLS within the Arduino environment.

 ![ILI9341 wiring](http://forum.squareline.io/uploads/default/original/1X/ada2663a9373029c41bd67c74cda975a8cd9c24c.jpeg)

---

<div class="post-metadata">

**Author:** ![bcuervo](http://forum.squareline.io/user_avatar/forum.squareline.io/bcuervo/32/772_2.png) [@bcuervo](http://forum.squareline.io/u/bcuervo)\
**Post date:** [August 8, 2023, 8:11pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577/5 "2023-08-08T20:11:39Z")

</div>

Here is my manual technique for getting the button locations. I run my basic program that shows the SLS image and spits out the X and Y locations of where I touch. I’m touching the left lower corner of the decrease button and you can see the X,Y coordinates on the serial monitor…

 ![Button press](http://forum.squareline.io/uploads/default/original/1X/bdbc3a22b3416076c0c7e162714613805088f545.jpeg)

 ![Void loop](http://forum.squareline.io/uploads/default/original/1X/0bd7f52b93ed0ac4d711311a7c0e6c0461e831c9.jpeg)

---

<div class="post-metadata">

**Author:** ![bcuervo](http://forum.squareline.io/user_avatar/forum.squareline.io/bcuervo/32/772_2.png) [@bcuervo](http://forum.squareline.io/u/bcuervo)\
**Post date:** [August 9, 2023, 12:53pm UTC](http://forum.squareline.io/t/lessons-learned-with-arduino/1577/6 "2023-08-09T12:53:55Z")

</div>

With the ui.ino modified code shown below I can interact with my screen using the SLS events (for example; clicking on a button to change screens) and I can see live data on the X and Y coordinate touch positions as shown in the last post. Again, this only works if you are using the XPT 2046 touch driver and a TFT\_eSPI library recognized display driver. Also, you have to _ **uncomment the #define TOUCH\_CS line** _ or the code won’t even compile. I’ve put in a request to SLS to make a note of this in their readme file. The touch extensions in the library can’t be accessed unless you uncomment that line. I spent a few frustrating hours trying to sort that out!

 ![image](http://forum.squareline.io/uploads/default/original/1X/53ad06868d1571d6811c11fc4baa6f2aba2bcf7e.png)

#include \<lvgl.h\>  
#include \<TFT\_eSPI.h\>  
#include \<ui.h\>

/_Don’t forget to set Sketchbook location in File/Preferencesto the path of your UI project (the parent foder of this INO file)_/

/_Change to your screen resolution_/  
static const uint16\_t screenWidth = 320;  
static const uint16\_t screenHeight = 240;  
uint16\_t touchX = 0, touchY = 0;

static lv\_disp\_draw\_buf\_t draw\_buf;  
static lv\_color\_t buf[screenWidth \* screenHeight / 10];

TFT\_eSPI tft = TFT\_eSPI(screenWidth, screenHeight); /\* TFT instance \*/

#if LV\_USE\_LOG != 0  
/\* Serial debugging \*/  
void my\_print(const char \* buf)  
{  
Serial.printf(buf);  
Serial.flush();  
}  
#endif

/\* Display flushing \*/  
void my\_disp\_flush( lv\_disp\_drv\_t \*disp, const lv\_area\_t \*area, lv\_color\_t \*color\_p )  
{  
uint32\_t w = ( area-\>x2 - area-\>x1 + 1 );  
uint32\_t h = ( area-\>y2 - area-\>y1 + 1 );

```
tft.startWrite();
tft.setAddrWindow( area->x1, area->y1, w, h );
tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
tft.endWrite();

lv_disp_flush_ready( disp );

```

}

/_Read the touchpad_/  
void my\_touchpad\_read( lv\_indev\_drv\_t \* indev\_driver, lv\_indev\_data\_t \* data )  
{  
//uint16\_t touchX = 0, touchY = 0;

```
bool touched = tft.getTouch( &touchX, &touchY, 600 );

if( !touched )
{
    data->state = LV_INDEV_STATE_REL;
}
else
{
    data->state = LV_INDEV_STATE_PR;

    /*Set the coordinates*/
    data->point.x = touchX;
    data->point.y = touchY;

    Serial.print( "Data x " );
    Serial.println( touchX );

    Serial.print( "Data y " );
    Serial.println( touchY );
}

```

}

void setup()  
{  
Serial.begin( 115200 ); /\* prepare for possible serial debug \*/

```
String LVGL_Arduino = "Hello Arduino! ";
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();

Serial.println( LVGL_Arduino );
Serial.println( "I am LVGL_Arduino" );

lv_init();

```

#if LV\_USE\_LOG != 0  
lv\_log\_register\_print\_cb( my\_print ); /\* register print function for debugging \*/  
#endif

```
tft.begin(); /* TFT init */
tft.setRotation( 3 ); /* Landscape orientation, flipped */

lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * screenHeight / 10 );

/*Initialize the display*/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init( &disp_drv );
/*Change the following line to your display resolution*/
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register( &disp_drv );

/*Initialize the (dummy) input device driver*/
static lv_indev_drv_t indev_drv;
lv_indev_drv_init( &indev_drv );
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register( &indev_drv );

ui_init();

Serial.println( "Setup done" );

```

}

void loop()  
{  
lv\_timer\_handler(); /\* let the GUI do its work \*/  
delay(5);

```
tft.getTouch( &touchX, &touchY, 600 );

        Serial.print( "Data x " );
    Serial.println( touchX );

    Serial.print( "Data y " );
    Serial.println( touchY );

```

}
