Control motor speed with npn and push button ( no driver)

i am trying to control my motor speed using npn and push button, simply i want to control the speed by clicking on the button so its start with speed 1 and second press -> speed 2 and so on

i stimulated the circuit on tinkercad and working as expected but when trying it on my IDE and UNO the code uploaded successfully but the motor not start even

int Button = 2;
int Motor = 9;

int button, Speed_Level;

void setup() {

  pinMode(Button, INPUT);
  pinMode(Motor, OUTPUT);  
}

void loop() {

  button = digitalRead(Button);

  if(button == 1) {
    Speed_Level++;
    delay(100);
    if(Speed_Level ==4) Speed_Level = 0;      
    }    
if(Speed_Level == 1) analogWrite(Motor, 50);
else if(Speed_Level == 2) analogWrite(Motor, 75);
else if(Speed_Level == 3) analogWrite(Motor, 100);
else analogWrite(Motor, 0);
}

Debug your program by adding code to serial.Print() to value you are using for analogwrite.

Compare your button wiring to this example

Initialize Speed_Level=0;

i would test everything independent of the code

  • make sure the battery can drive the motor when connected directly to the motor
  • make sure the motor works when driven thru the transistor with the base of the transistor connected to the + battery thru the resistor

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

loops executes 1000s of times a sec. when it detects a button press, Speed_Level will be incremented 1000s /sec

why not try just turning the output pin on when the button is pressed

after re check i found an wire not in its place, i correct the connection but the motor is rotating with one speed whenever i pressed the button and do not stop!


Because you have pin 2 tied to 5V. Your button switch is not wired properly

1 Like

You also need a 1K to 2K resistor between pin 9 and the transistor base

It would be much better if you posted an annotated schematic. My parts do not look like the ones in your picture. Also You know that thing called File 13 (trash can) put your 9V battery there and get something decent such as a power supply or several AA batteries in a pack.

i am using 2s of 18650 battary (tinkercad has only 9v)

after make sure the pin 2 is not connected to 5v and add 1k to the base and pin 8 the problem still running
when i can the power supply the motor start running in one level and do not stop

Do you know how fast loop( ) runs ?

  button = digitalRead(Button);

  if (button == 1)
  {
    Speed_Level++;
    delay(100);

    if (Speed_Level == 4)
    {
      Speed_Level = 0;
    }
  }
  • Look at switch changes in state, not switch level.

  • suggest you not take short cuts like all code on one line, it's a recipe for problems,

  • What is the part number of your transistor ?

  • Can you show us good images of the actual wiring ?

  • Don't use delay(...) in your sketches.

Pull the wire from pin 9 and connect it the GND. Did it stop?
If not, you still have a wiring problem or your transistor is bad.

Change delay(100); to 500

just modify the code but the problem still exist

iam using TIP120 NPN

here is an actual image not sure if it clear

when connect the pin 9 to GND port of arduino the motor still working

I don't see a TIP or a push button.
What is the 8 pin DIP?
This can't be your circuit

So sorry i uploaded wronge circuit just edited the reply

What is the part number of your transistor ?

Did you remove the orange wire connected to pin 9 and connected that orange wire into Gnd?

If yes, my guess is the transistor is bad.
Do you have a multimeter, DMM?

This does not look correct :thinking: