I am making a simple motion-tracking turret with 3 servos, an ESP32 camera, and Arduino UNO.
I received this error message when uploading my code:
Arduino: 1.8.15 (Windows 10), Board: "AI Thinker ESP32-CAM, 240MHz (WiFi/BT), QIO, Huge APP (3MB No OTA/1MB SPIFFS), 80MHz, None, Disabled"
Sketch uses 355509 bytes (11%) of program storage space. Maximum is 3145728 bytes.
Global variables use 26460 bytes (8%) of dynamic memory, leaving 301220 bytes for local variables. Maximum is 327680 bytes.
esptool.py v4.5.1
Serial port COM3
Connecting......................................
A fatal error occurred: Failed to connect to ESP32: Invalid head of packet (0x00): Possible serial noise or corruption.
For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html
the selected serial port For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html
does not exist or your board is not connected
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I did the basic troubleshooting of connecting to another COM port, updating drivers, disconnecting the external power supply while uploading, and pressing the boot button when it says, "Connecting........" when I try to upload. The port and board I selected are correct and whatever I do doesn't seem to work.
Here is my code:
#include <ESP32Servo.h>
#include <Wire.h>
#include <esp_camera.h>
// Define the servo pins
const int panPin = 10;
const int tiltPin = 11;
int motionY;
// Create the servo objects
Servo panServo;
Servo tiltServo;
// Initialize the servos
void setup() {
Serial.begin(9600);
Wire.begin();
panServo.attach(panPin);
tiltServo.attach(tiltPin);
// Set the pins as outputs
pinMode(panPin, OUTPUT);
pinMode(tiltPin, OUTPUT);
// Initialize camera
camera_config_t config;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
while (true);
}
}
// The main loop
void loop() {
// Check if there is any motion
if (detectMotion()) {
// Move servos so that the camera is centered on the movement
int panPos = 90;
int tiltPos = 90;
// Determine the direction of motion
// If the motionY is greater than a threshold, then the pan servo should be adjusted to the right
if (motionY > 100) {
panServo.write(panPos);
} else {
panServo.write(0);
}
// Center the servos
tiltServo.write(tiltPos);
// Pull the trigger
//servo3.write(90);
delay(100);
//servo3.write(0);
} else {
// Stop the servos
stopServos();
}
// Delay for a few milliseconds
delay(100);
}
// Function to detect motion
bool detectMotion() {
// Capture an image
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return false;
}
// Delay for a few milliseconds to let the camera stabilize
delay(10);
// Get the raw image data
uint8_t *rawImage = fb->buf;
// Convert the image data to grayscale
// This is not necessary since the `esp_camera.h` library already returns the image data in grayscale
//uint8_t *grayscaleImage = new uint8_t[fb->width * fb->height];
//for (int i = 0; i < fb->width * fb->height; i++) {
// grayscaleImage[i] = rawImage[i];
//}
// Free the raw image data
//delete[] rawImage;
// Find the center of the image
int centerX = fb->width / 2;
int centerY = fb->height / 2;
// Determine the direction of motion
int motionY = 0;
for (int i = 0; i < fb->width; i++) {
motionY += rawImage[centerX * fb->height + i];
}
// Free the grayscale image
//delete[] grayscaleImage;
esp_camera_fb_return(fb);
// If the motionY is greater than a threshold, then there is motion
return motionY > 100;
}
// Function to stop the servos
void stopServos() {
panServo.write(0);
tiltServo.write(0);
}
All the libraries are installed, i've restarted the pc, the Arduino IDE and the problem persists.
Could it be a connection problem? Here is my diagram:
Hopefully, I was able to provide all the information you need.
Any tips and help would be very much appreciated. Thank you!