Using TFT Display and Motor Shield on Arduino Mega [SOLVED]

Hi

My planned project is to use a TFT touch display to control dual stepper motors by having a series of touch buttons to control the stepper motors.

I have individually got both a TFT display working on a Arduino Mega and also the motor shield working on the same Arduino Mega but not both at the same time. In both cases they have been plugged into the Uno equivalent pins on the Mega.

I would like to have either the TFT display mounted on the Mega or the motor shield mounted on the Mega and the other (motor shield or TFT display respectively) connected to the spare pins on the Mega.

The TFT display needs to use the MCUFRIEND_kbv.h library to work and the motor shield needs the Adafruit Motor shield library to work.

I have spent the last three days trolling the web to see if I can find a way to plug the TFT display board into the spare (non Uno) pins on the Mega and have found that Adafruit had some options but none of them allow the TFT display to work (I only get a white screen), as it appears to only work with the MCUFRIEND_kbv.h library . In this case the I would use jumper leads to connect the pins of the TFT shield to the spare (non Uno) pins on the Mega

I have not been able to find a variant of MCUFRIEND_kbv that is specifically designed to allow you to select the pins that are used so that I can use the spare (non Uno) pins on the Mega. Sorry I dont know how these pins are called.

The other option is to use plug the TFT in normally and used the jumper leads to connect the motor shield to the spare (non Uno) pins on the Mega.

Any thoughts or suggestions on the correct approach are welcomed.

gregmew:
Hi

My planned project is to use a TFT touch display to control dual stepper motors by having a series of touch buttons to control the stepper motors.

I have individually got both a TFT display working on a Arduino Mega and also the motor shield working on the same Arduino Mega but not both at the same time. In both cases they have been plugged into the Uno equivalent pins on the Mega.

I would like to have either the TFT display mounted on the Mega or the motor shield mounted on the Mega and the other (motor shield or TFT display respectively) connected to the spare pins on the Mega.

The TFT display needs to use the MCUFRIEND_kbv.h library to work and the motor shield needs the Adafruit Motor shield library to work.

I have spent the last three days trolling the web to see if I can find a way to plug the TFT display board into the spare (non Uno) pins on the Mega and have found that Adafruit had some options but none of them allow the TFT display to work (I only get a white screen), as it appears to only work with the MCUFRIEND_kbv.h library . In this case the I would use jumper leads to connect the pins of the TFT shield to the spare (non Uno) pins on the Mega

I have not been able to find a variant of MCUFRIEND_kbv that is specifically designed to allow you to select the pins that are used so that I can use the spare (non Uno) pins on the Mega. Sorry I dont know how these pins are called.

The other option is to use plug the TFT in normally and used the jumper leads to connect the motor shield to the spare (non Uno) pins on the Mega.

Any thoughts or suggestions on the correct approach are welcomed.

Can you post the links to the TFT display and the motor shield that you are using?
Are you using an original Arduino Mega2560 or a clone?

The TFT Display is sold by Jaycar in Australia:

The Arduino Mega is also from Jaycar.
I suspect that it is a clone.

The motor shield is from Altronics:

The Stepper motor I am using is also from Altronics:

http://www.altronics.com.au/p/j0070-42-step-stepper-motor-and-lead/

Ultimately I need to run two stepper motors and this shield supports running two stepper motors so I will need additional digital I/O to drive the second stepper motor in the future.

By plugging in the TFT to the Mega using the Uno pins on the Mega I can run the demo program from MCUFRIEND_kbv called graphictest_kbv and it appears to work fine.

Running the diagnose_TFT_support example gives : tft.readID() finds: ID = 0x7783

Similarly if I plug the motor shield into the Uno pins on the Mega I can run the demo program from the Adafruit Motor Shield Library called StepperTest and it all works fine. The stepper motor goes through its test routine as indicated in the code.

