ESP32 S3 stop working afted code upload! No led

NerdMov
Posts: 5
Joined: Sat Jan 04, 2025 2:43 pm

ESP32 S3 stop working afted code upload! No led

Postby NerdMov » Sat Jan 04, 2025 2:50 pm

Hi Guys,
I'm trying to work at this project.https://www.youtube.com/watch?v=9B_DaUx8Z-U&t=605s with this code included. I compiled with no problem or error.

But after the upload it seem the esp32s3 stop working. All the led don't turn on if connected and i can't upload new code.
I tried with two different boards ad game me the same problem.

Any ideas? Also for recover the two boards?

Code: Select all

#include <WiFi.h>
#include <ESP32Servo.h>
#include <esp_now.h>
#include <DFRobotDFPlayerMini.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <FastLED.h>
#include <Wire.h>

#define BUTTON_PIN 1
#define NEOPIXEL_PIN 10
#define NUM_PIXELS 8
#define I2C_SDA_PIN 5
#define I2C_SCL_PIN 6
#define DFPLAYER_RX 7
#define DFPLAYER_TX 8

uint8_t receiverAddress[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t soundBoardMAC[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Sound effect board MAC

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
DFRobotDFPlayerMini dfPlayer;
CRGB leds[NUM_PIXELS];

unsigned long buttonPressStartTime = 0;
const unsigned long longPressThreshold = 1000;
const unsigned long veryLongPressThreshold = 5000;
const unsigned long extraLongPressThreshold = 8000;

enum Mode {
  ONBOARD_SOUND,
  WIRELESS_SOUND
};

Mode currentMode = ONBOARD_SOUND;

enum FadeState {
  OFF,
  FADING_ON,
  ON,
  FADING_OFF,
  ALWAYS_ON,
  BLAST
};

FadeState fadeState = OFF;
unsigned long fadeStartTime = 0;
const unsigned long fadeDuration = 500;

const int filterSize = 20;
float angleBuffer[filterSize] = {0};
int bufferIndex = 0;

const float movementThreshold = 0.8; // Increased sensitivity
const unsigned long angleCheckInterval = 50;
unsigned long lastAngleCheckTime = 0;

unsigned long blastStartTime = 0;
const unsigned long blastDuration = 1500; // 1.5 seconds
const unsigned long blastCooldown = 1000; // Cooldown between blasts

bool blastTriggered = false;
unsigned long lastBlastTime = 0;

void handleButtonPress();
void handleAccelerometer();
void handleNeoPixels();
void sendWirelessData(uint8_t data, const uint8_t *address);
float calculateMovingAverage(float newValue);

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  WiFi.mode(WIFI_STA);
  Serial.println("WiFi initialized");
  Serial.print("Controller MAC Address: ");
  Serial.println(WiFi.macAddress());

  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  Serial.println("ESP-NOW initialized");

  esp_now_peer_info_t peerInfo = {};
  memcpy(peerInfo.peer_addr, receiverAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add receiver peer");
    return;
  }
  Serial.println("Receiver peer added");

  memcpy(peerInfo.peer_addr, soundBoardMAC, 6);

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add soundboard peer");
    return;
  }
  Serial.println("Soundboard peer added");

  Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);

  if (!accel.begin()) {
    Serial.println("No ADXL345 detected");
    while (1);
  }
  Serial.println("ADXL345 initialized");

  FastLED.addLeds<NEOPIXEL, NEOPIXEL_PIN>(leds, NUM_PIXELS);
  FastLED.clear();
  FastLED.show();
  Serial.println("FastLED initialized");

  Serial1.begin(9600, SERIAL_8N1, DFPLAYER_RX, DFPLAYER_TX);
  if (!dfPlayer.begin(Serial1)) {
    Serial.println("DFPlayer Mini not detected");
    while (1);
  }
  dfPlayer.setTimeOut(500);
  dfPlayer.volume(30);
  Serial.println("DFPlayer Mini initialized");
}

void loop() {
  handleButtonPress();
  handleAccelerometer();
  handleNeoPixels();
}

