The normal operations of a motor are:
1. Spin clockwise
2. Spin anti-clockwise
3. Free wheel (ie coast to a halt)
4. Brake (ie try to stop immediately)
Other variations are that we probably also want to control the speed of the motor as well as the direction (clockwise/anti-clockwise) so that its not just 'full steam ahead' or 'full steam in reverse'. This always creates a challenge - as the more control you require tends to increase the number of precious output pins that you require for each motor. So we need to maintain flexibility whilst minimising the number of processor pins.
As a result - here is my 'tri-state' switch which helps to minimise the number of pins required and is used by a number of the different motor controllers that follow.
This diagram shows that each motor requies a 2 pin connection to your micro-controller via JP1, and provides 3 outputs to the motor controller stages.
Pin 1 - is used to Enable or Disable the motor controller chip. Whenever the controller is disabled then it is in 'coast' mode. So if we ask the motor to go in a particular clockwise direction then we can use this pin to set the speed that it turns. If it aways high then the motor rotates at full speed. If it is always low then the motor is disconnected and so doesn't turn. By using PWM we can control the speed of the motor from 100% to 0% duty cycle. So pin 1 is the 'throttle' or 'accelerator'.
Pin 2 - Sets the direction of the motor - Forward or Reverse. If this input is high then Input 1 is high, which turns on the transistor so Input 2 is low. This makes the motor turn one way. If this input is low then Input 2 is low, the transistor is off and so input 2 is high, and the motor turns the other way. But the cool thing is that if this pin is disconnected then Input 1 and Input 2 are both in the same state which means that the motor will brake. How do you 'disconnect' a wire that is soldered in - all we do is change the micro-processor pin to be an input pin and the built in resistors make this wire 'disconnected'.
So here is a logic chart
|High Output||High||Rotate motor clockwise|
|High Output||Low||Rotate motor anti-clockwise|
The transistor Q1 does not have to be a BC108 it can be ANY general purpose NPN transistor.
This circuit is utilised by many of the forthcoming examples so lets come up a price list for ONE motor now:-
2 x 10k 0.25w resistors
1 x 1k 0.25w resistor
1 x BC108 or general purpose NPN transistor
Price wise:- its less than $1