Motor, Potentiometer , L298n, Arduino board

Hello world,

I am going to be following this sites directions, almost, exactly. I have 2 questions.

1.) What if I wanted to add a second or third motor? how would this code change.
Now I would need another L298n and potentiometer for each motor. I believe I code still use one arduino.

2.) number one is more important If code is feasible, Is there any way I can make the potentiometer wireless? so I can control the speed of the motors from far away with a potentiometer rod?

Please advise,
Bull engineer

1 Like

1.) What if I wanted to add a second or third motor? how would this code change.

That depends. If you want to control the speed of two or three or a dozen motors with one potentiometer, then you simply replicate the statements that apply to one motor controller's pins, in setup() and loop().

If you want to control the speed of two or more motors with an equal number of potentiometers, then you replicate all the code in setup() and in loop().

Is there any way I can make the potentiometer wireless?

No. Potentiometers must be connected to the Arduino with wires.

Now, you could have two Arduinos, with the potentiometers, and transmitter, connected to one, and the motor controllers and receiver connected to other.

Which transmitter and receiver to use depends on how far you want to transmit data, how reliably, and how much you want to pay.

Paul Thanks for the help!

Is there any way you can do a quick copy and paste of the code I provided with the updated version?? I only know a little bit of python so that would be of great help. Secondly, Can you make an electrical schematic of the layout of your great design? 2 arduinos. I would love to see that build.

Is there any way you can do a quick copy and paste of the code I provided with the updated version?

I could, but ONLY after you define your requirements. I mentioned two distinct ways of dealing with multiple motors, and you said yes, that's what I want to do. Well, no, it isn't.

Paul,
Youre correct, I did not specify. I wanted to do equally potentiometers as motors. Thanks

I wanted to do equally potentiometers as motors

So, two potentiometers control the speed of one motor each?

Do you recognize what part(s) of your code read the value of the existing potentiometer?
Do you recognize how the Arduino knows where the potentiometer is connected?
Do you recognize what part(s) of your code define where the motor controller is connected?
Do you recognize what part(s) of your code set the direction and speed of the existing motor?

If you do, then copying, pasting, and editing those parts will be trivial.

Without know where you plan to connect the additional pot and the additional motor controller, no one else can do the copy/paste/edit for you. Besides, you wouldn't learn anything if someone else did the work for you.

Paul,

I want 4 motors and 4 potentiometers, One potentiometer controls one motor. I understand all of those requirements you've listed. Just would like to the code because you said it would be simple copy and paste. Thanks!

BullEngineer:
Paul,

I want 4 motors and 4 potentiometers, One potentiometer controls one motor. I understand all of those requirements you've listed. Just would like to the code because you said it would be simple copy and paste. Thanks!

But you still haven't defined where you will connect the other 3 potentiometers, or the other 3 motor controllers. So, no one else can write the code for you.

Paul,

This is where I need guidance? I dont know where to connect them.

This is where I need guidance?

Is that a question? Or a statement?

Can you see where that sketch expects the potentiometer to be connected? Do you actually have a potentiometer connected there?

If you can, and you do, just pick 3 more pins on the same side of the board and connect the potentiometers.

If you can't, you need a different hobby.

Paul,

I found this to maybe explain it better to you. So, now you should see how I have this thing built. Only difference is I have 4 motors and 4 potentiometers. Do you think you could take the code and show me how I add a 4th motor and a 4th potentiometer to it?

Please advise,
Bull Engineer

There's code in there for reading the potentiometers. Just swap the servo.write() code for whatever you use for running your motors.

You're going to have to make some attempt to do some of this yourself. Then we can help you get it going. But we're not here to just write your program for you.

Steve

slipstick:
There's code in there for reading the potentiometers. Just swap the servo.write() code for whatever you use for running your motors.

You're going to have to make some attempt to do some of this yourself. Then we can help you get it going. But we're not here to just write your program for you.

Steve

Steve,

yes I'm willing to work.
#include <Servo.h>

Servo myservo3;

Servo myservo5;

Servo myservo6;
Servo myservo8;

int potpin = 0;
int potpin2 = 1;

int potpin3 = 2;
int potpin4 = 3;

int val = 0;
int val2 = 0;

int val3 = 0;
int val4 = 0;

void setup()
{

myservo3.attach(9);
myservo5.attach(10);

myservo6.attach(11);
myservo8,attach(12);

}

void loop()
{

val = analogRead(potpin);
val = map(val, 3, 1023, 0, 176);

myservo3.write(val);

delay(25);

val2 = analogRead(potpin2);
val2 = map(val2, 3, 1023, 0, 176);

myservo5.write(val2);

delay(25);

val3 = analogRead(potpin3);
val3 = map(val3, 3, 1023, 0, 175);

myservo6.write(val3);

delay(25);

val4 = analogRead(potpin3);
val4 = analogRead(potpin3);

myservo8.write(val4);

delay(25);

}

