ESP32 Camera connection error and serial noise

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!

Why do you have the ESP32CAM connected to the same serial pins that the UNO uses for program upload via its USB connection ?

The youtube tutorials and projects online connect the UOR and UOT pins to the RX and TX pins. I am a beginner so I dont know much. I do know its connected this way for the ESP32 and the Arduino Uno to communicated with one another. If what I have learnt is wrong, please enlighten me the proper way.

Thank you taking the time to help me out!

I would suggest you start from the beginning.

Write and test some code to cause the servos to sweep on the UNO.

Get a USB to serial adapter and check you can program the ESP32CAM and it can take pictures etc.

As for your code, you appear to be attempting to drive the servos, that are shown connected to the UNO, with a program thats running on the ESP32CAM, thats not going to work.

1 Like

Thanks! I went back to the basics and did the servo sweep code file. No problems there so communication is ok. I then did the CameraWebServer code test. I've done it before and it worked. However, this time it's showing this error:

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"





















app_httpd.cpp:22:10: fatal error: fd_forward.h: No such file or directory

 #include "fd_forward.h"

Multiple libraries were found for "WiFi.h"

          ^~~~~~~~~~~~~~

 Used: C:\Users\emman\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\libraries\WiFi

compilation terminated.

 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi

exit status 1

fd_forward.h: No such file or directory

Error downloading https://dl.espressif.com/dl/package_esp32_index.json



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I installed the libraries, restarted the computer and the basic unplug and plug back in. Nothing seems to work so I was hoping you have an idea of what i'm doing wrong. Thank you

Your either missing a library or the files that belong to the sketch; I suspect the latter based on the double quotes. Did you follow an example on the web and missed some of the files?

The file I used is an example of the Arduino IDE for ESP32 Cameras. It's in the 'examples' once you install the esp library. I did it again this time by closing the file I used before and choosing the same example but a new one. I was finally able to upload the code and it worked this time. I bought a new cable for the Arduino Uno just in case but it was too long and didn't help me in the end.

So the camera works, It's connected to the Arduino UNO through pins RX, TX, 5v & GND. This is what I was instructed in a YT tutorial. I'm not quite sure where to go from here. As you mentioned, I won't be able to command the servos connected to Arduino if the program is for the ESP32. I'm confused, does that mean I will have to make different code for the ESP32CAM and Arduino UNO? How will I make them communicate with each other? Will I need to buy new parts?

Again, thank you for taking the time to help out a beginner like me!

I'm in the process of making a Motion tracking turret.

Here are my materials:

  • ESP32 Camera
  • 3 MG996r Servos
  • Arduino UNO
  • Small Nerf Blaster

Here is the diagram:

I tried making a single code file to program everything:

#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);
}

I ran into a lot of errors that I could not fix. I was told in a separate post* that it was not possible to program code for the ESP32 and run it on the Arduino UNO. I am a beginner and I don't know where to go from here. Are there additional parts I should purchase in order to make connections possible through this? Will I have to upload multiple code files? Is there anywhere like YouTube videos or articles I can learn to make this possible? I have searched the internet and there isn't enough information for projects like these.

Thank you for taking the time to read my post. Any tips & help would be very much appreciated, thank you!

*ESP32 Camera connection error and serial noise

Why starting a new topic? Continue the already existing one. There are tips and advice You need to consider.

1 Like

@ej_salga

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

Congratulations upon your first ‘real’ schematic!
You’ve tried to pull a lot of learning into your first post.

Just a heads-up for the connections on the bottom-most servo are interesting! Everyone gets caught this way when starting M
Don’t ‘cross’ wires at unintended connection points…

Enjoy the progress.

1 Like

@UKHeliBob @Railroader

My apologies, I thought posting in a different category would help get more information from different perspectives. Thanks for teaching me, I won't do this again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.