2.4 tft lcd touch shield arduino hello world quotation
// For better pressure precision, we need to know the resistance // between X+ and X- Use any multimeter to read it // For the one we"re using, its 300 ohms across the X plate TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED); tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW); tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN); tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN); tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE); tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA); // tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE); tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE); currentcolor = RED; pinMode(13, OUTPUT); }
void loop() { digitalWrite(13, HIGH); // Recently Point was renamed TSPoint in the TouchScreen library // If you are using an older version of the library, use the // commented definition instead. Point p = ts.getPoint(); // TSPoint p = ts.getPoint(); digitalWrite(13, LOW);
// if sharing pins, you"ll need to fix the directions of the touchscreen pins //pinMode(XP, OUTPUT); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); //pinMode(YM, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { /* Serial.print("X = "); Serial.print(p.x); Serial.print("\tY = "); Serial.print(p.y); Serial.print("\tPressure = "); Serial.println(p.z); */ if (p.y < (TS_MINY-5)) { Serial.println("erase"); // press the bottom of the screen to erase tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK); } // scale from 0->1023 to tft.width p.x = tft.width()-(map(p.x, TS_MINX, TS_MAXX, tft.width(), 0)); p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, tft.height(), 0)); /* Serial.print("("); Serial.print(p.x); Serial.print(", "); Serial.print(p.y); Serial.println(")"); */ if (p.y < BOXSIZE) { oldcolor = currentcolor;
if (p.x < BOXSIZE) { currentcolor = RED; tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE); } else if (p.x < BOXSIZE*2) { currentcolor = YELLOW; tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE); } else if (p.x < BOXSIZE*3) { currentcolor = GREEN; tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE); } else if (p.x < BOXSIZE*4) { currentcolor = CYAN; tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE); } else if (p.x < BOXSIZE*5) { currentcolor = BLUE; tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE); } else if (p.x < BOXSIZE*6) { currentcolor = MAGENTA; tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE); }
if (oldcolor != currentcolor) { if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED); if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW); if (oldcolor == GREEN) tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN); if (oldcolor == CYAN) tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN); if (oldcolor == BLUE) tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE); if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA); } } if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) { tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor); } } }
In this Arduino touch screen tutorial we will learn how to use TFT LCD Touch Screen with Arduino. You can watch the following video or read the written tutorial below.
For this tutorial I composed three examples. The first example is distance measurement using ultrasonic sensor. The output from the sensor, or the distance is printed on the screen and using the touch screen we can select the units, either centimeters or inches.
The third example is a game. Actually it’s a replica of the popular Flappy Bird game for smartphones. We can play the game using the push button or even using the touch screen itself.
As an example I am using a 3.2” TFT Touch Screen in a combination with a TFT LCD Arduino Mega Shield. We need a shield because the TFT Touch screen works at 3.3V and the Arduino Mega outputs are 5 V. For the first example I have the HC-SR04 ultrasonic sensor, then for the second example an RGB LED with three resistors and a push button for the game example. Also I had to make a custom made pin header like this, by soldering pin headers and bend on of them so I could insert them in between the Arduino Board and the TFT Shield.
Here’s the circuit schematic. We will use the GND pin, the digital pins from 8 to 13, as well as the pin number 14. As the 5V pins are already used by the TFT Screen I will use the pin number 13 as VCC, by setting it right away high in the setup section of code.
I will use the UTFT and URTouch libraries made by Henning Karlsen. Here I would like to say thanks to him for the incredible work he has done. The libraries enable really easy use of the TFT Screens, and they work with many different TFT screens sizes, shields and controllers. You can download these libraries from his website, RinkyDinkElectronics.com and also find a lot of demo examples and detailed documentation of how to use them.
After we include the libraries we need to create UTFT and URTouch objects. The parameters of these objects depends on the model of the TFT Screen and Shield and these details can be also found in the documentation of the libraries.
Next we need to define the fonts that are coming with the libraries and also define some variables needed for the program. In the setup section we need to initiate the screen and the touch, define the pin modes for the connected sensor, the led and the button, and initially call the drawHomeSreen() custom function, which will draw the home screen of the program.
So now I will explain how we can make the home screen of the program. With the setBackColor() function we need to set the background color of the text, black one in our case. Then we need to set the color to white, set the big font and using the print() function, we will print the string “Arduino TFT Tutorial” at the center of the screen and 10 pixels down the Y – Axis of the screen. Next we will set the color to red and draw the red line below the text. After that we need to set the color back to white, and print the two other strings, “by HowToMechatronics.com” using the small font and “Select Example” using the big font.
Ok next is the RGB LED Control example. If we press the second button, the drawLedControl() custom function will be called only once for drawing the graphic of that example and the setLedColor() custom function will be repeatedly called. In this function we use the touch screen to set the values of the 3 sliders from 0 to 255. With the if statements we confine the area of each slider and get the X value of the slider. So the values of the X coordinate of each slider are from 38 to 310 pixels and we need to map these values into values from 0 to 255 which will be used as a PWM signal for lighting up the LED. If you need more details how the RGB LED works you can check my particular tutorialfor that. The rest of the code in this custom function is for drawing the sliders. Back in the loop section we only have the back button which also turns off the LED when pressed.
In order the code to work and compile you will have to include an addition “.c” file in the same directory with the Arduino sketch. This file is for the third game example and it’s a bitmap of the bird. For more details how this part of the code work you can check my particular tutorial. Here you can download that file:
Displays are one of the best ways to provide feedback to users of a particular device or project and often the bigger the display, the better. For today’s tutorial, we will look on how to use the relatively big, low cost, ILI9481 based, 3.5″ Color TFT display with Arduino.
This 3.5″ color TFT display as mentioned above, is based on the ILI9481 TFT display driver. The module offers a resolution of 480×320 pixels and comes with an SD card slot through which an SD card loaded with graphics and UI can be attached to the display. The module is also pre-soldered with pins for easy mount (like a shield) on either of the Arduino Mega and Uno, which is nice since there are not many big TFT displays that work with the Arduino Uno.
The module is compatible with either of the Arduino Uno or the Arduino Mega, so feel free to choose between them or test with both. As usual, these components can be bought via the links attached to them.
One of the good things about this module is the ease with which it can be connected to either of the Arduino Mega or Uno. For this tutorial, we will use the Arduino Uno, since the module comes as a shield with pins soldered to match the Uno’s pinout. All we need to do is snap it onto the top of the Arduino Uno as shown in the image below, thus no wiring required.
This ease of using the module mentioned above is, however, one of the few downsides of the display. If we do not use the attached SD card slot, we will be left with 6 digital and one analog pin as the module use the majority of the Arduino pins. When we use the SD card part of the display, we will be left with just 2 digital and one analog pin which at times limits the kind of project in which we can use this display. This is one of the reasons while the compatibility of this display with the Arduino Mega is such a good news, as the “Mega” offers more digital and analog pins to work with, so when you need extra pins, and size is not an issue, use the Mega.
To easily write code to use this display, we will use the GFX and TFT LCD libraries from “Adafruit” which can be downloaded here. With the library installed we can easily navigate through the examples that come with it and upload them to our setup to see the display in action. By studying these examples, one could easily learn how to use this display. However, I have compiled some of the most important functions for the display of text and graphics into an Arduino sketch for the sake of this tutorial. The complete sketch is attached in a zip file under the download section of this tutorial.
As usual, we will do a quick run through of the code and we start by including the libraries which we will use for the project, in this case, the Adafruit GFX and TFT LCD libraries.
With this done, the Void Setup() function is next. We start the function by issuing atft.reset() command to reset the LCD to default configurations. Next, we specify the type of the LCD we are using via the LCD.begin function and set the rotation of the TFT as desired. We proceed to fill the screen with different colors and display different kind of text using diverse color (via the tft.SetTextColor() function) and font size (via the tft.setTextSize() function).
The shield is fully assembled, tested, and ready to go. No wiring, no soldering! Simply plug it in and load up the library - you"ll have it running in under 10 minutes!
Spice up your Arduino project with a beautiful touchscreen display shield with built in microSD card connection. This TFT display is 2.4" diagonal and colorful (18-bit 262,000 different shades)! 240x320 pixels with individual pixel control. As a bonus, this display has a optional capacitive touch panel and resistive touch panel with controller XPT2046 attached by default.
The shield is fully assembled, tested and ready to go. No wiring, no soldering! Simply plug it in and load up our library - you"ll have it running in under 10 minutes! Works best with any classic Arduino (UNO/Due/Mega 2560).
This display shield has a controller built into it with RAM buffering, so that almost no work is done by the microcontroller. You can connect more sensors, buttons and LEDs.
Of course, we wouldn"t just leave you with a datasheet and a "good luck!" - we"ve written a full open source graphics library at the bottom of this page that can draw pixels, lines, rectangles, circles and text. We also have a touch screen library that detects x,y and z (pressure) and example code to demonstrate all of it. The code is written for Arduino but can be easily ported to your favorite microcontroller!
If you"ve had a lot of Arduino DUEs go through your hands (or if you are just unlucky), chances are you’ve come across at least one that does not start-up properly.The symptom is simple: you power up the Arduino but it doesn’t appear to “boot”. Your code simply doesn"t start running.You might have noticed that resetting the board (by pressing the reset button) causes the board to start-up normally.The fix is simple,here is the solution.
In a number of applications there is need to display text or characters, for example in calculators, digital clocks and other devices. This is where a Liquid Crystal Display (LCD) comes in handy. In this tutorial I’ll show you how to interface a 16×2 LCD with Arduino.
LCD is short for Liquid Crystal Display which means this display uses liquid crystals to produce a visible image. When current is applied to these crystals, they turn opaque and block the backlight behind the screen and as a result that particular area will become dark compared to other. And that’s how characters are displayed on the screen.
Most of the common LCDs with 16 pins use the Hitachi HD44780 driver which is a parallel interface LCD controller chip. In this case we are using a 16×2 LCD which means it has 2 rows and 16 columns. Therefore this LCD can display 32 ASCII characters although more characters can be displayed by scrolling.
The LCDs come in other sizes like 16×1, 16×4, 20×4 but the code for running these screens remains the same. The LCD screen is made of pixel rectangles for displaying the characters. Each of these rectangles is made up of grids of 5×8 pixels as illustrated in the diagram below.
V0: controls the contrast and brightness of the LCD. Using a simple voltage divider with a potentiometer, we can make fine adjustments to the contrast.
RS(Register Select): this pin is used to differentiate commands from data. When RS pin is set to LOW, then we are sending commands to the LCD and when RS pin is set on HIGH we are sending data/characters to the LCD.
RW(Read/Write): This pin is for determining whether you are reading or writing data from the LCD. In this case the LCD is being used as an OUTPUT device therefore we need to keep this pin LOW to keep it in the WRITE mode.
E(Enable): Used for enabling the display. When this pin is set to LOW, the LCD does not care what is happening with R/W, RS, and the data bus lines. When this pin is set to HIGH, the LCD is processing the incoming data. When we need to execute an instruction, this pin is set as HIGH for a few milliseconds then back to LOW.
We will use just 6 digital input pins from the Arduino Board. The LCD’s registers from D4 to D7 will be connected to Arduino’s digital pins from 4 to 7. The Enable pin will be connected to pin number 2 and the RS pin will be connected to pin number 1.
To control the LCD with Arduino we need to install the LiquidCrystal.h library which contains all the functions for controlling data transmission to the display. This library usually comes pre-installed in the Arduino IDE but can also be got from the Arduino community website.
LiquidCrystal(): Used for creating an LCD object and the parameters of this object should be the numbers of the digital input pins for controlling the LCD. The syntax is LiquidCrystal(RS, Enable, D4, D5, D6, D7).
lcd.begin(): For setting the dimensions of the LCD. The number of rows and columns are specified as lcd.begin(columns, rows). For a 16×2 LCD, you would use lcd.begin(16,2)
lcd.Cursor(): This function creates a visible cursor. The cursor is a horizontal line placed below the next character to be printed to the LCD. To turn the cursor off we use the lcd.noCursor() function.
lcd.blink(): This function is used for displaying a blinking cursor. Use it in the void loop() section. The lcd.noBlink() functions disables the blinking cursor.
lcd.setCursor(): Place the cursor or any printed text at any position on the screen. This function’s syntax is lcd.setCursor(column, row) where the column and row coordinates are in the range 0 to 15 and 0 to 1 respectively.
lcd.print(): For printing text on the LCD. To print letters and words, place quotation marks (” “) around the text. For example, to print hello, world!, use lcd.print(“hello, world!”). To print numbers, no quotation marks are necessary. For example, to print 123456789, use lcd.print(123456789).
Sometimes there is a need to display messages with more than 16 characters on the LCD screen and in that case we need to apply some form of scrolling effect to be able to display the whole text message. The LiquidCrystal library has functions to necessitate this and these include:
lcd.noAutoscroll(): This is the opposite of the lcd.autoscroll() function and is normally used before or after lcd.autoscroll() to create sequences of scrolling text or animations.
lcd.scrollDisplayLeft(): This function moves the text printed on the LCD to the left. It is should be followed by a delay command to control the speed of scrolling. The function will move the text 40 spaces to the left before it loops back to the first character. Text strings longer than 40 spaces will be printed to line 1 after the 40th position, while the start of the string will continue printing to line 0.
The code below is for scrolling the text “Hello World” in the left and right directions using the scrollDisplayLeft() and scrollDisplayRight() functions. The scrolling is done by incrementing the position of the text by one in the respective direction.
In case you need specific qualities in the way the text is scrolled on the LCD or if you want to display messages which are longer than 40 characters , then you can achieve this using custom functions. For example the code below shows how we can use custom functions to display a scrolling message on the display.
I have created a function scrollText() for scrolling text. This function accepts four arguments; the row on which to display the scrolling text, the text to be displayed, the delay time between the shifting of characters, and the number of columns of the LCD.
CGROM: This is the Character Generator ROM which is the type of memory used for storing the permanent ASCII code fonts. These fonts are the ones we normally use for displaying messages on the LCD.
CGRAM: This is where the user defined characters are stored. This memory space can be modified and is limited to 64 bytes. This means that for a 5×8 based LCD, a maximum of eight custom characters can be stored in the CGRAM.
I have discussed more on how to display custom characters on the LCD in my other tutorial involving the use of an i2C adapter attached to the LCD to further reduce on the number of connections needed to run this display. You can check it out using this link:
We have developed more than 500 hundred vocational self learning kits to make world of electronics accessible and easy to understand to any person possible.
In addition to above products, LabsGuru has a vision to offer classes and online tutorials designed to help educate students, hobbyists and individuals in the wonderful world of engineering.
Hello, this a tutorial for beginners about the TFT LCD touch screen shield mounted on an Arduino UNO board, where we use some basic display functions and a little touch function, all this with simple and detailed functions.
Before proceeding, if you have the white screen problem or touch not detected or inverted you can check my previous tutorial it may help you with such issues ( Go to tutorial).
Here comes the “function of the show”, the code about the touch function creates two squares with different colors and then we associate every one with a simple function, you can check the paint example for more functions, this one shows the basic way of creating a button and it’s by associating the position where the square is drawn before, to a function like writing a text or clearing the screen then going to another page… it’s up to you now to create what you want.
This module is designed to plug directly into Arduino UNO R3 (or its clone) boards. It is compatible with CH340 and Atmega16u2 version boards, as well as Mega 2560. This LCD shield may also work with other boards, but the compatibility can"t be guaranteed.