This is the code I have so far. does it look reasonable?

Looks good apart from the odd servo naming, the , where a . should be and the fact that val4 is read from potpin3 (twice).

But didn't you say you were controlling DC motors and not servos? They need different code. As I said before you need to replace the servo.write()s with motor driver commands from your original code, probably analogWrite()s I'd guess.

Steve

slipstick:
Looks good apart from the odd servo naming, the , where a . should be and the fact that val4 is read from potpin3 (twice).

But didn't you say you were controlling DC motors and not servos? They need different code. As I said before you need to replace the servo.write()s with motor driver commands from your original code, probably analogWrite()s I'd guess.

Steve

Steve,
I worked on your suggestions. Here is what I printed

#include <Servo.h>

Servo mymotor3;

Servo mymotor5;

Servo mymotor6;
Servo mymotor8;

int potpin = 0;
int potpin2 = 1;

int potpin3 = 2;
int potpin4 = 3;

int val = 0;
int val2 = 0;

int val3 = 0;
int val4 = 0;

void setup()
{

mymotor3.attach(9);
mymotor5.attach(10);

mymotor6.attach(11);
mymotor8,attach(12);

}

void loop()
{

int reading1 = analogread(potpin);
int angle1 = reading1 / 6;

mymotor3.write(angle);

delay(25);

int reading2 = analogRead(potpin2);
int angle2 = reading2 / 6;

mymotor5.write(angle2);

delay(25);

int reading3 = analogRead(potpin3);
int angle3 = reading3 / 6;

mymotor6.write(angle3);

delay(25);

int reading4 = analogRead(potpin4);
int angle4 = reading4 / 6;

mymotor8.write(angle4);

delay(25);

}

So, I have 4 h-bridges in the mail with PWM control
https://www.newegg.com/Product/Product.aspx?Item=9SIAJ9P8XC1576&ignorebbr=1&source=region&nm_mc=KNC-GoogleMKP-PC&cm_mmc=KNC-GoogleMKP-PC-_-pla-Hadley+Tech+LTD-_-HI+-+Test+%26+Measurement+Tools-_-9SIAJ9P8XC1576&gclid=CjwKCAjwmq3kBRB_EiwAJkNDpxhYYECoaiFoaHf45sC38zHNX91HzJqKb5Vr4oAbnRVJ6RNVtR75ghoCd4IQAvD_BwE&gclsrc=aw.ds

there's the link

I know these H-bridges are for dual motors. But, I am planning on using 1 h-bridge per motor. Do you have any suggestions where to plug these in the electrical schematic/arduino? Then I will come up with a code for it.

Thank you for your help,

Bull engineer

Steve,

Another question regarding delay(25)

Will that allow me to move my potentiometers all at once with the same reaction time? or will it move one, then move two, then move 3, then move 4?

Dont think to hard about it as I will do some trial and error right now.

#include <Servo.h>

Servo mymotor3;

int potpin = 0;
int val = 0;

void setup()
{

mymotor3.attach(9);

}

void loop()
{

int reading1 = analogread(potpin);
int angle1 = reading1 / 6;

mymotor3.write(angle);

delay(25);

}

So, back to the h-bridge question, lets scale it down to one motor for simplicity. does this help?

Please advise,

Bullengineer

When you get whichever H-bridges you have ordered take to some time reading any documentation they come with. Mostly they need 2 or 3 pins per motor. The commands for H-bridges are pretty variable but none of them look like servo commands. I don't know why you want to use one per motor which will complicate the power wiring but it is your choice.

There are probably example programs provided for the H-bridge. I suggest you try one or two of them to get a good idea of how exactly what commands you need to use to drive motors.

Steve

I just read this thread for the first time, and I'm confused about what motors you have. The last post gave code for 4x servos, yet you talk of ordering L298-based modules which have nothing to do with servos, but are for DC motors.

So is the objective to have 4x servos AND 4x PWM-ed DC motors?

neiklot:
I just read this thread for the first time, and I'm confused about what motors you have. The last post gave code for 4x servos, yet you talk of ordering L298-based modules which have nothing to do with servos, but are for DC motors.

So is the objective to have 4x servos AND 4x PWM-ed DC motors?

The servos were for examples, I am not using servos in this project. I am using 4 DC motors that I want PWM-ed. I still have servo in my code, do I need to change this?

https://www.amazon.com/Axial-Electric-Motor-Brushed-AX24004/dp/B001454DOE/ref=sr_1_1?keywords=Axial+27T+Brushed+Electric+Motor&qid=1552505676&s=hi&sr=8-1

this is the motor I will be using

BullEngineer

Says in that link that those motors require an ESC which I think means they can't be run from an h-bridge using PWM.

(edit) But afaik, an ESC and such a motor then actually does act like a servo and can be controlled by the servo library. Not sure of the details though.