1302 RTC and other I2C device

Good day, I currently have a problem where I want to use my RTC (DS1302) to give me the time, whilst also pinging and receiving data from another slave device. Both i2c communication works separately but not together, when I combine the code nothing works at all. Help would be greatly appreciated! Note: I am using an ESP32 device. Here is my code:

#include <RtcDS1302.h>
#include <Wire.h>
#define DS1302_SCLK_PIN 22
#define DS1302_IO_PIN 21
#define DS1302_CE_PIN 2

ThreeWire myWire(DS1302_IO_PIN, DS1302_SCLK_PIN, DS1302_CE_PIN);
RtcDS1302<ThreeWire> Rtc(myWire);

#define I2C_SLAVE_ADDR 0x08 // 7-bit I2C address of the PIC16F1829

void setup()
{
    Serial.begin(9600);

    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);

    Rtc.Begin();

    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();

    Wire.begin();
    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }
}

void loop()
{
    RtcDateTime now = Rtc.GetDateTime();

    Wire.requestFrom(I2C_SLAVE_ADDR, 1); // Requests data from slave
    byte data = Wire.read(); // NACK sent by slave once read

    if (data != 0 && data != 255) // Use && instead of || to check for equality
    {
        if (data == 5)
        {
            Serial.println("Correct Token Received: 01x02x03x05");
            printDateTime(now);
            if (!now.IsValid())
            {
                // Common Causes:
                // 1) the battery on the device is low or even missing and the power line was disconnected
                Serial.println("RTC lost confidence in the DateTime!");
            }
        }
        if (data == 6)
        {
            Serial.println("Correct Token Received: 01x02x03x06");
            printDateTime(now);
            if (!now.IsValid())
            {
                // Common Causes:
                // 1) the battery on the device is low or even missing and the power line was disconnected
                Serial.println("RTC lost confidence in the DateTime!");
            }
        }
    }

    delay(500); // two seconds
}

void printDateTime(const RtcDateTime& dt)
{
    char datestring[26];

    snprintf_P(datestring,
                countof(datestring),
                PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
                dt.Month(),
                dt.Day(),
                dt.Year(),
                dt.Hour(),
                dt.Minute(),
                dt.Second());
    Serial.println(datestring);
}

Really?

You don't even get the serial output from:

I doubt that the RTC is I2C. According to your code it uses SPI or something similar. If you assign these signals to I2C pins then it will make impossible to use that I2C port.

2 Likes

I'm not familiar with the ESP32.

The DS1302 is not an I2C device (as far as I can see). So I suspect that ThreeWire interferes with I2C as you seem to be using the same pins.

Your problem is not related to Interfacing w/ Software on the Computer and hence moved to a more suitable location on the forum.

1 Like

Indeed it is not:

EDIT

More details:


Note that the data line, "I/O", is bidirectional.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.