LED turns on when servo turns

Hello,

I have a project where I'm using an espcam to read a qr code and if the qr code is correct, another esp32 will switch to UNLOCK state and turn a servo motor and then after 10 seconds, it will return to IDLE and turn it back to its original position. The esp32 will also sound an alarm if the qr code is incorrect in the ALARM state and then after 5 seconds, it will return to IDLE state. I also have a pir sensor checking for motion at the same time and when motion is detected, an LED is turned on for 10 seconds. I have a strange bug that I can't seem to find the reason for. When the servo motor moves, the LED turns on, both when the servo moves to 90 and when it returns to 0. It doesnt turn on when the state goes to ALARM, so I figured it has to be the servo motor thats somehow causing it to trigger rather than the changing of states. I'm using the MicroServo 9g SG90 servo motor and the HC-SR501 pir sensor. Any ideas?

#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP32Servo.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Servo myservo;
int current_servo_angle = 0;
#define servo 17
#define buzzer 25
#define pir 12
#define led 32

volatile bool motion = false;
unsigned long motion_millis = 0;
bool led_state = false;

typedef struct struct_message {
  char state[32];
} struct_message;
struct_message incomingData;

void IRAM_ATTR detectMotion() {
  motion = true;
}

enum class QRState : uint8_t {
  IDLE,
  ALARM,
  UNLOCK,
};

static QRState current_state = QRState::IDLE;

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  Serial.println(WiFi.macAddress());

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

  esp_now_register_recv_cb(onReceive);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 allocation failed");
    for (;;);
  }
  display.display();
  delay(2000);
  display.clearDisplay();
  updateDisplay("System Idle");

  myservo.attach(servo, 500, 2400);

  pinMode(buzzer, OUTPUT);
  pinMode(pir, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);

  attachInterrupt(digitalPinToInterrupt(pir), detectMotion, RISING);
  Serial.println("State = IDLE");
}

void loop() {
  stateMachine();
  motionDetection();
}

void updateDisplay(String message) {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print(message);
  display.display();
}

void motionDetection() {
  if (motion) {
    digitalWrite(led, HIGH);
    led_state = true;
    motion_millis = millis();
    motion = false;
  }

  if (led_state && (millis() - motion_millis >= 10000)) {
    digitalWrite(led, LOW);
    led_state = false;
  }
}

void onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
  memcpy(&incomingData, data, sizeof(incomingData));
  Serial.print("State received: ");
  Serial.println(incomingData.state);
}

void stateMachine() {
  static unsigned long qrmillis = millis();

  switch (current_state) {
    case QRState::IDLE:
      //Serial.println("State = IDLE");
      updateDisplay("System Idle");

      if (current_servo_angle != 0) {
        myservo.write(0);
        current_servo_angle = 0;
      }

      if (digitalRead(buzzer) == HIGH) {
        digitalWrite(buzzer, LOW);
      }

      if (strcmp(incomingData.state, "ACCESS_GRANTED") == 0) {
        qrmillis = millis();
        current_state = QRState::UNLOCK;
        Serial.println("State = UNLOCK");
        memset(incomingData.state, 0, sizeof(incomingData.state));

      } else if (strcmp(incomingData.state, "ACCESS_DENIED") == 0) {
        qrmillis = millis();
        current_state = QRState::ALARM;
        Serial.println("State = ALARM");
        memset(incomingData.state, 0, sizeof(incomingData.state));
      }
      break;

    case QRState::ALARM:
      //Serial.println("State = ALARM");
      updateDisplay("ALARM");

      if (digitalRead(buzzer) == LOW) {
        digitalWrite(buzzer, HIGH);
      }

      if (millis() - qrmillis >= 5000) {
        current_state = QRState::IDLE;
        Serial.println("State = IDLE");
      }
      break;

    case QRState::UNLOCK:
      //Serial.println("State = UNLOCK");
      updateDisplay("Unlocked");

      if (current_servo_angle != 90) {
        current_servo_angle = 90;
        myservo.write(current_servo_angle);
      }

      if (millis() - qrmillis >= 10000) {
        current_state = QRState::IDLE;
        Serial.println("State = IDLE");
      }
      break;
  }
}

I understand the use of "it" but you use too many... being more specific (ESP, servo, state, LED, alarm, ...) will help clarify your description.

Post an annotated schematic showing exactly how you have wired it. Be sure to include all power sources, grounds etc.