Controlling Motors With A Microcontroller

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
1
14%
5
6
86%
 
Total votes: 7

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Controlling Motors With A Microcontroller

Post by Benj »

Image

Microcontroller devices are capable of supplying small amounts of current to drive small low power circuitry but are unable to provide the power to drive high voltage or high current devices. This document deals with taking signals from the microcontroller and using them to drive up to two D.C. motors. The motor’s speed will be controlled by a PWM output. The motor’s direction will be controlled by a standard digital output pin. The device used to boost the power going to the motor is a L293 motor driver chip which is available from Rapid for £3.19, order code 82-0192.

Example of the L293 connected to the microcontroller and the output terminals with optional Bi-colour LEDs
Image

The digital output pin controls the direction of each individual motor. When the output is high the motor will be powered when the PWM signal is low. Alternatively when the direction output is low the motor will be powered when the PWM signal is high.

Example of how the direction and PWM output pins effect the motor voltage
Image

The example Flowcode file uses a PIC16F877A microcontroller with digital outputs RA0 and RA1 for the direction controls, RC1 and RC2 for PWM controls. Following are some example settings and the predicted motor output power.

Example of how to modify the direction and speed of a motor
Image
Attachments
Motor Control.zip
(196.86 KiB) Downloaded 1117 times

Woode
Posts: 10
Joined: Fri Dec 26, 2008 4:11 pm
Been thanked: 2 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by Woode »

Hiο»Ώ
I have just seen the article on ‘Controlling Motors with a Microcontroller’ and would like to point out the following:-

Taking Motor A, if both IN1 & IN2 are both 0 or 1 this is a ‘Fast motor stop’. A ‘Slow motor stop’ or free running stop is
where no voltage is applied to the motor and it can generate a back EMF as it is slowing.

A PWM motor drive normally lets the motor free run between pulses.

