Led intensity ESP32-CAM

Hello,

First of all I hope my subject is well categorized ^^.

I am currently working on ESP32-CAM and I am able to power the led using digitalWrite() function, but I can only give as argument "HIGH" or "LOW". However Intensity is too HIGH xD.

I would like to be able to control the intensity to get clear pictures.

I am a bit lost with all the ledc functions chnagements with the differents versions of the libraries.

Can someone take a look at my code and try to explain to me what functions I should use please ? I am not able to find this in a clear way on internet.

Thanks.

#include <WiFi.h>
#include <WebServer.h>
#include "esp_camera.h"
#include "Arduino.h"
#include "FS.h"                // SD Card ESP32
#include "SD_MMC.h"            // SD Card ESP32
#include "soc/soc.h"           // Disable brownout problems
#include "soc/rtc_cntl_reg.h"  // Disable brownout problems
#include "driver/rtc_io.h"
#include <EEPROM.h>            // read and write from flash memory

// define the number of bytes you want to access
#define EEPROM_SIZE 1

// Pin definition for CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27

#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

#define LED_PIN           4   // Pin pour la LED (GPIO 4)

int pictureNumber = 0;
const char* ssid = "Khu S";
const char* password = "khu@s2022";

WebServer server(80);

// Fonction pour gérer la page d'accueil du serveur
void handleRoot() {
  server.send(200, "text/html", "<form action=\"/capture\" method=\"POST\"><button type=\"submit\">Capture Image</button></form>");
}

// Fonction pour gérer la capture et l'envoi de l'image
void handleCapture() {
  // Allumer la LED avant de prendre la photo
  digitalWrite(LED_PIN, HIGH); // LED allumée
  delay(200); // Attendre 200 ms pour permettre à la LED de s'allumer
  
  camera_fb_t * fb = esp_camera_fb_get();  
  if (!fb) {
    server.send(500, "text/plain", "Camera capture failed");
    digitalWrite(LED_PIN, LOW); // Éteindre la LED
    return;
  }
  
  // Initialisation de la carte SD
  fs::FS &fs = SD_MMC;
  // Path where new picture will be saved in SD Card
  String path = "/picture" + String(pictureNumber) +".jpg";

  File file = fs.open(path.c_str(), FILE_WRITE);
  if (!file) {
    server.send(500, "text/plain", "Failed to open file for writing");
    esp_camera_fb_return(fb);
    digitalWrite(LED_PIN, LOW); // Éteindre la LED
    return;
  }

  file.write(fb->buf, fb->len); // Write image data to file
  file.close();
  
  // Répondre au client avec le fichier image
  server.sendHeader("Content-Type", "image/jpeg");
  server.sendHeader("Content-Length", String(fb->len));
  File imageFile = fs.open(path.c_str());
  server.streamFile(imageFile, "image/jpeg");
  imageFile.close();
  
  esp_camera_fb_return(fb);

  // Éteindre la LED après la capture
  digitalWrite(LED_PIN, LOW); // LED éteinte
}

void setup() {
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector

  Serial.begin(115200);
  
  // Connexion au réseau WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");

  // Imprimer l'adresse IP du serveur
  Serial.print("Web server started, visit: http://");
  Serial.println(WiFi.localIP());

  // Initialisation de la caméra
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG; 
  
  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  
  // Init Camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  // Configuration du GPIO pour la LED
  pinMode(LED_PIN, OUTPUT); // Configure GPIO 4 comme sortie
  digitalWrite(LED_PIN, LOW); // Assurez-vous que la LED est éteinte au démarrage

  // Initialisation de la carte SD
  if (!SD_MMC.begin("/sdcard", true)) { // Ajout du paramètre true
    Serial.println("SD Card Mount Failed");
    return;
  }

  uint8_t cardType = SD_MMC.cardType();
  if (cardType == CARD_NONE) {
    Serial.println("No SD Card attached");
    return;
  }

  // Configuration du serveur Web
  server.on("/", HTTP_GET, handleRoot);
  server.on("/capture", HTTP_POST, handleCapture);

  server.begin();
}

void loop() {
  server.handleClient();
}

This forum post should be helpful

1 Like