Go Down

Topic: UTOUCH Users---READ THIS!!! Add delay after myTouch.dataAvailable() (Read 19365 times) previous topic - next topic


For those of you who are using the www.henningkarlsen.com and experiencing strange behavior when trying to calibrate your screen, I learned that adding a delay(50); after myTouch.dataAvailable() fixed my problem.  Without this, my uTouch coordinates were corrupted and caused the cursor to show up in strange places misleading me into thinking the coordinates were off.  I also learned the calibration was reversing my screen coordinates causing strange things to occur...

Here is a simpler way to calibrate your screen.  First download the attached code and library files: uTouch.cpp and  uTouch.h.  Replace the library files from henningkarlsen
(note: you previously copied the library folder to ...MyDocuments/Arduino/Libraries/, right?)

First off: Open the .pde code and make sure to:
A) Enter the correct display driver in myGLCD(SSD1289,38,39,40,41);
B) Enter the correct screen orientation for both myGLCD.InitLCD(LANDSCAPE); and myTouch.InitTouch(LANDSCAPE);

Then, follow the steps below for a simpler, straightforward way of calibrating the touchscreen.

The program has the following functions that can be selected by changing the #define selector at the top of the code:
1) Map out the display coordinates: Prints numbers across the screen from left to right and up to down.
-Write down the extreme values (right  (X) and down (Y)).  I assume the left and up positions are zero.
-Open the .cpp file and enter these values under disp_X_size and disp_Y_size.  This sets the range of your screen.

2) Map out the touch coordinates: Prints the number coming back from your touch controller (0-4096) at a fixed location on the screen.
-Write down the extreme values (left, right,up, and down).
-Enter them into the uTouch.cpp  under touch_x_left, etc.. and disp_x_size, etc...
-Note: If you have a reversed coordinate system, modify the math under getX()or getY(). The new uTouch.cpp is simplified so the offset and scaling is straightforward. (Note reversed does not mean you can just add a *-1!)

3) Put them together! Prints X location at scaled X touch, Prints Y location at scaled Y touch.

4) Have fun! Draw pixel at touch location.

I should mention that this approach was initiated by

I had a similar problem with a Mega 2560 R3, Sainsmart 4.2", and the Elecfreaks v2.2 adapter. Having dug through the UTouch.cpp file, there is a section at about line 30:-


Well, you haven't posted your sketch so it's likely that the problem is actually there.

You only need a delay if your loop() is a really fast, tight loop, for example if that the data from UTouch is not being processed.
Do not IM me. I will not respond. Ask questions in the forum.


The problems described here are specific to the crappy SainSmart products which ARE NOT, nor will they ever be, supported by any of my libraries!



My thanks goes out to you for the very helpful libraries.  I did not realize the SainSmart controllers were so crappy.  I saw a bunch of brands with similar products and chose one based on the form factor.  Perhaps the design has some cross talk interference? But now I know.

This post is not to replace your code, but to provide a simple troubleshooting approach for those who are trying to unravel why their screen will not calibrate.  I don't know if all the posters who have this problem stem from SainSmart, but I can tell you, they are common.

The code I used that gave the error is the draw pixel program that come with the Utouch library.  Certain odd things like changing a color before drawing a pixel would also cause strange corruption of the coordinates.  Adding delay in other places did not work.  However, adding the delay after the data available command worked, which is the same delay that is listed in the paint colors example.  Is there a reason it is added there?


I have heard of these issues several times and every single time it has been with a SainSmart display. I have not had any reports of this on any of the supported display modules nor have I ever experienced it myself...

The delays in the QuickPaint example are there to "debounce" the "buttons".



Hello Henning and all,

I think the problem is related to 10kohm series resistors in Saintmart shields.
I also noticed that some delay is needed for the touch function.
The resistor with some capacitance in the circuitry slows down the CS signal and there's not enough time that data is valid in the touch AD-converter.
I found out that even 1us delay is enough.

