The motor runs at half the speed

Hi:

I read the pulse (1 to 2 ms) and map it to a -255,255 range. Then I analogWrite the absolute value (abs()) in the pin that goes to the enable pin in a L293. But the motor barely run at half the speed. The battery is well charged because if I connect the motor directly to it it will run at the right speed.

What am I doing wrong?

Thanks

Post your code

You need to provide information about the power supply and the motor voltage and current requirements. A circuit diagram would help. The L293 needs protective diodes and is limited in the amount of motor current that it can handle.

raschemmel:
Post your code

chThr = pulseIn(pinchThr, HIGH) // READ RC PULSE FROM PIN pinchThr;
.
// MAP IT FROM 1000,2000 TO -255,255 (1000,2000 MAY VARY AFTER CALIBRATION)
pwm_M1 = map(chThr,minPulseWidthThr,maxPulseWidthThr,-minMaxPWM,minMaxPWM);

.
// SEND PULSE TO ENABLE PIN IN A L293B
analogWrite(pinpwm_M1,abs(pwm_M1))

jremington:
You need to provide information about the power supply and the motor voltage and current requirements. A circuit diagram would help. The L293 needs protective diodes and is limited in the amount of motor current that it can handle.

The motors are FF-050 from CD-ROM, very stable and quiet motors, not even capacitors are needed with these ones. At stall, they draw less than 400mA, and the L293B is capable of handling 1 amp per motor.

Your problem is you don't understand the difference between a servo signal (RC pulse) and PWM . The servo signal has a center frequency of 1500uS and only varies */- 500 uS.. You can't send negative PWM values .

  // MAP IT FROM 1000,2000 TO -255,255 (1000,2000 MAY VARY AFTER CALIBRATION)
pwm_M1 = map(chThr,minPulseWidthThr,maxPulseWidthThr,-minMaxPWM,minMaxPWM);

Your PWM range must be 0 to 255. It doesn't accept negative values.

In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

http://arduino.cc/en/Tutorial/PWM

raschemmel:
Your problem is you don't understand the difference between a servo signal (RC pulse) and PWM . The servo signal has a center frequency of 1500uS and only varies */- 500 uS.. You can't send negative PWM values .

  // MAP IT FROM 1000,2000 TO -255,255 (1000,2000 MAY VARY AFTER CALIBRATION)

pwm_M1 = map(chThr,minPulseWidthThr,maxPulseWidthThr,-minMaxPWM,minMaxPWM);




Your PWM range must be 0 to 255. It doesn't accept negative values.

Well, according to this: map() - Arduino Reference

"The function also handles negative numbers well, so that this example
y = map(x, 1, 50, 50, -100);
is also valid and works well."

I didn't say the Map function could not use negative numbers. I said the AnalogWrite (and motor control Enable pins that accept PWM)
do not take negative numbers. .If you want the motor to turn the opposite direction you need to change the state of the direction pin. .You did not post your entire code so we don't know what "pwm_M1" mean other than what the name says it means, which is a PWM input to a motor control that only accepts PWM. We don't know what you are doing with this pwm_M1 AFTER the Map function because you didn't post your entire code. We can only assume that you are using it as the argument for an analogWrite statement, because there is no other way to get a PWM signal (short of bit banging) from the arduino to a motor control. If you post your ENTIRE code then maybe we can figure out what is going wrong. I am simply saying that although the Map function has no problem at all with negative numbers, they are not valid for analogWrite statements. (which of course, without your code, we don't know if that is what you are using the pwm_M1 for).
(which is another way of saying there is no such thing as a negative TTL PWM signal.)
I just saw this , which confirms the above:

// SEND PULSE TO ENABLE PIN IN A L293B
analogWrite(pinpwm_M1,abs(pwm_M1))

Example

/* Map an analog value to 8 bits (0 to 255) */
void setup() {}

void loop()
{
int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);
}

Do you see any negative values for the analogWrite() statement ?

As you can see, I am not using negative values for the analogWrite function. I'm using abs() to take the absolute value (which is always equal or greater than zero). And when the value es negative, I enable the pin that makes the motor turn in the opposirte direction. All of that is working, the motor turn proportionally from zero (RC stick in the middle) to maximum in both directions.

The problem is that it will never run at max speed. I took the example from here:
https://www.sparkfun.com/tutorials/348

He is doing basically the same, though he uses the -500,500 range:

