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
}
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.
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?
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!
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.
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.