How to Use 2.4" TFT LCD Shield With Arduino Mega


37,885

24

23

Introduction: How to Use 2.4" TFT LCD Shield With Arduino Mega

My name is Bay Yolal, hello everybody!

Today, I am going to show you that we want to use 2.4" TFT LCD shield (There is mcufriend.com writes on this shield) works with Arduino Mega, because it is not so easy to run each other. But with this instructions, you can run and modify codes easily according to this tutorial.

Step 1: Assembly

This step is very easy. But please focus on Arduino Mega's Power side's Vin and between Analog In's A0 pins. This gap is our referance point to put this shield on Arduino Mega.

Step 2: Upload the Code

<p>#include  <Adafruit_GFX.h>   </p><p>#include "SWTFT.h" </p><p>#define LCD_CS A3 
#define LCD_CD A2 
#define LCD_WR A1 
#define LCD_RD A0 </p><p>SWTFT tft;</p><p>void setup(void) {
  Serial.begin(9600);
  Serial.println("TFT LCD Shield Test");</p><p>  tft.reset();</p><p>  uint16_t identifier = tft.readID();
  Serial.print("Your LCD driver chip name is: ");
  Serial.println(identifier, HEX);
  tft.begin(identifier);
  Serial.println("Working Well");
}</p><p>void loop(void) {
  for(uint8_t rotation=0; rotation<4; rotation++) {
    tft.setRotation(rotation);
    testText();
    delay(2000);
  }
}</p><p>unsigned long testText() {
  tft.fillScreen(0x0000);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.println();
  tft.setTextColor(0xFFFF);  tft.setTextSize(3);
  tft.println("Hello World!");
  tft.println();
  tft.setTextColor(0xFFE0); tft.setTextSize(2);
  tft.println("A Yellow Text Here");
  tft.println();
  tft.setTextColor(0x07E0);
  tft.setTextSize(3);
  tft.println("A Green Text Here");</p><p>  return micros() - start;
}</p>

On your Arduino IDE, select your COM port, select your board as Arduino Mega. Then upload these codes into your Arduino Mega board. You will see your codes are simply working.

Step 3: Necessary Libraries

1. Download libraries.rar file.

2. Go to start >> libraries >> Arduino >> Libraries >> paste these two files in there.

3. Paste the code from a step ago it was given to Arduino IDE screen

4. The library is normally for Arduino UNO, to make it work with Arduino MEGA go to the library folder and find SWTFT.cpp file and open it with notepad >> Find these code:

// Use the include which corresponde to your arduino

If you want to use your 2.4" TFT LCD Shield works with only Arduino MEGA uncomment this line as deleting //

//	#include "mega_24_shield.h"

If you want to use your 2.4" TFT LCD Shield works with only Arduino UNO uncomment this line as deleting //

//	#include "uno_24_shield.h"

Save and close, then return to Arduino IDE and enjoy :D

Step 4: Other Informations

This work is depending on these materials:

  • Code provided by Smoke And Wires
  • http://www.smokeandwires.co.nz
  • This code has been taken from the Adafruit TFT Library and modified
  • By us for use with our TFT Shields / Modules
  • For original code / licensing please refer to //http://www.smokeandwires.co.nz

Note: I only edit these codes to make all easy for beginners and who wants speed programming. But I also worked a little bit on it. Check the video above for original codes.

Note: I have solved this problem with help of this topic

Note: My old youtube video ison here

Note: Also on my website you may check the same informations.

.

That's all !

Step 5: BONUS: Reading This Instructable

NOTE: This instructable's step is aimed for visually impaired people to hear the sentences to learn what's going on here. Watch the video, it explains all steps.

Arduino All The Things! Contest

Participated in the
Arduino All The Things! Contest

3 People Made This Project!

Recommendations

  • Pi Day Speed Challenge

    Pi Day Speed Challenge
  • Trash to Treasure Contest

    Trash to Treasure Contest
  • Microcontroller Contest

    Microcontroller Contest

23 Comments

0
christianglz018
christianglz018

4 months ago

