Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Code to vary Dual MC33926 motor shield speeds with potentiometer

I’m an Arduino novice and trying to do something which I feel should be really simple, but having no luck.
I’m trying to write a sketch that allows me to vary the speed of Motor 1 in one direction with a potentiometer.

The wiper of the pot is wired to Analog input 3, one side to ground and the other to 5V on the arduino.

The Demo sketch uploads and runs fine, so I know the Arduino and the Pololu shield are running fine

My sketch here verifies and uploads but I’m obviously missing something because the motor does not respond to the pot.

Thanks in advance for your help
T

// Speed control potentiometers

int SpeedControl1 = A3;  
int SpeedControl2 = A4;

  //Pin map
  
int  _nD2 = 4;
int  _M1DIR = 7;
int  _M1PWM = 9;
int  _M2DIR = 8;
int  _M2PWM = 10;
int  _nSF = 12;
int  _M1FB = A0;
int  _M2FB = A1;

int speed = 0;

// Public Methods //////////////////////////////////////////////////////////////


void setup()

{
  
    // Set all the motor control pins to outputs
// Define pinMode for the pins and set the frequency for timer1.

  pinMode(_M1DIR,OUTPUT);
  pinMode(_M1PWM,OUTPUT);
  pinMode(_M2DIR,OUTPUT);
  pinMode(_M2PWM,OUTPUT);
  pinMode(_nD2,OUTPUT);
  digitalWrite(_nD2,HIGH); // default to on
  pinMode(_nSF,INPUT);



}


void loop() {

    _M1PWM = analogRead(SpeedControl1); 
  _M2PWM = analogRead(SpeedControl2);

// Set speed for motor 1, speed is a number betwenn -400 and 400



      analogWrite(_M1PWM,speed * 51 / 80); // map 400 to 255
    }

Hello.

It seems like you are confusing the variables you are using to store your pin numbers and the variable you are using for the speed value. For example, the first line of your loop is storing your analog reading from the potentiometer into _M1PWM, which already contains your value for the M1PWM pin number. I think you intended to use the speed variable you made instead.

Also keep in mind the default analogRead() function will return a value between 0 and 1023, and analogWrite() values go from 0 to 255. (In the future, you can find that type of information on the Arduino reference page.) So, you will need to adjust the expression in your analogWrite function. If you make those corrections, your loop should look something like this:

void loop() {
    speed = analogRead(SpeedControl1); 
    analogWrite(_M1PWM, speed / 4);     // map 1023 to 255
}

By the way, it would simplify what you have to program a lot if you used our library. Is there a reason you are not using that?

- Patrick

Hi Patrick,

Thanks so much for your help. Yes, that makes perfect sense, and it worked finally, thank you!

Meanwhile as for the library I’m all for simplifying things and initially I attempted to go that route but -and here my inexperience is obviously showing-
it was confusing for me to understand how much or how little I need to include in my own code (still learning).

I did manage to fix an additional problem which was whining noise in the motor by adding a different library which I discovered through a bit more research which allowed me to set the PWM frequency much higher (to 20k).

As a side note my next goal is to set up encoders so that Motor 2 will slave/ adjust speed to sync with Motor 1. Would the library make that easier to set up?
(I suppose this would most likely be the start of another topic eh?)

thanks again,
Thomas

If you want to understand how you could use our library, I would suggest checking out the demo sketch in it since that is a fairly minimal example of how to use it. Then you could modify our example to meet your needs.

You could use our library in a program that is also reading encoders, but our library does not have any specific support for that, so it will not make adding that functionality much easier.

- Patrick