As indicated in my first post what I need to be able to do is use either the TFT display or the motor shield and connect them via jumpers to the pins that are not covered by the other shield when it is plugged into the Uno pins on the Mega.

Thank you for taking the time to help.

I realised after I posted this yesterday that I could have asked for help on the Display Forum so if I need to go there please let me know.

please go back and edit you reply and use proper url tags.

Details of my previous post with url tags. Sorry I missed this when I read the How to use this forum page.
If I have still got this wrong please let me know but it seems to work on the preview. Can you also advise where there is a description on how to use the url tags. I sort of found it as an example on the bottom of the following page and played with it until it appears to be working.

How to use this forum page

[The TFT Display is sold by Jaycar in Australia:

](240x320 LCD Touch Screen for Arduino | Jaycar Electronics)

[The Arduino Mega is also from Jaycar. I suspect that it is a clone.

](Duinotech MEGA 2560 r3 Main Board | Jaycar Electronics)

[The motor shield is from Altronics:

](Funduino L293D Motor Shield For Arduino - Altronics)

The Stepper motor I am using is also from Altronics:

Ultimately I need to run two stepper motors and this shield supports running two stepper motors so I will need additional digital I/O to drive the second stepper motor in the future.

By plugging in the TFT to the Mega using the Uno pins on the Mega I can run the demo program from MCUFRIEND_kbv called graphictest_kbv and it appears to work fine.

Running the diagnose_TFT_support example gives :

tft.readID() finds: ID = 0x7783

Similarly if I plug the motor shield into the Uno pins on the Mega I can run the demo program from the Adafruit Motor Shield Library called StepperTest and it all works fine. The stepper motor goes through its test routine as indicated in the code.

As indicated in my first post what I need to be able to do is use either the TFT display or the motor shield and connect them via jumpers to the pins that are not covered by the other shield when it is plugged into the Uno pins on the Mega.

Given that the TFT display seems more intensive in communication with the Mega I am wondering if the answer lies in a solution with the TFT display mounted and the stepper motor bridge connected by flying leads?

Thank you for taking the time to help.

gregmew:
Details of my previous post with url tags. Sorry I missed this when I read the How to use this forum page.
If I have still got this wrong please let me know but it seems to work on the preview. Can you also advise where there is a description on how to use the url tags. I sort of found it as an example on the bottom of the following page and played with it until it appears to be working.

Similarly if I plug the motor shield into the Uno pins on the Mega I can run the demo program from the Adafruit Motor Shield Library called StepperTest and it all works fine. The stepper motor goes through its test routine as indicated in the code.

As indicated in my first post what I need to be able to do is use either the TFT display or the motor shield and connect them via jumpers to the pins that are not covered by the other shield when it is plugged into the Uno pins on the Mega.

Given that the TFT display seems more intensive in communication with the Mega I am wondering if the answer lies in a solution with the TFT display mounted and the stepper motor bridge connected by flying leads?

Thank you for taking the time to help.

thank you.

I need to read the datasheets and schematics. this may take me a day or two.

The TFT display should be mounted on the Mega2560 clone.

hello

How good are you at solder and unsoldering?

I need to do more research on this.
Can the header pins on the motor shield be changed to
female long pin headers?
This would allow for the motor shield and TFT to be "stacked"

I have to look at the schematics and libraries to know if this is possible.

I can do the soldering if I have to. I also agree that the TFT should be mounted on the Mega. I can hardwire this shield and mount it remotely if I need to.

From my read of the following:

Understanding the Adafruit Motor Shield Library

most of the pins on the Uno part of the Mega are used for the motor shield.

the following is from AFMotor.h which I have included below these snipits and is the library that is called.

// Bit positions in the 74HCT595 shift register output
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR4_A 0
#define MOTOR4_B 6
#define MOTOR3_A 5
#define MOTOR3_B 7
// Arduino pin names for interface to 74HCT595 latch
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

Maybe there are more that I don't recognise??

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