it helped me out
I have a code to use the touch
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
// You can include several fonts but WARNING! They take up a lot of space.
//#include <Fonts/FreeSansBold24pt7b.h>
// Adafruit Touch screen library (if required)
#include <TouchScreen.h>
#include "SWTFT.h"
/*
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin*/
#define TS_MINX 525
#define TS_MINY 217
#define TS_MAXX 229
#define TS_MAXY 889
// Assign human-readable names to some common 16-bit 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
// Make an array of those values too
int tftColours[] = {BLUE, RED, GREEN, CYAN, MAGENTA, YELLOW, WHITE};
// Create the TFT object with all pin assignments
// This leaves pins 2, 3, 4, 5 free. Also pin 12 if you don't use SD card.
//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
SWTFT tft;
// Set up the touch screen element of the TFT screen
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A1 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 7 // can be a digital pin
#define SENSITIVITY 300
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, SENSITIVITY);
//----------------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP
//----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
tft.reset();
tft.begin(0x9325); // SDFP5408
tft.setRotation(1);
//randomSeed(analogRead(A1));
}
//----------------------------------------------------------------------------
// LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
//----------------------------------------------------------------------------
void loop() {
tft.fillScreen(BLACK);
tft.setCursor(80, 85);
tft.setTextColor(WHITE);
// tft.setFont(&FreeSansBold24pt7b);
tft.setTextSize(1);
tft.println("HELLO");
tft.setCursor(50, 145);
tft.setTextColor(WHITE);
// tft.setFont(&FreeSansBold24pt7b);
tft.setTextSize(1);
tft.println("Dumplings");
delay(1000);
// Simple button
// x, y, w, h, colour
tft.fillRoundRect(120, 180, 75, 40, 3, WHITE);
//tft.setFont();
tft.setTextSize(3);
tft.setTextColor(BLACK);
tft.setCursor(140, 190);
tft.print("OK");
// User touches screen
waitOneTouch();
tft.fillScreen(BLACK);
tft.setCursor(30, 30);
tft.setTextColor(WHITE);
// tft.setFont(&FreeSansBold24pt7b);
tft.setTextSize(1);
tft.println("Es la mejor");
tft.setCursor(15, 90);
tft.println("integradora?");
delay(500);
tft.fillRoundRect(0, 120, 160, 100, 3, GREEN);
// tft.setFont();
tft.setTextSize(6);
tft.setTextColor(WHITE);
tft.setCursor(45, 150);
tft.print("Si");
tft.fillRoundRect(161, 120, 160, 100, 3, RED);
// tft.setFont();
tft.setTextSize(6);
tft.setTextColor(WHITE);
tft.setCursor(205, 150);
tft.print("No");
//delay(1000);

}
//----------------------------------------------------------------------------
// Has user touched the button?
//----------------------------------------------------------------------------
TSPoint waitOneTouch() {
// Get a touch in the required button area (or anywhere if flag set)
TSPoint p;
do {
p = ts.getPoint();
p.x = map(p.x, TS_MAXX, TS_MINX, 320, 0);
p.y = map(p.y, TS_MAXY, TS_MINY, 240, 0);
pinMode(XM, OUTPUT); // Pins configures again for TFT control
pinMode(YP, OUTPUT);
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tZ = "); Serial.print(p.z);
Serial.print("\n");
} while (p.y < 88 || p.y > 150 );
// All done, return the touch coordinates object
return p;
}

0
Ralphxyz
Ralphxyz

9 months ago

I see someone 4 years ago had the same problem and never answered! "swap' was not declared in this scope" probably the most common problem with downloaded code. What does it mean, not declared in this scope?

void SWTFT::drawLine3Byte(int16_t x0, int16_t y0,
int16_t x1, int16_t y1,
uint8_t r, uint8_t g, uint8_t b) {
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0); This swap is found, declared in this scope
swap(x1, y1); This swap is found, declared in this scope
}
if (x0 > x1) {
swap(x0, x1); This swap is not declared in this scope
swap(y0, y1);
}

Thanks,
Ralph

0
ronaldb89
ronaldb89

