2.4″ ILI9341 TFT Touch Screen with Arduino.

2.4" ILI9341 tft touch screen cover

TFT LCD modules are among the most popular devices in a number of microcontroller projects especially serial port modules because they take few I/O pins and their usage is generally simple. In this tutorial we look at how to interface the 2.4″ TFT LCD touch display with the ILI9341 driver with Arduino.

You can also have a look at other types of displays for reference from my other posts:

  • How to use the 1.44″ TFT ST7735 color display with Arduino.
  • Interfacing OLED displays with Arduino.
  • 2.4″ ILI9341 TFT LCD touch display description.

    This is not just a LCD break but also includes an SD card slot.It’s a 240 x 320 pixels resolution, 2.4 inch TFT LCD screen with touch ability and uses the ILI9341 driver The display uses serial interface and is controlled by 5 wires (CS, RS, SCL, SDA, RST) . The SD card also uses hardware SPI interface (CS / MOSI / MISO / SCK).

    2.4" ili9341 tft touch screen backside components

    Regular TFT LCD signal line.

    • VCC: 5V/3.3V power input
    • GND: Ground
    • CS: LCD chip select signal, low level enable
    • RESET: LCD reset signal, low level reset
    • DC: LCD register / data selection signal, high level: register, low level: data
    • MOSI: SPI bus write data signal
    • SCK: SPI bus clock signal
    • LED: Backlight control, high level lighting
    • MISO: SPI bus read data signal

    Touch screen signal line

    • T_CLK : Touch SPI bus clock signal
    • T_CS : Touch screen chip select signal, low level enable
    • T_DIN : Touch SPI bus input
    • T_DO : Touch SPI bus output
    • T_IRQ :Touch screen interrupt signal, low level when touch is detected

    You can buy this display using the link below:

    Interfacing the 2.4 ” TFT display with Arduino

    Lets first use the display as a regular TFT display without touch function. For this we only connect the first nine pins of the display.

    The SPI module’s pin can only input a 3.3V high level, while the Arduino output has a high level of 5V. However this display module is not 5V tolerant.

    This problem can be overcome by connecting the Arduino and the display module through an external level conversion module, so that The 5V high level of the Arduino output is converted to 3.3V by the level conversion module and then input to the display module. In case you don’t have such a conversion module you can just use some 10K resistors as shown in the schematic below.

    2.4" tft touch screen with Arduino normal use schematic
    regular tft lcd pin connections

    Code for running the ILI9314 TFT Display.

    A number of libraries have been developed to ease the use of this 2.4″ TFT LCD touch screen display with Arduino. However I found a number of interesting libraries with examples that can help beginners understand the working of this display at www.lcdwiki.com. I have shown how to download and use these resources in a video at the end of this tutorial.

    From the above link you can get LCDWIKI_GUI.hLCDWIKI_SPI.h and LCDWIKI_TOUCH.h libraries which are important for controlling the 2.4″ TFT LCD touch screen using ILI9341 drivers. You can use the example below to test the display.

    #include <LCDWIKI_GUI.h> //Core graphics library
    #include <LCDWIKI_SPI.h> //Hardware-specific library
    
    //paramters define
    #define MODEL ILI9341
    #define CS    A5    
    #define CD    A3
    #define RST   A4
    #define MOSI  11
    #define MISO  12
    #define SCK   13
    #define LED   A0   //if you don't need to control the LED pin,you should set it to -1 and set it to 3.3V
    
    //the definiens of software spi mode as follow:
    //if the IC model is known or the modules is unreadable,you can use this constructed
    function
    LCDWIKI_SPI mylcd(MODEL,CS,CD,MISO,MOSI,RST,SCK,LED); //model,cs,dc,miso,mosi,reset,sck,led
    //if the IC model is not known and the modules is readable,you can use this constructed function
    //LCDWIKI_SPI mylcd(240,320,CS,CD,MISO,MOSI,RST,SCK,LED); //width,height,cs,dc,miso,mosi,reset,sck,led
    
    //define some colour values
    #define  BLACK   0x0000
    #define BLUE    0x001F
    #define RED     0xF800
    #define GREEN   0x07E0
    #define CYAN    0x07FF
    #define MAGENTA 0xF81F
    #define YELLOW  0xFFE0
    #define WHITE   0xFFFF
    
    //clear screen
    void fill_screen_test()
    {
      mylcd.Fill_Screen(BLACK);  
      mylcd.Fill_Screen(RED);
     mylcd.Fill_Screen(GREEN);
      mylcd.Fill_Screen(BLUE);
      mylcd.Fill_Screen(BLACK);
    }
    
    //display some strings
    void text_test()
    {
      mylcd.Set_Text_Mode(0);
      
      mylcd.Fill_Screen(BLACK);  
      mylcd.Set_Text_Back_colour(BLACK);
      mylcd.Set_Text_colour(WHITE);
      mylcd.Set_Text_Size(1);
      mylcd.Print_String("Hello World!", 0, 0);
      
      mylcd.Set_Text_colour(YELLOW);
      mylcd.Set_Text_Size(2);
      mylcd.Print_Number_Float(1234.56,2,0, 8, '.', 0, ' ');
      
      mylcd.Set_Text_colour(RED);
      mylcd.Set_Text_Size(3);
      //mylcd.Print_String("DEADBEEF", 0, 24); 
     mylcd.Print_Number_Int(0xDEADBEF, 0, 24, 0, ' ', 16); 
    
      mylcd.Set_Text_colour(GREEN);
      mylcd.Set_Text_Size(5);  
      mylcd.Print_String("Groop", 0, 56); 
    
      mylcd.Set_Text_Size(2);
      mylcd.Print_String("I implore thee,", 0, 96);
    
      mylcd.Set_Text_Size(1);
      mylcd.Print_String("my foonting turlingdromes.", 0, 112);
      mylcd.Print_String("And hooptiously drangle me", 0, 120);
      mylcd.Print_String("with crinkly bindlewurdles,", 0, 128);
      mylcd.Print_String("Or I will rend thee", 0, 136);
      mylcd.Print_String("in the gobberwarts", 0, 144);
      mylcd.Print_String("with
    my blurglecruncheon,", 0, 152);
      mylcd.Print_String("see if I don't!", 0, 160);
    }
    
    //draw some oblique lines
    void lines_test(void)
    {
        mylcd.Fill_Screen(BLACK);
          mylcd.Set_Draw_color(GREEN);
        int i = 0;   
        for(i = 0; i< mylcd.Get_Display_Width();i+=5)
        {
           mylcd.Draw_Line(0, 0, i, mylcd.Get_Display_Height()-1);
         }
         for(i = mylcd.Get_Display_Height()-1; i>= 0;i-=5)
         {
           mylcd.Draw_Line(0, 0, mylcd.Get_Display_Width()-1, i);
         }
         
         mylcd.Fill_Screen(BLACK); 
     mylcd.Set_Draw_color(RED);
        for(i = mylcd.Get_Display_Width() -1; i>=0;i-=5)
        {
          mylcd.Draw_Line(mylcd.Get_Display_Width()-1, 0, i, mylcd.Get_Display_Height()-1);
         }
        for(i = mylcd.Get_Display_Height()-1; i>=0;i-=5)
        {
          mylcd.Draw_Line(mylcd.Get_Display_Width()-1, 0, 0, i);
         }
         
         mylcd.Fill_Screen(BLACK); 
          mylcd.Set_Draw_color(BLUE);
         for(i = 0; i < mylcd.Get_Display_Width();i+=5)
        {
          mylcd.Draw_Line(0, mylcd.Get_Display_He
    ight()-1, i, 0);
         }
         for(i = 0; i < mylcd.Get_Display_Height();i+=5)
        {
          mylcd.Draw_Line(0, mylcd.Get_Display_Height()-1, mylcd.Get_Display_Width()-1, i);
         }
    
         mylcd.Fill_Screen(BLACK);
          mylcd.Set_Draw_color(YELLOW);
         for(i = mylcd.Get_Display_Width()-1; i >=0;i-=5)
        {
     mylcd.Draw_Line(mylcd.Get_Display_Width()-1, mylcd.Get_Display_Height()-1, i, 0);
         }
         for(i = 0; i<mylcd.Get_Display_Height();i+=5)
        {
          mylcd.Draw_Line(mylcd.Get_Display_Width()-1, mylcd.Get_Display_Height()-1, 0, i);
         }
    }
    
    //draw some vertical lines and horizontal lines
    void h_l_lines_test(void)
    {
        int i=0;
       mylcd.Fill_Screen(BLACK);
         mylcd.Set_Draw_color(GREEN);
        for(i =0;i<mylcd.Get_Display_Height();i+=5)
        {
          mylcd.Draw_Fast_HLine(0,i,mylcd.Get_Display_Width()); 
          delay(5);
     }
         mylcd.Set_Draw_color(BLUE);
         for(i =0;i<mylcd.Get_Display_Width();i+=5)
        {
          mylcd.Draw_Fast_VLine(i,0,mylcd.Get_Display_Height()); 
               delay(5);
        }
    }
    
    //draw some rectangles
    void rectangle_test(void)
    {
      int i = 0;
       mylcd.Fill_Screen(BLACK);
         mylcd.Set_Draw_color(GREEN);
       for(i = 0;i<mylcd.Get_Display_Width()/2;i+=4)
       {
     mylcd.Draw_Rectangle(i,(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2+i,mylcd.Get_Display_Width()-1-i,mylcd.Get_Display_Height()-(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2-i);  
            delay(5);
       }
    }
    
    //draw some filled rectangles
    void fill_rectangle_test(void)
    {
     int i = 0;
       mylcd.Fill_Screen(BLACK);
         mylcd.Set_Draw_color(YELLOW);
       mylcd.Fill_Rectangle(0,(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2,mylcd.Get_Display_Width()-1,mylcd.Get_Display_Height()-(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2);
        mylcd.Set_Draw_color(MAGENTA);
       for(i = 0;i<mylcd.Get_Display_Width()/2;i+=4)
       {
     mylcd.Draw_Rectangle(i,(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2+i,mylcd.Get_Display_Width()-1-i,mylcd.Get_Display_Height()-(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2-i);  
            delay(5);
       }
       for(i = 0;i<mylcd.Get_Display_Width()/2;i+=4)
       {
      mylcd.Set_Draw_color(random(255), random(255), random(255));
          mylcd.Fill_Rectangle(i,(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2+i,mylcd.Get_Display_Width()-1-i,mylcd.Get_Display_Height()-(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2-i);  
            delay(5);
       }
    }
    
    //draw some filled circles
    void fill_circles_test(void)
    {
      int r=10,i=0,j=0;
      mylcd.Fill_Screen(BLACK);
       mylcd.Set_Draw_color(MAGENTA);
      for(i=r;i<mylcd.Get_Display_Width();i+=2*r)
      {
        for(j=r;j<mylcd.Get_Display_Height();j+=2*r)
        {
     mylcd.Fill_Circle(i, j, r);
        }
      }
    }
    
    //draw some circles
    void circles_test(void)
    {
      int r=10,i=0,j=0;
       mylcd.Set_Draw_color(GREEN);
      for(i=0;i<mylcd.Get_Display_Width()+r;i+=2*r)
      {
        for(j=0;j<mylcd.Get_Display_Height()+r;j+=2*r)
        {
          mylcd.Draw_Circle(i, j, r);
        }
      }  
    }
    
    //draw some triangles
    void triangles_test(void)
    {
       int i = 0;
       mylcd.Fill_Screen(BLACK);
       for(i=0;i<mylcd.Get_Display_Width()/2;i+=5)
       {
     mylcd.Set_Draw_color(0,i+64,i+64);
          mylcd.Draw_Triangle(mylcd.Get_Display_Width()/2-1,mylcd.Get_Display_Height()/2-1-i,
                        mylcd.Get_Display_Width()/2-1-i,mylcd.Get_Display_Height()/2-1+i,
                        mylcd.Get_Display_Width()/2-1+i,mylcd.Get_Display_Height()/2-1+i);                   
       }
    }
    
    //draw some filled triangles
    void fill_triangles_test(void)
    {
       int i = 0;
       mylcd.Fill_Screen(BLACK);
     for(i=mylcd.Get_Display_Width()/2-1;i>0;i-=5)
       {
        
          mylcd.Set_Draw_color(0,i+64,i+64);
          mylcd.Fill_Triangle(mylcd.Get_Display_Width()/2-1,mylcd.Get_Display_Height()/2-1-i,
                        mylcd.Get_Display_Width()/2-1-i,mylcd.Get_Display_Height()/2-1+i,
                        mylcd.Get_Display_Width()/2-1+i,mylcd.Get_Display_Height()/2-1+i);                   
          mylcd.Set_Draw_color(i,0,i);
          mylcd.Draw_Triangle(mylcd.Get_Display_Width()/2-1,mylcd.Get_Display_Height()/2-1-i,
     mylcd.Get_Display_Width()/2-1-i,mylcd.Get_Display_Height()/2-1+i,
                        mylcd.Get_Display_Width()/2-1+i,mylcd.Get_Display_Height()/2-1+i);                   
       }
    }
    
    //draw some round rectangles
    void round_rectangle(void)
    {
       int i = 0;
       mylcd.Fill_Screen(BLACK);
         for(i = 0;i<mylcd.Get_Display_Width()/2;i+=4)
       {
           mylcd.Set_Draw_color(255-i,0,160-i);
     mylcd.Draw_Round_Rectangle(i,(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2+i,mylcd.Get_Display_Width()-1-i,mylcd.Get_Display_Height()-(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2-i,8);
            delay(5);
       } 
    }
    
    //draw some filled round rectangles
    void fill_round_rectangle(void)
    {
         int i = 0;
       mylcd.Fill_Screen(BLACK);
         for(i = 0;i<mylcd.Get_Display_Width()/2;i+=4)
       {
           mylcd.Set_Draw_color(255-i,160-i,0);
     mylcd.Fill_Round_Rectangle(i,(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2+i,mylcd.Get_Display_Width()-1-i,mylcd.Get_Display_Height()-(mylcd.Get_Display_Height()-mylcd.Get_Display_Width())/2-i,8);
            delay(5);
       } 
    }
    
    void setup() 
    {
      mylcd.Init_LCD();
      fill_screen_test();
      delay(500);
      text_test();
      delay(500);
      lines_test();
      delay(500);
      h_l_lines_test();
      delay(500);
      rectangle_test();
      delay(500);
      fill_rectangle_test();
      delay(500);
      fill_circles_test();
      delay(500);
      circles_test();
      delay(500);
      triangles_test();
     delay(500);
      fill_triangles_test();
      delay(500);
       round_rectangle();
       delay(500);
       fill_round_rectangle();
       delay(3000);
    }
    
    void loop() 
    {
      //rotate for four directions
     for(uint8_t rotation=0; rotation<4; rotation++) 
     {
        mylcd.Set_Rotation(rotation);
        text_test();
        delay(2000);
     }
    }
    
    

    When the above code is uploaded to the Arduino, different graphics will be shown on the TFT display. You may observe that the graphics move slowly and this is because the processing power of 8 bit Arduino uno is only 2Kb of RAM which is low for driving a display with high resolution. Therefore we may need a more powerful microcontroller to drive this display.

    Using the touch functionality of the 2.4″ TFT ILI9341 display.

    To be able to use the touch functionality of the display, we connect the remaining five pins according to this schematic diagram below.

    2.4" ili9341 tft touch scree with arduino touch functionality schematic
    touch screen pin connections

    We can use the code below to test the touch functionality.When this code is uploaded to the Arduino we can be able to use a touch pen to write on the screen using text of different colors and thickness.

    Code for using Touch functionality of ILI9341 TFT Display.

    #include <LCDWIKI_GUI.h> //Core graphics library
    #include <LCDWIKI_SPI.h> //Hardware-specific library
    #include <LCDWIKI_TOUCH.h> //touch screen library
    
    //paramters define
    #define MODEL ILI9341
    #define CS   A5    
    #define CD   A3
    #define RST  A4
    #define MOSI  11
    #define MISO  12
    #define SCK   13
    #define LED  A0   //if you don't need to control the LED pin,you should set it to -1 and set it to 3.3V
    
    //touch screen paramters define
    #define TCS   2
    #define TCLK  3
    #define TDOUT 4
    #define TDIN  5
    #define TIRQ  6
    
    //the definiens of software spi mode as follow:
    //if the IC model is known or the modules is unreadable,you can use this constructed function
    LCDWIKI_SPI my_lcd(MODEL,CS,CD,MISO,MOSI,RST,SCK,LED); //model,cs,dc,miso,mosi,reset,sck,led
    //if the IC model is not known and the modules is readable,you can use this constructed function
    //LCDWIKI_SPI my_lcd(240,320,CS,CD,MISO,MOSI,RST,SCK,LED); //width,height,cs,dc,miso,mosi,reset,sck,led
    
    //the definiens of touch mode as follow:
    LCDWIKI_TOUCH my_touch(TCS,TCLK,TDOUT,TDIN,TIRQ); //tcs,tclk,tdout,tdin,tirq
    
    #define  BLACK   0x0000
    #define BLUE    0x001F
    #define RED     0xF800
    #define GREEN   0x07E0
    #define CYAN    0x07FF
    #define MAGENTA 0xF81F
    #define YELLOW  0xFFE0
    #define WHITE   0xFFFF
    uint16_t color_mask[] = {0xF800,0xFFE0,0x07E0,0x07FF,0x001F,0xF81F}; //color select
    
    #define COLORBOXSIZE my_lcd.Get_Display_Width()/6
    #define PENBOXSIZE my_lcd.Get_Display_Width()/4
    
    int16_t old_color, current_color,flag_colour;
    int16_t old_pen,current_pen,flag_pen;
    boolean show_flag = true;
    
    void show_string(uint8_t *str,int16_t x,int16_t y,uint8_t csize,uint16_t fc, uint16_t bc,boolean mode)
    {
        my_lcd.Set_Text_Mode(mode);
        my_lcd.Set_Text_Size(csize);
        my_lcd.Set_Text_colour(fc);
        my_lcd.Set_Text_Back_colour(bc);
        my_lcd.Print_String(str,x,y);
    }
    
    //show color select menu
    void show_color_select_menu(void)
    {
       uint16_t i;
       for(i = 0;i<6;i++)
       {
           my_lcd.Set_Draw_color(color_mask[i]);
           my_lcd.Fill_Rectangle(i*COLORBOXSIZE, 0, (i+1)*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
       }  
       my_lcd.Set_Draw_color(GREEN);
       my_lcd.Fill_Round_Rectangle((my_lcd.Get_Display_Width()-20)/3+10, COLORBOXSIZE/2+2, (my_lcd.Get_Display_Width()-20)/3*2+10,COLORBOXSIZE/2+20, 5);
     show_string("OK",CENTER,COLORBOXSIZE/2+4,2,RED, BLACK,1);
    }
    
    //show pen size select menu
    void show_pen_size_select_menu(void)
    {
      uint16_t i;
      my_lcd.Set_Text_Mode(1);
      my_lcd.Set_Text_Size(2);
      my_lcd.Set_Text_colour(GREEN);
      my_lcd.Set_Text_Back_colour(BLACK);
      for(i = 0;i<4;i++)
      {
          my_lcd.Print_Number_Int(i+1, 5+PENBOXSIZE*i, (COLORBOXSIZE/2-16)/2, 0, ' ',10);
          my_lcd.Set_Draw_color(RED);
          my_lcd.Fill_Rectangle(25+PENBOXSIZE*i, COLORBOXSIZE/2/2-i, PENBOXSIZE*(i+1)-10,
    COLORBOXSIZE/2/2+i);
      }
       my_lcd.Set_Draw_color(GREEN);
       my_lcd.Fill_Round_Rectangle((my_lcd.Get_Display_Width()-20)/3+10, COLORBOXSIZE/2+2, (my_lcd.Get_Display_Width()-20)/3*2+10,COLORBOXSIZE/2+20, 5);
       show_string("OK",CENTER,COLORBOXSIZE/2+4,2,RED, BLACK,1);
    }
    
    //show main menu
    void show_main_menu(void)
    {
       my_lcd.Set_Draw_color(YELLOW);
       my_lcd.Fill_Round_Rectangle(5, 0, (my_lcd.Get_Display_Width()-20)/3+5,COLORBOXSIZE/2+20, 5);
     my_lcd.Fill_Round_Rectangle((my_lcd.Get_Display_Width()-20)/3*2+15, 0, (my_lcd.Get_Display_Width()-20)/3*3+15,COLORBOXSIZE/2+20, 5);
       my_lcd.Set_Draw_color(MAGENTA);
       my_lcd.Fill_Round_Rectangle((my_lcd.Get_Display_Width()-20)/3+10, 0, (my_lcd.Get_Display_Width()-20)/3*2+10,COLORBOXSIZE/2+20, 5);
       show_string("COLOUR",8+((my_lcd.Get_Display_Width()-20)/3-72)/2-1,((COLORBOXSIZE/2+20)-16)/2,2,BLUE, BLACK,1);
       show_string("CLEAR",(my_lcd.Get_Display_Width()-20)/3+10+((my_lcd.Get_Display_Width()-20)/3-60)/2-1,((COLORBOXSIZE/2+20)-16)/2,2,WHITE, BLACK,1);
    show_string("PEN",(my_lcd.Get_Display_Width()-20)/3*2+15+((my_lcd.Get_Display_Width()-20)/3-36)/2-1,((COLORBOXSIZE/2+20)-16)/2,2,BLUE, BLACK,1);
     }
    
    void setup(void) 
    {
      my_lcd.Init_LCD();
      my_lcd.Set_Rotation(0); 
      my_touch.TP_Set_Rotation(2);
      my_touch.TP_Init(my_lcd.Get_Rotation(),my_lcd.Get_Display_Width(),my_lcd.Get_Display_Height()); 
      my_lcd.Fill_Screen(BLACK);
      show_main_menu();
      current_color = RED;
      current_pen = 0;
    }
    
    void loop()
    {
    comme:
      my_touch.TP_Scan(0);
     if (my_touch.TP_Get_State()&TP_PRES_DOWN) 
      {
             if(my_touch.y< COLORBOXSIZE/2+20) 
             {
                  if(((my_touch.x>5)&&(my_touch.x < ((my_lcd.Get_Display_Width()-20)/3+5)))&&!flag_pen) //select color
                  {
                    flag_colour = 1;
                    if(show_flag)
                    {
     my_lcd.Set_Draw_color(BLACK);
                        my_lcd.Fill_Rectangle(0,0,my_lcd.Get_Display_Width()-1,COLORBOXSIZE/2+20);
                        show_color_select_menu();
                    }
                    show_flag = false;
                    switch(current_color)
                    {
                      case RED:
                      {
                        my_lcd.Set_Draw_color(WHITE);
                        my_lcd.Draw_Rectangle(0, 0, COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        break;  
                      }
     case YELLOW:
                      {
                           my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(COLORBOXSIZE, 0, 2*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                           break; 
                      }
                      case GREEN:
                      {
                           my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(2*COLORBOXSIZE, 0, 3*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
    break; 
                       }
                      case CYAN:
                      {
                           my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(3*COLORBOXSIZE, 0, 4*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                           break;                 
                      }
                      case BLUE:
                      {
                           my_lcd.Set_Draw_color(WHITE);
    my_lcd.Draw_Rectangle(4*COLORBOXSIZE, 0, 5*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                           break;   
                      }
                      case MAGENTA:  
                      {
                           my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(5*COLORBOXSIZE, 0, 6*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                           break;   
                      }
                      default:
                          break;
                    }
                 }
                 if(flag_colour)
                 {
      if(my_touch.y < COLORBOXSIZE/2)
                     {
                        old_color = current_color;
                        if (my_touch.x < COLORBOXSIZE) 
                        { 
                            current_color = RED; 
                            my_lcd.Set_Draw_color(WHITE);
                            my_lcd.Draw_Rectangle(0, 0, COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        } 
                        else if (my_touch.x <
    COLORBOXSIZE*2) 
                        {
                            current_color = YELLOW;
                            my_lcd.Set_Draw_color(WHITE);
                            my_lcd.Draw_Rectangle(COLORBOXSIZE, 0, 2*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        } 
                        else if (my_touch.x < COLORBOXSIZE*3) 
                        {
                            current_color = GREEN;
                            my_lcd.Set_Draw_color(WHITE);
                           
    my_lcd.Draw_Rectangle(2*COLORBOXSIZE, 0, 3*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        } 
                        else if (my_touch.x < COLORBOXSIZE*4) 
                        {
                            current_color = CYAN;
                            my_lcd.Set_Draw_color(WHITE);
                            my_lcd.Draw_Rectangle(3*COLORBOXSIZE, 0, 4*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        } 
                        else if (my_touch.x <
    COLORBOXSIZE*5) 
                        {
                            current_color = BLUE;
                            my_lcd.Set_Draw_color(WHITE);
                            my_lcd.Draw_Rectangle(4*COLORBOXSIZE, 0, 5*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        } 
                        else if (my_touch.x < COLORBOXSIZE*6) 
                        {
                            current_color = MAGENTA;
                            my_lcd.Set_Draw_color(WHITE);
                           
    my_lcd.Draw_Rectangle(5*COLORBOXSIZE, 0, 6*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                        }
                        if(old_color != current_color)
                        {
                            switch(old_color)
                            {
                                case RED:
                                {
                                  my_lcd.Set_Draw_color(RED);
                                  my_lcd.Draw_Rectangle(0, 0, COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                             
     break;  
                                }
                                case YELLOW:
                                {
                                     my_lcd.Set_Draw_color(YELLOW);
                                     my_lcd.Draw_Rectangle(COLORBOXSIZE, 0, 2*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                                     break; 
                                }
                                case GREEN:
                                {
                                     my_lcd.Set_Draw_color(GREEN);
                       my_lcd.Draw_Rectangle(2*COLORBOXSIZE, 0, 3*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                                     break; 
                                 }
                                case CYAN:
                                {
                                     my_lcd.Set_Draw_color(CYAN);
                                     my_lcd.Draw_Rectangle(3*COLORBOXSIZE, 0, 4*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                                     break;
                    
                                }
                                case BLUE:
                                {
                                     my_lcd.Set_Draw_color(BLUE);
                                     my_lcd.Draw_Rectangle(4*COLORBOXSIZE, 0, 5*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                                     break;   
                                }
                                case MAGENTA:  
                                {
                                     my_lcd.Set_Draw_color(MAGENTA);
                         my_lcd.Draw_Rectangle(5*COLORBOXSIZE, 0, 6*COLORBOXSIZE-1, COLORBOXSIZE/2-1);
                                     break;   
                                }
                                default:
                                    break;                        
                            }
                        }
                    }
                    else if(my_touch.y< COLORBOXSIZE/2+20)
                    {
        if((my_touch.x>(my_lcd.Get_Display_Width()-20)/3+10)&&(my_touch.x<(my_lcd.Get_Display_Width()-20)/3*2+10))
                         {
                             my_lcd.Set_Draw_color(BLACK);
                             my_lcd.Fill_Rectangle(0,0,my_lcd.Get_Display_Width()-1,COLORBOXSIZE/2+20);
                             show_main_menu();
                             flag_colour = 0;
                             show_flag = true;
                             goto comme;
                         }
     }
                }
                if((my_touch.x>((my_lcd.Get_Display_Width()-20)/3*2+15))&&(my_touch.x < (((my_lcd.Get_Display_Width()-20)/3*3+15)))&&!flag_colour) //select pen size
                {
                    flag_pen = 1;
                    if(show_flag)
                    {
                        my_lcd.Set_Draw_color(BLACK);
                        my_lcd.Fill_Rectangle(0,0,my_lcd.Get_Display_Width()-1,COLORBOXSIZE/2+20);
            show_pen_size_select_menu();
                    }
                    show_flag = false;
                    switch(current_pen)
                    {
                       case 0:
                       {
                           my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(0, 0, PENBOXSIZE-1, COLORBOXSIZE/2-1);
                           break;
                       }
                       case 1:
                       {
                           my_lcd.Set_Draw_color(WHITE);
                my_lcd.Draw_Rectangle(PENBOXSIZE, 0, 2*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                           break; 
                       }
                       case 2:
                       {
                           my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(2*PENBOXSIZE, 0, 3*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                           break;   
                       }
                       case 3:  
                       {
                          
    my_lcd.Set_Draw_color(WHITE);
                           my_lcd.Draw_Rectangle(3*PENBOXSIZE, 0, 4*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                           break;                
                       }
                       default:
                           break;
                      }              
                  }
                  if(flag_pen)
                  {
                       if(my_touch.y < COLORBOXSIZE/2)
                       {
                          old_pen = current_pen;
      if(my_touch.x < PENBOXSIZE)
                          {
                              current_pen = 0;
                              my_lcd.Set_Draw_color(WHITE);
                              my_lcd.Draw_Rectangle(0, 0, PENBOXSIZE-1, COLORBOXSIZE/2-1);
                          }
                          else if(my_touch.x < 2*PENBOXSIZE)
                          {
                              current_pen = 1;
                              my_lcd.Set_Draw_color(WHITE);
                             
    my_lcd.Draw_Rectangle(PENBOXSIZE, 0, 2*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                           }
                           else if(my_touch.x < 3*PENBOXSIZE) 
                           {
                               current_pen = 2;
                               my_lcd.Set_Draw_color(WHITE);
                               my_lcd.Draw_Rectangle(2*PENBOXSIZE, 0, 3*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                           }
                           else if(my_touch.x < 4*PENBOXSIZE)
                           {
                  current_pen = 3;
                               my_lcd.Set_Draw_color(WHITE);
                               my_lcd.Draw_Rectangle(3*PENBOXSIZE, 0, 4*PENBOXSIZE-1, COLORBOXSIZE/2-1);               
                           }
                           if(old_pen != current_pen)
                           {
                               switch(old_pen)
                               {
                                     case 0:
                                     {
                                        
    my_lcd.Set_Draw_color(BLACK);
                                         my_lcd.Draw_Rectangle(0, 0, PENBOXSIZE-1, COLORBOXSIZE/2-1);
                                         break;
                                     }
                                     case 1:
                                     {
                                         my_lcd.Set_Draw_color(BLACK);
                                         my_lcd.Draw_Rectangle(PENBOXSIZE, 0, 2*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                             break; 
                                     }
                                     case 2:
                                     {
                                         my_lcd.Set_Draw_color(BLACK);
                                         my_lcd.Draw_Rectangle(2*PENBOXSIZE, 0, 3*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                                         break;   
                                     }
                                     case 3:  
                                     {
                             my_lcd.Set_Draw_color(BLACK);
                                         my_lcd.Draw_Rectangle(3*PENBOXSIZE, 0, 4*PENBOXSIZE-1, COLORBOXSIZE/2-1);
                                         break;                
                                     }
                                     default:
                                         break;           
                               }      
                           }
                       }
                       else if(my_touch.y <
    COLORBOXSIZE/2+20)
                       {
                           if((my_touch.x>(my_lcd.Get_Display_Width()-20)/3+10)&&(my_touch.x<(my_lcd.Get_Display_Width()-20)/3*2+10))
                           {
                              my_lcd.Set_Draw_color(BLACK);
                              my_lcd.Fill_Rectangle(0,0,my_lcd.Get_Display_Width()-1,COLORBOXSIZE/2+20);
                              show_main_menu();
                              flag_pen = 0;
                              show_flag = true;
                  goto comme;
                           }  
                       }
                  }
                  if(((my_touch.x>((my_lcd.Get_Display_Width()-20)/3+10))&&(my_touch.x < ((my_lcd.Get_Display_Width()-20)/3*2+10)))&&!flag_colour&&!flag_pen) 
                  {
                      my_lcd.Set_Draw_color(BLACK);  
                      my_lcd.Fill_Rectangle(0,COLORBOXSIZE/2+21,my_lcd.Get_Display_Width()-1,my_lcd.Get_Display_Height()-1);
                  }
             }
    if (((my_touch.y-current_pen) > COLORBOXSIZE/2+20) && ((my_touch.y+current_pen) < my_lcd.Get_Display_Height()))  //drawing
          {
            my_lcd.Set_Draw_color(current_color);
            my_lcd.Fill_Circle(my_touch.x,  my_touch.y,current_pen);
        }
      }
    }