// Pin definitions D1 mini
const int MOT_PIN = 4; //using pin D2 on d1 mini
const int LED_PIN = LED_BUILTIN;
void setup() {
// Initialize fan and LED pins as outputs
pinMode(MOT_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Turn on the motor and LED
digitalWrite(MOT_PIN, HIGH);
digitalWrite(LED_PIN, LOW);
// Wait
delay(9000);
// Turn off the notor and LED
digitalWrite(MOT_PIN, LOW);
digitalWrite(LED_PIN, HIGH); // d1 mini off = high
// wait
delay(18000);
}
That is an absolute maximum spec, you should never draw more than about half of that. When you run directly into a transistor base, you are probably drawing too much current. Also you are quoting an Uno/Mega current spec but using an ESP, which has an even lower max current spec.
5V? Aren't you using an ESP? Also, ESP pins are limited to about 12 mA, not 20. I told you about that difference in post #8. People will also be telling you to include the base voltage drop...
Regards you original question as it is stated, it is possible that the flyback pulse from the motor is continuously resetting the board, which keeps the motor on. To find out, put one serial print at the beginning of your sketch. If it's resetting, it will print repeatedly...
I will switch to Arduino Uno (5v pins) after Jim-P pointed out the 3.3v esp/5v arduino pin difference.
Esp (esp 5v pin) & 5v motor are powered with 5v battery
The esp pinout is 3.3v 12ma
Transistor base pin is 5v?
Is 1.2v voltage drop required to match base pin voltage to esp pin voltage or not?
Also how come 3-pin transistor pin order varies even though the transistor package look the same. Some transistor have base pin in the middle, some left pin.
You have the transistor between +5 and the motor, which is a high side switch. You need to have it as a low side switch, between 0V and the motor. E to 0V, C to motor, B via resistor to Arduino output. Flyback diode across the motor is essential.