Motor speed control help

I have everyything wired correctly exept need some help on code

  const int motor1Pin = 3;    
  const int motor2Pin = 4;    
  const int enablePin = 9;   
  int potPin = 0;
  int potValue = 0;
  
  void setup() {
  

    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    pinMode(potPin, INPUT);
    digitalWrite(enablePin, HIGH); 
    digitalWrite(motor1pin, HIGH);
    digitalWrite(motor2pin, LOW);
  }
  void loop() {
  }

Pot is a 10k pot connected to ground, +5v, and analoge 0.
Motor is connected to the sn754410 h bridge
All wanting to do is vary the speed I have done tuns of research but no luck using the pot and this hbridge (examples)
any help is appreciated

You need to connect the h bridge to a pin that will do PWM.
Remove the pot pin assignment from the code.
then in the loop read the pot:-
potValue = analogRead(potPin);
and use the value to set the PWM
analogWrite(h_bridge_Pin, potValue/4);

That should do it if you have your hardware wired up right.
See:-
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html
and:-

http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Both hbridge pins are connected to a digital pin. Is it not possible to read a value from an analog pin and use the h bridge pins to digital?
Reason is I need 3 analog pins for 3 pots. I'll be using 3 digital pins per motor so 9 digital pins and 3 analog total.

int motor1Pin = 3;    
int motor2Pin = 4;    
int enablePin = 9;  
int potPin = 0;
int speed = 0;
  void setup() {
    Serial.begin (9600);
  

    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
 
    
    digitalWrite(enablePin, HIGH);
    
  }
  void loop() {
    
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
speed = analogRead(potPin);
speed = 793 + (speed/6);
analogWrite (enablePin, speed);
Serial.println(speed);

  }

this works :slight_smile:

int motor1Pin = 3;    
int motor2Pin = 4;    
int enablePin = 9;  
int potPin = 0;
int potValue = 0;

  void setup() {
    Serial.begin (9600);
  

    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
 
    
    digitalWrite(enablePin, HIGH);
    
  }
  void loop() {
    
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
potValue = analogRead(potPin);
analogWrite(enablePin, potValue/4);
Serial.println(potValue);


  }

reading 0 to 1024
Using 2 pots and no luck i only get one motor turning:

int motor1Pin = 3;    
int motor2Pin = 4;    
int motor3Pin = 5;
int motor4Pin = 6;
int enablePin = 9;  
int potPin1 = 0;
int potPin2 = 1;
int potValue1 = 0;
int potValue2 = 1;
  void setup() {
    Serial.begin (9600);
  

    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
 
    
    digitalWrite(enablePin, HIGH);
    
  }
  void loop() {
    
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
potValue1 = analogRead(potPin1);
analogWrite(enablePin, potValue1/4);
Serial.println(potValue1);
  {
  digitalWrite(motor3Pin, LOW);
  digitalWrite(motor4Pin, HIGH);
  potValue2 = analogRead(potPin2);
  analogWrite(enablePin, potValue2/4);
  Serial.println(potValue2);
}


  }

That didnt work..
So I tried this:

int motor1Pin = 3;    
int motor2Pin = 4;    
int motor3Pin = 5;
int motor4Pin = 6;
int enablePin = 9;  
int potPin1 = 0;
int potPin2 = 1;
int potValue1 = 0;
int potValue2 = 1;
  void setup() {
    Serial.begin (9600);
  

    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(motor3Pin, OUTPUT);
    pinMode(motor4Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
 
    
    digitalWrite(enablePin, HIGH);
    
  }
  void loop() {
    
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
potValue1 = analogRead(potPin1);
analogWrite(enablePin, potValue1/4);
Serial.println(potValue1);
  {
  digitalWrite(motor3Pin, LOW);
  digitalWrite(motor4Pin, HIGH);
  potValue2 = analogRead(potPin2);
  analogWrite(enablePin, potValue2/4);
  Serial.println(potValue2);
}


  }

Both motors are running but I dont think its recognizing the analog pin 2 for potpin2 to control motor 5,6

reading 0 to 1024

Impressive. :wink:

You can not control the speed of the motor with a digital pin. You need a PWM pin.

If you need more analog inputs you can get 8 extra analog ins with a CD4051 analog multiplexor IC, the price is that you will use 3 digital pins and one analog to control it, so netto you will get 7 more analog ins.

There's an example in the playground for using the 4051.

both pots are connected to 5v ground
1 pot is connected to analog 0
1 pot is connected to analog 1
motor #1 is connected to digital 3 and 4
motor #2 is connected to digital 5 and 6

both are working except one motor stays on and the other is controllable from no speed to high speed

im thinking i might have the wiring wrong or definately the code?

I am getting both pot values from printlin...
Tried some different code but no luck. Both pots are on the same +5v rail coming out of the arduino.

both are working except one motor stays on and the other is controllable from no speed to high speed

I would say that is none are working.

Is it not possible to read a value from an analog pin and use the h bridge pins to digital?

Yes it is!
You need to connect one of the H bridge pins to a PWM pin and do an analogue write to it. Did you read those links?

Look up analogWrite in the arduino reference

On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

Ok yes got carried away :).
I did look at the links :).
I can control one motor with one pot. Eventually I'll be doing 3 motors and 3 pots.
I did make a board with 2 h bridges and 1 arduino. ATM I'm prototyping on a breadboard before I go and lay out on my board.
Here's a question: how do I call the analog pins 0 to 5?
Like this:
int analogpin1 = 0 (analog pin 0)
int analogpin2 = 1
int analogpin3 = 2
?

