arduino mega + rtc (DS3231) + Adafruit Motor Shield V2.3 + touch shield

Hi guys.
the point of this project is to create automatic fertilizer dozer.
I use a RTC, because i don’t want it to “reset” if the power goes out, and I can check everytime when was the last “deliver”.
I’m using the touch screen so i can add features later,
the thing is, when I use everything but the motor shield everything go as planed, if I stack the touch shield on the motor shield the RTC doesn’t work anymore. (haven’t tested the motors yet)
I’m thinking this as something to the common use of SDA and SCL, but really I don’t have a clue.
( I use different tabs just to keep everything organized, because I plan for this code to grow a lot)

I got this motor shield because it says “Only two GPIO pins (SDA & SCL) plus 5v and GND. are required to drive the multiple motors, and since it’s I2C you can also connect any other I2C devices or shields to the same pins.” and I knew the RTC used SDA & SCL too.

Any advice?

thanks in advance,

datasheets/schematics:

Adafruit Motor Shield V2.3

RTC module:
RTC module datasheet
DS3231SN schematics

Touch Screen

here’s my code:

main tab

//MOTOR CONTROL
//include adafruit motor library
#include <AFMotor.h>
//define motor (pin)
AF_DCMotor motor(1);

//RTC
//include DS3231 (RTC) library
#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);
//rtc power pin
const int vcc_rtc = 53; 

//TOUCH Screen
#include <Adafruit_TFTLCD.h> 
#include <Adafruit_GFX.h>    
#include <TouchScreen.h>

#define LCD_CS A3 
#define LCD_CD A2 
#define LCD_WR A1 
#define LCD_RD A0 
#define LCD_RESET A4 

//caliration
#define TS_MINX 190
#define TS_MINY 163
#define TS_MAXX 950
#define TS_MAXY 902

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 8   // can be a digital pin
#define XP 9   // can be a digital pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

boolean buttonEnabled = true;

void setup() {
  //power RTC  (because it was easier)
  pinMode(vcc_rtc, OUTPUT);
  digitalWrite(vcc_rtc, HIGH);
 
  
  // Setup Serial connection
  Serial.begin(9600);
  Serial.print("Starting...");
  

  // Initialize the rtc object
  rtc.begin();

  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014

  //SCREEN
  tft.reset();
  tft.begin(0x9325);
  tft.setRotation(1);
  
  tft.fillScreen(BLACK);

  //Draw white frame
  tft.drawRect(0,0,319,240,WHITE);
  
  //Print "Hello" Text
  tft.setCursor(100,30);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.print("Hello");
  
  //Print "USER!" text 
  tft.setCursor(80,100);
  tft.setTextColor(RED);
  tft.setTextSize(4);
  tft.print("User!");
  
  //Create Red Button
  tft.fillRect(60,180, 200, 40, RED);
  tft.drawRect(60,180,200,40,WHITE);
  tft.setCursor(80,188);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Tell me the time!");

  

  

}

void loop() {
 
  
  screen_content();
 
}

RTC tab

void rtc_show() {
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");

  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.print(rtc.getTimeStr());
  Serial.print(" -- ");

  //send temp
   Serial.print(rtc.getTemp());
   Serial.println(" C ");

  //wait 1 second to show again  
  delay (1000);
}

Motors tab

void move_motor () {

  //this motor only works between 210 and the top speed (255)
  motor.setSpeed(255);
  motor.run(FORWARD);
 // motor.run(BACKWARD);
  delay(1000);  
  motor.run(RELEASE);
  delay(1000);
}

Screen tab

void screen_content () 
{
  TSPoint p = ts.getPoint();  //Get touch point
  
  if (p.z > ts.pressureThreshhold) {

   Serial.print("X = "); Serial.print(p.x);
   Serial.print("\tY = "); Serial.print(p.y);
   Serial.print("\n");
   
  //convert values to pixels
   p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
   p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240);
       
   if(p.x>60 && p.x<260 && p.y>180 && p.y<220 && buttonEnabled)// The user has pressed inside the red rectangle
   {
    buttonEnabled = false; //Disable button
        
    //This is important, because the libraries are sharing pins
   pinMode(XM, OUTPUT);
   pinMode(YP, OUTPUT);
    
    //Erase the screen
    tft.fillScreen(BLACK);
    
    //Draw frame
    tft.drawRect(0,0,319,240,WHITE);
    
    tft.setCursor(50,50);
    tft.setTextColor(WHITE);
    tft.setTextSize(3);
    tft.print(rtc.getTimeStr());
    rtc_show();
   }
   delay(10);  
  }
}

main.ino (2.27 KB)

motores.ino (256 Bytes)

RTC.ino (369 Bytes)

screen.ino (917 Bytes)

Check the notes at the head of this forum about how to post code. As it is, details on the hardware, about which you say nothing, is probably more relevant. You may find that the problem is simply inadequate power.

the rtc and tft may have conflicting pins you need schematics of both as well as motor shield

tried to make a decent post, with the datasheets/schematics

Are you able to use all the various hardware items individually on the Mega? i.e. the connections are OK?

kendoik: tried to make a decent post, with the datasheets/schematics

There were no schematics in what you posted. The motor shield and the RTC both use I2C so there shoudn't be pin conflict issues there but the LCD shield may have pin conflicts with the i2c pins. Do you have a datasheet or photos of the LCD shield?

Also, not sure which RTC library you are using but one of the more common ones named DS3231 (the rinkydink one) does not use the Wire library and instead implements the i2c protocol itself. The author of that library stated that he never tested it for peaceful coexistence with the Wire library so there might be issues when that library and the Wire library (which the motor shield library uses) are both using the AVR i2c pins & h/w. Read the bold yellow note here: http://www.rinkydinkelectronics.com/library.php?id=73

--- bill

One other thing. The DS3231 RTC module that you have is not intended to use a CR2032 battery. So if you are using a CR2032 as opposed to a LIR2032, you need to do a h/w modification to disable the charging circuit.
It is a simple modification that involves cutting a trace on the PCB.
I attached a photo of the trace that needs to be cut to disable the charging circuit.

— bill

I'm feeling this is a little too ambitious for my level of knowledge...

Nick_Pyner: Are you able to use all the various hardware items individually on the Mega? i.e. the connections are OK?

yes, individualy everything works

bperrybap: Do you have a datasheet or photos of the LCD shield?

I can't identify the right schematics... here are the photos |500x500 |500x500

bperrybap: The motor shield and the RTC both use I2C so there shoudn't be pin conflict issues there but the LCD shield may have pin conflicts with the i2c pins.

If I remove the motor shield (since it's not beeing "called" at ay moment in my code) the RTC+LCD work fine. when i put the motor shield back I can't even get readings from the RTC on the serial monitor