analogWrite is not working - while trying to control DC motor speed I'm trying to control the speed of DC Motor using 


Arduino R3
NPN Transistor (2N2222A331)
10k potentiometer
2M ohm resistor
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() 
{
 Serial.begin(9600);
 pinMode(motorPin,OUTPUT);
}


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


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


tried different combinations of resistors(100,1k,10k,1M,2M ohm) so as to vary voltage that goes to DC Motor
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
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));
  delay(100);
}


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

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

On the Arduino side 47k trimmer and the USB