I Need Help Turning Motor With A Button

I need help making a motor turn on and off with a button. When a button is pressed, the LEDs and the motors should concurrently turn on (we isolated it to just one LED, button and motor). The motor slowly builds up speed when the button is pressed, and caps at the maximum rpm set by the potentiometer. The battery gives power to the motor. After two minutes the motor should stop. The circuit works perfectly on TinkerCAD, but when we test it in real life only the LED turns on. Please tell me what I can do to fix this.

Here is my code:

const int motorPins[3] = {9, 10, 11};       // Motor PWM pins
const int buttonPins[3] = {2, 3, 4};        // Button pins
const int ledPins[3] = {5, 6, 7};           // LED pins
const int potPin = A0;                      // Potentiometer pin

bool motorStates[3] = {false, false, false};
unsigned long motorStartTimes[3] = {0, 0, 0};
bool lastButtonStates[3] = {HIGH, HIGH, HIGH};
int currentSpeeds[3] = {0, 0, 0};  // Track current speeds for easing

const unsigned long MOTOR_RUNTIME = 120000UL;  // 2 minutes in ms
const int SPEED_STEP = 2;         // Decrease value to increase time to speed
const int EASE_DELAY = 30;        // Delay between speed steps (ms)

void setup() {
  for (int i = 0; i < 3; i++) {
    pinMode(motorPins[i], OUTPUT);
    pinMode(buttonPins[i], INPUT_PULLUP);
    pinMode(ledPins[i], OUTPUT);
  }
  Serial.begin(9600);
}

void loop() {
  int potValue = analogRead(potPin);
  int targetSpeed = map(potValue, 0, 1023, 0, 255);
  unsigned long currentTime = millis();

  for (int i = 0; i < 3; i++) {
    bool buttonState = digitalRead(buttonPins[i]);

    // Detect falling edge (button press)
    if (lastButtonStates[i] == HIGH && buttonState == LOW) {
      motorStates[i] = !motorStates[i];  // Toggle motor state
      if (motorStates[i]) {
        motorStartTimes[i] = currentTime;
      }
      delay(200);  // Debounce
    }
    lastButtonStates[i] = buttonState;

    // Check for 2-minute timeout
    if (motorStates[i] && currentTime - motorStartTimes[i] >= MOTOR_RUNTIME) {
      motorStates[i] = false;
    }

    // Gradually ease motor speed up or down
    int desiredSpeed = motorStates[i] ? targetSpeed : 0;

    if (currentSpeeds[i] < desiredSpeed) {
      currentSpeeds[i] += SPEED_STEP;
      if (currentSpeeds[i] > desiredSpeed) currentSpeeds[i] = desiredSpeed;
    } else if (currentSpeeds[i] > desiredSpeed) {
      currentSpeeds[i] -= SPEED_STEP;
      if (currentSpeeds[i] < desiredSpeed) currentSpeeds[i] = desiredSpeed;
    }

    analogWrite(motorPins[i], currentSpeeds[i]);
    digitalWrite(ledPins[i], motorStates[i] ? HIGH : LOW);
  }

  delay(EASE_DELAY);  // Helps smooth out the ramping effect
}



You need to show your code and also the schematic of your project

The link to TinkerCAD could be good to have too

Thank you for your reply. I have uploaded both like you asked.

Please enclose the code inside "CODE" tags (edit your post again, select the whole and only code, then click the "<CODE/>" top button).

Pls enclose your code inside the code tags as asked

What is the reference of the transistor used? Are you sure you wired it correctly?

The motor doesn't rotate at all? If you put your hand on it while you click on the button, do you feel something or nothing at all?

I am pretty sure it is wired correctly. I had multiple people check it. Whenever I touch the motor, I don't feel anything turning at all. I am using a NPN PN222 transistor.

  1. Does the motor work at all? Test: connect the motor directly to battery.
  2. Is the transistor switching correctly? Test: remove wire to Arduino and connect to 0V and 5V

Once you have established that the hardware works correctly, proceed to test if the transistor can be controlled by setting output pin high or low.

Hi, @david_o2
Welcome to the forum.
Thanks for the information, but we need a better schematic.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What part number is the transistor controlling the motor current?
Can you please post links to data/spec of your motor?

Do you have a DMM? Digital MultiMeter?

Tom.... :smiley: :+1: :coffee: :australia:

Did you check that the value you get from your pot is okay? Does it really go from 0 to 1023?

Could you try to put a LED in place of the motor? CAREFUL!! Adapt the value of the resistor.

tl;dr: The code works fine, so it is def a wiring or circuit issue.

Here is what is actually doing the debouncing.

  delay(EASE_DELAY);  // Helps smooth out the ramping effect
}

This comment

      delay(200);  // Debounce
    }

is misleading; the debounce algorithm is flawed in that it only debounces the leading edge.

This common pattern sees both edges and uses a delay() on each:

// Detect change
    if (lastButtonStates[i] != buttonState) {
// and if it is to LOW, do the thing
      if (buttonState == LOW) {
        motorStates[i] = !motorStates[i];  // Toggle motor state
        if (motorStates[i]) {
          motorStartTimes[i] = currentTime;
        }
      }
      delay(25);  // Debounce. Srsly, 20 ms should handle a good pushbutton
    }
    lastButtonStates[i] = buttonState;

You can leave a short delay in there and it will always work. On the same hand, or is it the other, with the loop throttling delay, there is no need for it.

I tested this in your code, so. I also set the two minutes down to a tolerable 7 seconds for testing. Life is short!

a7

I have posted a schematic for the circuit.

I have posted a schematic for the circuit.

Also, I have just tested that the code works with a different motor. The motor that worked was the fan blade 3-6 V motor. The motor that I am currently using for my project is the Globact 21T 550 motor. When i tested that motor, I discovered that I can hear a little bit of noise coming from the motor, which I think means that it isn't getting enough power.

I have posted a picture for the two motors below.


What kind of battery do you think I need to power the motor correctly?

Motor Specifications:
Item name: Globact 550 21t Motor
Color: Black
Size: 550
Turns: 21T
Watts: 40-91W
Voltage: 7.4V (2S Lipo) / 6V-8.4V (5-7 cells NiMH)
Max Amps: 70A
KV(RPM/Volt): 21T
Max RPM: 12900
Waterproof level: IP65
Diameter: 36mm
Length: 55mm
Length of extend shaft: 16mm
Shaft diameter: 3.175mm

I don't see a flyback diode across that motor. Good-bye, transistor.

I also don't see a transistor in the picture. Which means if it's there, it's tiny. TO-92? No flyback diode? Good-bye, transistor.

1 Like

I'm sorry, but I am very new to this. What is a flyback diode and why is it needed? Also I am using an NPN transistor.

Do you have anything like google available to you on your internets over there? Srsly we love helping but you gotta meet us kinda closer to halfway.

a7

Hi,
What is the part number of your NPN transistor?
I looks like it will have a very short life with that 70A rated motor and NiMh battery.

Tom.... :smiley: :+1: :coffee: :australia:

The transistor is an NPN transistor PN2222. However, we have access to s8050 NPN transistor. We are using a 7.2 V 3000 mAh battery and a 9 V 550 mAh battery for testing.