• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 CAM Project without deep sleep mode with PIR sensor

Q&A Forum › Category: ESP32 › ESP32 CAM Project without deep sleep mode with PIR sensor
0 Vote Up Vote Down
Ashish Garg asked 5 years ago

Dear Mam,
As per this project https://rntlab.com/question/esp32-cam-project-without-deep-sleep-mode/, in this, I was using a Magnetic switch but now I am using PIR sensor. Just replacing Magnetic switch with PIR. But I don’t know why the PIR sensor output pin is always HIGH and continuously  I am getting the output. So can you please help me with this. What I am missing.
code:https://pastebin.com/sze4ctBV
Thanks

Question Tags: ESP32-Cam
8 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.

I think that is happening because GPIO16 is internally connected to a pull-up resistor. See it here: https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2020/03/ESP32-CAM-AI-Thinker-schematic-diagram.png?ssl=1

Additionally, when the PIR motion sensor detects motion, its state stays HIGH for a few seconds. For the PIR sensor we use, it’s usually 7 seconds, if I’m not wrong.

Are you printing in the Serial Monitor the state of the PIR sensor? Does it change?

When using a PIR motion sensor with the ESP32, it is better to use interrupts instead.

A few days ago, I’ve experimented with interrupts on the ESP32-CAM pins, here’s what I found:
GPIO 13: works well
GPIO 15 : works with inverted logic
GPIO 12: Guru meditation error when it gets a HIGH signal
GPIO 14: works well
GPIO 2: works well
GPIO 4: doesn’t work as expected.

If you don’t need to use the microSD card, you’ll have other pins to experiment with the PIR sensor.

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

Hello Mam, Thank you for the detailed response. I tried various pins as recommended by you and using Interrupt. But now I am getting GuruMeditation error and Camera probe error. It might be that I am doing some mistake in implementing Interrupt.
Code: https://pastebin.com/p4KyusaQ
Serial Monitor:https://imgur.com/FxcTjs3
Serial Monitor:https://imgur.com/2BLvBEy
Thanks

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.

I spend the last couple of hours trying to implement your project, but no luck. I was not able to use interrupts and the microSD card at the same time.

I’ve implemented the same project, but saving the photos in SPIFFS. Here’s the code:https://pastebin.com/fGW8fXdA
With the ESP32-CAM you need to implement interrupts as follows (in this case in GPIO 14):

esp_err_t err = gpio_isr_handler_add(GPIO_NUM_14, &detectsMovement, (void *) 1);
  if (err != ESP_OK) {
  Serial.printf("handler add failed with error 0x%x \r\n", err);
}
err = gpio_set_intr_type(GPIO_NUM_14, GPIO_INTR_POSEDGE);
  if (err != ESP_OK) {
  Serial.printf("set intr type failed with error 0x%x \r\n", err);
}

And the callback function:

static void IRAM_ATTR detectsMovement(void * arg) {
  Serial.println("Motion Detected");
  takePicture = true;
}

I tested the example I’ve sent you with a PIR motion sensor on GPIO 14 and it worked well.

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

Dear Mam,
Thank you, Mam, for your help. I really appreciate. And I tried the sketch, it is working fine. Thank you so much, Mam.

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

Hello Mam, as I am new to the SPIFFS file system, and as per me, after a certain limit the flash will be full and I have to delete the files.

So, is it possible within the code, that after sending an email with the attachment we can delete that file from the flash?

I was trying this simple sketch:

#include "SPIFFS.h"

void setup() {

Serial.begin(115200);

if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
Serial.println("\n\n---BEFORE REMOVING---");
listAllFiles();

SPIFFS.remove("/photo.jpg");

Serial.println("\n\n---AFTER REMOVING---");
listAllFiles();

}
void listAllFiles(){
File root = SPIFFS.open("/");
File file = root.openNextFile();

while(file){

Serial.print("FILE: ");
Serial.println(file.name());
Serial.print(file.size());
Serial.println(" bytes");
file = root.openNextFile();
}
}

void loop() {}

Please correct me if I am wrong, as it might be a basic question.

Thanks

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
I think that should work.
But if you overwrite the same photo name, I don’t think you’ll have memory problems.
Regards,
Sara

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

OK, and I found this link https://github.com/pellepl/spiffs/wiki/FAQ#how-long-will-my-spi-flash-live and as I understood, it says if we do one write per second than the flash can survive for 355 days and I think this is sufficient for this device.
Thanks a lot, Mam. 

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
Thanks for sharing that link.
It’s very informative.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.