Having trouble with DS1307 mounting in an Arduino Nano

Hi guys!

I'm trying to connect my RTC (DS1307) to my arduino nano.

These are the steps that I have made.

  1. Connect the rtc to the arduino nano

VCC -> 5V
GND -> GND
SDA -> A4
SCL -> A5

  1. Upload a I2C scanner to see if the rtc can be read

It says address found at 0x50 and 0x60
So I guess the rtc is working fine

  1. Set the time by running the DS1307 library

  2. I commented the rtc.setDOW rtc.setTime and rtc.setDate

  3. Upload it to the nano

  4. I uncommented the rtc.setDOW rtc.setTime and rtc.setDate

  5. Upload it to the nano

  6. I tried looking at the serial monitor but it displays nothing. Not even squares or some weird characters. I checked the other baud rates but it still gives me nothing.

I think that we need to see your code.

groundFungus:
I think that we need to see your code.

Oh, sorry. Here' s the code

// DS1307_Serial_Easy
// Copyright (C)2016 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS1307-library to
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS1307: SDA pin -> Arduino Analog 4 or the dedicated SDA pin
// SCL pin -> Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS1307: SDA pin -> Arduino Digital 2 or the dedicated SDA pin
// SCL pin -> Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS1307: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin
// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS1307: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the
// hardware I2C interfaces.
//
// You can connect the DS1307 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//

#include <DS1307.h>

// Init the DS1307
DS1307 rtc(SDA, SCL);

void setup()
{
// Setup Serial connection
Serial.begin(115200);
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}

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

// Set the clock to run-mode
rtc.halt(false);

// The following lines can be uncommented to set the time
rtc.setDOW(MONDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(0, 24, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(27, 3, 2018); // Set the date to October 3th, 2010
}

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

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

// Send time
Serial.println(rtc.getTimeStr());

// Wait one second before repeating :slight_smile:
delay (1000);
}

According to the Adafruit tutorial the DS1307 is at baud rate of 57600.

You may want to read the Adafruit tutorial.

.

ieee488:
According to the Adafruit tutorial the DS1307 is at baud rate of 57600.

You may want to read the Adafruit tutorial.

.

I tried editing Serial.begin(115200) to 57600 but it still gives me nothing :frowning:

(deleted)

spycatcher2k:
have you confirmed you can Serial.print from another sketch?

I tried using the same rtc on my mega and it works perfectly fine. But when I switched to nano (I have double checked the connections using a connectivity test) the serial monitor is not flashing any result

(deleted)

spycatcher2k:
NOT what I asked!
Can you Serial.print from another sketch on your nano?

Hi, I don't know if this is what you are referring to but I tried this tutorial and I successfully serial.print from that sketch. http://www.instructables.com/id/Setting-the-DS1307-Real-Time-Clock-using-the-Seria/

If it's not what you meant. Let me know. Sorry but Im not really an expert in arduino

I connected a DS1307 (TinyRTC) module to my Uno, uploaded and ran the posted code. Seems to work fine on the Uno. Nano is 5V, right? Are you using a bare chip or a module (which one)?

Monday 27.03.2018 -- 00:24:00
Monday 27.03.2018 -- 00:24:01
Monday 27.03.2018 -- 00:24:02
Monday 27.03.2018 -- 00:24:03
Monday 27.03.2018 -- 00:24:04
Monday 27.03.2018 -- 00:24:05


Here's my setup

UPDATE

I tried uploading a new sketch I found online.

// Example program for the MD_DS1307 library
//
// Allows testing of all library functions and RTC chip from the Serial Monitor.

#include <MD_DS1307.h>
#include <Wire.h>

#define PRINTS(s) Serial.print(F(s));
#define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }

void setup()
{
Serial.begin(57600);
PRINTS("[MD_DDS1307_Test]");

usage();
}

void usage(void)
{
PRINTS("\n?\thelp - this message");
PRINTS("\n\ntr\tread the current time");
PRINTS("\ntw yyyymmdd hhmmss dw\twrite the current date, time and day of week (1-7)");
PRINTS("\n\nrr\tread the contents of RAM buffer");
PRINTS("\nrw aa nn vv [vv...]\twrite RAM address hex aa with nn hex values vv");
PRINTS("\n\ns\tstatus of the RTC");
PRINTS("\nd\tcalculate day of week from current date");
PRINTS("\n\nc n v\twrite the value v to status n, where n is");
PRINTS("\n\t0 - Clock Halt (n 0=run, 1=halt)");
PRINTS("\n\t1 - SQW Enable(n 0=halt, 1=run)");
PRINTS("\n\t2 - SQW Type (on) (n 1=1Hz, 2=4Mhz, 3=8Mhz, 4=32MHz)");
PRINTS("\n\t3 - SQW Type (off) (n 0=low, 1=high)");
PRINTS("\n\t4 - 12 hour mode (n 0=24h, 1=12h)\n");
}

