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

I just just replay this step and it is not working now

I even tried to add the wire to GND in the uno and the breadboard and motor did not work

The npn is new just bought it today

When i add it the motor is rotate slowly and when remove the speed of motor increase
I think it is unless here also

Without the 2K resistor in the circuit properly it won't be new for long. You are stressing Vbe without it.

Put the resistor in like you shown in post #9

This is the third time.

What is the part number of your transistor ?


  • You must have a resistor between Arduino pin 9 and the NPN base !


Are these soldered ?

That's a good question. He never said it's a TIP120. It shows as TIP120 in his frizzy but it could be anything

1 Like

TIP120 NPN
I reply this before

  • no not soldered but the are very tight

You might have damaged the transistor base and/or Arduino pin 9.

Add a resistor.

  • Do you have a new TIP120 ?

You can try this sketch when you get things wired properly.

//********************************************^************************************************
//  https://forum.arduino.cc/t/control-motor-speed-with-npn-and-push-button-no-driver/1184925
//
//  
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       20/23/02    Running code
//
//
//

#define PRESSED            HIGH
#define RELEASED           LOW

//GPIOs
const byte Button        = 2;
const byte Motor         = 9;
const byte heartbeatLED  = 13;

//Variables
byte lastButton          = RELEASED;
byte Speed_Level;

//timing stuff
unsigned long heartbeatTime;
unsigned long switchesTime;


//                                      s e t u p ( )
//********************************************^************************************************
void setup()
{
  pinMode(Button, INPUT);

  pinMode(Motor, OUTPUT);
  pinMode(heartbeatLED, OUTPUT);

} //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
    else digitalWrite(heartbeatLED, HIGH);
  }

  //************************************************              T I M E R  check switches
  //is it time to check our switches ?
  if (millis() - switchesTime >= 50ul)
  {
    //restart this TIMER
    switchesTime = millis();

    checkSwitches();
  }

} //END of   loop()


//                               c h e c k S w i t c h e s ( ) 
//********************************************^************************************************
void checkSwitches()
{
  byte state;

  //************************************************              Button
  state = digitalRead(Button);

  //has there been a state change in the switch ?
  if (lastButton != state)
  {
    //update to the new state
    lastButton = state;

    //*******************************
    if (state == PRESSED)
    {
      Speed_Level++;
      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);
      }
    }

  } //END of this switch

  //************************************************              Next switch code

} //END of   checkSwitches()


//********************************************^************************************************
1 Like

Ok will try with new TIP120

Remember to add a resistor 470R to 1k between Arduino pin 9 and the TIP120 transistor Base.

Are you building this for a specific project that requires a TIP120 or are just trying to learn?

I recommend something like below rather than a bipolar transistor to drive that small motor

thanks Larry and 2112 it is now working successfully with the larry's code, the problem was with TIP120 seem to be fire when there is no resistor to the base

i also make some codes to switch a led with each level and it is working

//********************************************^************************************************
//  https://forum.arduino.cc/t/control-motor-speed-with-npn-and-push-button-no-driver/1184925
//
//  
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       20/23/02    Running code
//
//
//

#define PRESSED            HIGH
#define RELEASED           LOW

//GPIOs
const byte Button        = 2;
const byte Motor         = 9;
const byte heartbeatLED  = 13;
const byte ledpin  = 3;
const byte ledpin1  = 4;
const byte ledpin2  = 5;
//Variables
byte lastButton          = RELEASED;
byte Speed_Level;

//timing stuff
unsigned long heartbeatTime;
unsigned long switchesTime;


//                                      s e t u p ( )
//********************************************^************************************************
void setup()
{
  pinMode(Button, INPUT);

  pinMode(Motor, OUTPUT);
  pinMode(heartbeatLED, OUTPUT);
  pinMode(ledpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);

} //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
    else digitalWrite(heartbeatLED, HIGH);
  }

  //************************************************              T I M E R  check switches
  //is it time to check our switches ?
  if (millis() - switchesTime >= 50ul)
  {
    //restart this TIMER
    switchesTime = millis();

    checkSwitches();
  }

} //END of   loop()


