闪光灯 LED 控制
闪光灯 LED 控制
本节介绍 ESP32-CAM 内置闪光灯的控制方法。学习完成后,您将能够:
- 控制 ESP32-CAM 内置闪光灯的开关
- 通过 MQTT 远程控制闪光灯
- 实现拍照时的闪光同步
- 调整闪光灯闪烁模式
Flash LED Hardware
Section titled “Flash LED Hardware”ESP32-CAM 内置一个高亮白色 LED,连接到 GPIO4:
┌────────────────────────────────────────────┐│ ESP32-CAM 闪光灯电路 │├────────────────────────────────────────────┤│ ││ GPIO4 ──→ 电阻 ──→ NPN 晶体管 ──→ LED ││ │ ││ 5V/3.3V ──┘ ││ ││ 控制方式: GPIO 高低电平 ││ 电流: ~20-50mA (取决于亮度设置) ││ 有效距离: 1-3 米 (低光环境) ││ │└────────────────────────────────────────────┘Basic Flash Control
Section titled “Basic Flash Control”#define FLASH_PIN 4 // ESP32-CAM 内置 LED
void setup() { pinMode(FLASH_PIN, OUTPUT); digitalWrite(FLASH_PIN, LOW); // 初始关闭}
// 闪光灯控制函数void flashOn() { digitalWrite(FLASH_PIN, HIGH);}
void flashOff() { digitalWrite(FLASH_PIN, LOW);}
void flashToggle() { digitalWrite(FLASH_PIN, !digitalRead(FLASH_PIN));}Flash Modes
Section titled “Flash Modes”1. 常亮模式
Section titled “1. 常亮模式”// 用于视频监控或持续照明void flashContinuous(bool enable) { digitalWrite(FLASH_PIN, enable ? HIGH : LOW);}2. 拍照闪光模式
Section titled “2. 拍照闪光模式”// 拍照时闪光同步void takePhotoWithFlash() { // 开启闪光灯 digitalWrite(FLASH_PIN, HIGH); delay(200); // 等待 LED 稳定
// 拍照 camera_fb_t* fb = esp_camera_fb_get();
// 关闭闪光灯 digitalWrite(FLASH_PIN, LOW);
if (!fb) { Serial.println("Photo failed"); return; }
// 处理图片... esp_camera_fb_return(fb);}3. 闪烁指示模式
Section titled “3. 闪烁指示模式”// 用于状态指示或通知void flashBlink(int count, int intervalMs) { for (int i = 0; i < count; i++) { digitalWrite(FLASH_PIN, HIGH); delay(intervalMs); digitalWrite(FLASH_PIN, LOW); if (i < count - 1) { delay(intervalMs); } }}PWM 亮度控制
Section titled “PWM 亮度控制”如果硬件支持,可以使用 PWM 控制亮度:
// 使用 LEDC (PWM) 控制闪光灯亮度#define FLASH_PIN 4#define LEDC_CHANNEL 0#define LEDC_TIMER 0#define LEDC_FREQ 5000 // 5KHz PWM#define LEDC_RES 8 // 8位分辨率 (0-255)
void setupFlashPWM() { ledcSetup(LEDC_CHANNEL, LEDC_FREQ, LEDC_RES); ledcAttachPin(FLASH_PIN, LEDC_CHANNEL);}
// 设置亮度 0-255void setFlashBrightness(int brightness) { brightness = constrain(brightness, 0, 255); ledcWrite(LEDC_CHANNEL, brightness);}
// 渐亮效果void flashFadeIn(int durationMs) { int steps = 50; int stepDelay = durationMs / steps; for (int i = 0; i <= 255; i += (255 / steps)) { ledcWrite(LEDC_CHANNEL, i); delay(stepDelay); }}MQTT Flash Control
Section titled “MQTT Flash Control”ESP32 端
Section titled “ESP32 端”// 通过 MQTT 控制闪光灯void mqttCallback(char* topic, byte* payload, unsigned int length) { String command = String((char*)payload).substring(0, length);
if (command == "flash_on") { flashContinuous(true); client.publish("esp32cam/status", "flash_on"); } else if (command == "flash_off") { flashContinuous(false); client.publish("esp32cam/status", "flash_off"); } else if (command.startsWith("flash_blink")) { // 命令格式: flash_blink:5:200 (闪烁5次,间隔200ms) int colon1 = command.indexOf(':'); int colon2 = command.indexOf(':', colon1 + 1); if (colon1 > 0 && colon2 > 0) { int count = command.substring(colon1 + 1, colon2).toInt(); int interval = command.substring(colon2 + 1).toInt(); flashBlink(count, interval); } } else if (command.startsWith("flash_brightness:")) { int value = command.substring(17).toInt(); setFlashBrightness(value); }}Node-RED 控制
Section titled “Node-RED 控制”[Dashboard 按钮] ──→ [MQTT Out: esp32cam/command]// Function: 闪光灯控制面板逻辑var action = msg.payload;
switch(action) { case "flash_on": msg.payload = "flash_on"; break; case "flash_off": msg.payload = "flash_off"; break; case "flash_blink": msg.payload = "flash_blink:3:500"; // 3次,500ms间隔 break; case "flash_auto": // 拍照时自动闪光 (通过拍照命令控制) msg.payload = "take_photo"; break; default: return null;}
msg.topic = "esp32cam/command";return msg;# 测试闪光灯开mosquitto_pub -t "esp32cam/command" -m "flash_on"# 预期: LED 亮起,状态返回 "flash_on"
# 测试闪光灯关mosquitto_pub -t "esp32cam/command" -m "flash_off"# 预期: LED 熄灭,状态返回 "flash_off"
# 测试闪光灯闪烁mosquitto_pub -t "esp32cam/command" -m "flash_blink:5:200"# 预期: LED 闪烁 5 次,每次 200ms| 问题 | 原因 | 解决方案 |
|---|---|---|
| LED 不亮 | GPIO4 未配置或损坏 | 检查 pinMode 设置 |
| LED 亮度不足 | 供电不足 | 使用外部 5V 电源 |
| 拍照时闪光不同步 | 延迟时间不够 | 增加 flash 开启后的 delay |
| LED 闪烁不稳定 | PWM 频率不匹配 | 调整 LEDC 频率参数 |
Common Customer Questions
Section titled “Common Customer Questions”Q1: 闪光灯的有效范围是多少?
Section titled “Q1: 闪光灯的有效范围是多少?”ESP32-CAM 内置 LED 在完全黑暗环境中有效范围约 1-3 米。如果需要更远的照明,建议外接大功率 LED 或红外补光灯。
Q2: 频闪是否对 LED 寿命有影响?
Section titled “Q2: 频闪是否对 LED 寿命有影响?”正常使用 (每日几百次) 对 LED 寿命影响很小。但不建议长时间高频率使用,LED 会发热。
Q3: 能否在夜间使用红外拍摄?
Section titled “Q3: 能否在夜间使用红外拍摄?”ESP32-CAM 的传感器没有红外滤镜,可以在 LED 上加红外滤光片实现红外照明。但默认内置 LED 是白光。
✅ 推荐做法:
- 拍照前提前 200ms 开启闪光灯让其稳定
- 拍照完成后立即关闭闪光灯省电
- 使用 PWM 控制亮度适应不同场景
- 闪光灯状态通过 MQTT 反馈
❌ 避免做法:
- 长时间持续开启闪光灯(发热/耗电)
- 在强光环境下使用闪光灯
- 拍照时闪光和拍照同时触发(需预亮)
- 忽略 LED 最大电流限制
Summary
Section titled “Summary”- ESP32-CAM 闪光灯连接在 GPIO4,通过高低电平控制
- PWM 可实现亮度调节 (0-255)
- 拍照闪光需要预亮 200ms 后拍照再关闭
- 闪烁模式可用于状态指示或通知
- MQTT 远程控制闪光灯的各种模式