There is no GND between the esp32 and the L298.
Please post designated schematics for helpers not knowing the circuits by heart.
What's voltages on the power sources?
You can not source power for a motor driver and motor with the ESP32. You must use an external power supply for the motor driver and motor power source.
I have connected VIN and GND to external 5v power supply of ESP32
The L298n module is connected to 12 power supply
I connectd the GND pin of esp32 to L298n module GND to make it as common.
The code doesn't seem anything wrong with it. I burnt an Arduino Uno too by powering the Uno with the 5V regulator, even if it's 5V it's too high current for an Arduino to handle so i'd reccomend using a Lm7805 regulator connected straight to the power source then plugging in the 5V regulated output to the 5V pin but i still can't reccomend it. Because if it's too high current (Arduino's reccomended max current (mA = A/1000):~750mA) it'll burn the onboard 5V regulator and kaput. If you're not sure it'll handle it, either get a 7.4V rechargable battery or just a 9V battery. Also you need common grounding otherwise it won't work. What i mean is: 12V on the L298N, 5V on the Arduino if you're using a L298N or derivatives needs common grounding so 12V Negative goes to 5V Negative.