/*
 * Usage Notes:
 * For PIC32, all features work properly with the following two exceptions:
 *
 * 1) Because the PIC32 only has 5 PWM outputs, and the AFMotor shield needs 6
 *    to completely operate (four for motor outputs and two for RC servos), the
 *    M1 motor output will not have PWM ability when used with a PIC32 board.
 *    However, there is a very simple workaround. If you need to drive a stepper
 *    or DC motor with PWM on motor output M1, you can use the PWM output on pin
 *    9 or pin 10 (normally use for RC servo outputs on Arduino, not needed for 
 *    RC servo outputs on PIC32) to drive the PWM input for M1 by simply putting
 *    a jumber from pin 9 to pin 11 or pin 10 to pin 11. Then uncomment one of the
 *    two #defines below to activate the PWM on either pin 9 or pin 10. You will
 *    then have a fully functional microstepping for 2 stepper motors, or four
 *    DC motor outputs with PWM.
 *
 * 2) There is a conflict between RC Servo outputs on pins 9 and pins 10 and 
 *    the operation of DC motors and stepper motors as of 9/2012. This issue
 *    will get fixed in future MPIDE releases, but at the present time it means
 *    that the Motor Party example will NOT work properly. Any time you attach
 *    an RC servo to pins 9 or pins 10, ALL PWM outputs on the whole board will
 *    stop working. Thus no steppers or DC motors.
 * 
 */
// <BPS> 09/15/2012 Modified for use with chipKIT boards


#ifndef _AFMotor_h_
#define _AFMotor_h_

#include <inttypes.h>
#if defined(__AVR__)
    #include <avr/io.h>

    //#define MOTORDEBUG 1

    #define MICROSTEPS 16                       // 8 or 16

    #define MOTOR12_64KHZ _BV(CS20)             // no prescale
    #define MOTOR12_8KHZ _BV(CS21)              // divide by 8
    #define MOTOR12_2KHZ _BV(CS21) | _BV(CS20)  // divide by 32
    #define MOTOR12_1KHZ _BV(CS22)              // divide by 64

    #define MOTOR34_64KHZ _BV(CS00)             // no prescale
    #define MOTOR34_8KHZ _BV(CS01)              // divide by 8
    #define MOTOR34_1KHZ _BV(CS01) | _BV(CS00)  // divide by 64
    
    #define DC_MOTOR_PWM_RATE   MOTOR34_8KHZ    // PWM rate for DC motors
    #define STEPPER1_PWM_RATE   MOTOR12_64KHZ   // PWM rate for stepper 1
    #define STEPPER2_PWM_RATE   MOTOR34_64KHZ   // PWM rate for stepper 2
    
#elif defined(__PIC32MX__)
    //#define MOTORDEBUG 1
    
    // Uncomment the one of following lines if you have put a jumper from 
    // either pin 9 to pin 11 or pin 10 to pin 11 on your Motor Shield.
    // Either will enable PWM for M1
    //#define PIC32_USE_PIN9_FOR_M1_PWM
    //#define PIC32_USE_PIN10_FOR_M1_PWM

    #define MICROSTEPS 16       // 8 or 16

    // For PIC32 Timers, define prescale settings by PWM frequency
    #define MOTOR12_312KHZ  0   // 1:1, actual frequency 312KHz
    #define MOTOR12_156KHZ  1   // 1:2, actual frequency 156KHz
    #define MOTOR12_64KHZ   2   // 1:4, actual frequency 78KHz
    #define MOTOR12_39KHZ   3   // 1:8, acutal frequency 39KHz
    #define MOTOR12_19KHZ   4   // 1:16, actual frequency 19KHz
    #define MOTOR12_8KHZ    5   // 1:32, actual frequency 9.7KHz
    #define MOTOR12_4_8KHZ  6   // 1:64, actual frequency 4.8KHz
    #define MOTOR12_2KHZ    7   // 1:256, actual frequency 1.2KHz
    #define MOTOR12_1KHZ    7   // 1:256, actual frequency 1.2KHz

    #define MOTOR34_312KHZ  0   // 1:1, actual frequency 312KHz
    #define MOTOR34_156KHZ  1   // 1:2, actual frequency 156KHz
    #define MOTOR34_64KHZ   2   // 1:4, actual frequency 78KHz
    #define MOTOR34_39KHZ   3   // 1:8, acutal frequency 39KHz
    #define MOTOR34_19KHZ   4   // 1:16, actual frequency 19KHz
    #define MOTOR34_8KHZ    5   // 1:32, actual frequency 9.7KHz
    #define MOTOR34_4_8KHZ  6   // 1:64, actual frequency 4.8KHz
    #define MOTOR34_2KHZ    7   // 1:256, actual frequency 1.2KHz
    #define MOTOR34_1KHZ    7   // 1:256, actual frequency 1.2KHz
    
    // PWM rate for DC motors.
    #define DC_MOTOR_PWM_RATE   MOTOR34_39KHZ
    // Note: for PIC32, both of these must be set to the same value
    // since there's only one timebase for all 4 PWM outputs
    #define STEPPER1_PWM_RATE   MOTOR12_39KHZ
    #define STEPPER2_PWM_RATE   MOTOR34_39KHZ
    
