Arduino 3.2" TFT Touchscreen Mega mirrored/reversed - Car LCD gauge project

Hello,

I'm having trouble with the Arduino Mega (copy), with 3.2" LCD Touchscreen + Screen Shield.

I'm currently running the examples, the touchscreen is acting weird:

For the URTouch_QuickDraw Example, when I draw on one side, the screen draws on the other side.

// URTouch_QuickDraw 
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// This program is a quick demo of how to use the library.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin 
// numbers in the setup.
//

#include <UTFT.h>
#include <URTouch.h>

// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
//UTFT myGLCD(ITDB32S,38,39,40,41);
// -----------------------------------------------------------
//The default LCD_Module is ITDB32S,
//For QDtech 3.2inch LCD Module,
//you should change to ILI9341_16 or CTE32_R2
UTFT    myGLCD(ILI9341_16,38,39,40,41);
//or
//UTFT myGLCD(CTE32_R2,38,39,40,41);

// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
URTouch  myTouch( 6, 5, 4, 3, 2);

void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
}

void loop()
{
  long x, y;
  
  while (myTouch.dataAvailable() == true)
  {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    if ((x!=-1) and (y!=-1))
    {
      myGLCD.drawPixel (x, y);
    }
  }
}

Similarly, for the URTouch_ButtonTest Example, touching the buttons doesn't register.

// URTouch_ButtonTest 
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin 
// numbers in the setup.
//

#include <UTFT.h>
#include <URTouch.h>

// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT    myGLCD(ILI9341_16,38,39,40,41);

// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
URTouch  myTouch( 6, 5, 4, 3, 2);

// Declare which fonts we will be using
extern uint8_t BigFont[];

int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";

/*************************
**   Custom functions   **
*************************/

void drawButtons()
{
// Draw the upper row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.printNumI(x+1, 27+(x*60), 27);
  }
// Draw the center row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
    if (x<4)
      myGLCD.printNumI(x+6, 27+(x*60), 87);
  }
  myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (10, 130, 150, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 130, 150, 180);
  myGLCD.print("Clear", 40, 147);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (160, 130, 300, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (160, 130, 300, 180);
  myGLCD.print("Enter", 190, 147);
  myGLCD.setBackColor (0, 0, 0);
}

void updateStr(int val)
{
  if (stCurrentLen<20)
  {
    stCurrent[stCurrentLen]=val;
    stCurrent[stCurrentLen+1]='\0';
    stCurrentLen++;
    myGLCD.setColor(0, 255, 0);
    myGLCD.print(stCurrent, LEFT, 224);
  }
  else
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    delay(500);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    myGLCD.setColor(0, 255, 0);
  }
}

// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

/*************************
**  Required functions  **
*************************/

void setup()
{
// Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);
  drawButtons();  
}

void loop()
{
  while (true)
  {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      
      if ((y>=10) && (y<=60))  // Upper row
      {
        if ((x>=10) && (x<=60))  // Button: 1
        {
          waitForIt(10, 10, 60, 60);
          updateStr('1');
        }
        if ((x>=70) && (x<=120))  // Button: 2
        {
          waitForIt(70, 10, 120, 60);
          updateStr('2');
        }
        if ((x>=130) && (x<=180))  // Button: 3
        {
          waitForIt(130, 10, 180, 60);
          updateStr('3');
        }
        if ((x>=190) && (x<=240))  // Button: 4
        {
          waitForIt(190, 10, 240, 60);
          updateStr('4');
        }
        if ((x>=250) && (x<=300))  // Button: 5
        {
          waitForIt(250, 10, 300, 60);
          updateStr('5');
        }
      }

      if ((y>=70) && (y<=120))  // Center row
      {
        if ((x>=10) && (x<=60))  // Button: 6
        {
          waitForIt(10, 70, 60, 120);
          updateStr('6');
        }
        if ((x>=70) && (x<=120))  // Button: 7
        {
          waitForIt(70, 70, 120, 120);
          updateStr('7');
        }
        if ((x>=130) && (x<=180))  // Button: 8
        {
          waitForIt(130, 70, 180, 120);
          updateStr('8');
        }
        if ((x>=190) && (x<=240))  // Button: 9
        {
          waitForIt(190, 70, 240, 120);
          updateStr('9');
        }
        if ((x>=250) && (x<=300))  // Button: 0
        {
          waitForIt(250, 70, 300, 120);
          updateStr('0');
        }
      }

      if ((y>=130) && (y<=180))  // Upper row
      {
        if ((x>=10) && (x<=150))  // Button: Clear
        {
          waitForIt(10, 130, 150, 180);
          stCurrent[0]='\0';
          stCurrentLen=0;
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(0, 224, 319, 239);
        }
        if ((x>=160) && (x<=300))  // Button: Enter
        {
          waitForIt(160, 130, 300, 180);
          if (stCurrentLen>0)
          {
            for (x=0; x<stCurrentLen+1; x++)
            {
              stLast[x]=stCurrent[x];
            }
            stCurrent[0]='\0';
            stCurrentLen=0;
            myGLCD.setColor(0, 0, 0);
            myGLCD.fillRect(0, 208, 319, 239);
            myGLCD.setColor(0, 255, 0);
            myGLCD.print(stLast, LEFT, 208);
          }
          else
          {
            myGLCD.setColor(255, 0, 0);
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
            delay(500);
            myGLCD.print("            ", CENTER, 192);
            delay(500);
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
            delay(500);
            myGLCD.print("            ", CENTER, 192);
            myGLCD.setColor(0, 255, 0);
          }
        }
      }
    }
  }
}

I ran example URTouch_Calibration, it works fine.

Could there possibly be a difference between the copy of the Mega board that I get and the genuine Arduino version of the Mega board?