//                               c h e c k S w i t c h e s ( ) 
//********************************************^************************************************
void checkSwitches()
{
  byte state;

  //************************************************              Button
  state = digitalRead(Button);

  //has there been a state change in the switch ?
  if (lastButton != state)
  {
    //update to the new state
    lastButton = state;

    //*******************************
    if (state == PRESSED)
    {
      Speed_Level++;
      if (Speed_Level >= 4)
      {
        Speed_Level = 0;
        
      }              //set oldstate  initial as current  initial
       

      //*******************
      if (Speed_Level == 1)
      {
        analogWrite(Motor, 50);
        
        
       digitalWrite(ledpin, HIGH);//on
        
       digitalWrite(ledpin1, LOW);//off
        
       digitalWrite(ledpin2, LOW);//off
      
      }
      
      
      
       
      //*******************
      else if (Speed_Level == 2)
      {
        analogWrite(Motor, 75);
       
       digitalWrite(ledpin, LOW);//off
        
       digitalWrite(ledpin1, HIGH);//on
        
       digitalWrite(ledpin2, LOW);//off
      }

      //*******************
      else if (Speed_Level == 3)
      {
        analogWrite(Motor, 100);
        
        digitalWrite(ledpin, LOW);//off
        
       digitalWrite(ledpin1, LOW);//off
        
       digitalWrite(ledpin2, HIGH);//on
      }

      //*******************
      else
      {
        analogWrite(Motor, 0);
        
        digitalWrite(ledpin, LOW);//off
        
       digitalWrite(ledpin1, LOW);//off
        
       digitalWrite(ledpin2, LOW);//off
      }
    }
  }
}
//END of   checkSwitches()

but now i want to ( if i hold the button for 3s at any level the led and motor stop)

i tried this code but did not working

//********************************************^************************************************
//  https://forum.arduino.cc/t/control-motor-speed-with-npn-and-push-button-no-driver/1184925
//
//  
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       20/23/02    Running code
//
//
//

#define PRESSED            HIGH
#define RELEASED           LOW

//GPIOs
const byte Button        = 2;
const byte Motor         = 9;
const byte heartbeatLED  = 13;
const byte ledpin  = 3;
const byte ledpin1  = 4;
const byte ledpin2  = 5;
//Variables
byte lastButton          = RELEASED;
byte Speed_Level;
byte currSwitch;
unsigned long timeStart;
bool bCheckingSwitch;

//timing stuff
unsigned long heartbeatTime;
unsigned long switchesTime;


//                                      s e t u p ( )
//********************************************^************************************************
void setup()
{
  pinMode(Button, INPUT);

  pinMode(Motor, OUTPUT);
  pinMode(heartbeatLED, OUTPUT);
  pinMode(ledpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  bCheckingSwitch = false;

} //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
    else digitalWrite(heartbeatLED, HIGH);
  }

  //************************************************              T I M E R  check switches
  //is it time to check our switches ?
  if (millis() - switchesTime >= 50ul)
  {
    //restart this TIMER
    switchesTime = millis();

    checkSwitches();
  }

} //END of   loop()


