Help wiring this push button with DC Motor pls help

According to this code,

const int BUTTON_PIN = 2;        // Arduino pin connected to button's pin
const int MOTOR_PIN = 9;          // Arduino PWM pin connected to motor driver

byte ledState = LOW;             // The current state of the built-in LED
int lastButtonState;            // The previous state of the button
int currentButtonState;         // The current state of the button

void setup()
{
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);     // Set button pin to input pull-up mode
  pinMode(LED_BUILTIN, OUTPUT);          // Set LED pin to output mode
  pinMode(MOTOR_PIN, OUTPUT);            // Set motor pin to output mode

  lastButtonState = digitalRead(BUTTON_PIN); // Read the button state
}

void loop()
{
  currentButtonState = digitalRead(BUTTON_PIN); // Read the new button state
  if (lastButtonState == HIGH &&currentButtonState == LOW)
  {
    Serial.println("The button is pressed");
    ledState = !ledState; // Toggle state of LED
  }
  lastButtonState = currentButtonState; // Save the last button state

  // control LED according to the toggled state
  digitalWrite(LED_BUILTIN, ledState);

  if (ledState == HIGH)
  {
    // Set the motor speed
    analogWrite(MOTOR_PIN, 250);
  }
  else
  {
    // Turn off the motor
    analogWrite(MOTOR_PIN, 0);
  }

  delay(100);
}

The wiring diagram doesn't work with this, what should i change on the wiring diagram inorder for the code to work. Here is the wiring diagram

@jim-p

First you need to post you code correctly.
In the IDE, click on Edit, then Copy for Forum, that will copy your code. Then just paste the code here. Regulay copy and paste does not work.

Which transistor are you using?

done, and npn transistor @jim-p

What part number?
The pinouts are different for differemt transistors. You may have your's backwards.

@jim-p BC 547A 051

The transistor is wired right but the pushbotton does not look right.
Wire like this

@jim-p now the light on the arduino doesn't turn on like before when the button is pressed, and the motor still doesn't move.

You must have some bad connections somewhere.
Make sure all the wires make a tight fit into the breadboard.
Make sure the pushbutton is in the right way.
Make sure the ground wire is connected.

To test the motor connections, remove the yellow wire from the Uno and connect it to 5V. The motor should turn on.

@jim-p doesn't the button need the power from the arduino?

No
You have the pinMode set to INPUT_PULLUP. That will automatically connect an internal resistor between the button_pin and 5V. So the input will be HIGH when not pushed and go LOW when pushed

@jim-p , I am still not able to get the motor to work, the motor works when connected straight to power . Can you please make the wiring diagram again so I can see if I am doing everything correct.

Ok, give me a few minutes.

thank you so much :slight_smile:

My batteries and motor are a little different.

One error. I have the button connected to pin 3 and it should be pin 2 like you have.

So change it from 3 to 2

@jim-p still doesn't work, no light on arudino when the button is pressed

10k resistor is too high, use 330, 390 or 470 Ohm. wire button like this. Get rid of the green wire.

Low PWM values (below about 30) may not start the motor.

Did you connect the pushbutton to pin 2 and not 3?