Any suggestions?

Thanks in advance!

@kelvinkkong, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Although there might be differences, your problem will not be caused by them. Your board seems to be a genuine Arduino Mega and not a clone.

First off. I assume that all the UTFT examples work with your constructor:

UTFT    myGLCD(ILI9341_16,38,39,40,41);

Now run the URTouch examples e.g. URTouch_Calibration
Follow the instructions to edit the

C:\Users\ ... \Documents\Arduino\libraries\URTouch\URTouchCD.h

Then run the other URTouch examples.

If you have a problem, copy paste your edited URTouchCD.h file to your message.

There is nothing unusual with Touch Panels that are connected XY or YX. Or if the X direction is backwards. Just report your URTouchCD.h results.

David.

I would like to ask if anyone knows how to manually wire up the ILI9341 display (without using the LCD shield), like this in this video:

I need to connect breadboard/sensors to the Mega board, so I can't use the LCD shield as it is in the way.

There is no datasheet/manual from the store I bought the LCD screen from.

It think this is it:

http://wiki.sainsmart.com/index.php/3.2"_TFT_Touch_LCD

Thanks

Please reply to #3 first. i.e. using a proper Adapter Shield.

We should get that working first.

I am happy to advise on the Due but only when you have the Mega2560 working 100%.

David.

You can consider a screw shield. E.g. https://www.desertcart.co.za/products/96481772-prototype-screw-terminal-block-shield-board-kit-mega-2560-r31.

Place it between the Mega and the TFT shield and you have all your connections with screw terminals which is more reliable.

Hello sterretje,

None of my local stores have one for the Mega. This project is my uni assignment, and it is due next week. Found one on Ebay, but I can't wait a week for shipping.
So @david_prentice , so I still proceed with your request?

I found one for the Uno though at my local store.

I also have the 2.8" TFT LCD display:

Here's the 2.8" LCD connected to the Uno.
As I am writing, I thought to myself that I need more pins, if I use this setup.

XC4630

I can unplug D10-D13 as I don't need the SD card feature.
I'll just use the Mega board with the 2.8" screen as it has more pins.

I don't know what extra pins you require for your Uni assignment.
The Uno Shield will plug into the Mega2560 and work 100%. (but slower than a Uno)
The Uno Shield will plug into the Due and work 100%. (but slightly faster than a Uno)
You can access the 2x18 header with the shield in place.

Regarding the 40-pin TFT and the 40-pin Adapter Shield. You can access A8-A14, D8-D13, D14-D21 with the Adapter Shield in place. e.g. with some angle male-header strip.

So for the 2 minutes involved in running URTouch_Calibration.ino and the 1 minute to copy-paste the output, we can get your Touch working properly.

David.

LibURTouch

@david_prentice How do I open the H File?

Edit: I opened it in notepad. Edited the code.
URTouch_ButtonTest works properly.

I haven't used shields before.
Why do I need a shield?

"Regarding the 40-pin TFT and the 40-pin Adapter Shield. You can access A8-A14, D8-D13, D14-D21 with the Adapter Shield in place. e.g. with some angle male-header strip."

I'm making a LCD car display, for now, to read off oil temperature and pressure sensors. I need to ground the Mega Board to my car's metal body.
All of the grounds are taken by the LCD Shield.
So, that's not an option.

I guess using the 3.2" Display is out the window, unless I'm able to map the pins via wires.
Directly connecting the 2.8" Display to the Mega board, there's still 1 ground left free to use.

Regards to the angle male header strips, is it recommended to manually bend vertical male header strips?

Thanks.

So that has solved your original problem. i.e. Mega2560 + Adapter + 3.2 inch (40-pin)

No. Straight male header strip is not long enough to bend. Buy ready-made angle header. Local Ebay or Main Distributor will have this in stock. Or ask the University Laboratory staff.

There are two GND pins on the 2x18. There are two 5V pins on the 2x18.

It may be a University project. I would try to make it robust but the most important thing is your "7 days left to complete the project"
For a practical device to use in a real car I would want to assemble with mating pcbs and soldered joints. e.g. with Protoshield or Protoboard.

Seriously. Look at the pinout diagrams for Mega2560 and Due. Think carefully about which pins you have available and how easy it is to connect external electronics.

David.

It may be a University project. I would try to make it robust but the most important thing is your "7 days left to complete the project"
For a practical device to use in a real car I would want to assemble with mating pcbs and soldered joints. e.g. with Protoshield or Protoboard.

It's just a first yr Intro to Mechatronics (arduino), so making the enclosure, assembly is not in the criteria. I did choose a project that is quite hard itself.
Although this will be my long term project, I'll definitely getting an enclosure going.

Hear is my project planned setup:


12V - 5V USB converter connected from car battery, to Mega board.
Should I power via jack or USB?

Oil Temperature Sensor:
-Ground wire - I can wire this to nearby ground, or I can run it back with 5V wire to Arduino.
-5V wire.

Oil pressure senor:
-Ground
-Signal
-5V
3 wire loom, so run back to Arduino.

Oil sandwich plate to mount inbetween oil filter and sump for the sensors.

Ground wire from Arduino to car body.


I have a fresh Uno proto board. Would you like me to use it?

Any other suggestions?

I also have a Nextion Display arriving in the next day or two. Seems like it is easier to display via its program. I'm might consider using it.

Thanks alot.

There are lots of "electrical" considerations.

The TFT backlight takes 100mA - 300mA.
The 12V USB converter is probably going to be buck arrangement.
A linear chip like a 7805 will waste a lot of heat.

Ground loops in automobile wiring might be noisy, damage chips, ...
I have never attempted automobile electronics. There must be lots of Application Notes, Tutorials, advice ...

David.