XKC Y25 with DS1307RTC Headaches

Ok, here goes! So I'm basically building a system to control my garden water tank system. Automatic overflow system via pump, watering plants and all that jazz... especially when I'm not around!

Got the code all working using the 'Non-contact' water sensor with a matrix display and decided I needed a timer circuit to set the times for the 'Watering' part of the system. The problem aired when I added the timer and programming to the sketch... nothing would work, at first I thought it was a clock or address confliction with the Matrix display. Basically the call for RTC.read function from library would err.

Took the whole thing apart to narrow down the issue, and it turns out to be the XKC Y25 sensor, every time I hook it to the board it stops the clock cycle and impairs the RTC. function. I'm hoping somebody with a better understanding of electronics can resolve the issue. I reckon it's some issue with it being a capacitance device.

The board I originally put everything together on was an Mega2560, everything worked sweet, without the timer element. Once that was added all halted. So just focussing using this code:

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>


#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
 
// Display Size and CS Pin
#define MAX_DEVICES 4
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN 10
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);    // CLK_PIN, CS_PIN, MAX_DEVICES);
int i = 0;
void setup() {
  Serial.begin(9600);
  pinMode(3, INPUT);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
  myDisplay.begin();
  myDisplay.setIntensity(5);
  myDisplay.displayClear();
  
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    i = i + 1;
    myDisplay.print(i);
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
    //delay(2000);
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

Circuit is as simple as it is, but now using a leonardo instead of the Mega. Just the power rail via an SDEC and a sep DESK PSU regulated output 5v with shared ground as it should be. Even if I connect the sensor... the clock will kinda halt, or rather the RTC.#### function call to library ceases to execute, and power consumption is negligible 10 ~ 30 mA at most!

So this is the image, the matrix is basically just showing the incrementing integer 'i' so I could see when the devices halted after trying numerous experiments to solve the issue.

IF I remove the XKC Y25 output from any of the digital inputs/pins with or without declaration I can connect VCC to sensor or vica-versa, but ground/VCC/output cannot be all connected.

From the image you will also see a relay board connected... but I only just added that to test other avenues to solve the problem.

Driving me bonkers, as this is the only hurdle holding me back before I can final assemble and deploy. All help welcome guys n gals.

Forgot to add, I got the sensor from Amazon and use it with logic 1 on output on detection of water:

EDIT: I should've also added I got the whole thing working on the Mega, timer and all, using straight logic '1' wire for sensor input whilst testing. When I replaced the straight logic with sensor outfit... No go!

I don't understand your last para. No idea why you need more than a NANO either. Are you using a real RTC and not the ZS-042? Is the conflicting device also I2C? That may be an issue so change the address of one of the devices if it can be changed (usually a solder bridge)

Read the pinned post re 'How to get the most from the forum'. Then make a hand drawn wiring diagram and post a clear picture of that. Post all the code in code tags and post the error log in code tags.

Hi Sonofcy, with reference to last para you didn't understand. I didn't have all the sensors to hand, just the one for testing and the system uses 5 sensors in all. So I simulated the inputs from the sensors by connecting a logic 1 as 5v to the nominated inputs at the time. Also sensor is not an I2C device, just three outputs GND/5V/OUTPUT-SIGNAL and code is as above! Ignore the Matrix element in the sketch, as it was just used as a test, it works not with or without that.

Hope this suffices for the circuit diagram, assume the power is derived from the MEGA/Leonardo main unit from USB and PC.

Thanks for the reply!

Hmmm, no takers then. Clearly no-ones come across this issue before. My only work-around is to have the sensors, via a separate PSU/5V line and relay bank or a set of opto-isolators/couplers feed input into the 5 individual pins with a pull down 10K resistor in tow. This is the only way I can keep these sensors working with this project. Opto route will be best if i'm honest.

So an update to this issue of using the above capacitance sensor in my small project. I’ve ordered TLP281 IC as a module as opto-couplers to run the sensors independent of the same circuit.

Will update again when I receive delivery and integrate this into my project. Hopefully this will help anyone else that comes across this issue.

Try adding 0.1µF and 10µF capacitors across the power lines. These will help filter out high-frequency noise generated by the capacitive sensor, which is likely the main cause of your I2C communication failures. Additionally, consider placing it in a shielded enclosure and using shielded or twisted-pair wiring for your I2C lines to reduce electromagnetic interference.

Thanks for the reply dhamu, I have already tried 0.1µF as noise suppression, but no harm in trying with additional 10µF. Will be surprised if it works as this isn’t a case of intermittent failure of clock signal, just outright failure. BUT hey, will give anything a go right now to minimise board components! Thanks!

No joy dhamu, even picked up a modified sensor from DFRobots website with an intermediatory PCB with suppression caps:

..and still no joy. :man_shrugging:

So will now proceed with opt-coupling, not an expensive tbvh, just takes a little board space as I need two of them because each only handles 4 sensors and I have 6 to connect up!

OK, so I received the TLP281 module and proceeded to test wire this up… and guess what, same problem, timer module went haywire… I straight away thought that there was something else sinister going on and the first two sensor pins which I was using for testing were simply tripping the system on a LOW signal! …and all other pins had no issue with the sensors!!!

So stupid me was using pins 2 and 3 for the initial two sensors which as you know, are the Interrupt PINS active LOW, or rather trailing LOW edge of clock signal, only I thought these were disabled by default but apparently this is not the case. I can indeed disable the INT0 and INT1 interrupts but decided not to as, firstly they could mess up on-board clock signal, and secondly I may need them at a later date.

SO, in conclusion, the problem was of my own making, but for anyone else, avoid using these pins as digital inputs, as by default the Interrupts are enabled.

Prog and hardware working sweet as, now :joy: …Duhh… bit of a mess, sorry but it’s just prototyping stage atm.

Thanks to everyone chipping in to help sort the issue, much appreciated guys.

Glad to hear you've got it working, but I think there is something else going on here. It should be quite legit to use those pins for digital input. They are clearly marked for that purpose.

It is indeed the case. Without using attachInterrupt( ) or setting up pin change interrupts, the pins 2 and 3 are normal i/o pins.

Yes, sorry.. I think I should be more precise about what I said… What I should’ve said is that I believe under normal operation of input/output duties it should perform ok, BUT if you’re using an external time/clock module such as the DS1307x and I assume the DS3231 module, expect this CLOCK signal to be interrupted automatically by the default setting of the Arduino on both or either, PIN2 & PIN3.

To power or provide HIGH/LOW signals on the pins is absolutely fine or for that matter to use as INPUTS. You really have to understand the logic of the interrupts to realise why it will impede/halt the EXTERNAL clock signals and point to an error related solution as the functions kind of proceed on a FIFO principle, as there isn’t multithreading capabilities… so a bit of a back-pedal for me as I’m used to taking advantage of multithreading lol.

Thanks for the reply Cattledog, as I said above… If you HAVE to use an external Clock module and HAVE to use either PINS 2 or 3 as inputs, then yes Interrupts will have to be disabled, and that can come at a price depending on your project.

Good luck all!!

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