# Control hacked servo motor speed using L293D and PWM

I've got a L293D and a PIC16F877A outputting PWM. Not real PWM, just using Timer0 to turn ON a pin for 2 ms and OFF for 18 ms (total of 20 ms period).

W.R.T. to the above schematic I got VSS, VC, CHIP 1 and CHIP 2 up to 5V.

My current method of controlling the motor is to turn input 1 and 2 on-off from the PWM, to control the outputs 1 and 2. My motor is a hacked servo motor that runs 360 degrees.

EDIT: My motor is a pre-hacked RC servo motor, specifically the Cytron C36S, hacked to run continuously (i.e. behave as a normal motor, no longer as a typical servo motor)

### My approach

When input 1 is ON and input 2 is OFF, it prompts the motor to run one way, input 1 is not always ON. Since input 1 is the PWM output from the PIC, it'll turn ON (HIGH) for 2 ms and OFF (LOW) for 18 ms in continuous cycle of 20 ms.

But using this setup, my motor isn't properly working; sometimes it runs slower than usual (this is actually what I want) but most of the time it just hums in its place, no movement whatsoever.

How do I improve this configuration, is there a better way to control motor speed using L293D? If possible I want to use l293D as buying new components/ICs are too much of a hassle...

• Can you please provide (much) more information about your "hacked servo motor". Is this an RC modelling "servo" or a "real" 'servo motor' (which is rather different to an RC servo) or what? A part number and/or picture would help. An RC servo usually has electronics that translate pulse % in a PWM stream to position. You say " ... runs 360 degrees ...". Do you mean that it moves one turn fro 0 to 360 degrees or goes around continually. Do you want continuous rotary motion or moving to a position or what? – Russell McMahon Aug 23 '11 at 4:08
• My bad. It's a RC servo motor hacked so that it runs continuously like a normal motor. Already edited my post for that. – Sodrohu Aug 23 '11 at 4:51
• Could you explain how you hacked the servo? Did you remove the potentiometer, or just the stopper? If you apply a longer/shorter pulse width what happens? Also, VSS should be named VCC on your pin diagram. A schematic of connections would probably be useful to confirm things are as they should be. A timing diagram of pulses applied to input 1 and 2 would be even better :-) – Oli Glaser Aug 23 '11 at 7:27
• I bought the servos from Cytron pre-hacked, so I dunno what exactly they did to turn the servo into a normal-like motor. I did try to input 5V and 0 on it, it runs continuously like a normal motor, so I assume if I input a PWM the results should be the same with that of a normal motor. I'm very sure of that since the white wire(the one where you input the pulse input in normal servos) is missing...The L293D schematic I just took from Google; I would've put a figure of my connection, but my rep is <10 so no go... – Sodrohu Aug 23 '11 at 7:50
• if YOUR SERVO HAS 2 WIRES THEN YOU ARE "IN TROUBLE". If so they have either done arcane things inside or have simply turned it into a geared motor, as I suggested. Here is a link where they are using them as wheel drivers with 2 wires . - What you REALLY need to do is to ask the supplier for a spec sheet or a web page reference. - How many wires? Have you got any sort of spec sheet? - Any web reference? – Russell McMahon Aug 23 '11 at 12:47

Based on your (further) info, if there is no control wire, I think the reason it is not moving properly is that what you have now is basically a DC motor. To control a standard DC motor with PWM, you simply use a duty cycle between 0% (off) and 100% (full speed)

The current needed will be a lot higher than a servo control wire, as rather than controlling the feedback, you are controlling the actual power to the motor. This is okay as you have the L293D though, which will supply more current than a uC pin can, and exactly what they were designed for.

I suspect that the 2ms is too low to provide enough power to start the motor turning - if you have a 20ms period, 2/20 = 10% of full "power" (roughly - note actual power is I squared R, so at 10% will actually be 1/100th of power at 100% - RMS voltage would perhaps be a better term here) Try a higher value and see where the motor starts reliably. Let us know the results.

Here is a page that explains the differences quite well:

Dc vs Servo

• Actually that's the whole point: The servo motor is meant to run as a normal DC motor. The PWM output from PIC doesn't directly power the motor, it goes into input of L293D enabling it to run one direction. However, since it's a PWM input, the L293D input is on and off all the time, so in theory the motor is also on and off all the time, reducing its overall speed. Unfortunately it's not fully working all the time, often the motor failed to run at all, just hum...I'll try to input a grreater value of delay to see if it works. – Sodrohu Aug 23 '11 at 9:35

As you are hopefully aware (and should be if not, and should have told us if you are :-) ) the PR23 open source robot, available also from Cytron, uses two C36S's as wheel motors and has a full circuit and code and documentation. Probably a user group too. You need to exhaust that avenue first

PR23 motor drive circuit

PR23:

Lots of hand holding:

No. File Description Action

Here is the PR23 MANUAL:

The following is from page 7 of the above manual. It sounds like there were 9or should have been) two versions of the motor you bought:

• To move the motor, just connect the power supply to the terminal on the motor while to move it in opposite direction, change the polarity of the connection between the power supply and the terminal. For this line following robot, we will be using two motors.

1.2 Motor driver L298D In a line following robot, usually the motor is powered by a different source from the main circuit the motor will move faster and more powerful. For our application, 4.8V is more suitable. Refer to PR23 schematic, user can see that there are 6 pins connected to the microcontroller and 2 pins to each motor. Out of which is the microcontroller. Therefore, an additional component is required to enable the microcontroller to control the motors.

