How to Program / Upload Code to ESP32-CAM AI-Thinker (Arduino IDE)

The ESP32-CAM AI-Thinker development board can be programmed using Arduino IDE. This guide shows how to program and upload code to the ESP32-CAM (AI-Thinker) development board using Arduino IDE.

How to program upload code to ESP32-CAM board Arduino IDE with FTDI Programmer

The ESP32-CAM AI-Thinker module is an ESP32 development board with an OV2640 camera, microSD card support, on-board flash lamp and several GPIOs to connect peripherals. However, it doesn’t have a built-in programmer. You need an FTDI programmer to connect it to your computer and upload code.

Install the ESP32 Add-on

To program the ESP32-CAM board with Arduino IDE, you need to have Arduino IDE installed as well as the ESP32 add-on. Follow the next tutorial to install the ESP32 add-on, if you haven’t already:

Program ESP32-CAM (Upload Code with Arduino IDE)

To upload code to the ESP32-CAM (AI-Thinker) using Arduino IDE, follow the next exact steps.

Connect the ESP32-CAM board to your computer using an FTDI programmer. Follow the next schematic diagram:

ESP32-CAM connected to an FTDI Programmer to upload program using Arduino IDE

Note: the order of the FTDI pins on the diagram may not match yours. Make sure you check the silkscreen label next to each pin.

Many FTDI programmers have a jumper that allows you to select 3.3V or 5V. Make sure the jumper is in the right place to select 5V.

FTDI Programmer select jumper cap to 5V

Important: GPIO 0 needs to be connected to GND so that you’re able to upload code.

ESP32-CAMFTDI Programmer
GNDGND
5VVCC (5V)
U0RTX
U0TRX
GPIO 0GND

To upload code to the ESP32-CAM using Arduino IDE, follow the next steps:

1) Go to Tools Board and select AI-Thinker ESP32-CAM. You must have the ESP32 add-on installed. Otherwise, this board won’t show up on the Boards menu.

2) Go to Tools Port and select the COM port the ESP32-CAM is connected to.

3) For demonstration purposes, you can upload a blank sketch to your board:

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

4) Then, click the Upload button in your Arduino IDE.

Program ESP32-CAM with Arduino IDE

5) When you start to see some dots on the debugging window, press the ESP32-CAM on-board RST button.

Program and upload code to ESP32-CAM with Arduino IDE
ESP32-CAM Press RESET RST on-board button to restart

After a few seconds, the code should be successfully uploaded to your board.

ESP32-CAM AI Thinker Module Arduino IDE Done Uploading Success Message

6) When you see the “Done uploading” message, you need to remove GPIO 0 from GND and press the RST button to run your new code.

Common Errors and How to Fix Them

If you don’t follow the previous instructions exactly, you may get the following errors:

Failed to connect to ESP32: Timed out waiting for packet header

ESP32-CAM Failed to connect to ESP32: Timed out waiting for packet header

This error means that the ESP32-CAM is not in flashing mode or it is not connected properly to the FTDI programmer.

Brownout detector or Guru meditation error

When you open your Arduino IDE Serial Monitor and the error message “Brownout detector was triggered” is constantly being printed over and over again. It means that there’s some sort of hardware problem.

It’s often related to one of the following issues:

  • Poor quality USB cable;
  • USB cable is too long;
  • Board with some defect (bad solder joints);
  • Bad computer USB port;
  • Or not enough power provided by the computer USB port.

Solution: 

  • Try a different shorter USB cable (with data wires);
  • Use a different computer USB port or use a USB hub with an external power supply;
  • Some readers were using 3.3V and reported that when powering the ESP32-CAM with 5V, the issue was fixed.

Board at COMX is not available – COM Port not selected

ESP32-CAM COM Port Not Selected

If you get the following error or similar:

serial.serialutil.SerialException: could not open port 'COM8': WindowsError(2, 'The system cannot find the file specified.')
Failed to execute script esptool
the selected serial port Failed to execute script esptool
 does not exist or your board is not connected
Board at COM8 is not available

It means that you haven’t selected the COM port in the Tools menu. In your Arduino IDE, go to Tools Port and select the COM port the ESP32 is connected to.

It might also mean that the ESP32-CAM is not establishing a serial connection with your computer or it is not properly connected to the USB connector.

Other errors

For a more extensive list of the most common problems with the ESP32-CAM and how to fix them, read our ESP32-CAM Troubleshooting Guide.

Learn how to program and build 17 projects with the ESP32-CAM using Arduino IDE DOWNLOAD »

Learn how to program and build 17 projects with the ESP32-CAM using Arduino IDE DOWNLOAD »

Wrapping Up

We hope this guide helps you get started programming your ESP32-CAM AI-Thinker using Arduino IDE.

Check all our projects with the ESP32-CAM:

There are different models of ESP32 camera development boards that might be more suitable for your projects. So, you might also like reading:

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

