Audio stops playing after rtc is connected

I am trying to play a song using arduino while at the same time display time using a rtc board using the tmrpcm library. However, if the rtc board is connected no sound is played.

I do understand that the tmrpcm may be interfering with the wire.h library but even commenting the same doesn't resolve the problem.

I am using the following code:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <RTClib.h>
#include<Wire.h>
#include <Arduino.h>
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
#include "SevenSegmentFun.h"
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

const int ProxSensor=3;
int inputVal = 0;
#define CLK 9
#define DIO 7

// The amount of time (in milliseconds) between tests
#define TEST_DELAY   2000

SevenSegmentFun    display(CLK, DIO);

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(9600);
  if(!SD.begin(SD_ChipSelectPin))
  {
    Serial.println("SD fail");
    return;
  }
  while (!Serial); // for Leonardo/Micro/Zero
  tmrpcm.speakerPin=9;
  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(2019, 10, 19, 01,41,00));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  pinMode(ProxSensor,INPUT);    //Pin 2 is connected to the output of proximity sensor
  display.begin();
}

void loop(){
  DateTime now = rtc.now();
  display.print((now.unixtime() / 86400L )-17963);
  Serial.print((now.unixtime() / 86400L )-17963);
  while(digitalRead(ProxSensor)==LOW)      //Check the sensor output
  {
    tmrpcm.setVolume(5);
    tmrpcm.play("test.wav");
      repeat:
    if(digitalRead(ProxSensor==LOW))
      goto repeat;
  }
}

Can anyone please help me?

I do not think that the problem is the RTC but the seven segment display.
The problem is a double pin definition - take a look here:

#define CLK 9
...
tmrpcm.speakerPin=9;

You try to use pin 9 for two purposes:
As pin for speaker output of TMRpcm (on Arduino UNO).
As clock pin for seven segment display.

Try to use another pin for "CLK".

uxomm:
I do not think that the problem is the RTC but the seven segment display.
The problem is a double pin definition - take a look here:

#define CLK 9

...
tmrpcm.speakerPin=9;




You try to use pin 9 for two purposes:
As pin for speaker output of TMRpcm (on Arduino UNO).
As clock pin for seven segment display.

Try to use another pin for "CLK".

I did change that.... The problem still persists.
The problem is when I remove the rtc, the audio functions smoothly but when it is connected no audio is played

What Arduino board are you using?
Can you give a link to you libraries?
There is more than one RTC library called "RTClib". :slight_smile:

When I take an Arduino UNO, a SD card module and a DS1307 RTC with this sketch, I get a time stamp every second via serial and i can turn on music sending p via serial and stop music sending s:

// TMRpcm und RTC

//TMRpcm
// https://github.com/TMRh20/TMRpcm
#include <SD.h>               // need to include the SD library
#define SD_ChipSelectPin 10
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm;                // create an object for use in this sketch

// RTC
// https://github.com/adafruit/RTClib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

unsigned long lastMillis;
const unsigned long interval = 1000;

void setup() {
  //TMRpcm
  tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

  //RTC
  Wire.begin();
  rtc.begin();
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(__DATE__, __TIME__));
  // to adjust the rtc to 2018-08-24 15:36:09
  // rtc.adjust(DateTime(2018, 8, 24, 15, 36, 9));

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");
    return;   // don't do anything more if not
  }
}

void loop() {
  if (Serial.available()) {
    int inByte = Serial.read();
    if (inByte == 'p') {            //send the letter p over the serial monitor to start playback
      tmrpcm.play("music");
    }
    if (inByte == 's') {            //send the letter s to stop playback
      tmrpcm.stopPlayback();
    }
  }

  if (millis() - lastMillis > interval) {
    lastMillis = millis();
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('-');
    Serial.print(now.month(), DEC);
    Serial.print('-');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }
}

I suspect the seven segment display may cause the problem (please post a link to this library). It may use timer 1, which is also used by TMRpcm. So there might be a timer conflict.

uxomm:
What Arduino board are you using?
Can you give a link to you libraries?
There is more than one RTC library called "RTClib". :slight_smile:

When I take an Arduino UNO, a SD card module and a DS1307 RTC with this sketch, I get a time stamp every second via serial and i can turn on music sending p via serial and stop music sending s:

