How to enable the ds3231 RTC alarm for a certain period of time

For a project where I'm using the ESP32-CAM with a battery (18650 battery) I need the power consumption as low as it can. The sleepmode doesn't do it for me. It still consumes way to much than needed. I found an article where they use an RTC with a mosfet to power on and off a ESP32. I got it working that the ESP32-CAM turns on every minute.

The problem I have is that is goes off way to fast. I need the ESP32-CAM to be on until a photo is send to the server.

This is the electrical diagram I'm using.

This is my code example I use for the RTC with mosfet.

#include <RTClib.h>
#include <Wire.h>
#include <Arduino.h>
// #include "soc/soc.h"
// #include "soc/rtc_cntl_reg.h"

#define I2C_SDA 14
#define I2C_SDL 15

RTC_DS3231 rtc;

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

  Wire.begin(I2C_SDA, I2C_SDL);

  // initializing the rtc
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC!");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    // this will adjust to the date and time at compilation
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  //we don't need the 32K Pin, so disable it
  rtc.disable32K();

  // set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
  // if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);

  // stop oscillating signals at SQW Pin
  // otherwise setAlarm1 will fail
  rtc.writeSqwPinMode(DS3231_OFF);

  // turn off alarm 2 (in case it isn't off already)
  // again, this isn't done at reboot, so a previously set alarm could easily go overlooked
  rtc.disableAlarm(2);

  // schedule an alarm
  if (!rtc.setAlarm1(
        DateTime(0,0,0,0,0,0),
        DS3231_A1_Second  // this mode triggers the alarm when the seconds match. See Doxygen for other options
        )) {
    Serial.println("Error, alarm wasn't set!");
  }
}

void loop() {
  // print current time
  char date[10] = "hh:mm:ss";
  rtc.now().toString(date);
  Serial.println(date);

  // using setAlarm1, the next alarm could now be configurated
  if (rtc.alarmFired(1)) {

    Serial.println("Do something");

    rtc.clearAlarm(1);
    Serial.println("Alarm cleared");
  }

  Serial.println(" ");

  delay(1000);
}

How can I make it so that the RTC stays on for a longer time period when the alarm is triggered?

When the ESP32 goes to sleep how are you powering down the camera and other attached components?

The power goes through the mosfet. So when the alarm isn't triggered there is no power flowing and every thing is off.

You appear to have the I2C running on the CLK and CMD pins used by the SD card ?

post an image of the project.

Your question is not clear to me. I use the sd card to store the pictures. The RTC is connected via i2c to the esp.

The 5V RTC is directly connected to the 3V3 MCU?

post an image of your project.

Also, the ESP32 cam is sensitive to V's and needs a good supply of current upon image taking and Wifi sending.

You have the I2C connected on pins 14,15 which are also used by the SD card.

The SD card is not an I2C device.

The red and black wire are connected to a 5V power supply.

What does that mean for me and how do I solve that issue?

First you should look at the ESP32CAM Schematic, just because the board has pins numbered it does not mean they are free to use, as in not connected to other stuff.

The RTC and the SD card could interfere with each other, the SD card might lock up for instance. It might work if the RTC is setup and used before the SD card is set up and used, or vice versa, but dont expect to be able to use both at the same time.

Ultimatly you can, for reliability, put the I2C on the UOT and UOR serial pins, since for an installed application you dont normally need Serial output.

Use diodes to allow the RTC and the Arduino to each control the MOSFET. Have the Arduino hold the MOSFET on until it is done with its work.

Whilst using a pin on the ESP32CAM to control the power sounds good, which unused pin would you use ?

I would use GPIO 16, because it not connected with the sd card or camera.

That sounds great for my use. Can you give a more detailed explanation on how to perform that solution?

Does the schematic of the ESP32CAM say that pin is not connectred to anything ?

Just don't clear the alarm until you've finished saving the photo?

I found the schematic here: https://randomnerdtutorials.com/esp32-cam-ai-thinker-pinout/

From what I can see is that GPIO 16 is connected to the PSRAM CS pin. I don't know if that would be a problem. The only free pins are GPIO 1 and GPIO 3.

In the loop I have a if statement for the alarm. Even when I clear the alarm after the picture take function it still turns off before it can even connect to the internet, so that doesn't work. Something makes the alarm go off immediately.