23 thoughts on “How to Program / Upload Code to ESP32-CAM AI-Thinker (Arduino IDE)”

  1. Hi Rui,
    Thanks for very precise tutorials on ESP8266 and ESP32 projects 🙂
    I purchased 2 pcs ESP32 cameras from different Ebay vendors and are experiencing the same issue with both of them.
    After some hours of the camera functioning perfectly it disconnects from wifi (does not reply to ping even) but will reconnect again after power cycling camera.
    I have fitted an electrolytic across power header pins to stabilise power supply (5v) to camera.
    Also tested connecting to 2 different brand wifi access points (close to camera so signal strength is strong) also used 5volt 2 Amp regulated power supply for camera with no improvements.
    Have you had this problem before with the ESP32 camera boards ?

    Reply
    • All the ESP32 seem to drop of the WiFi eventually. You can fix it with adding a few lines to your code by checking is the WiFi is still online and if not to reconnect to the WiFi. I have added these lines to all my code and it works.

      In setup add:

      WIFI_Connect();

      And in loop:

      if (WiFi.status() != WL_CONNECTED) WIFI_Connect(); //Make sure wifi is still connected

      And add this to be save before setup() to avoid compiler warnings.

      void WIFI_Connect()
      {
      WiFi.disconnect();
      WiFi.mode(WIFI_STA);
      WiFi.begin(SSID, PASSWORD); //Use #define SSID “YourWiFi” etc the top etc
      for (int i = 0; i < 60; i++) {
      if ( WiFi.status() != WL_CONNECTED ) vTaskDelay(500); //Need this to feed the watchdog!
      }
      if (debug) Serial.println(“Connected to wifi”);
      Serial.println(WiFi.macAddress());
      }

      Reply
  2. Hi! Thanks for the tutorial!
    I’m trying to connect a gpio extender (PCF8574), and it uses an i2c protocol. Do you know how can I set up a new i2c bus for the extender?

    Reply
  3. Rui, I can seem to get past the camera setup on my esp32-cam boards. I have tried several different boards and cables, different board configs and progammer settings. I can get the program to load but when the esp32-cam is reset always ends in detected camera not supported
    Camera probe failed with error 0x20004.

    Reply
  4. Hi, Thanks for the tutorial.
    i am unable to upload cam web server program to my board. It shows sketch is large and timed out waiting.
    In tools, there is no partition scheme shown to change to Large app OTA.
    May i know how to add that partition scheme ??
    or any other fixes?

    Reply
    • Hi.
      If you’re having that issue, select the board “ESP32 Wrover Module”
      Then, you can select the partition scheme to Huge APP(3MB No OTA)
      Regards,
      Sara

      Reply
      • I tried as Wrover Module and AI Thinker. In both the cases, after compiling the sketch, it shows connecting_____……….____……… and goes on ! to show the error A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
        Checked CP2102 programmer, tried uploading with Arduino via Tx/Rx and nothing worked. previously a month ago, i was able to do the same.
        what may be the bug ? any fixes?

        Reply
  5. Hi,

    I have recently buy an ESP32 CAM and I have a doubt: I hav the same FTDI programmer that you used in the document.

    Must I switch jumper to 3.3 V or to 5V ? I thought that must be in 3.3V taking in account that the ESP32 is 3.3 V compatible in its inputs and outputs but a I read in your document and saw in the picture that jumper is in 5 V.

    Thank you very much

    Reply
    • Hi Eduardo.
      Yes, put the jumper in 5V.
      In our case, it worked in both configurations. However, many of our readers had problems with the 3.3V configuration – that’s why we recommend 5V.
      Regards,
      Sara

      Reply
  6. Hey, thank you for the great tutorial,
    it runs perfectly with the programmer hooked on it, but can it be run without the FTDI once it was programmed? I am not getting it to work the moment I run it without the interface. As it is not hooked up, I can’ debug it.
    Any suggetions?

    Thank you
    Rocco

    Reply
    • Hi.
      How are you powering your ESP32-CAM?
      We used a 5V power adapter as shown in this image and it worked fine: randomnerdtutorials.com/wp-content/uploads/2019/03/5V-power-supply-ESP32-CAM.jpg?w=750&ssl=1
      Regards,
      Sara

      Reply
    • Were you able to figure this out? I have the same problem.

      I can even disconnect the TX/RX lines from the FTDI (so only 5V and GND are connected to the ESP32-CAM), plug that into my computer, and it works.

      I take that same setup and plug the USB cable into a 5V USB power bank — nothing. Or I use my lab power supply and connect it to 5V and GND on the ESP32-CAM…again nothing.

      What’s the magic about plugging the USB cable into a computer USB port? Not making any sense to me.

      Thanks.

      Reply
  7. Hi Rui/Sara,

    I am trying to upload the code to ESP32-S cam board(It not branded as AI thinker but with espressif logo, link- rb.gy/fr7q66) using FTDI programmer . I am struggling with error “A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header” from past two days. Connections to FTDI programmer from ESP32 cam are perfect. TX /RS are not swapped. IO0 is connected to GND and I am supplying 5v using Jumper cap on FTDI, also tried the workaround of holding RST button for long time but still getting the error. Can I assume that board is faulty ? If possible could you please let me if there is any way to test at least if board is working.

    Reply
    • Hi.
      That board, looking at the photo seems OK.
      In this article we already discuss the main issues and possible ways to solve them.
      Maybe you can check your FTDI programmer? Are you sure it works OK?
      What FTDI programmer are you using?
      We’re using the one as shown in the photo in this tutorial.
      Regards,
      Sara

      Reply
      • Hi Sara,

        I just did a loop back test with FTDI programmer using putty, I am able to see the echo. So I can say FTDI programmer is working fine.

        Reply
  8. Hi Rui & Sara,

    I successfully flashed the Sketch in my ESP32-CAM. Mine is a Chinese brand so I was not expecting it will work properly. Before doing the flashing, I did several research on different sites so I learned that the FTDI programmer must be in 5V config and ESP32-CAM works better in 5V supply. FTDI programmer is my preferred programmer for ESP-01 by the way.
    I encountered the expected errors and bugs so to speak.

    The only thing that bothers me is my ESP32-CAM will only work when the Tx-Rx are still connected? Is this expected?

    I was thinking 5v and GND are sufficient.

    Reply

Leave a Reply to Ed Cañeda Cancel reply

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.