DC Motor speed control by using pwm

I am using Arduino Uno, and MPU6050(3 axis imu sensors), not gate, and L298 dc motor drive.

My algorithm is very easy. Receive an angle how the balancing robot is tilt. Decision speed of motors by using the angle. For example, when robot is tilt n degrees forth or back, robot will go forth or back with speed proportional n.

My code is similar to the following code. Sorry, it is difficult to type on my phone. TT

``````Angle = getComplementaryAngle();

Speed = pidCompute(abs(Angle));
// Speed range: 0 to 255

if (Angle > 0.4) {

DigitalWrite(dirA, HIGH);

DigitalWrite(dirB, HIGH);

analogWrite(enA, Speed);

analogWrite(enB, Speed);

}

else if (Angle < -0.4) {

DigitalWrite(dirA, LOW);

DigitalWrite(dirB, LOW);

analogWrite(enA, Speed);

analogWrite(enB, Speed);

}

else {

analogWrite(enA, 0);

analogWrite(enB, 0);

}
``````

I don't know why left motor's speed is different from right motor's one. I checked the voltage of motor pin. Both pins' voltage are same as 11V. I think pwm is wrong.

When Arduino makes two pwm output, it causes some problems?

• You haven't told us what is going wrong. Are enA and enB both pwm pins? – Gerben May 25 '15 at 10:52
• As Gerben notes - not all pins are able to provide analog output. Have you chosen pins that do? Try outputting PWM to a resistor load - say 10K Ohms, observing PWM outputs and tilting the robot by hand across its range. Do the PWM output vary as expected? You have not provided a circuit diagram , model of motors, voltages used, which pins are doing what and more. You do not say what dira, dirb do or how or even why. We can guess but may guess wrong. There is MUCH room for things to be wrong in the areas you have not told us about. – Russell McMahon May 25 '15 at 11:06
• ah... sorry. I will change my question and ask again. – Danny_Kim May 25 '15 at 19:16
• Your code is "similar" to that or that is your code? why don't you post the actual code? What a waste of everyone's time it would be if your error is something that is different in your "real" code. – Delta_G Aug 20 '19 at 23:26