Dc motor and push button refuse to work

I have a super simple circuit and action I am trying to achieve but it doesn't work properly and I am not sure why.

I am trying to get a motor to turn on and rotate for a given amount of time (starting of with 1 or 2 seconds) by pressing a switch. I only want this occurrence to happen once when the switch has been pressed. Then it will only happen again if the switch is pressed again.

For some reason the motor completely ignores the action/question of if the button has been pressed or not and it repeatedly just spins on and off. I have tried all I can think of and have tested the motor separate to the push switch. I am able to turn it on and off without the switch. But no matter what as soon as i implement the switch it doesn't have any impact on the motor. I have also tested the switch circuit with an LED, getting the LED to do exactly what i want the motor to do and this went fine. So I am confused.

Components being used:

Momentary Push button with a 10k ohm resistor

3V DC motor with a PN2222 transistor, 220 ohm resistor and a 1N4001 diode.

My Code is as follows:

int buttonpin = 2;

int motor = 3;

void setup() {  // initialize digital pin 13 as an output.

   pinMode(motor, OUTPUT);

   pinMode(buttonpin, INPUT);

   

}

// the loop function runs over and over again forever

void loop() {

  if (digitalRead (buttonpin) == HIGH) // if button is pressed

  { 

    Motor_on(); 

  }

  else

{

  digitalWrite(motor,LOW);

}

}

void Motor_on(){

  {

   digitalWrite(motor, HIGH); // turn the LED on (HIGH is the voltage level)

   delay(2000); // wait for a second}

   digitalWrite(motor, LOW); // turn the LED off by making the voltage LOW

   delay(1000);

  }

}

The motor and button is set up as followed:

Would love some assistance as I have done more complicated circuitry and projects than this on a PIC but I can't seem to figure out the solution to this, and this is only the first part of my project!

Write a simpler sketch that only reports the state of the button, for example by printing it to Serial.

Are you sure you have the button connected correctly?

It is generally not a good idea to connect both 5v and GND to the button, as it is very easy to get the orientation incorrect and short out your Arduino.

If the resistor is for pulling down the pin then connect to the pin that goes to the Arduino... and use a 10k resistor.

It is also not a good idea to power a motor using the Arduino 5v... it does not provide sufficient current. Better to have a seperate power supply for that.

You need to test your setup components separately. Serial.print to confirm the button state. Try powering the motor directly with 5v.

It is better to wire the switch to ground and enable the internal pullup.
The switch will read HIGH when not pressed and LOW when pressed so adjust the program logic as necessary.

And wire to diagonal terminals to avoid the short.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.