Simple code to identify the color red.
My hardware connections via jumper wires are:
- GND of the Arduino Uno to the GND of the ESP32-CAM;
- 5V output of the Arduino Uno to the 5V input of the ESP32-CAM;
- TXD pin of the ESP32-CAM to the pin 1 on the Arduino Uno;
- RXD pin of the ESP32-CAM to the pin 0 on the Arduino Uno.
The code:
#include <Wire.h>
#include <esp_camera.h>
// Define the pins for ESP32-CAM
#define ESP_TX 1 // Connect ESP32-CAM TX to Arduino Uno RX (pin 1)
#define ESP_RX 0 // Connect ESP32-CAM RX to Arduino Uno TX (pin 0)
void setup() {
Serial.begin(115200); // Initialize serial communication with the computer
Serial1.begin(115200); // Initialize serial communication with ESP32-CAM
Serial.println("Arduino setup");
// Wait for serial communication to be established
while (!Serial) {
delay(10);
}
Serial.println("ESP32-CAM Serial Communication Initialized");
// Camera setup
camera_config_t config;
config.pin_d0 = 21;
config.pin_d1 = 22;
config.frame_size = FRAMESIZE_VGA; // Adjust frame size as needed
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
}
void loop() {
// Your code goes here
// Example: Send a message from Arduino Uno to ESP32-CAM
Serial1.println("Hello from Arduino Uno!");
// Capture an image
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Capture failed");
return;
}
// Process the captured image
int redPixelCount = 0;
for (int i = 0; i < fb->len; i += 3) {
// Check if the pixel is red (adjust thresholds as needed)
if (fb->buf[i] > 100 && fb->buf[i + 1] < 50 && fb->buf[i + 2] < 50) {
redPixelCount++;
}
}
// Print the result to the Serial Monitor
if (redPixelCount > 100) {
Serial.println("Red detected!");
} else {
Serial.println("No red detected.");
}
// Release the frame buffer
esp_camera_fb_return(fb);
delay(1000); // Adjust delay as needed
}
Board:
AI Thinker ESP32-CAM;
Error:
A fatal error occurred: Failed to connect to ESP32: No serial data received.