const char *dow2String(uint8_t code)
{
static const char *str[] = { "---", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

return(str

);
}


const char *ctl2String(uint8_t code)
{
  static const char *str[] = 
  {
    "CLOCK_HALT",
    "SQW_RUN",
    "SQW_TYPE_ON",
    "SQW_TYPE_OFF",
    "12H MODE"
  };

  return(str[code]);
}

const char *sts2String(uint8_t code)
{
  static const char *str[] = 
  {
    "ERROR",
    "ON",
    "OFF",
    "1Hz",
    "4KHz",
    "8KHz",
    "32KHz",
    "HIGH",
    "LOW"
  };

  return(str[code]);
}

uint8_t htoi(char c)
{
  c = toupper(c);
  
  if (c >= '0' && c <= '9')
      return(c - '0');
  else if (c >= 'A' && c <= 'F')
      return(c - 'A' + 10);
  else
      return(0);
}
        
uint8_t i2dig(uint8_t mode)
// input 2 digits in the specified base
{
  uint8_t  v = 0;
  char    c[3] = { "00" };

  c[0] = ReadNext();
  c[1] = ReadNext();

  switch (mode)
  {
    case DEC: v = atoi(c);  break;
    case HEX: v = (htoi(c[0]) << 4) + htoi(c[1]); ;  break;
  }
  
  return(v);
}

char htoa(uint8_t i)
{
  if (i >= 0 && i <= 9)
    return(i + '0');
  if (i >= 10 && i <= 15)
    return(i - 10 + 'a');

  return('?');
}

const char *p2dig(uint8_t v, uint8_t mode)
// print 2 digits leading zero
{
  uint8_t n = 0;
  static char c[3] = { "00" };

  switch(mode)
  {
    case HEX:
    {
      c[0] = htoa((v >> 4) & 0xf);
      c[1] = htoa(v & 0xf);
    }
    break;
  
    case DEC:
    {
      c[0] = ((v / 10) % 10) + '0';
      c[1] = (v % 10) + '0';
    }
    break;
  }

  return(c);
}

void showStatus()
{
  PRINT("\nClock Halt:\t", sts2String(RTC.status(DS1307_CLOCK_HALT)));
  PRINT("\nIs running:\t", RTC.isRunning());
  PRINT("\nSQW Output:\t", sts2String(RTC.status(DS1307_SQW_RUN)));
  PRINT("\nSQW Type (on):\t", sts2String(RTC.status(DS1307_SQW_TYPE_ON)));
  PRINT("\nSQW Type (off):\t", sts2String(RTC.status(DS1307_SQW_TYPE_OFF)));
  PRINT("\n12h mode:\t", sts2String(RTC.status(DS1307_12H)));
}

void printTime()
{
  PRINT("", RTC.yyyy);
  PRINT("-", p2dig(RTC.mm, DEC));
  PRINT("-", p2dig(RTC.dd, DEC));
  PRINT(" ", p2dig(RTC.h, DEC));
  PRINT(":", p2dig(RTC.m, DEC));
  PRINT(":", p2dig(RTC.s, DEC));
  if (RTC.status(DS1307_12H) == DS1307_ON)
    PRINT(" ", RTC.pm ? "pm" : "am");
  PRINT(" ", dow2String(RTC.dow));
}

void showTime()
{
  RTC.readTime();
  PRINTS("\n");
  printTime();
}

void showRAM()
{
  #define  MAX_READ_BUF  (DS1307_RAM_MAX / 8)  // do 8 lines
 
  uint8_t  buf[MAX_READ_BUF];

  for (int i=0; i<DS1307_RAM_MAX; i+=MAX_READ_BUF)
  {
    RTC.readRAM(i, buf, MAX_READ_BUF);
    
    PRINT("\n", p2dig(i, HEX));
    PRINTS(":");
    for (int j = 0; j < MAX_READ_BUF; j++)
      PRINT(" ", p2dig(buf[j], HEX));
    PRINTS("  ");
    for (int j=0; j<MAX_READ_BUF; j++)
    {
      if (isalnum(buf[j]) || ispunct(buf[j]))
      {
        PRINT(" ", (char)buf[j]);
      }
      else
        PRINTS(" .");
    } 
  }
}

void writeRAM()
{
  uint8_t  addr = i2dig(HEX);
  uint8_t  len = i2dig(HEX);
  uint8_t  val[DS1307_RAM_MAX];

  if ((len == 0) || (len > DS1307_RAM_MAX))
  {
    PRINTS("\nInvalid data length");
    return;
  }
  
  for (int i=0; i<len; i++)
    val[i] = i2dig(HEX);
  
  PRINT("\nAddress 0x", p2dig(addr, HEX));
  PRINTS(" write value");
  for (int i=0; i<len; i++)
    PRINT(" ", p2dig(val[i], HEX));
  
  PRINT("\n", RTC.writeRAM(addr, val, len));
  PRINTS(" bytes written");
}

void showDoW(void)
{
  RTC.readTime();
  PRINT("\nCalculated DoW is ", dow2String(RTC.calcDoW(RTC.yyyy, RTC.mm, RTC.dd)));
}

void writeTime()
{
  RTC.yyyy = i2dig(DEC)*100 + i2dig(DEC);
  RTC.mm = i2dig(DEC);
  RTC.dd = i2dig(DEC);
  
  RTC.h = i2dig(DEC);
  RTC.m = i2dig(DEC);
  RTC.s = i2dig(DEC);
  
  RTC.dow = i2dig(DEC);
  
  PRINTS("\nWriting ");
  printTime();
  
  RTC.writeTime(); 
}

void writeControl()
{
  char  c = ReadNext();
  uint8_t  item, value;
  
  switch (c)
  {
    case '0':  // halt
      item = DS1307_CLOCK_HALT;
      c = ReadNext();
      switch (c)
      {
        case '0': value = DS1307_OFF;  break;
        case '1': value = DS1307_ON;  break;
        default: goto error;
      }
      break;
      
    case '1':  // enable
      item = DS1307_SQW_RUN;
      c = ReadNext();
      switch (c)
      {
        case '0': value = DS1307_OFF;  break;
        case '1': value = DS1307_ON;   break;
        default: goto error;
      }
      break;
      
    case '2':  // type on
      item = DS1307_SQW_TYPE_ON;
      c = ReadNext();
      switch (c)
      {
        case '1': value = DS1307_SQW_1HZ;    break;
        case '2': value = DS1307_SQW_4KHZ;   break;
        case '3': value = DS1307_SQW_8KHZ;   break;
        case '4': value = DS1307_SQW_32KHZ;  break;
        default: goto error;
      }
      break;
      
    case '3':  // type off
      item = DS1307_SQW_TYPE_OFF;
      c = ReadNext();
      switch (c)
      {
        case '0': value = DS1307_SQW_LOW;   break;
        case '1': value = DS1307_SQW_HIGH;  break;
        default: goto error;
      }
      break;
      
    case '4':  // 12 h mode
      item = DS1307_12H;
      c = ReadNext();
      switch (c)
      {
        case '0': value = DS1307_OFF;  break;
        case '1': value = DS1307_ON;   break;
        default: goto error;
      }
      break;
      
    default:
 error:
      PRINTS("\nBad control element or parameter");
      return;
  }
  
  // do it
  PRINT("\nControlling ", ctl2String(item));
  PRINT(" value ", sts2String(value));
  
  RTC.control(item, value);
  
  return;
}

char ReadNext()
// Read the next character from the serial input stream, skip whitespace.
// Busy loop with a delay.
{
  char  c;
  
  do
  {
    while (!Serial.available())
      delay (50);  // wait for the next character
    c = Serial.read();
  } while (isspace(c));

  return(c);
}

void loop()
{
   char  c;
   
   // we need to get the next character to know what command we want to process 
   c = ReadNext();
   switch (toupper(c))
   {
     case '?': usage();       break;
     
     // Status and control
     case 'S': showStatus();  break;
     case 'C': writeControl();  break;
     case 'D': showDoW();     break;
     
     // Time functions
     case 'T':  // Display updates      
       c = ReadNext();
       switch (toupper(c))
       {
         case 'R': showTime();  break;
         case 'W': writeTime();  break;
         default: goto no_good;
       }
       break;
    
     // RAM functions
     case 'R':  // Display updates      
       c = ReadNext();
       switch (toupper(c))
       {
         case 'R': showRAM();   break;
         case 'W': writeRAM();   break;
         default: goto no_good;
       }
       break;

       default:  // don't know what to do with this! 
no_good:           // label for default escape when we can't process a character 
        {
          PRINT("\nBad parameter '", Serial.print(c));
          PRINTS("'");
          while (Serial.available())    // flush the buffer
            c = ReadNext();
        }
        break;
   }     
   PRINTS("\n");
 }


Here's the result.
[IMG]http://i64.tinypic.com/33mq2ci.png[/IMG]

Why is that on the other sketch, nothings appearing on the serial monitor?

My plan is to trigger the servo at a specific time using the rtc. Here's my sketch

/*

  • Austin Tarango
  • Jan 10, 2017
  • Sunrise Alarm Code
    */
    #include <DS1307.h> //Include the clock library
    #include <Servo.h>

Servo myservo;

// Changable Vars
int setHour = 2; // Set hours to wake (military time)
int setMin = 18; // Set minute to wakeint Relay = 9; // Set pinout with with PWM
int pos =0;
int Relay =11;

// Set up Vars
DS1307 rtc(SDA, SCL);
Time t;
void start();

void setup()
{
pinMode(Relay, OUTPUT);
Serial.begin(115200); // Match to serial monitor
rtc.begin();
myservo.attach(11);
}

void loop()
{
t = rtc.getTime(); // Make a time class called 't'

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

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

// Send time
Serial.println(rtc.getTimeStr());

if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!
{
start();
}

// Wait one second before repeating
delay (1000);
}

void start()
{
for (pos = 0; pos <= 60; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for (pos = 60; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}

The problem is that the servo is not triggering at the specified time so that's why I'm troubleshooting the RTC.

Please help :frowning:

Is my RTC busted? Or the nano

The problem is that the servo is not triggering at the specified time

No. The problem is that you get output that you haven't shared, but still want us to tell you what is wrong.

Have you actually gotten the RTC to work? Can you print the time to the serial monitor?