11) Basic Motor Controllers

Motor controllers are wired differently for each type, so I cannot go over how to wire every single motor controller. If you need instructions on how to wire your motor controller, please refer to the manual that came with the motor controller, or contact your teacher or a mentor.

The type of motor controller that I will be referring to has the pins of forward, reverse, and enable pins. (NOTE: This refers to bilge pumps, and not brush-less motor controllers)

Forward pin: 5 [any PWM pin, preferably right besides its reverse pin]
Reverse pin: 6 [any PWM pin, preferably right besides its forward pin]
Enable pin (or /EN): +5 Volts (we will always enable the motor)
Ground to ground: Make sure you wire up the grounds as a common ground. It doesn't really matter which one you choose, but make sure that every motor controller you use has its ground plugged into the Arduino. If you don't, it work even budge.

The logic on how to control a motor controller is that in order to go in one direction, the other direction has to be off. To enable variable speeds such as (25% and 50%), we use PWM ports. PWM ports supply "analogWrite", which supplies a "pulse train" to the motor. Pulse width modulation can be seen by the sketch below.
Where the top part of the graph is on, and the lower part is off.
50% has the same length for both on and off
25% has the off time 3 times larger than its on time making it 1/4, as 25%
100% as just plain old on. (the beginning part was the transition to on)
speed=on/(on+off), but the function will do all this magic for you.

analogWrite is a lot like digitalWrite, except instead of HIGH and LOW, you can choose your speed!
But there is one problem, analogWrite does not use 0%~100% as it's speed. It uses 0~255 (Which is the size of an unsigned byte, 8 bits). To get full speed we use "analogWrite(port,255)"

So lets try to get this motor going! (PLEASE DO NOT RUN THE CODE BELOW, it is an example of what NOT to do)
void setup()
{
  analogWrite(5,255);//Forward   delay(1000);
  analogWrite(6,255);//Reverse   delay(1000);
}
But this should work, right? Unfortunately, this may be damaging to some motor controllers. It seems to work in the first second, but then it turns off (and the two directional lighting turns on at the same time if you have them). We need to turn one off when we use another, so the proper way to write this is:
void setup()
{
  analogWrite(5,255);//Forward   digitalWrite(6,LOW);//Turn reverse off      delay(1000);
  
  analogWrite(6,255);//Reverse   digitalWrite(5,LOW);//Turn forward off      delay(1000);
}
Now that we have that done, you can try changing the value 255 to something like: 128 (half speed), 64 (25%), or 192 (75%)
I really don't expect anyone do do code like this. In fact, I do not want to see anyone using this method for their motor controller. Yes, we still will use analogWrite, but we will make it easier to control it. Its time to make a new void function that will be called setMotor, and it will only have one argument, speed.
void setMotor(int speed)
{
  if (speed>=0)
  {
    analogWrite(5,speed*2.55);
    digitalWrite(6,LOW);
  }
  else   {     analogWrite(6,-speed*2.55);
    digitalWrite(7,LOW);
  }
}
And we would use it like:
void setup()
{
}

void loop()
{
  setMotor(100);
  delay(1000);
  setMotor(-50);
  delay(1000);
}

void setMotor(int speed)
{
  if (speed>=0)
  {
    analogWrite(5,speed*2.55);
    digitalWrite(6,LOW);
  }
  else   {     analogWrite(6,-speed*2.55);
    digitalWrite(5,LOW);
  }
}

So now that we know how to use a motor controller, have fun. Make different functions for each motor, such as setMotorLeft(), setMotorRight(), and setMotorTop() for each type you have.
Comments