I'm trying to control the speed of DC Motor using

  1. Arduino R3
  2. NPN Transistor (2N2222A331)
  3. 10k potentiometer
  4. 2M ohm resistor
  5. Diode

I'm not using L298N and I'm also not using L293D.

DC motor is running, but the speed of DC motor doesn't seem to change with changes in potentiometer. Circuit seems to be alright as DC motor is running.

The code is as follows

int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;

void setup() 

void loop() 
 potValue = analogRead(potPin);  
 motorValue = map(potValue, 0, 1023, 0, 255);
 analogWrite(motorPin, motorValue);  
 Serial.print("potentiometer = " );     
 Serial.print("t motor = ");

Whenever I tweak the Potentiometer, I can see the relative changes from Arduino "Serial Monitor" (both Potentiometer values and Motor values), but the speed doesnt seem to change physically.

Things that I tried

  1. tried different combinations of resistors(100,1k,10k,1M,2M ohm) so as to vary voltage that goes to DC Motor
  2. tried to check with different Output pins like 3,5,9

Nothing works, DC motor speed isn't changing.

this leads me - to believe that, my analogwrite isn't working. please help me understand as to what i'm i doing wrong?

The Circuit is as follows


  • set the value with analogWrite only if it changed. now you restart PWM in every loop
    – Juraj
    Commented Mar 3, 2019 at 6:47

3 Answers 3


Two things to test:

  1. Have you tried to put an LED (and resistor to protect it) in the motor's place? If the brightness changes, your setup (Arduino+transistor) is working. If not, maybe you've killed your 2N2222 by giving it a wrong diode or something else is wrong.
  2. Have you tried reducing the PWM frequency? An LED can follow a fast change, but a motor can't. Also, by turning on-off the motor less frequently you dissipate less power on your transistor and diode. A quick search gave me this: this.

Referring to this datasheet you have connected the transistor the wrong way around. Also, 1M resistor at the base is way too high. Bipolar transistors are current amplifiers (amplification at about 50-100 in your case) and with 1MOhm at the base there will be no current through the base at all. I would try with 2k for a beginning. I hope you have another transistor because this one seems to be broken and I'm pretty sure that it's not a fault in the analog output. If the output stage of the uC was broke there would be no reaction (at least in a correct circuit)


Well, 2M resistor to the base? If the motor is running with that, the transistor must be broken (shorted). Just try to check if the motor is running with disconnected base.

You can also check PWM output by connecting LED with resistor (no, not 2M ohms, just about 330 ohms). For the transistor 1k should be sufficient maybe little less (depends on motor current and h21e amplification coeficient).

Also there are some versions 2N2222 with emitter and collector swapped. And reversed transistor has much lower current amplification coeficient.

And I hope you are using external power supply, USB is not a good idea. Starting power is usually way too much for the overcurrent protections.

Anyways, this works for me:

void setup() {
  pinMode(9, OUTPUT);

void loop() {
  analogWrite(9, map(analogRead(A0), 0, 1023, 0, 255));

2N2222A + 1k + PC FAN + 12V Fan Power supply.

E -> Gnd,
C -> FAN-,
FAN+ -> 12V,
12V GND -> Arduino GND,
Pin 9 -> 1k -> B

On the Arduino side 47k trimmer and the USB

  • Thanks for the reply !! ....but issue remains !! Motor is not running with disconnected base !! i checked PWM output with LED and resistor (dimming brightness of LED) ....it seems to work as per expectation Tried swapping the connections of emitter and collector .....DC motor ran but no speed control adriuno dint powerup with 9v battery for the DC motor circuit....so im using USB as power supply !! im running out of options , please do help me !! Commented Mar 3, 2019 at 10:19
  • @SharathChandra Works for me perfectly with PC FAN and external 12V PSU. Even with 2ms delays.
    – KIIV
    Commented Mar 3, 2019 at 11:27
  • And also DC motor + antiparallel shottky is working (with own PSU)
    – KIIV
    Commented Mar 3, 2019 at 11:43
  • Can you vary the speed of a plain DC motor using PWM? I would think pulsed DC would play havoc with it, giving strange results. I'd expect to need an RC filter to convert the pulsed output into a fairly constant DC output. I know some servos are made to run on DC, but a normal DC motor?
    – Duncan C
    Commented Mar 3, 2019 at 17:24
  • @DuncanC Sure I can, but it's without the load. It's a small dc motor from some old walkman. However as it's DC motor with commutator, I don's see why it should be so bad, maybe bigger EMI. And the power peaks gives some extra torque at low RPMs.
    – KIIV
    Commented Mar 3, 2019 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.