DC motor control with potentiometer

Hi Guys,

I am a newbie to the world of Arduino. I am currently doing a project that requires a potentiometer to control a DC motor which I have successfully managed to do! but I need my code written in a way that I can control 2 DC motors with 2 potentiometers separately. Can anyone assist with this, please?

Thanks

pot_motor_control.ino (430 Bytes)

Can anyone assist with this, please?

What do you need help with? If you can read one potentiometer, and control the speed of one motor, doing that for two is trivial.

Hi PaulS

I am new to this game and I am unsure of how to do this. I Have attached my code; can you help me with this, please?

Thanks again

I am unsure of how to do this.

You are unsure of how to read from two different analog pins? You are unsure of how to write to two different PWM pins?

What, exactly, is it you need help with?

Please don't have the motor wired directly across those pins 12 and 3?

Hi PaulS

I have tried duplicating the code for the other motor and potentiometer but have been unsuccessful. I need assistance on writing the code for the second motor and potentiometer.

Thanks

Ian230689: I have tried duplicating the code for the other motor and potentiometer but have been unsuccessful.

Show the code that didn't work. Seems to me you would just do the same on two other pins. Maybe you're analogWrite()-ing a non-PWM pin.

BUT PLEASE RESPOND TO THIS FIRST:

Please don't have the motor wired directly across those pins 12 and 3?

arduin_ologist: Please don't have the motor wired directly across those pins 12 and 3?

I am using an Arduino motor shield for the DC motor and the pot is connected to analogue pin A2 along with GRD and Vin. I am not using pin 12 and 3.

Thanks for the input

And when you try use two motors, what pin are you analogWrite()-ing?

arduin_ologist:
Show the code that didn’t work. Seems to me you would just do the same on two other pins. Maybe you’re analogWrite()-ing a non-PWM pin.

BUT PLEASE RESPOND TO THIS FIRST:

Please don’t have the motor wired directly across those pins 12 and 3?

The code I have used controls the motor with the potentiometer perfectly. I just need the code written so I can control 2 motors with 2 potentiometers separately.

please see code below:

Thanks

void setup() {
pinMode(12,OUTPUT); //Channel A Direction Pin Initialize
}

void loop() {

if (analogRead(A2) > 512)
{
digitalWrite(12, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A2)-255); //Channel A Speed
}

if(analogRead(A2) <= 512)
{
digitalWrite(12, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A2)); //Channel B Speed
}

}

Ian230689: I have tried duplicating the code for the other motor and potentiometer

Let's see that, then...

So when you tried with 2 potentiometers what 2 pins did you connect them to? And where was the 2nd motor connected?

Even if we wanted to write your code for you we couldn’t do it without knowing those things.

Steve

arduin_ologist:
Let’s see that, then…

I’m assuming that pin 12 represents the positive and negative terminal on the Arduino motor shield as I’m not using PWM. so when I wrote the second part of the code I assumed the other terminal for the second motor to be 13. I soldered the pots in series so they both had 5v across them and a GRD, I then connected remaining wire from the pot 1 into analogue pin A2 and for pot 2 I connected this into analogue A5.

I had no luck with this so far but works fine with 1 pot controlling 1 motor.

I think is my code! any help would be appreciated.

Please see code below for 2 pots and motors.

void setup() {
pinMode(12,OUTPUT); //Channel A Direction Pin Initialize
pinMode(13,OUTPUT); //Channel A Direction Pin Initialize
}

void loop() {

if (analogRead(A2) > 512)
{
digitalWrite(12, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A2)-255); //Channel A Speed
}

if(analogRead(A2) <= 512)
{
digitalWrite(12, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A2)); //Channel B Speed
}

if (analogRead(A5) > 512)
{
digitalWrite(13, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A5)-255); //Channel A Speed
}

if(analogRead(A5) <= 512)
{
digitalWrite(13, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A5)); //Channel B Speed
}

}

slipstick:
So when you tried with 2 potentiometers what 2 pins did you connect them to? And where was the 2nd motor connected?

Even if we wanted to write your code for you we couldn’t do it without knowing those things.