Serial.println(map(ch2, 1000,2000,-500,500)); // center at 0

(just printing the values, he is not driving a motor here, but it's the same idea.)

I tried with -500,500 to worse results.

Blaming it to the L293B driver, I swapped the pin (HIGH or LOW, no PWM here) that goes to the input (direction) to the enable pin in the L293B and the motor ran at full speed.

Besides that, LEDS (and resistor) are wired in parallel with the outputs to monitor the activity: 2 LEDS wired to the Arduino OUTPUT PINS that go to the L293B INPUTS for direction, and 1 LED from the Arduino OUTPUT PIN that goes to the ENABLE PINS in the L293B, these ones the PWM for speed control.

Is there something else about analogWrite() I don't know?

Ok, I didn't want to bother you adding all my code, but when I trid it says that it exceeds the allowed length (9500 characters)..

But later I found that someone uses constrain() after map():

turn = map(ch1,1000,2000,-500,500);
turn = constrain(turn, -255, 255);

I will try it later.

Hi, you can post your sketch as an attachment.
Have to checked that 255 written directly to the PWM pin will produce full speed.
Let make sure its not hardware thats the problem, a simple code change will check it.

Have you written all this program at once or did you create it section by section testing it as you build?
Just write a program that controls the motor and see if that works, then build from there.

Tom.... :slight_smile:

How do I post the sketch as an attachment? I don't see a button for that purpose. Or should send you a message with the attachment?

rva1945:
Hi:

I read the pulse (1 to 2 ms) and map it to a -255,255 range. Then I analogWrite the absolute value (abs()) in the pin that goes to the enable pin in a L293. But the motor barely run at half the speed. The battery is well charged because if I connect the motor directly to it it will run at the right speed.

What am I doing wrong?

Thanks

Using an L293.

This chip has darlington output drivers, which waste 1.0 to 1.5V each (top
and bottom) when carrying significant current, so yout motor will see about 2.5V
less than the battery/supply voltage. You either have to allow for that by using
a higher supply voltage or change to using a better driver chip such as a MOSFET
H-bridge. (I assume you have about 6V supply and 6V motor?)

And if its the L293B (as opposed to L293D) its essential to use 4 free-wheel
diodes as shown in the datasheet or you can damage/destroy the driver.

Now I understand the output drop after cascading in the darlington scheme, I know what you are talking about.

Yes a MOSFET H bridge would give me (almost?) 100% of power.

I'm not using the diodes as these motors are really "weak" and don't spike, but I will consider it.

Thanks

Anyway the LEDS connected to the out pins (PWM) bright dimly as compared to the leds connected to the pins (HIGH/LOW) that control the motors direction.

So before reaching the L239 I can see that the pulses don't seem to be 100% high of the duty cycle. But I do see that the pulse sent to the motors is 255 at full speed.

rva1945:
Now I understand the output drop after cascading in the darlington scheme, I know what you are talking about.

Yes a MOSFET H bridge would give me (almost?) 100% of power.

I'm not using the diodes as these motors are really "weak" and don't spike, but I will consider it.

Thanks

Use diodes. The motors might behave on light load and act resistive, but once
the brushes wear and spark and the motor is under stall conditions things may
be different - diodes are cheap.

To post an attachment, use the "Additioanal Options " button below and then select the file.

Well, I am posting my dualmotordriver.ino file.

It has comments in Spanish but the code is simple, you will understand it. Feel free to ask any question.

Thanks,
Robert

dualmotordriver.ino (14.1 KB)

There is a lot of code there and I haven't reviewed it in detail, but the logic to derive the PWM value passed to analogWrite() is complex and could easily be producing a value you don't expect.

To start with, I suggest you create a test sketch that does nothing but run the motor(s) at full speed and confirm the hardware is working correctly. Once you've done that, add some debug print statements to show what values you're actually passing to analogWrite() in your real sketch - I suspect you'll find they're wrong, pointing to bugs in your calibration logic.

I understand your guidelines, but, I added Serial.print lines after analogWrite, and the value that I send to the enable pin (in the L293) is 255, now, shouldn't that value make the motors run at full speed? I know there is a 1.5V drop in the L293, but I measured the voltage and it's 5V from a 7.5V battery, so the voltage drop is 2V!

AND the LEDS that I wired in parallel with those outputs bright dimly, so I guess 255 is not producing a 100% duty cycle. If I had an oscilloscope...