// TMRpcm und RTC

//TMRpcm
// GitHub - TMRh20/TMRpcm: Arduino library for asynchronous playback of PCM/WAV files direct from SD card. Arduino Uno,Nano,Mega etc supported
#include <SD.h>              // need to include the SD library
#define SD_ChipSelectPin 10
#include <TMRpcm.h>          //  also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm;                // create an object for use in this sketch

// RTC
// GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

unsigned long lastMillis;
const unsigned long interval = 1000;

void setup() {
  //TMRpcm
  tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

//RTC
  Wire.begin();
  rtc.begin();
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(DATE, TIME));
  // to adjust the rtc to 2018-08-24 15:36:09
  // rtc.adjust(DateTime(2018, 8, 24, 15, 36, 9));

Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");
    return;  // don't do anything more if not
  }
}

void loop() {
  if (Serial.available()) {
    int inByte = Serial.read();
    if (inByte == 'p') {            //send the letter p over the serial monitor to start playback
      tmrpcm.play("music");
    }
    if (inByte == 's') {            //send the letter s to stop playback
      tmrpcm.stopPlayback();
    }
  }

if (millis() - lastMillis > interval) {
    lastMillis = millis();
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('-');
    Serial.print(now.month(), DEC);
    Serial.print('-');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }
}




I suspect the seven segment display may cause the problem (please post a link to this library). It may use timer 1, which is also used by TMRpcm. So there might be a timer conflict.

This is the link to the library I'm using

uxomm:
What Arduino board are you using?
Can you give a link to you libraries?
There is more than one RTC library called "RTClib". :slight_smile:

When I take an Arduino UNO, a SD card module and a DS1307 RTC with this sketch, I get a time stamp every second via serial and i can turn on music sending p via serial and stop music sending s:

// TMRpcm und RTC

//TMRpcm
// GitHub - TMRh20/TMRpcm: Arduino library for asynchronous playback of PCM/WAV files direct from SD card. Arduino Uno,Nano,Mega etc supported
#include <SD.h>               // need to include the SD library
#define SD_ChipSelectPin 10
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm;                // create an object for use in this sketch

// RTC
// GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

unsigned long lastMillis;
const unsigned long interval = 1000;

void setup() {
 //TMRpcm
 tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

//RTC
 Wire.begin();
 rtc.begin();
 // following line sets the RTC to the date & time this sketch was compiled
 // rtc.adjust(DateTime(DATE, TIME));
 // to adjust the rtc to 2018-08-24 15:36:09
 // rtc.adjust(DateTime(2018, 8, 24, 15, 36, 9));

Serial.begin(9600);
 if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
   Serial.println("SD fail");
   return;   // don't do anything more if not
 }
}

void loop() {
 if (Serial.available()) {
   int inByte = Serial.read();
   if (inByte == 'p') {            //send the letter p over the serial monitor to start playback
     tmrpcm.play("music");
   }
   if (inByte == 's') {            //send the letter s to stop playback
     tmrpcm.stopPlayback();
   }
 }

if (millis() - lastMillis > interval) {
   lastMillis = millis();
   DateTime now = rtc.now();
   Serial.print(now.year(), DEC);
   Serial.print('-');
   Serial.print(now.month(), DEC);
   Serial.print('-');
   Serial.print(now.day(), DEC);
   Serial.print(' ');
   Serial.print(now.hour(), DEC);
   Serial.print(':');
   Serial.print(now.minute(), DEC);
   Serial.print(':');
   Serial.print(now.second(), DEC);
   Serial.println();
 }
}




I suspect the seven segment display may cause the problem (please post a link to this library). It may use timer 1, which is also used by TMRpcm. So there might be a timer conflict.

I tried using this code and once again the same problem occurred. It only happens when the rtc is connected to the arduino. It seems as if the rtc is somehow trying to interfere with the processing of the tmrpcm library.
I am using arduino nano, with my sd card module powered from the icsp pins.

sdey:
GitHub - bremme/arduino-tm1637: Arduino library for using a 4 digit seven segment display with TM1636 or TM1637 driver IC

This is the link to the library I'm using

But you were asked for the RTC library called "RTClib" not the display module.