Controlling speed of dc motor

Hi all, need some help with my code. Using arduino uno and a sn754410 motor driver ic to drive my 12v dc motor to run forward, pause, backward, pause, and loop. But I don't know why the speed forwards is much faster than backwards?

Also, would it be recommended if I use a potentiometer to control my speed instead? What's the difference?

int speedPin = 3;       // H-bridge enable pin for speed control
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
int speed_value_motor1; // value for motor speed
int val = 0; // Variable used to set the speed of the motors.
  
  void setup() {

    // set all the other pins you're using as outputs:
    pinMode(speedPin, OUTPUT);
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH); 
  }
  
  void loop() {
    val = 125; //A value used for setting the speed of the motor, about 70% of its speed.
    
    //spin motor backward for five seconds
   analogWrite(enablePin, val); // set the speed of the motors with PWM
  digitalWrite(motor1Pin, LOW);
  digitalWrite(motor2Pin, HIGH);
  delay(5000); //

  //turn off motor direction
  digitalWrite(motor1Pin,LOW);
 digitalWrite(motor2Pin,LOW);
 delay(2000);

 //spin motor forward for five seconds
 analogWrite(enablePin, val); // set the speed of the motors with PWM
  digitalWrite(motor1Pin, HIGH);
  digitalWrite(motor2Pin, LOW);
  delay(5000);

    
//stop for two second
val=0;
analogWrite(enablePin, val);
delay(2000);
  }
int speedPin = 3;       // H-bridge enable pin for speed control
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)

Which of these is really connected to pin 3?

Ah sorry speedPin=9. Just changed it, no difference. It is connected to leg one of motor driver ic right?

I don't recommend using a potentiometer.

My guess is that the SN754410 is damaged and that is why the speeds are different. An easy way to check is to swap the leads to the motor, and if the motor spins faster while going backward then it is the driver. I've burnt up plenty of motor drivers in my past.

jodi: Ah sorry speedPin=9. Just changed it, no difference. It is connected to leg one of motor driver ic right?

If you are using an Arduino Shield, then can you tell us which one. Otherwise, can you describe how you have the IC connected to the Arduino, power supply, and motor.

Nice catch! I’ve replaced the sn754410 and it works fine. But now I’m worried cause I’m not too sure how I burnt that out.

I’ve connected it like in the diagram.

Oh wait the board is usually connected to my computer and works fine. I connected the 9v battery to the jack and there was smoke coming from the digital pins… I guess that burnt out my ic. I added the resistors and capacitors after that… pray that works

Good documentation! What is the DC resistance of the motor? You probably want to use some flyback diodes. Diodes in the SN754410 are just for ESD protection they probably cannot handle the induction kick from the motor. Also, it looks like the leads of the capacitor that is connected to Vcc1 on the SN754410 are shorted together which isn't a problem but the capacitor isn't doing anything when it’s used that way.

Where exactly was the smoke coming from?

What are the values of the resistors? It's difficult to tell from the image. Any value between 1K and 100K should be fine.

I wish I could take credit, but I got it from here http://itp.nyu.edu/physcomp/Labs/DCMotorControl. Well, the weird wonky parts that looks like they don’t belong there are my pathetic attempts in microsoft word.

Ok I’ve changed the capacitor on Vcc1 to connect to the ground like in the diagram. Did I get that right? Sorry total noob here but where exactly do I connect the flyback diodes to?

The smoke came somewhere from digital 1-7 pins. And I have no idea what the resistance of the motor is. I got it from a junk store and all the uncle told me was it runs on 12volts lol

BTW, they are using the software package called fritzing which is free and easy to use.

The capacitor is backwards in the image. The white stripe should be connected to ground. Swap the leads and it will be good. Connect the diodes as shown in my ascii-art below (I'm having a problem attaching images) for each of the leads of the motor. Do you have away to measure resistance like with a multimeter? If not, do you have a physically large resistor to limit the current to the motor. Can you tell us the colors/colours :) that are on the resistors.

12V __|/|__to motor__|/|__ GND
      |\|            |\|

Or check out page 2 on this PDF document. It's better than my ascii-art. http://www.ti.com/lit/ds/symlink/l293d.pdf

Ok I've connected 1 diode (GW209 IN4007) next to each motor lead connected to ground. The capacitors are 25v 10uF and 25v 47uF. 2 resistors 0.5w 10k each, blue in colour, colour band - brown red blue blue brown.

Thanks so much for your help and patience so far, really appreciate it :)

Those resistors are 126M not 10k but they are not required. Also, pinMode() has an INPUT_PULLUP option, so you won’t need a resistor for the switch. You should use four diodes like in the image below.

Untitled.png

I've added the 4 diodes and removed the resistors. I've removed the switch too (cos i didn't want it but dont think that will make a difference). But when I connected the 12c battery, another sn754410 chip burnt out :(

Using arduino uno and a sn754410 motor driver ic to drive my 12v dc motor

You've yet to tell us anything about this 12V motor, except it's voltage. Is the current required by the motor in the range that the SN754410 can manage?