void handleButtonPress() {
  static int lastButtonState = HIGH;
  int buttonState = digitalRead(BUTTON_PIN);
  unsigned long currentTime = millis();

  if (buttonState == LOW && lastButtonState == HIGH) {
    buttonPressStartTime = currentTime;
  } else if (buttonState == HIGH && lastButtonState == LOW) {
    unsigned long pressDuration = currentTime - buttonPressStartTime;
    if (pressDuration >= extraLongPressThreshold) {
      currentMode = (currentMode == ONBOARD_SOUND) ? WIRELESS_SOUND : ONBOARD_SOUND;
      Serial.print("Mode switched to: ");
      Serial.println(currentMode == ONBOARD_SOUND ? "ONBOARD_SOUND" : "WIRELESS_SOUND");
    } else if (pressDuration >= veryLongPressThreshold) {
      if (fadeState == ALWAYS_ON) {
        fadeState = OFF;
        Serial.println("Exiting ALWAYS_ON mode");
      } else {
        fadeState = ALWAYS_ON;
        Serial.println("Entering ALWAYS_ON mode");
      }
    } else if (pressDuration >= longPressThreshold) {
      uint8_t data = 102;
      sendWirelessData(data, receiverAddress);
      Serial.println("Long press detected, data sent");
    } else {
      uint8_t data = 101;
      sendWirelessData(data, receiverAddress);
      Serial.println("Short press detected, data sent");
    }
    delay(200);
  }

  lastButtonState = buttonState;
}

void handleAccelerometer() {
  unsigned long currentTime = millis();

  if (currentTime - lastAngleCheckTime < angleCheckInterval) {
    return;
  }
  lastAngleCheckTime = currentTime;

  sensors_event_t event;
  accel.getEvent(&event);

  float angle = atan2(event.acceleration.y, event.acceleration.z) * 180 / PI;
  float filteredAngle = calculateMovingAverage(angle);

  static float lastFilteredAngle = 0;

  if (abs(filteredAngle - lastFilteredAngle) < movementThreshold) {
    return;
  }

  float accelerationChange = event.acceleration.x;
  float yAngle = atan2(event.acceleration.x, event.acceleration.z) * 180 / PI;
  float zAngle = atan2(event.acceleration.x, event.acceleration.y) * 180 / PI;

  if (!blastTriggered && accelerationChange < -0.9 && abs(yAngle) < 45 && abs(zAngle) < 30) {
    if (fadeState == ON) {
      if (currentTime - lastBlastTime > blastCooldown) {
        fadeState = BLAST;
        blastTriggered = true;
        blastStartTime = millis();
        if (currentMode == WIRELESS_SOUND) {
          sendWirelessData(13, soundBoardMAC);
        } else {
          dfPlayer.play(3);
        }
        Serial.println("BLAST activated");
        lastBlastTime = currentTime;
      }
    }
  }

  lastFilteredAngle = filteredAngle;

  if (filteredAngle >= 35 && fadeState == OFF) {
    if (currentMode == WIRELESS_SOUND) {
      sendWirelessData(12, soundBoardMAC);
    } else {
      dfPlayer.play(2);
    }
    fadeState = FADING_ON;
    fadeStartTime = millis();
    Serial.println("Starting to fade on");
  } else if (filteredAngle < 35 && fadeState == ON) {
    if (currentMode == WIRELESS_SOUND) {
      sendWirelessData(11, soundBoardMAC);
    } else {
      dfPlayer.play(1);
    }
    fadeState = FADING_OFF;
    fadeStartTime = millis();
    Serial.println("Starting to fade off");
  }
}

void handleNeoPixels() {
  unsigned long currentTime = millis();

  switch (fadeState) {
    case OFF:
      FastLED.clear();
      FastLED.show();
      break;

    case FADING_ON:
      if (currentTime - fadeStartTime < fadeDuration) {
        int brightness = map(currentTime - fadeStartTime, 0, fadeDuration, 0, 255);
        for (int i = 0; i < NUM_PIXELS; i++) {
          leds[i] = CRGB(brightness, brightness, brightness);
        }
        FastLED.show();
      } else {
        for (int i = 0; i < NUM_PIXELS; i++) {
          leds[i] = CRGB(255, 255, 255);
        }
        FastLED.show();
        fadeState = ON;
        Serial.println("Fade on complete");
      }
      break;

    case ON:
      for (int i = 0; i < NUM_PIXELS; i++) {
        leds[i] = CRGB(255, 255, 255);
      }
      FastLED.show();
      break;

    case FADING_OFF:
      if (currentTime - fadeStartTime < fadeDuration) {
        int brightness = map(currentTime - fadeStartTime, 0, fadeDuration, 255, 0);
        for (int i = 0; i < NUM_PIXELS; i++) {
          leds[i] = CRGB(brightness, brightness, brightness);
        }
        FastLED.show();
      } else {
        FastLED.clear();
        FastLED.show();
        fadeState = OFF;
        Serial.println("Fade off complete");
      }
      break;

    case ALWAYS_ON:
      for (int i = 0; i < NUM_PIXELS; i++) {
        leds[i] = CRGB(255, 255, 255);
      }
      FastLED.show();
      break;

    case BLAST:
      if (currentTime - blastStartTime < blastDuration) {
        for (int i = 0; i < NUM_PIXELS; i++) {
          int flicker = random(100, 150); // Adjust brightness for darker yellow
          if (random(3) == 0) {
            leds[i] = CRGB(flicker, flicker / 2, 0); // Darker yellow with occasional orange flicker
          } else {
            leds[i] = CRGB(flicker, flicker, flicker); // White with flicker
          }
        }
        FastLED.show();
        delay(50); // Adjust flicker speed
      } else {
        FastLED.clear();
        FastLED.show();
        blastTriggered = false;
        fadeState = ON;
        Serial.println("BLAST complete");
      }
      break;
  }
}