//                               c h e c k S w i t c h e s ( ) 
//********************************************^************************************************
void checkSwitches()
{
  byte state;

  //************************************************              Button
  state = digitalRead(Button);

  //has there been a state change in the switch ?
  if (lastButton != state)
  {
    //update to the new state
    lastButton = state;

    //*******************************
    if (state == PRESSED)
    {
      Speed_Level++;
      if (Speed_Level >= 4)
      {
        Speed_Level = 0;
        
      }              //set oldstate  initial as current  initial
       

      //*******************
      if (Speed_Level == 1)
      {
        analogWrite(Motor, 50);
        
        
       digitalWrite(ledpin, HIGH);//on
        
       digitalWrite(ledpin1, LOW);//off
        
       digitalWrite(ledpin2, LOW);//off
      
      }
      
      
      
       
      //*******************
      else if (Speed_Level == 2)
      {
        analogWrite(Motor, 75);
       
       digitalWrite(ledpin, LOW);//off
        
       digitalWrite(ledpin1, HIGH);//on
        
       digitalWrite(ledpin2, LOW);//off
      }

      //*******************
      else if (Speed_Level == 3)
      {
        analogWrite(Motor, 100);
        
        digitalWrite(ledpin, LOW);//off
        
       digitalWrite(ledpin1, LOW);//off
        
       digitalWrite(ledpin2, HIGH);//on
      }

      //*******************
      else
      {
        analogWrite(Motor, 0);
        
        digitalWrite(ledpin, LOW);//off
        
       digitalWrite(ledpin1, LOW);//off
        
       digitalWrite(ledpin2, LOW);//off
      }

      if( currSwitch == HIGH)
        {
            //in that case, get the time now to start the
            //5-sec delay...
            timeStart = millis();
            //...indicate we're now timing a press...
            bCheckingSwitch = true;
            
        }//if
        else
        {
            //was a rising edge (button being released)
            //cancel checking the switch timing now since 
            //it's not being held hown
            bCheckingSwitch = false;
            
        }//else
        
        //and save the 
        lastButton = currSwitch;
        
    }//if

    //if we haven't seen a rising edge, keep checking the
    //timing
    if( bCheckingSwitch )
    {
        //if the millis count now is 3000 or more higher
        //than it was when the press was detected (timeStart),
        //turn off the LED
        if( (millis() - timeStart ) >= 3000 )

          analogWrite(Motor, 0);
        
          digitalWrite(ledpin, LOW);//off
        
          digitalWrite(ledpin1, LOW);//off
        
          digitalWrite(ledpin2, LOW);//off  
     }
    }
  }
//END of   checkSwitches()

can you help with that?

1 Like

Looks like you are almost there. :+1:

What is not working ?

first press the go to level two second press go to level one and third press go to level three seem it is not arrange or something
and when i hold button to stop leds and motor, the leds start blinking and motor speed go from 1 to 3 as well with the led and no stop

You had the correct idea but did not follow it through properly.

Try this version:

You have an opportunity to ask questions if you don't fully understand what is happening.

//********************************************^************************************************
//  https://forum.arduino.cc/t/control-motor-speed-with-npn-and-push-button-no-driver/1184925
//
//
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       20/23/02    Running code
//
//
//

#define PRESSED            HIGH
#define RELEASED           LOW

#define ENABLED            true
#define DISABLED           false

#define LEDon              HIGH
#define LEDoff             LOW

#define MOTORoff           0

//GPIOs
const byte Button        = 2;
const byte ledpin        = 3;
const byte ledpin1       = 4;
const byte ledpin2       = 5;
const byte Motor         = 9;
const byte heartbeatLED  = 13;

//Variables
byte lastButton          = RELEASED;
byte Speed_Level;
byte currSwitch;

bool bCheckingSwitch     = DISABLED;

//timing stuff
unsigned long heartbeatTime;
unsigned long switchesTime;
unsigned long threeSecondTime;

//                                      s e t u p ( )
//********************************************^************************************************
void setup()
{
  pinMode(Button, INPUT);

  pinMode(Motor, OUTPUT);
  pinMode(heartbeatLED, OUTPUT);
  pinMode(ledpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);

} //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
    else digitalWrite(heartbeatLED, HIGH);
  }

  //************************************************              T I M E R  check switches
  //is it time to check our switches ?
  if (millis() - switchesTime >= 50ul)
  {
    //restart this TIMER
    switchesTime = millis();

    checkSwitches();
  }

  //************************************************              T I M E R  three seconds
  //if enabled, is it time to turn things OFF ?
  if (bCheckingSwitch == ENABLED && millis() - threeSecondTime >= 3000ul)
  {
    //we are finished with this TIMER
    bCheckingSwitch = DISABLED;

    analogWrite(Motor, MOTORoff);
    digitalWrite(ledpin, LEDoff);
    digitalWrite(ledpin1, LEDoff);
    digitalWrite(ledpin2, LEDoff);
  }

} //END of   loop()