Steve

Hi steve,

I’m assuming that pin 12 represents the positive and negative terminal on the Arduino motor shield as I’m not using PWM. so when I wrote the second part of the code I assumed the other terminal for the second motor to be 13. I soldered the pots in series so they both had 5v across them and a GRD, I then connected remaining wire from the pot 1 into analogue pin A2 and for pot 2 I connected this into analogue A5.

I had no luck with this so far but works fine with 1 pot controlling 1 motor.

I think is my code! any help would be appreciated.

Please see code below for 2 pots and motors.

void setup() {
pinMode(12,OUTPUT); //Channel A Direction Pin Initialize
pinMode(13,OUTPUT); //Channel A Direction Pin Initialize
}

void loop() {

if (analogRead(A2) > 512)
{
digitalWrite(12, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A2)-255); //Channel A Speed
}

if(analogRead(A2) <= 512)
{
digitalWrite(12, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A2)); //Channel B Speed
}

if (analogRead(A5) > 512)
{
digitalWrite(13, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A5)-255); //Channel A Speed
}

if(analogRead(A5) <= 512)
{
digitalWrite(13, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A5)); //Channel B Speed
}

}

Ian230689: I'm assuming that pin 12 represents the positive and negative terminal on the Arduino motor shield as I'm not using PWM.

Of course you are using PWM, that's what analogWrite() does. But what makes you think that pin 3 is the pin that controls both motors? On the Arduino Motor Shield the PWM pin for channel B is pin 11.

Can you please post a picture showing clearly how your two motors are connected to the shield and how everything is powered?

Steve

No you don't put the two pots in series: they are in fact in parallel with one end of each to 5V and the other end of each to ground. Then the wipers go to the analog pins.

Maybe you should post a schematic so we can see what you actually have. (Series would mean having the "top" of one to 5V, the "bottom" of that one to the "top" of the other one, and the "bottom" of that other one to ground.)

You can't assume anything about which pins a shield uses: the documentation should surely say which pin is which?

Hi Steve,

This is the setup for just one pot and one motor. (Which works perfect with the first code I sent you) I don’t have any of the set up of two motors and two pots at the moment.

But all I have done for two motors and two pots is solder the pots in series and connected one pot to pin A2 and the other to A5.

It’s powered with a 9V plug and the motors are connected to the positive and negative terminal on the mortor shield.

Thanks

Ian

A picture of the setup that works is not helpful.

Feel free to post again when you have a picture that shows the setup that you're having trouble with. Things like exactly which terminals you are connecting things to are important. But fairly obviously if you really have both motors connected to the same positive/negative terminals then you can't control them separately.

And if you really have the pots connected in SERIES then nothing is going to work.

Steve

Hi Steve,

I’ll post again soon when I have both pots and motors set up.

Thanks for the input! It’s appreciated.

slipstick:
A picture of the setup that works is not helpful.

Feel free to post again when you have a picture that shows the setup that you’re having trouble with. Things like exactly which terminals you are connecting things to are important. But fairly obviously if you really have both motors connected to the same positive/negative terminals then you can’t control them separately.

And if you really have the pots connected in SERIES then nothing is going to work.

Steve

Hi Steve,

Please see attached schematics on how I have set up for two motors and two pots.

Please see my code below for this setup.

void setup() {
pinMode(12,OUTPUT); //Channel A Direction Pin Initialize
pinMode(13,OUTPUT); //Channel A Direction Pin Initialize
}

void loop() {

if (analogRead(A2) > 512)
{
digitalWrite(12, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A2)-255); //Channel A Speed
}

if(analogRead(A2) <= 512)
{
digitalWrite(12, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A2)); //Channel B Speed
}

if (analogRead(A5) > 512)
{
digitalWrite(13, HIGH); //Channel A Direction Forward
analogWrite(3, 0.5*analogRead(A5)-255); //Channel A Speed
}

if(analogRead(A5) <= 512)
{
digitalWrite(13, LOW); //Channel A Direction Reverse
analogWrite(3, 255-0.5*analogRead(A5)); //Channel B Speed
}

}

Cheers