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)
U0R (RX)TX
U0T (TX)RX
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.

Arduino IDE 2 Upload Button

5) When you start to see some dots on the debugging window, you may need to press the ESP32-CAM on-board RST button if it doesn’t go automatically into flashing mode.

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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

74 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
    • Eduardo you are correct, the jumper should be set to 3.3V. The [ESP32 chip] only runs at 3.3V and so you should never drive its input pins higher than that. If you do, current will flow through the built-in protection diodes into the 3.3V bus, and this can damage the ESP32. The FTDI jumper must be set to 3.3V.

      BUT with this 3.3V jumper set correctly, the VCC output voltage is too low to power up the [ESP32 board]. The problem is that the [ESP32 board] needs 5V but the [ESP32 chip] can only handle 3.3V. The [ESP32 board] takes the 5V input and regulates it down to 3.3V for the [ESP32 chip]. That regulator won’t work if you only give the board 3.3V to start with. So the red wire in the hook-up diagram is incorrect.

      The correct solution is to give the [ESP32 board] the +5V it requires from the terminal pad labeled “5V” on the FTDI board. On my FTDI board this pad is located 4 holes up from the end on the long edge of the board. I had to solder a wire to it, there isn’t a pin header on this edge.

      When you power the [ESP32 board] from “5V” and set the jumper to 3.3V then the configuration is good and you won’t damage anything.

      Jim Luschen

      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
        • Dear Sara Santos,

          Please re-read NickB’s comment. He is correct on all accounts.
          Remember there are designers trying to work with the
          ESP32-CAM. We are designing after market products using
          this board. We need to run it WITHOUT the FTDI as the power
          source. We want to run it with a VCC and a GND.

          I am confident in saying that there is an error in this board.
          I will look at the circuit diagram but I am not going expend much
          time doing so.

          If you have direct contact with the design/manufacture of this board, tell
          them it has a power source design error and for them to fix it.

          Please let me know when a new version of the board is released.

          Sara, thank you for all of your valuable comments thus far.

          Anxiously awaiting new version,
          Rex Rooples

          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
  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
  9. WARNING: This hookup is wrong and can damage your ESP32. On FTDI1232 when the jumper is placed in the 5v position it also drives the serial I/O pins at the 5v rail (on FT232RL VCCIO is then connected to 5v instead of the onboard 3.3v 50ma regulator and since the ESP32 input pins are not 5v tolerant, it can be damaged). Moreover, in that configuration you simply cannot then connect the FTDI VCC output pin to the 3.3v pin on the ESP32-Cam because of the low current output on the FT232RL. Instead LEAVE the jumper in the 3.3v position and connect the ESP32-Cam 5v pin to the 5v jumper pin. In that way logic levels will be at 3.3v and power to the ESP32-Cam will be supply through its onboard regulator. I have seen other hookups leaving the jumper at the 3.3v position and supplying the ESP32-Cam directly from the FTDI1232 and of course that is equally wrong. Either hookup may work but not reliably and the 5v jumper position could very well damage your ESP32-Cam.

    Reply
  10. Can I use a USBASP instead of the FTDI? Isn’t it just serial TX/RX data?

    I don’t have a FTDI but a couple USBASPs.

    I want to program my ESPCAM:-)

    Reply
  11. Hello Folks:

    I am so grateful for this forum. Only after I switched to 5 V mode from 3.3V did the camera start working properly–otherwise I was bogged down with “brownout” issues for nearly a whole day! I do have a problem however–there are a lot of fine horizontal lines in the picture–is there any way to prevent this? Or am I expecting too much from a $10 diy camera? OR is this part of the normal diy experience???

    Reply
    • Hi.
      The horizontal lines are not normal.
      It may be a weak wi-fi signal or the camera might be not connected properly.
      You can disconnect the camera and connect it again to make sure that everything is good.
      Regards,
      Sara

      Reply
  12. The FTDI board produces regulated 3.3V why can’t I just attach directly to the 3.3V rail on the ESP32? If I were making a custom circuit using an ESP32 I would just be powering everything off of 3.3V anyway, going through 5V seems like an unneeded step.

    Also looking at the schematic for the ESP32-CAM board (https://github.com/SeeedDocument/forum_doc/raw/master/reg/ESP32_CAM_V1.6.pdf) I don’t see anything on the board that needs 5V aside from the regulator

    Reply
    • Hi.
      You are right.
      Our projects worked well with 3.3V.
      However, many of our readers reported that they were able to upload code only after powering 5V.
      That also works for us. That’s why we recommend 5V. It works for everyone.
      Regards,
      Sara

      Reply
      • Hi. I contacted Rui about the ESP32 CAM. After hours and hours of trying to upload the program for the Camera Web Server, I finally got it to load. However, after removing the grounding wire and resetting the camera all I see on the serial monitor is the recognition of the Cam but then as it tries to connect to the internet I get screen after screen of :
        ‘……………………………………………………………………………………………………………………’.
        Can you tell me what this means or what the program is trying to do? This is the part of my original question that Rui did not address. Looking forward to hearing from you.

        Reply
      • I would like to interject 2 points:
        1. The FTDI should be set for 3.3 volts, and if it will not program, Move the wire from the pin of the FTDI, to the 5 volt output of the FTDI.(and leave the other end connected to the 5V input of the ESPCam.) That way, you have 5V power to the voltage regulator, but are only driving the UART pins with 3.3 volts.

        An easier, more reliable way to program with the FTDI, is to place a jumper between GND and IO0, start serial monitor, Press the reset button. The serial monitor will display something like:
        12:18:39.238 -> ets Jun 8 2016 00:22:57
        12:18:39.238 ->
        12:18:39.238 -> rst:0x1 (POWERON_RESET),boot:0x3 (DOWNLOAD_BOOT(UART0/UART1/SDIO_REI_REO_V2))
        12:18:39.238 -> waiting for download

        Once it is “Waiting for Download” remove the jumper, and Upload your Skech.
        The ESP will be reset once it has finished loading, and should begin operating.

        Reply
        • I would like to interject 2 points:
          1. The FTDI should be set for 3.3 volts, and if it will not program, Move the wire from the pin of the FTDI, to the 5 volt output of the FTDI.(and leave the other end connected to the 5V input of the ESPCam.) That way, you have 5V power to the voltage regulator, but are only driving the UART pins with 3.3 volts.

          An easier, more reliable way to program with the FTDI, is to place a jumper between GND and IO0, start serial monitor, Press the reset button. The serial monitor will display something like:
          12:18:39.238 -> ets Jun 8 2016 00:22:57
          12:18:39.238 ->
          12:18:39.238 -> rst:0x1 (POWERON_RESET),boot:0x3 (DOWNLOAD_BOOT(UART0/UART1/SDIO_REI_REO_V2))
          12:18:39.238 -> waiting for download

          Once it is “Waiting for Download” remove the jumper, and Upload your Skech.
          The ESP will be reset once it has finished loading, and should begin operating.

          That should have been:
          Remove the wire from the Vcc Pin of the FTDI, and connect it to the 5 volt output of the FTDI.(and leave the other end connected to the 5V input of the ESPCam.)

          Reply
        • I would like to interject 2 points:
          1. The FTDI should be set for 3.3 volts, and if it will not program, Remove the wire from the Vcc Pin of the FTDI, and connect it to the 5 volt output of the FTDI.(and leave the other end connected to the 5V input of the ESPCam.) That way, you have 5V power to the voltage regulator, but are only driving the UART pins with 3.3 volts.

          An easier, more reliable way to program with the FTDI, is to place a jumper between GND and IO0, start serial monitor, Press the reset button. The serial monitor will display something like:
          12:18:39.238 -> ets Jun 8 2016 00:22:57
          12:18:39.238 ->
          12:18:39.238 -> rst:0x1 (POWERON_RESET),boot:0x3 (DOWNLOAD_BOOT(UART0/UART1/SDIO_REI_REO_V2))
          12:18:39.238 -> waiting for download

          Once it is “Waiting for Download” remove the jumper, and Upload your Skech.
          The ESP will be reset once it has finished loading, and should begin operating.

          Reply
  13. esp32 cam
    I press reset to get IP address, and in Arduino serial monitor i get

    Jun 8 2016 00:22:57

    rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0xoo,q_drv:0x00,d_drv:0x00,cs0_drv:0xoo,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len1216
    ho 0 tail 12 room 4
    load:0x40078000,len:10944
    load:0x40080400,len:6388
    entry 0x400806b4

    . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    The row of dots just continues indefinitely with no more information printed
    Any ideas about what is happening?.

    Reply
  14. Hi,
    if my devices is powered off for a while maybe a few days I can#t connect to my Network only if i push the reset button. My esp32cam is in a box and I don’t want open it every time if I loose my connectivity. Any ideas to change it?

    Reply
  15. The wiring did NOT work as shown and was unable to flash the esp32cam.

    I changed the ground pin wire around to the ground that is next to the 5v pin and it worked as intended.

    Having said that, the rest of the guide was good. Thanks

    Reply
  16. Hi Rui,
    This tutorial is very didactic but I think it may have something outdated.
    I’m using Arduino IDE 1.8.19 and ESP32 V 2.0.2.
    My camera works once, with a dark image and then it crashes.

    IP Address: http://192.168.1.110
    Taking a photo…
    Picture file name: /photo.jpg
    The picture has been saved in /photo.jpg – Size: 41088 bytes
    Taking a photo…
    Picture file name: /photo.jpg
    The picture has been saved in /photo.jpg – Size: 0 bytes
    Taking a photo…
    Picture file name: /photo.jpg

    IP Address: http://192.168.1.110
    E (4678) camera: Camera probe failed with error 0x105(ESP_ERR_NOT_FOUND)
    Camera init failed with error 0x105ets Jun 8 2016 00:22:57

    But using the example CameraWebServer of AI-Thinker Cam ESP32 it works fine.

    Thank you.

    Reply
  17. I have the sketch uploaded successfully, however, I just get . . . . . and the AI Thinker ESP32-cam never connects to WiFi. I tried the separate sketch to test the WiFi connection and get the same result. I tried another ESP32 board and it connects with the WiFi just fine. I have double checked the connections and don’t see any other problem. (Note: my router is located in another room–could the signal strength be too weak?)

    Reply
  18. Hi there,
    i am also using esp32 cam. your territorial has helped me a lot. Today i have a project for using image processing using esp32 can over the live feed i want to use background subtraction on it can you help me a little over it.
    if it is possible please recommend similar prjects too for better understanding

    Reply
  19. Hi Rui and Sara,
    Flashed Blink sketch-OK with CP21o2, CP2103 will not work- but any other sketch for streaming video gives all sorts of errors and none gives de ESP32’s IP address.
    Your comments are welcome and appreciated.
    Best regards,
    Alfredo Gonzalez

    Reply
      • Hi Sara:
        Thanks for your fast response. I did not mention it before, but I find your tutorials very useful.
        Tried again using the ESP32-CAM-MB and get the following in the Serial Monitor:

        rst:0x1 (POWERON_RESET), boot: 0x13 (SPI_FAST_FLASH_BOOT)
        config: 0, SPIWP:0xee
        clk_drv: 0x00, q_drv:0x00, d_drv:0x00, csd_drv: 0x00, hd_drv: 0x00, wp_drv: 0x00,
        mode:DI=,clock div: 1
        load: 0x3ff0030, len:1344
        load: 0x0040078000, len: 13924
        ho 0 tail 12 room 4
        load: 0x0400804, len: 3600
        entry 0x400805f0

        Then I tried with the CP2102 breakout, not pressing reset at connecting………
        Get the same results in Serial Monitor as above.

        Next, CP2102, but pressing reset at Connecting……………
        1st try:
        Error:A fatal error occurred: invalid head of packet (0x65): Possible serial noise or corruption
        2nd try:
        Error: A fatal error ocurred: unable to verify chip connection (invalid head of packet (0x78): Possible serial noise or corruption.).
        Next tried programming with the MB adapter. sketch seems to load ok. Removed camera board from adapter and connected to PC through CP2102, pushed reset and got two print-outs of the sam results as above but with the followig lines added:
        E (508) camera: Detected camera not supported
        E(508) camera: Camera probe failed with error 0x106 (ESP_ERR_NOT_SUPPORTED)
        Camera init failed with error 0x106

        Hope you can help me.
        Many thanksfor your time.
        Best regards
        Alfredo Gonzalez

        Reply
  20. Thanks !!!
    I read many sites before land here. You explain the crucial steps. Finally, I could program my board…
    Thanks again !.

    Reply
  21. If you want to run the ESP32-CAM off 5V it is probably “safer” to put the jumper on the 3V3 position and run a separate Dupont wire from the 5V pin on the programmer to the 5V pin on the CAM.

    This way the UART logic levels are at 3V3.

    Reply
  22. Hi,

    I’m trying my first ESP32-Cam project with a WROVER from Freenove.
    When building my application, do I have to define BOARD_WROVER_KIT somewhere so that the compilation of the source code will make the correct assumptions in terms of pins assignment ? (I see #if BOARD_WROVER_KIT several times)
    Thanks, B.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

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