OK, I have removed the lines that change the SDA and SCL pins to output, and that works fine. And I will get to your code when I can, so I can understand how it all works, and to maybe lower the sleep current even more. One thing that did occur (and perhaps am misidentifying the cause) is that when I raised the voltage of the MCU power by switching to fresh batteries (I had been deliberately using un-fresh AAA batteries so that I'd know things would work over time as the batteries were used), the activation of the 12-V power reoccurred immediately after powering off. What seemed to solve this was to insert a delay(100) line right after turning off the VCC power to the clock (when the clock needs to switch to its own battery). Perhaps without that delay, new code was missed as the clock switched over. In any case, all is working now, through a few days of testing with various VCC voltages.
Are you saying the 12V mosfet turns on for 10 seconds, then turns off, but then turns back on again after you power down the RTC? I don't see anything in your code that would cause that. Perhaps it's a circuit issue.
One thing I wanted to mention, in case it ever happens to you, is that I had trouble using the FALLING trigger in my sketch. The INT pin would go low, but the Pro Mini didn't wake up. The problem was intermittent. I thought it was a breadboard contact problem, but was never able to find anything. I ended up changing the trigger to LOW, with the DETACH instruction in the ISR, and then it worked perfectly. So I concluded it wasn't a contact problem, but I don't have any other explanation.
I think you may be correct about something wrong with the circuit. I've been having more inconsistent results and noticed now that the DS3231's battery drains quickly. It seems turning off the clock's VCC power during the MCU's sleep cycle works OK for a while (a day or so with a fresh clock battery) and then doesn't--so maybe when the clock's own battery drops too low the clock doesn't keep time or produce the alarms consistently. I cannot figure out what's wrong in the circuit. For now I have taken out the clock battery and am just using VCC all the time to power the clock. This seems to work OK so far. It uses more power than I'd like; I'll need to double-up on the VCC battery capacity.
Can you measure the battery current when Vcc is on, and when it's off? Actually, if you've taken the coin battery out, you could just supply 3.3V there, and measure the current. When on battery power, it should be a microamp or two, and presumably zero when Vcc is on.
I'll try to get a number for that current, but I know it was high, both with the VCC on and without--I could sit and watch the voltage of the clock battery go down. What I've done now is to build a new board without any of the surface-mount clock hardware--no DS3231 chip, no resistors, and no capacitor in that section of the board. I installed an Adafruit DS3231 clock module instead. So far, I am almost back to the best performance I had earlier--rather than 3 uA I have 5--probably the module uses a little more power. I have left intact the the "disconnect this jumper to use clock module" trace. So far, the clock module battery voltage seems consistent. So it seems to me that there is something wrong with the design of the surface-mount setup--perhaps putting in the option to use either the surface-mount chip or the module introduced some problem. If you see anything I can try with the clock chip do let me know--I would rather use that to get rid of the wires I need for the module. But for now I have my fingers crossed that the testing goes OK with the clock module installed--at least I may finally be able to move forward with this project!
If all the parts, including the pullup resistors, were removed from the board, then it seems leaving the jumper intact should be ok.
Do you have a link to the Adafruit module?
According to your schematic, the positive battery terminal is connected only to pin 14 of the DS3231. You might want to check to see if that's actually true. If your surface mount DS3231 is not defective, then I don't see how battery current could be high unless B+ is also connected to something else.
On the bare board I checked continuity between the positive batt solder hole and all the surface-mount clock chip pin spots; only pin 14 beeped. I did the same for the ground terminals and they all matched the schematic. The Adafruit module is their 3013.
Got inconsistent results with the board clock after a while too. It worked overnight but when I turned it all off and on again the system worked erratically. I'll investigate more and get back here.
According to the schematic of the 3013, the "NC" pins are left unconnected. While that's certainly logical, the DS3231 datasheet says they must all be grounded. I don't know what difference it would make.
You only posted part of your project schematic. If you'd like to post the whole thing, perhaps someone will see something that's not right.
Well, I managed to download your schematic before you deleted the post. But I didn't copy the description, so I hope you will post it again in some form.
Do you have problems if the 12V supply is in override mode, so that the mosfet isn't doing anything? Are the grounds of the Vcc supply and the 12V supply connected together?
I don't really see anything that jumps out at me as being a problem. All the Arduino boards have a 0.1uF cap to ground on the Aref pin, but I don't think you're using ADC, so that probably doesn't matter.
Also, I'm having trouble finding the buck regulator. Is it perhaps an LM2596?
Here is the complete schematic. After I posted it earlier I had a successful run with a new board so I thought it was solved (but then had problems once again).
Here’s the description: The system switches camera power on and off and activates the shutter. The ATMEGA328P Pin 13 controls a MOSFET, which switches 11.1V LiPo power input to a voltage converter. This converts the LiPo power to the camera voltage. An optocoupler closes the shutter circuit. The LiPo power is used only when the camera is on. The option for the DS3231 module rather than a surface-mount exists because in a previous version of the PCB we had issues with the surface-mount.
I have a replicate of this (minus the surface mount circuitry and the programming headers) on a breadboard, using the Adafruit 3013 DS3231 module. The breadboard version seems to work fine. The PCB version sometimes works for a while and then quits; and sometimes just doesn't work. Sometimes the power stops switching. Sometimes the cycle repeats without a delay. Recently I had the camera firing when it shouldn’t.
At first, I thought that using the clock module rather than the surface-mount chip was successful, but with longer testing, that failed too.
I have tried using the alarm trigger as "LOW" rather than "FALLING", and the problems persisted.
One aspect of the program that especially perplexes me is when to call for pullup resistors and clock pin outputs and inputs.
The voltage converter I am using: https://www.hobbyelectronics.net/review_dctodcmodule.html
I'll try the override mode; yes the grounds of the Vcc supply and the 12V are connected together.
My latest-built pcb worked at first and then after a re-start the converter just stayed on all the time, with about 1.5 volts measured at Pin 13 all the time--with only the 12V connected (no logic power connected). I tried connecting 12V directly to the converter (leaving everything else intact) but nothing changed.
I just don't know what to suggest. The only connections from the 328P to the camera power, etc., are the optocoupler LED and mosfet gate. So assuming the mosfet is ok, there's really nothing to explain the constant 1.5V on PD7 even if the buck converter isn't performing correctly. Your code sets PD7 as an output, so it has to be either Vcc or ground. So how does it get to be 1.5V? Is there a leak to ground somewhere?
What's the source of the logic power?
You're using some string functions in your code. I don't have any experience with them, but have seen several comments on this board that they don't work on Arduinos because of the lack of ram. If you run out of ram, everything becomes unpredictable. But I don't really know about that.
What do you mean by "re-start"?
Sorry, but I just don't have an answer.
Edit: I didn't read carefully enough. If you measure 1.5V on pin 13 with no Vcc powering the 328P, then something is leaking in somewhere. That pin is gounded by R5, so it should be at ground. Is anything connected to JP1, JP2 or ISP1?
The source of the logic power is 3 AAA alkaline batteries. I will look at those string functions and try to get them out of there. By re-start I just meant I disconnected the power plugs and plugged them back in again. There is nothing connected to JP1 or ISP1, but currently this latest board has a DS3231 module connected to JP2.
The buck converter has some large-ish capacitors on it; could they be holding voltage up at the pin? Just wondering--I have not idea. The voltage at the converter input pins were 1.5 V also.
But the 328P isn't connected to the buck converter. It's only connected to the gate of the mosfet, which should have no current path to anything else except possibly a little leakage current, which should be taken care of by the pulldown resistor. And on top of that, there is a protection diode inside the 328P going from each I/O pin to the Vcc pin. So what is the voltage on Vcc when the voltage on pin 13 is 1.5V? Are you sure the mosfet is still good?
Well, all I can suggest is to do a lot of voltage measurements to see if what you measure is what you expect. If the breadboard version works fine on the same sketch, then the sketch should be good, even with the string stuff. The breadboard version works for an extended period, right?
So it seems either something is wrong with the board or something is different about the parts being used. I assume you're using the DIP version of the 328P on the breadboard. What version are you using on the PC board? Same question about the mosfets.
On the breadboard version of this circuit - if you remove the logic power supply, do also you get 1.5V on pin 13 there?
I did not measure 1.5V on the breadboard. But I built another pcb and didn't measure 1.5V on that either. Voltages seem to change slowly over time all over. I ran the new pcb overnight and it worked fine until the next morning. At that point I couldn't get it to work; the voltage at the converter input was 9V even after I removed the 12V power. I thought the MOSFET must be bad so I removed it. It tested ok. I put it back in and all worked ok. I have tried measuring voltages all over and it seems things change over time. I wonder whether the converter holding charge on its input can back up to the MOSFET and cause it to leak current or malfunction?
Any charge in the interter will be applied to the mosfet drain. But I don't see why that should cause a problem. For testing purposes, you might try temporarily replacing the converter with a resistor and LED, and see if that works properly.
It's possible that switching the low side of the inverter is somehow causing a problem. The alternative would be to use a P-channel mosfet to switch the high side, but that would require a board re-design and an additional NPN transistor.
I'm really at a loss to explain what's happening. I wish someone more knowledgeable than me would participate here and help us both out.
Edit: When the voltages start changing, is the 5V supply to your 328P still good?
Yes, the voltage to the 328p says good. I wonder whether i need a ground on the converter. As I recall, I did that at the start and it didn't work. I'll try the led and see what the voltages do with that.
Grounding the converter is what the mosfet does. 12V is always present at the + side of the converter input, but the circuit isn't completed until the mosfet connects the negative side to ground. Or did you mean something else?