void sendWirelessData(uint8_t data, const uint8_t *address) {
  esp_now_send(address, &data, sizeof(data));
}

float calculateMovingAverage(float newValue) {
  static float sum = 0;
  static int count = 0;
  static float readings[filterSize];

  sum -= readings[bufferIndex];
  readings[bufferIndex] = newValue;
  sum += readings[bufferIndex];
  bufferIndex = (bufferIndex + 1) % filterSize;
  count = min(count + 1, filterSize);

  return sum / count;
}


Andrea

dmitrij999
Posts: 82
Joined: Sat Mar 02, 2019 8:06 pm

Re: ESP32 S3 stop working afted code upload! No led

Postby dmitrij999 » Tue Jan 07, 2025 11:03 am

Hello!

Which board you used? Which flash method? For ESP32S3, there are 2 methods available:
- thru UART0
- thru built-in USB Serial/JTAG debugger

If the second method is the only method, please check Arduino settings to be sure that CDC on boot disabled and USB Serial/JTAG debugger enabled.

To recover your board, hold IO0 button (BOOT) and connect to PC. Check COM-port number in Device manager (IT'S IMPORTANT! If CDC on boot enabled, then ESP32S3 will create its own CDC device not capable to reflash module, COM port number may differ) and update it in Arduino. Then, try to reflash.

NerdMov
Posts: 5
Joined: Sat Jan 04, 2025 2:43 pm

Re: ESP32 S3 stop working afted code upload! No led

Postby NerdMov » Tue Jan 07, 2025 1:36 pm

Hi dmitri thanks for your reply.
I use an ESP32S3 MIni like this. I simply connect throu USB and upload the code via Arduino IDE, like I always do with other board like ARDUINO NANO or ESP32
71bNTc6QOlL._AC_SL1500_.jpg
71bNTc6QOlL._AC_SL1500_.jpg (175.68 KiB) Viewed 990 times
I have this setting and CDC was enabled but it gave me the same problem also if it's disabled. I upload the code and the status led turn off.
71bNTc6QOlL._AC_SL1500_.jpg
71bNTc6QOlL._AC_SL1500_.jpg (175.68 KiB) Viewed 990 times
Andrea
Attachments
Screenshot 2025-01-07 143509.png
Screenshot 2025-01-07 143509.png (30.94 KiB) Viewed 990 times

dmitrij999
Posts: 82
Joined: Sat Mar 02, 2019 8:06 pm

Re: ESP32 S3 stop working afted code upload! No led

Postby dmitrij999 » Tue Jan 07, 2025 7:07 pm

Logically, USB CDC on boot should be disabled to prevent flashing/monitoring problems in future, but leave USB Mode in "Hardware CDC/JTAG". This will obviously work when you recover your board.
As I mentioned before, "USB CDC on boot" substitutes built-in hardware Serial/JTAG adapter, so it's not possible to flash ESP32S3 without runarounds.

If you still didn't recover your board, then proceed the manual I provided previously to recover. It's needed to do this as fast as possible, to recover before ESP32S3 exit the USB Serial/JTAG debugger in BOOT mode.
It's possible to observe the numbers of COM port if you just connect the ESP32S3 and connect it to PC with BOOT (IO0) button held.

NerdMov
Posts: 5
Joined: Sat Jan 04, 2025 2:43 pm

Re: ESP32 S3 stop working afted code upload! No led

Postby NerdMov » Wed Jan 15, 2025 11:23 am

Hi Dimitri.
I resolved changing the ESP32. The status lud remain off but they're working!

Who is online

Users browsing this forum: No registered users and 52 guests