ESP32: Wrong boot mode detected

I have just purchased an ESP32 Cam module. I set it up and loaded two separate sketches which worked first time. When I loaded a third program (time lapse) that worked but I couldn't load anything after that one. I get the error message:

A fatal error occurred: Failed to connect to ESP32: Wrong boot mode detected (0xb)! The chip needs to be in download mode.

I think it might have something to do with the code of that final, third program:

/****************************************************************************
 *   ESP32 Hacks (c) 2021 Marcio Teixeira                                   *
 *                                                                          *
 *   https://github.com/marciot/esp32-hacks                                 *
 *                                                                          *
 *   This program is free software: you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation, either version 3 of the License, or      *
 *   (at your option) any later version.                                    *
 *                                                                          *
 *   This program is distributed in the hope that it will be useful,        *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for more details.                           *
 *                                                                          *
 *   To view a copy of the GNU General Public License, go to the following  *
 *   location: <http://www.gnu.org/licenses/>.                              *
 ****************************************************************************/

#include <SD_MMC.h>
#include "esp_camera.h"

#define MINUTES_BETWEEN_PHOTOS 0.5

// Pins for ESP32-CAM

#define FLASH_PIN         4
// Camera pins
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27

#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

bool startMicroSD() {
  Serial.print("Starting microSD... ");

  // Pin 13 needs to be pulled-up
  // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sd_pullup_requirements.html#pull-up-conflicts-on-gpio13
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  if(SD_MMC.begin("/sdcard", true)) {
    Serial.println("OKAY");
    return true;
  } else {
    Serial.println("FAILED");
    return false;
  }
}

bool startCamera() {
  // Turn off the flash
  pinMode(FLASH_PIN, OUTPUT);
  digitalWrite(FLASH_PIN, LOW);

  // Initialize the camera hardware
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  
  // Set resolution based on whether we have extra memory
  if(psramFound()){
    Serial.println("PSRAM found. Maximum XGA resolution supported.");
    config.frame_size = FRAMESIZE_XGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    Serial.println("PSRAM not found. Maximum SVGA resolution supported.");
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

  // Start the camera
  Serial.print("Starting camera... ");
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.println("FAILED");
    return false;
  } else {
    Serial.println("OKAY");
    return true;
  }
}

void takePhoto(String filename) { 
  // Take a photo and get the data

  camera_fb_t *fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Unable to take a photo");
    return;
  }

  // Make sure it is a JPEG
  if (fb->format != PIXFORMAT_JPEG) {
     Serial.println("Capture format not JPEG");
     esp_camera_fb_return(fb); // Return the photo data
     return;
  }

  // Save the picture to the SD card

  File file = SD_MMC.open(filename.c_str(), "w");
  if(file) {
    Serial.println("Saving " + filename);
    file.write(fb->buf, fb->len);
    file.close();

    // Momentarily blink the flash
    digitalWrite(FLASH_PIN, HIGH);
    delay(100);
    digitalWrite(FLASH_PIN, LOW);
    delay(500);
  } else {
    Serial.println("Unable to write " + filename);
  }

  // Return the picture data
  esp_camera_fb_return(fb);
}

void setup() {
  // Initialize the peripherals
  Serial.begin(115200);
  while(!Serial) delay(100);

  startMicroSD();
  startCamera();

  delay(5000); // Delay 5 seconds before first photo
}

void loop() {
  // Keep a count of the number of photos we have taken
  static int number = 0;
  number++;

  // Construct a filename that looks like "/photo_0001.jpg"
  
  String filename = "/photo_";
  if(number < 1000) filename += "0";
  if(number < 100)  filename += "0";
  if(number < 10)   filename += "0";
  filename += number;
  filename += ".jpg";
  
  takePhoto(filename);

  // Delay until the next photo

  delay(MINUTES_BETWEEN_PHOTOS * 60 * 1000);
}

I've searched the web and lots of people are saying to press the "boot" button when uploading a sketch. My module doesn't have one!
Here's a link to the one that I bought:
https://www.ebay.co.uk/itm/254810284860

Any help will be greatly appreciated

Thanks
1 Like

Certain I/O pins have to be in specific states for boot to work. If you added hardware which violates this the boot will fail. I would check for hardware attached to these pins. See for example
https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/boot-mode-selection.html
or any of the other search results.

Same problem:

Failed to connect to ESP32: Wrong boot mode detected (0xb)

first attempt with an ESP32 having previously used the ESP8266 with no issues - or at least none that were not of my own making. Google and you will find many posts of others with the same problem - cannot upload to an esp32.

I'm using an esp32 from AZDelivery, generating my code with VScode, compiling and uploading using the arduino IDE 1.8.19 running under linux mint.

I had uploaded a couple of example sketches without issue, but then I found I could not upload with the stated error.

Online suggestions are pressing the boot and reset buttons on upload - didn't work, connecting a 1 - 3uF capacitor between EN and Gnd - didn't work.

What did work:

DO NOT USE GPIO2

I had a simple sketch using analogread on a potentiometer connected to GPIO2.

After much head-scratching (a couple of days). I found this:
https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/boot-mode-selection.html#boot-mode

It says:

GPIO2 must also be either left unconnected/floating, or driven Low, in order to enter the serial bootloader.

Which was not true in my case. Changed to using GPIO15 and leaving GPIO2 disconnected - the problem went away, no problem uploading sketches.

Posted in the hope that it will prevent someone else spending a couple of WTF days on why can't I upload?

Now my next problem, use an MS wireless 3500 mouse and MS ergonomic wireless keyboard - yes in spite of running linux, I like some M$ hardware :slightly_smiling_face:. With the esp32 plugged in the keyboard is verging on the un-usable - missing keystrokes or deciding to auto repeat (or worse auto-delete). Not a problem I had with the esp8266. This on a ThinkCentre M720Q. Probably should be subject to another post on another forum, but open to offers...

1 Like

I found that sketches wouldn't upload if there was a micro SD card in the module. Removing this, the sketches would upload seamlessly. Sorted!

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