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 <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 8
#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.println("SD fail");
  while (!Serial); // for Leonardo/Micro/Zero
  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

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
      goto repeat;

Can anyone please help me?

2 Answers 2


The TMRpcm and one of your other libraries might use the same timer on Uno which causes one of them to break. Either get a Arduino Mega which can run TMRpcm on TIMER3,4 or 5 or use libraries that don't require a timer.

  • 2
    In what way does the Wire library use a timer?
    – Majenko
    Oct 26, 2019 at 14:25
  • @Majenko probably for timing the SCK pulses.
    – Coder_fox
    Oct 26, 2019 at 17:57
  • I can't find any reference of what it is used for and if used at all but it seems that the usage of the TWBR breaks many things related to timer one.
    – Coder_fox
    Oct 26, 2019 at 18:07
  • Can you cite some evidence for this? This is new to me - and the TWI peripheral has nothing to do with any timer peripherals - the baud rate generator is internal to TWI and uses the system clock as its source.
    – Majenko
    Oct 26, 2019 at 23:32
  • @SoumendraDey, explain please how did this answer help, because it should not
    – Juraj
    Oct 27, 2019 at 9:20

The problem is

  goto repeat;

ProxSensor==LOW is false so 0. you read the pin 0 and the outcome is random. The surrounding wiring can affect that. If digitalRead(0) is zero the wav starts playing immediately again. At least use

while (digitalRead(ProxSensor) == LOW);
  • I am already using while (digitalRead(ProxSensor) == LOW) in my code Oct 26, 2019 at 17:46
  • @SoumendraDey, read the answer again and slow
    – Juraj
    Oct 26, 2019 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.