cotestatnt / AsyncTelegram2

Powerful, flexible and secure Arduino Telegram BOT library. Hardware independent, it can be used with any MCU capable of handling an SSL connection.
MIT License
83 stars 25 forks source link

HC-SR501 and ESP32-CAM does not work simultaneously with SD card #116

Open tiffass opened 11 months ago

tiffass commented 11 months ago

Example ESP32-CAM-PIR.ino does not work with motion sensor HC-SR501. When the sensor is connected at the initialization stage, the error "SD Mount Failed" occurs. And accordingly further the code works incorrectly. More precisely, motion is detected, but photos are not sent to telegrams. The error "Failed to open file in writing mode" falls out in the serial port. If the sensor is turned off during the start of the module, then everything is OK. At rest, the output of the sensor is 0, and as I understand it, this prevents the SD card from being initialized. Can this be fixed somehow?

cotestatnt commented 10 months ago

It would appear that you are using pins used by the SD module. Have you tried connecting the HC-SR501 module differently?

tiffass commented 10 months ago

Yes, I connected the HC-SR501 to the pin, which is registered in the example #define PIR_PIN GPIO_NUM_13 and the same pin is used in the ESP32-CAM for the SD card. And for some reason, this pin 13 is used in all the examples found on the network to connect a motion sensor. From the description of the module, it can be seen that there are pins 0,1,3,16 available. Pin 0 in this case is also not desirable to use, because. it is involved when the module is loaded. 1.3 is Serial, you can use it, but then it will not be possible to fully use the serial port monitor. Only gpio16 remains? ESP32-CAM-pinout-new

cotestatnt commented 10 months ago

The example provided with library, use the onboard SD card reader in 1-bit mode. In this way only GPIO02 is used to read and write data to the SD card and the other pins are free to use.

if (!FILESYSTEM.begin("/sdcard", true, true)) {
....

The problem is due to how the pin has been configured to easily test the sketch: the input is tested as a simple button that closes towards GND, but normally the PIR sensors work in reverse generating a positive pulse every time an object is detected.

If you set the PIN correctly and change the related if code block like this it should work as expected.

   // PIR Motion Sensor setup
  pinMode(PIR_PIN, INPUT);      
  .....
  .....

    // PIR motion detected 
    // could be on interrupt, but PIR sensor usually keep signal HIGH for enough time
    if(digitalRead(PIR_PIN)) {   
    .....
    .....
tiffass commented 10 months ago

@cotestatnt No, I tried before creating the issue and it doesn't work either. If I understand correctly, then according to this https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sd_pullup_requirements.html#pull-up-conflicts-on-gpio13 on Pin 13 to initialize the card should be logical 1. And at the output of the sensor in the absence of movement - logical 0. Therefore, when starting esp32-cam on pin 13 - 0 and the SD card does not initialize

sd

And another problem was discovered - after some time of work (2-5 minutes), the connection with the server is lost Unable to connect to Telegram server

Work is restored only after the module is rebooted

tgs