//                               c h e c k S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
  byte state;

  //************************************************              Button
  state = digitalRead(Button);

  //has there been a state change in the switch ?
  if (lastButton != state)
  {
    //update to the new state
    lastButton = state;

    //*******************************
    if (state == PRESSED)
    {
      Speed_Level++;
      //don't go over 3
      if (Speed_Level >= 4)
      {
        Speed_Level = 0;
      }

      //enable the TIMER
      bCheckingSwitch = ENABLED;

      //start the TIMER
      threeSecondTime = millis();

      //*******************
      if (Speed_Level == 1)
      {
        analogWrite(Motor, 50);
        digitalWrite(ledpin, LEDon);
        digitalWrite(ledpin1, LEDoff);
        digitalWrite(ledpin2, LEDoff);
      }

      //*******************
      else if (Speed_Level == 2)
      {
        analogWrite(Motor, 75);
        digitalWrite(ledpin, LEDoff);
        digitalWrite(ledpin1, LEDon);
        digitalWrite(ledpin2, LEDoff);
      }

      //*******************
      else if (Speed_Level == 3)
      {
        analogWrite(Motor, 100);
        digitalWrite(ledpin, LEDoff);
        digitalWrite(ledpin1, LEDoff);
        digitalWrite(ledpin2, LEDon);
      }

      //*******************
      else
      {
        analogWrite(Motor, MOTORoff);
        digitalWrite(ledpin, LEDoff);
        digitalWrite(ledpin1, LEDoff);
        digitalWrite(ledpin2, LEDoff);
      }
    }

    //the switch was released
    else
    {
      //disable the TIMER
      bCheckingSwitch = DISABLED;
    }
  }

}//END of   checkSwitches()

//********************************************^************************************************


1 Like

@mazeen Take LarryD up on that offer. There is a lot in his code to unwrap and fully understand. Point out what you don't understand. That's the best way to learn, one-on-one with an expert

1 Like

i am walking though you code but my first impression (what is heartbeatLED? why should i add this even its not exist on the pin13 as in the code?)

The heartbeat LED is optional.

This LED gives us a reasonable visual indication that code is executing.

This LED should toggle every 1/2 second.

If we were to add more code to our sketch, this LED can indicate if there is any code blocking occurring.

If the LED stays ON or OFF after we add new code to a sketch we know there is something wrong.

Without this LED we would not know for sure if the code was executing properly.

The LED has its limits, however, highly recommend using one in your projects, at least until you have fully debugged your sketch.

If you use pin 13, there is a LED on this pin found somewhere on the Arduino board.

If you placed this LED on the front of a project case, it can also act as a power indicator.

1 Like

i though i understand the code 100% but after i test got confused

when i just press the button to hold for 3s the led and motor go to the next level is not they should wait on their level so if i released the button before 3s they go to next level if i reach 3s hold they shut down?

and

is not this part of the code

//is it time to check our switches ?
  if (millis() - switchesTime >= 50ul)
  {
    //restart this TIMER
    switchesTime = millis();

    checkSwitches();
  }

  //************************************************              T I M E R  three seconds
  //if enabled, is it time to turn things OFF ?
  if (bCheckingSwitch == ENABLED && millis() - threeSecondTime >= 3000ul)
  {
    //we are finished with this TIMER
    bCheckingSwitch = DISABLED;

    analogWrite(Motor, MOTORoff);
    digitalWrite(ledpin, LEDoff);
    digitalWrite(ledpin1, LEDoff);
    digitalWrite(ledpin2, LEDoff);
  }

} //END of   loop()

make the the led and motor reset so if i am on level 1 and hold for 3s so the motor and led turn off when i press the level should start from begnning at level 1 but after i tested it begin from the next level

If you want the sequence to restart, you need to add Speed_Level = 0; somewhere.

Let’s see if you can place this at the appropriate location in the sketch.