3 years ago

Hello sir I used your codes and libraries for testing my tft lcd and arduino mega 2560 but after I paste or follow your instructions i get this errors

Serial.begin(9600);

^

sketch_oct08a:8: error: 'Serial' does not name a type

Serial.println("TFT LCD Shield Test"); tft.reset(); uint16_t identifier = tft.readID();

^

sketch_oct08a:8: error: 'tft' does not name a type

Serial.println("TFT LCD Shield Test"); tft.reset(); uint16_t identifier = tft.readID();

so on and so fort? what do you think the problem is?

thanks in advance

0
jonathanm301
jonathanm301

4 years ago

didn't simply work for me, it won't compile. unmexpected # and then why is there HTML type tags in the code? got rid of them and the first error gone but then states fatal error and won't compile because no adafruit GFX file. so I will install library and try again but didn't state it was needed ion tutorial.

0
Bay Yolal
Bay Yolal

Reply 4 years ago

Please download my library in there. I am sorry about HTML that this is because of this website.

2
jonathanm301
jonathanm301

4 years ago

one odd thing on serial monitor is

TFT LCD Shield Test
Your LCD driver chip name is: 0
Working Well

what should chip names be?

1
jonathanm301
jonathanm301

4 years ago

so got the files from the second page installed libraries got rid of errors but screen is just scrammbled like snow on a tv without reception

0
jonathanm301
jonathanm301

4 years ago

and now this

C:\Arduino\libraries\SWTFT\SWTFT.cpp:634:16: error: 'swap' was not declared in this scope

swap(x0, x1);

0
dilawarc
dilawarc

4 years ago

but sir what about touch screen!!!!!!on mega2560

0
Bay Yolal
Bay Yolal

Reply 4 years ago

I haven't use that but there are touchpad code example for UNO but I haven't tried for MEGA yet.

0
JohnR122
JohnR122

4 years ago

no I dont need it connected to the internet just save to sd card, say every 20 minutes something like that

0
Bay Yolal
Bay Yolal

Reply 4 years ago

Okey, I did it. Please check my recent instructable page. Thank you.

0
JohnR122
JohnR122

Reply 4 years ago

thank you very much

0
JohnR122
JohnR122

4 years ago

all I need is a script that I can use with the touch screen. so that can be a weather station. the rain gauge is a seesaw type. I have a heap of sensors now and if I need more I will just buy them.

I would like it to save data to a micro sd card say rain and temp and this is where I fall down as I know I can upload scrips to them as long as they run but I dont know enough of the routines, to put everything together I can follow most of what been done, as I used to programme in stuff like turbo pascal many years ago

as for sensors I have a couple of starter kits and they have alsorts of bits and bobs in them. but at this point about all I can do is things like change whats shown on screen and make the led blink

0
Bay Yolal
Bay Yolal

Reply 4 years ago

How was it? https://www.instructables.com/id/Arduino-Mega-24-TFT-LCD-Shield-DHT11-Temperature-a/

0
JohnR122
JohnR122

4 years ago

wow it works have it on uno and mega can you show me a simple way of using this as a weather station have the sensors humidity etc temp a heap of them do you have a script to do this not a plant waterer just a weather station please

0
Bay Yolal
Bay Yolal

Reply 4 years ago

I can prepare temperature and humidity reader with LCD display to learn what is the values at this time. But you want internet connected weather station for a specific city, aren't you? Could you please explain a little bit more what is your need? Thank you :)

0
JohnR122
JohnR122

Reply 4 years ago

dont know if its easier but I have a tft 320 qvt screen as well a mega 2560 and a couple of uno's plus a heap of shields

0
JohnR122
JohnR122

Reply 4 years ago

no that would be fine temp humidity pressure and rain gauge just want to have a good use for that screen

0
Bay Yolal
Bay Yolal

Reply 4 years ago

Okey, I got it. I will write a new instructable to show you how I do. Could you please wait a little for me to prepare the project. Don't miss my instructables, so could you please click on follow button to follow me to don't miss any of my instructables? Thank you very much! :)