Touchscreen and SD card slot connection


I have attached a touchscreen TFT display with SD card slot (320QVT). I have the problem, after initializing the SD card stops working touch (returns the maximum value). Initialize SD is OK. The touch panel is connected to the Arduino Mega 2560 to pins MISO 50, MOSI 51, SCK 52, SS 53. SD card I have connected to pins MISO 50, MOSI 51, SCK 52 and SS pin 10. Can someone please advise me where is the problem? Thank you for all ideas and advices.

#include <UTFT.h>
#include <Wire.h>
#include <WProgram.h>
#include <dht11.h>
#include <UTouch.h>
//#include <SD.h>
#include <SPI.h>

#include <SdFat.h>
SdFat sd;
SdFile myFile;

extern uint8_t SmallFont[];

UTFT myGLCD(ITDB32S,38,39,40,41);
UTouch myTouch(52,53,50,51,2);

const int chipSelect = 10;
int x, y;

void setup()
  pinMode(10, OUTPUT);

  if (!sd.begin(chipSelect)) {

The problem is the library you're using. UTouch does SPI over a bit-banging interface (SPI software emulation) so as soon as you initialize the SD library the hardware configuration is turned back to SPI hardware mode and the pins of the touch displays aren't accessible for the UTouch library anymore.

The good news are that it's not that complicated to modify the UTouch library to use the SPI hardware instead of accessing the pin registers directly or even easier: just call SPI.begin() after every operation done on the touchscreen and initTouch() before (although the former is the smarter and faster solution).

Thank you very much for your reply, I am very glad that it has a solution. The best option is probably really edit a library UTouch. Unfortunately, I am still not success .. Can you please help me and write where I make the necessary changes in the file UTouch.h? Thank you very much for your response.

Post the code that you tried to modify! And tell us which of the two option you favor respectively choose to use in your project (just in case it's not obvious from your code).

I prefer to change the library UTouch, but honestly I do not know which part of the code I have to make the change. Thank you for your help.

#ifndef UTouch_h
#define UTouch_h

#define UTOUCH_VERSION	124

#if defined(__AVR__)
	#include "Arduino.h"
	#include "hardware/avr/HW_AVR_defines.h"
#elif defined(__PIC32MX__)
	#include "WProgram.h"
	#include "hardware/pic32/HW_PIC32_defines.h"
#elif defined(__arm__)
	#include "Arduino.h"
	#include "hardware/arm/HW_ARM_defines.h"

#define PORTRAIT			0
#define LANDSCAPE			1

#define PREC_LOW			1
#define PREC_MEDIUM			2
#define PREC_HI				3
#define PREC_EXTREME		4

class UTouch
		int16_t	TP_X ,TP_Y;

				UTouch(byte tclk, byte tcs, byte tdin, byte dout, byte irq);

		void	InitTouch(byte orientation = LANDSCAPE);
		void	read();
		bool	dataAvailable();
		int16_t	getX();
		int16_t	getY();
		void	setPrecision(byte precision);

		void	calibrateRead();
		regtype *P_CLK, *P_CS, *P_DIN, *P_DOUT, *P_IRQ;
		regsize B_CLK, B_CS, B_DIN, B_DOUT, B_IRQ;
		byte	T_CLK, T_CS, T_DIN, T_DOUT, T_IRQ;
		long	_default_orientation;
		byte	orient;
		byte	prec;
		byte	display_model;
		long	disp_x_size, disp_y_size, default_orientation;
		long	touch_x_left, touch_x_right, touch_y_top, touch_y_bottom;

		void	touch_WriteData(byte data);
		word	touch_ReadData();


One thing I see that could cause problems is you are activating the SD SPI, then using the SPI bus to start the touchscreen. Set D10 HIGH also. That will disable the SD SPI.

void setup()
  pinMode(10, OUTPUT);
// add this

You should post the contents of the UTouch.cpp file also.

edit: The SD library has a bug that doesn't release the SPI MISO line. That could also cause problems. Read reply #15 here:

Thus it appears that already could work. Before working with the SD card should be as written below, call SPI.begin () and after working with SD card call SPI.end () and initTouch (). It's a simple option, but pleasant work would be to modify the library UTouch. Thank you so much for your advice and if anyone knows how to edit a library UTouch, it would be nice.

    File dataFile ="setup.txt", FILE_READ);

Hi, I am trying to achieve the same thing, I'd like to convert the UTouch library to use hardware SPI instead of bit banging, please see my post here.

If anyone could help me out that would be great :)