0

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() 
{
 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

  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

Circuit

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

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

  • 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 !! – Sharath Chandra Mar 3 at 10:19
  • @SharathChandra Works for me perfectly with PC FAN and external 12V PSU. Even with 2ms delays. – KIIV Mar 3 at 11:27
  • And also DC motor + antiparallel shottky is working (with own PSU) – KIIV Mar 3 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 Mar 3 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 Mar 3 at 17:35
0

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.
0

Referring to this datasheet you have connected the transistor the wrong way around. Also, 1M resistor at the base is way to 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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