Got a better understanding.
As long as an h bridge pin is connect to an analog pin, I can either use it using a potentiometer or setting the speed automatically via 0 to 1024 (255?). I could throw in a switch correct? With an if statement? Also pin 1 on the sn754410 could be on +5v either from arduino or separate power source. Since I'm only going one direction that'll save 3 pins since I tie one motor pin to ground and the other to the h bridge. In total could do 4 motors one way with the h bridge.
I'm still needing help to wire the 3 pots or 2 until other arrives to manually control the speed of each motor.
Currently if this will work:
pot 1 to analog 0
pot 2 to analog 1
pot 3 to analog 2
h bridge pin to any of the digital pins that can do pwm ( 3, 5, 6, 9, 10, and 11)
correct?
Also will the servo library help me?

As long as an h bridge pin is connect to an analog pin,

This is where you are going wrong. The analogue pins are analogue input ONLY, they are not analogue outputs.
You connect each pot to an analogue pin. The H bridge must be connected to the digital pins. Some digital pins (the PWM ones) you can use the analogue write to set the PWM that is the on off ratio of this pin.
To drive a motor set one H bridge output high or low, this determines the direction of the motor. The other h bridge pin should be set using the analogue write. A value of 0 to this pin will keep it low and a value of 255 will keep it high.
A h bridge makes the motor turn when both it's pins are at different levels.
Therefore if the direction pin is low then the PWM pin will produce stop when you write 0 to it and fastest when you write 255 to it.
This is reversed when the direction pin is high. Then when you write 0 it will go the fastest and when you write 255 it will go the fastest.

Also will the servo library help me

No

If you are using the sn754410 then this has an enable pin. You can control the speed like I said above or feed the enable pin with the PWM signal. This applies passive breaking to the motor where the motor costs when the PWM pin says it is off, the other form is active breaking where the motor is actually forced to slow down when the PWM says it is off. Depending on your application you might want to choose one. If the motor is under heavy load the the passive breaking will be better otherwise it is best to use active breaking.

Alright... :wink:
But am I calling the analog pins correctly?

Yes

int motor1Pin = 6;
int motor2Pin = 5;    
int enablePin = 9;  
int potPin = 0;
int potPin1 = 1;
int potValue = 0;
int potValue1 = 0;

  void setup() {
    Serial.begin (9600);
  

    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);

    
    digitalWrite(enablePin, HIGH);
    
  }
  void loop() {
digitalWrite(motor1Pin, HIGH);    
potValue = analogRead(potPin);
analogWrite(enablePin, potValue/4);
Serial.println(potValue);

digitalWrite(motor2Pin, HIGH);
potValue1 = analogRead(potPin1);
analogWrite(enablePin, potValue1/4);
Serial.println(potValue1);


  }

This doesnt work quite well if at all.. say pot on analog 0 and control motor1 or pot on analog 1 and control motor 2

MikMo: You can not control the speed of the motor with a digital pin. You need a PWM pin.

Yes you can; it would be complicated, but should be doable.

Basically, you tie an interupt ISR to the clock, then in that ISR, based on a divisor variable, you pulse the digital I/O pin for the PWM output (up to approximately the max frequency of the clock driving the ISR).

Right?

Heck, for all I know, this is how the PWM pins are coded in the Arduino library... :smiley:

How does the arduino know which pin is what? Like say digital pin or analog when I'm setting them up?

int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int val1 = 0;
int val2 = 0;
int motor1Pin = 2; // h bridge pin connected to analog 2
int motor2Pin = 3; // h bridge pin connected to analog 3
int enablePin = 9; //h bridge enable pin connected to digital 9


void setup()
{
 Serial.begin(9600);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);


  


}
void loop()
{

  val1 = analogRead(pot1);
  analogWrite(motor1Pin, val1);
  Serial.println(val1);
  delay(100);

  val2 = analogRead(pot2);
  analogWrite(motor2Pin, val2);
  Serial.println(val2);
  delay(100);
}

both motors are on full blast but I can read the pot value in the serial monitor....
Heres how I have it wire:

H bridge
digital9 --[]-- +5v
analog 0 --[]-- Not connected
Motor1 + --[]-- Not connected
Ground --[]-- Ground
Ground --[]-- Ground
Motor 2+ --[]-- Not Connected
Analog 1 --[]-- Not Connected
Motor Supply +3v --[]-- Not Connected

pot 1:
Right Pot pin to +5v
Middle Pot pin to analog 0
Left Pot pin to ground

pot 2:
Right Pot pin to +5v
Middle Pot pin to analog 1
Left Pot pin to ground

And the negative motor pins to ground.

motorpin1 should be on, like, digital PWM 5
motorpin2 should be on, like, digital PWM 6

that worked :slight_smile:
Heres the code I now can fully control both motors serperately in one direction (which is all I need)

int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int val1 = 0;
int val2 = 0;
int motor1Pin = 5; // h bridge pin connected digital 5
int motor2Pin = 6; // h bridge pin connected to digital 6
int enablePin = 9; //h bridge enable pin connected to digital 9


void setup()
{
 Serial.begin(9600);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);



  


}
void loop()
{

  val1 = analogRead(pot1);
  analogWrite(motor1Pin, val1 / 4);
  Serial.println(val1);
 
  val2 = analogRead(pot2);
  analogWrite(motor2Pin, val2 / 4);
  Serial.println(val2);

}

Now the next part is going to be tricky... I'm wanting to add a switch if High I can manually control the motors if low then the arduino will automatically control them through pwm...
Where do I need to start?

Glad it helped.