I need help making my motors faster

I am trying to make a motor move by controlling it with a potentiometer. It works but my motor is moving very slow and I don't know what to do. I am new to coding and have no idea of what I am doing. Any help to make the motor move faster would be great.

// Define pins
const int potPin = A0;       // Pin for potentiometer
const int motorPin1 = 3;     // Motor driver input 1
const int motorPin2 = 4;     // Motor driver input 2
const int enablePin = 5;     // Motor driver enable pin

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  int potValue = analogRead(potPin);  // Read value from potentiometer
  Serial.println(potValue);  // Print pot value for debugging

  // Map the potentiometer value to motor speed
  int motorSpeed = map(potValue, 0, 1023, 0, 255);

  if (potValue < 512) {
    // Rotate motor in one direction
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    analogWrite(enablePin, motorSpeed);
  } else {
    // Rotate motor in the opposite direction
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    analogWrite(enablePin, 255 - motorSpeed); // Reverse speed
  }

  delay(100); // Small delay for stability
}
1 Like

What are the specs of the motors that you are using?
How do you drive them; please provide a schematic containing the details of all components (including specifications of used power supplies).

You should work out the math for a pot-value of 500.

And other interesting values.

You might be better off mapping from -255 to 255 and then deal with the sign separately.

I see you are printing the pot values. Maybe also printout motorSpeed and 255-motorSpeed. Go slowly from min-Pot to max-pot and see if it works as you expect it to.

To test if code can make the DC motor move any faster, connect the power supply + and - directly to each motor + and -... that will be the maximum speed.

A gear "ratio" might be given on the motor case. A larger ratio gives a faster speed, but less torque.

Your code does not "increase speed" in both directions (on both sides of "512")... using the map() function from post #3 is necessary to do this. I made some adjustments to the code, now you can see potentiometer and motor speeds...

sketch.ino for wokwi.com
// https://forum.arduino.cc/t/i-need-help-making-my-motors-faster/1319694/

// Define pins
const int potPin = A0;       // Pin for potentiometer
const int motorPin1 = 3;     // Motor driver input 1
const int motorPin2 = 4;     // Motor driver input 2
const int enablePin = 5;     // Motor driver enable pin

int oldPotValue, oldMotorSpeed;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  int potValue = analogRead(potPin);  // Read value from potentiometer

  // Map the potentiometer value to motor speed
  int motorSpeed = map(potValue, 0, 1023, 0, 255);

  // show only changes
  if (oldPotValue != potValue || oldMotorSpeed != motorSpeed) {
    oldPotValue = potValue;
    oldMotorSpeed = motorSpeed;
    Serial.print("P|");
    spacepad(potValue);
    Serial.print(potValue);  // Print pot value for debugging
    for (int i = 0; i < potValue / 24; i++)
      Serial.print(" ");
    Serial.print(" ");
    Serial.print("P");  // Print pot value for debugging
    Serial.print(" ");
    Serial.print(motorSpeed);  // Print pot value for debugging
    Serial.print(" ");
    Serial.println(255 - motorSpeed); // Print pot value for debugging
  }
  if (potValue < 512) {
    // Rotate motor in one direction
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    analogWrite(enablePin, motorSpeed);
  } else {
    // Rotate motor in the opposite direction
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    analogWrite(enablePin, 255 - motorSpeed); // Reverse speed
  }
  // delay(100); // Small delay for stability
}

void spacepad(int value) {
  if (value < 1000) Serial.print(" ");
  if (value < 100) Serial.print(" ");
  if (value < 10) Serial.print(" ");
}
diagram.json for wokwi.com
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -106.8,
      "left": 131.4,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -78,
      "left": 131.4,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -49.2,
      "left": 131.4,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -10.9, "left": 172.6, "attrs": {} }
  ],
  "connections": [
    [ "nano:GND.2", "led3:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "nano:3", "led3:A", "green", [ "v0" ] ],
    [ "nano:4", "led2:A", "green", [ "v0" ] ],
    [ "nano:5", "led1:A", "green", [ "v0" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v19.2", "h-57.1" ] ],
    [ "pot1:SIG", "nano:A0", "green", [ "v28.8", "h-125.2" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v38.4", "h-87.2" ] ]
  ],
  "dependencies": {}
}

Hi, @riekco21
Welcome to the forum.

Thanks for using code tags. :+1:

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.

Does you motor run at the speed you need when connected directly to the motor power supply?

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

1 Like

Which motor? What is the motor's rated voltage and current?
Which motor driver are you using? What is your power supply's voltage and current rating?

In case that was not clear enough, your code for mapping and transforming of the potentiometer value limits your peak PWM duty cycle to 127/255 or 50%.

Try this mod (untested).

// Define pins
const int potPin = A0;       // Pin for potentiometer
const int motorPin1 = 3;     // Motor driver input 1
const int motorPin2 = 4;     // Motor driver input 2
const int enablePin = 5;     // Motor driver enable pin
bool dir;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  int potValue = analogRead(potPin);  // Read value from potentiometer
  Serial.println(potValue);  // Print pot value for debugging 
  
  // Map the potentiometer value to motor speed
  int motorSpeed = map(potValue, 0, 1023, 0, 255);
  dir = motorSpeed < 128;
  if (dir) {
    // Rotate motor in one direction
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    analogWrite(enablePin, 2 * (128 - motorSpeed));
  } else {
    // Rotate motor in the opposite direction
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    analogWrite(enablePin, 2 * (motorSpeed -128)); // Reverse speed
  }

  delay(100); // Small delay for stability
}

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