Those resistors are ok in this kind of experimental kit, cause it allows other usage of same pins.


I have tested several displays with the old ITead shields which use 10k resistors... No problems with those.



Thanks for the responses... Wish I knew how bad these SainSmart boards were! My Mega just tanked with the error below, so I am returning it for an ElecFreaks or ITEAD touchscren.  To my novice eyes, all three of the websites looked pretty hokey!  But I feel much better knowing that the latter brands are tested.  Again, no intention at knocking your wonderful libraries, Henning --- a shame that a few bad apples can frustrate the whole process.

avrdude: stk500v2_getsync(): can't communicate with device: resp=0x01
---Loop back test worked fine; new bootloader using Nick Gammon's code loaded fine; but still cannot upload simple blink sketch.  Perhaps something to do with a faulty reset?  If you have ideas I will start another thread, but don't want to divert this one.
-SainSmart Mega 2560 R3


have you measured the waveform from CS signal with 10kohm resistor?
There's no issues related to SainSmart quality.



Please don't get pissy about this. Your libraries are gold and make UTFT displays usable. The work you've put in is unbelievable, and something I couldn't hope to replicate. Much appreciated.

Unfortunately, many of us are cost driven, so we buy the crappy Sainsmart displays. My work around is not me having a shot at you, it's giving (I hope) the poor among us a chance to use our crappy displays and your libraries to get an actual result.

I found that using Publisher, exporting as a .png, then using Paint to resize and your utilities can give whole font families and custom buttons that display perfectly. I'm progressively creating all the monospaced fonts I can in *.c format and will package them in a zip when I'm done.

If you're offended, I apologise. That was not my intention. I'm just trying to make UTFT displays usable to as many Arduino users as possible.



Just received my ElecFreaks 3.2" TFT touch screen and shield for the mega.  Works like a charm.  No problems with screen calibration, flickering, etc.. Now this is how a shield should work!  Best money I've spent on an Arduino project thus far.


I have a 'crappy' Sainsmart and was having problems with the touch screen. I was using the Utouch library. The button test example did not correlate the touch position with the screen display. Press 8 and 1 would light up, etc. Tried to calibrate and no difference. I then used an old library 'ITDB02 Touch Mod' and it now works fine.  I found the library deep in the Sainsmart website. It would have been nice for Sainsmart to provide a downloadable library which worked!


I then used an old library 'ITDB02 Touch Mod' and it now works fine.  I found the library deep in the Sainsmart website. It would have been nice for Sainsmart to provide a downloadable library which worked!

Please note that SainSmart does not have any rights to distribute that library. ITDB02_Touch is copyrighted by me and I have never given SainSmart any rights to use it commercially.



I FIXED IT on my Sainsmart board.

Me too has found the joy in arduino and bought a cheap sainsmart Mega 2560 from Asia, with shield and Touch-TFT.
The touch did not work at all, not possible to calibrate. I studied the data waves with oscilloscope and found that the signals from touch IC XPT2046, in to the mega was ugly due to the 10K resistor. The resistor should not be needed as the touch IC signal is 3,3V but it has over and undershoot pikes, so it probably needs some filtering. When I used the oscilloscope probe without damping resistor in probe it suddenly worked. The oscilloscope adds a load of 1M ohm. So I started experimenting. After replacing the 10K resistor at D_OUT with 50K and connecting 10K between touch IC side of the 50K and to ground, it works and after calibration everything is perfekt.
Strange though, as this still makes the data signal look bad with curved flanks. I wonder if the problem is in the TFT, sheild or in the mega board (all from sainsmart), but clearly the data signal needs some attention, it has nothing to do with the software.

Thanks Henning for the librarys.
/ Weman


..After replacing the 10K resistor at D_OUT with 50K and connecting 10K between touch IC side of the 50K and to ground, it works and after calibration everything is perfekt..
Thank you Weman, i fixed my crappy SainSmart board according your procedure.

Go Up