For this project, we will be using L298D for this purpose. A servo motor usually needs 4.8V or 6.0V to operate. Higher voltage will generate more power to the motor, thus the 6 pins, 3 is for the left motor and the other 3 is for the right motor. Now lets concentrate on only 1 side of L298D, 3 pins for the microcontroller and 2 pins for the motor. From the 3 pins, 2 pins is for the direction of the motor and 1 pin (connected to Pin C1 or C2) is for PWM which is to control the speed of the motor.

If controlling of speed is not required, just provide this pin with 5 volt to enable it to move. The direction of the motors depends on the connection of the terminal but can also be determined through the program. Therefore, the sample program has to be modified according to user robot

This is a "Radio Control servo" or just perhaps "servo" but it is NOT a "servomotor" in the normal sense. It includes a basic servomotor but also includes a controller, which is more than just a servomotor AND makes the requirement different.

You need to

• Understand how RC servos work.

• Understand what you are trying to do compared to what the servo is trying to do.

The normal "hack" to make an RC servo run continually as if it was only a geared motor is two fold.

• Mechanical: The end stop(s) which limit travel are removed. Sidecutters are usually your friend with plastic geared RC servos.

• Electrical: You need to understand how an RC servo works. Details below - the internal position feedback pot is replaced by two fixed resistors. The controller thinks the motor is at a fixed position and drives towards it forever.

Here is a Flickr photostream showing a continuos servo modifcation

I pointed it to that page as it has a number of links there. You can go back a few photos in the sequence to see the resistor fitting I mention above. I've included some similar links below.

It seems likely that you do not understand the requirements for driving an RC srevo OR that the hack has been severe.

• Power
Ground
Signal

If not, please advise. If so then this is how it is intended to be controlled.

• Power is applied to the Power and ground leads.

• A pulse train that meets the manufacturers specs is applied to the signal lead (relative to the ground lead). Pulse train specs vary between manufacturers but typically a 1 mS pulse indicates hard "left" a 1.5 mS pulse indicates centred and a 2 mS pulse indicates hard 'right". See page 5 in this Cytron servo controller manual.

If you remove the servo feedback pot and replace it with two equal value resistors then the electronics 'thinks' that the motor is at mid position.

• If you command it to go left it will seek leftwards forever.

• If you command it to go right it will go rightwards forever.

• If you command it to centre it will 'see' the two resistors set at midpoint and will stop (as it appears to be centred). You will probably need to "fine tune" the pulse width sent to exactly match the resistor set point to achieve stopping.

The problem: The control pulses do not control the speed - only the position. As you are 'telling it' that the position is wrong (not enough left or not enough right) it will hurry left or right as fast as its motor will carry it. You can probably alter motor speed somewhat by varying the power supply voltage within approved specifications but this is liable to be less than satisfactory.

There is a way to control speed after a fashion but it also may be unacceptable.

• Motor position is updated about every 20 mS.

• Usually you will be outputting either a 1 mS pulse then a ~= 20 mS delay to go "left" or a 2 mS pulse followed vy an ~ 20 mS delay to go "right". (Total time for a command cycle is flexible, only pulse width matters.

• Instead, send a "right" command, wait a small while and then send a "centre" command. The motor will advance a small distance and then stop. Repeat. The motor will incrementally rotate at its single fixed speed but with pauses between movements.

The table below comes from this BE report It states that the 36s rotates at 0.96 s/rev at 4.8V and 0.84 S/rev at 6V. Close to 1 second/rev in both cases. Voltage variation is not going to help much. However, if you send say 60 command sequences per second the motor will have rotated about 360/60 = 6 degrees during that time. If you send right-centre-right-centre-right ... continually at 60 sequences per second the motor can be expected to run at about half speed. There may be a rate that is too fast for the controller - possibly not. You may have to use

"right - pause X mS - centre - pause Y mS - right - ..." to get consistent speed reduction.


The problem (or a problem) with this system is that the result will be jerky, and more so as the speed is reduced with longer off periods. There are several ** possible** solutions.

• (1) Increase command rate. The maximum pulse length is 2 mS. The typical interpulse length is ~= 20 mS BUT the controller may accept far smaller times between commands. You MAY be able to send say 200 commands/second (5 mS between pulse start edges.) If so then you MAY be able to achieve "smoothish" control over a reasonable speed range. Experimentation is easy - Just use the right- pause - centre pause sequence as above and vary the pauses. Right = (say) 1.1 mS pulse. Left = (say) 1.9 mS pulse. Centre ~~ 1.5 mS pulse BUT you will need to find the exact range of values that allows stopping.

Note that starting and stopping the motor in this way will almost certainly reduce available power as the motor will take a finite time to reach operating speed range.

• (2) Throw away the servo's internal controller. The servo is a geared motor with additional electronics. The electronics are not made to do what you are trying to do with them. Removong the Rpulse decoder and PWming the motor directly would work for you. There will be an H bridge driver in there Bbut it may be inside the decoder IC. You will want to find somebody who understands what is required.

• (3) Buy a geared motor. You are attempting to reinvent a geared motor. These are available at hobby shops and elsewhere. Also lurking inside every cordless screwdriver (usually no H bridge). A geared motor plus drive electronics plus PWM will probably do what you want easier and better.