The L293D data sheet I have does not explain this but if you look at the sheet for the L293B-E it does.
(See http://www.Rapidonline.com again)

Even this sheet does not give the configuration for a free running PWM drive, it shows the ‘Enable’ being used as a
motor on/off.

The ‘Enable’ needs to be the PWM signal and IN1 & IN2 a direction and a NOT direction signal. Unfortunately this
requires 3 signals not 2, but the motor will then free run between the PWM pulses. :cry:

At slow speeds I would think that the breaking effect of a fast motor stop would be very noticeable.

Note:- The EBO11 Power Board, that uses L293D’s, cannot be used for a free running PWM drive.

Note of general interest:- The back EMF of a free running motor is proportional to the speed. This is used by some
control systems to adjust the pulse width so that the speed stays virtually constant under varying load conditions - a
“Feed Back” controller.

Woode
Posts: 10
Joined: Fri Dec 26, 2008 4:11 pm
Been thanked: 2 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by Woode »

Further to my last post on this subject (25 April 2009)
I have been doing some experiments and found the following:-

If you take IN1 and IN2 through diodes to a pull down resistor and connect the EN1/2 terminal to this instead
of the supply you can get the truth table below:-

IN1 -- IN2 --- Function
0 ----- 0 ----- Slow Motor Stop
0 ----- 1 ----- Forward
1 ----- 0 ----- Reverse
1 ----- 1 ----- Fast Motor Stop

So you can get the best of both worlds with only 2 control signals per motor. (Not 3 as I originally said)

Watch this space for a 4x bi-directional PWM DC motor drive with short circuit protection, using a ECIO28 that I am
developing.

PS. SGS Thomson Application note AN240/1288 is worth a look. (st-an298.pdf)

Bgraham
Posts: 14
Joined: Fri Dec 07, 2007 10:50 pm
Location: Springdale Arkansas USA
Contact:

Re: Controlling Motors With A Microcontroller

Post by Bgraham »

Ok, I have read a lot of post, seached the internet and even read a book, and I still am not sure how to set flowcode to controls motors. I really do not know which outputs go to what on the motors or if the pwm have any output or what.

I guess I need some on the basic understanding of the pwm works. I can see the icon, enable both or one, and even change the Duty cycle, but what do I have to hook up to to get the motors to move?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by Benj »

Hello,

The PWM output pins are normally marked as CCP1 and CCP2 on the chip diagram representing index values 1 and 2 on the PWM component.

To drive a motor from a single PWM channel you simply need the PWM pin to go through some kind of amplification, say a npn transistor before going through the motor and then to ground.
motor.jpg
motor.jpg (10.18 KiB) Viewed 18487 times
If you need full directional motor control then you will probably need two PWM signals and also two direction signals as shown in the diagram at the top of the topic. These signals then drive the motor with a standard H bridge configuration.

Bgraham
Posts: 14
Joined: Fri Dec 07, 2007 10:50 pm
Location: Springdale Arkansas USA
Contact:

Re: Controlling Motors With A Microcontroller

Post by Bgraham »

Benji

Thanks for the input. Should I be able to see this output of CCPx as a waveform on a scope? Also when I run a sim should the pins that match the CCPx light up a LED on the screen?

Thaks

Bill

Bgraham
Posts: 14
Joined: Fri Dec 07, 2007 10:50 pm
Location: Springdale Arkansas USA
Contact:

Re: Controlling Motors With A Microcontroller

Post by Bgraham »

Benj

I have tried to down load the motor control zip file and my computer gives me and error, that the file is bad or corrupted.

Just wanted to let you know

Bill

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by Benj »

Hello Bill,

Thanks for letting me know about the file, I have re-uploaded it and it seems to be ok now.

Yes you should be able to see the output waveform on a scope similar to the waveform shown by the Flowcode PWM component when simulated. As far as I know the chip diagram and LED components etc connected to the output pin will not respond under simulation. As far as I am aware simulation of the PWM is limited to the PWM component only.

Bgraham
Posts: 14
Joined: Fri Dec 07, 2007 10:50 pm
Location: Springdale Arkansas USA
Contact:

Re: Controlling Motors With A Microcontroller

Post by Bgraham »

Ben

I have gotten the PWM to work, I also had my students breadboard the L293 and that work great. I worte a program that was to make the motor move in the clockwise direction and if a switch was press then the motors would stop. I did this in marcos. I trun on two outputs to go forward in the macro but as soon as the program jump to go to the check input marco the outputs weny off. Is this normal, or is threr something else I should to lock the outputs on untill the stop macro.

Bill

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by Benj »

Hello Bill.

When reading the inputs are you reading the entire port including the motor control output pins? If you are then all the pins will be converted to input pins and therefore any output logic level will be lost. You will probably need to use the input icon with masking or in single bit mode so you do not convert the output pins back to inputs.

ethanehunt
Posts: 1
Joined: Thu Nov 17, 2011 8:11 am
Contact:

Re: Controlling Motors With A Microcontroller

Post by ethanehunt »

Servos are DC motors with built in gearing and feedback control loop circuitry. And no motor drivers required. They are extremely popular with robot, RC plane, and RC boat builders. Most servo motors can rotate about 90 to 180 degrees. Some rotate through a full 360 degrees or more.

However, servos are unable to continually rotate, meaning they can't be used for driving wheels, unless they are modified (how to modify), but their precision positioning makes them ideal for robot legs and arms, rack and pinion steering, and sensor scanners to name a few. Since servos are fully self contained, the velocity and angle control loops are very easy to implement, while prices remain very affordable. To use a servo, simply connect the black wire to ground, the red to a 4.8-6V source, and the yellow/white wire to a signal generator (such as from your micro-controller). Vary the square wave pulse width from 1-2 ms and your servo is now position/velocity controlled.

Woode
Posts: 10
Joined: Fri Dec 26, 2008 4:11 pm
Been thanked: 2 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by Woode »

In the past 10-11 years a lot has changed in the world of electronics. Servos, Stepper Motors, Brush Less DC motors and Arduino computers etc.

I never completed my PWM project because of the difficulty of providing overload/short circuit protection for the L293/L298 H bridges. There is now another H bridge that solves this problem, a DRV8871 module. The modules are short circuit protected and have the over current limit set to 2A but this can increased to 3.6A by changing an on board resistor. They have a voltage range of 6.5V to 45V so this should be enough for almost all applications. The module is 20x24mm but is slightly more expensive than the L293/L298 H bridges but well worth it.

The DRV8871 modules only need 2 PWM input signals, forward and reverse, both low and the motor will free run, both high and the motor will brake, similar to the L293/L298 H bridges but without the third input terminal.

If there are not enough PWM outputs, an electronic changeover switch like a DG419 or NC75B3157 can be used to switch one PWM signal between the Module’s two inputs.

Short circuit/overload indication can be provided by a simple circuit that does not use any computer I/O, it is a shame that the module does not include this circuit. It works by comparing the input and output pulses, if there is a short/overload there will be no output pulse, so a light is turned on. The capacitor stops it flickering at low duty cycles. Built on to Vero board it is about 20x27mm.

PWM frequency should be in the range of 50-100Hz for the best results. The higher the frequency the more it is like DC and the advantages of a PWM drive is lost.

Happy programming to all.

OL Ind Comp.jpg
OL Ind Comp.jpg (68.46 KiB) Viewed 10023 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling Motors With A Microcontroller

Post by medelec35 »

Woode wrote:I never completed my PWM project because of the difficulty of providing overload/short circuit protection for the L293/L298 H bridges.
What I would do is choose a suitable resistor for current sense that will give a suitable voltage drop, when current limit reached.
The voltage across current sense resistor is fed into ADC.
Then when ADC goes above a preset value, PWM is changed to 0, until a reasonable time has elapsed or power is recycled.
Martin

Post Reply