#endif

// Bit positions in the 74HCT595 shift register output
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR4_A 0
#define MOTOR4_B 6
#define MOTOR3_A 5
#define MOTOR3_B 7

// Constants that the user passes in to the motor calls
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4

// Constants that the user passes in to the stepper calls
#define SINGLE 1
#define DOUBLE 2
#define INTERLEAVE 3
#define MICROSTEP 4

/*
#define LATCH 4
#define LATCH_DDR DDRB
#define LATCH_PORT PORTB

#define CLK_PORT PORTD
#define CLK_DDR DDRD
#define CLK 4

#define ENABLE_PORT PORTD
#define ENABLE_DDR DDRD
#define ENABLE 7

#define SER 0
#define SER_DDR DDRB
#define SER_PORT PORTB
*/

// Arduino pin names for interface to 74HCT595 latch
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

class AFMotorController
{
  public:
    AFMotorController(void);
    void enable(void);
    friend class AF_DCMotor;
    void latch_tx(void);
    uint8_t TimerInitalized;
};

class AF_DCMotor
{
 public:
  AF_DCMotor(uint8_t motornum, uint8_t freq = DC_MOTOR_PWM_RATE);
  void run(uint8_t);
  void setSpeed(uint8_t);

 private:
  uint8_t motornum, pwmfreq;
};

class AF_Stepper {
 public:
  AF_Stepper(uint16_t, uint8_t);
  void step(uint16_t steps, uint8_t dir,  uint8_t style = SINGLE);
  void setSpeed(uint16_t);
  uint8_t onestep(uint8_t dir, uint8_t style);
  void release(void);
  uint16_t revsteps; // # steps per revolution
  uint8_t steppernum;
  uint32_t usperstep, steppingcounter;
 private:
  uint8_t currentstep;

};

uint8_t getlatchstate(void);

#endif

I have connected the motor shield to the Mega and confirmed correct operation of a piece of example code and then manually connected all the digital pins except 13. I also connected +5V and 0V for power and it sort of works but not correctly. just vibrates the stepper motor and moves a little bit where it should do a full revolution as part of the test code.

This implies to me that there is another pin that needs connecting.

Time to pack it in tonight.

I will try a few more pin connections tomorrow

Thanks again for your help.

Hi

I have solved this by getting a special code written for the touch Screen by David Prentice (many thanks to David) to allow me to connect it directly to PORTA, PORTC and PORTK.

The 8 data Data pins are connected to PORTA
The digital control pins are connected to PORTC
The two analogue pins are connected to PORTK

This leaves the normal "Uno" pins on the Mega available to mount the motor shield.

I can now control the stepper motor using the touch screen.