WARNING: NO MOTOR SHOULD EVER BE CONNECTED DIRECTLY TO A RASPBERRY PI
However, if you would like to build a neat little vehicular robot then you can do so by connecting up a pair of cheap Stepper Motors through a couple of very cheap ULN2003s.
The cheap 5V steppers that are widely available (BY28) don’t turn very fast (maximum about 12 RPM) which actually makes them idea as beginners motors as your vehicle won’t run away during testing 🙂
These motors have 5 connections, 1 for power and the rest to control the stepping. (Their technical name is 5 Wire Unipolar Stepper Motor)
To use them in Scratch, you need to connect them up as follows
StepperA should end up (via the ULN2003 buffer of course) being controlled by pins 11,12,13 and 15 – Stepper B is via pins 16,18,22 and 7.
Pin 9 of the ULN2003s should be connected to +ve on the motor battery pack.
(The battery +ve MUST NOT be connected to any pins on the Pi)
You can then simply using variables MotorA and MotorB as before with simple DC motors but this time they will each control 4 pins at a time.
The advantage of using stepper motors is that they can be just as easily stepped backwards as forwards. So to make MotorA go backwards at full speed simply use set MotorA to -100
As well as treating the stepper as a continuously rotating motor, you can simply tell the stepper to change its “position” by a number of steps.
Note the use of change and not set
Advanced Stepper Control
Stepper motors need a delay between each step – this is currently set to 0.003 secs. You can control this value by using a variable called StepDelay.
You can also change the type of stepping mode from 2Coil (default value that gives maximum turning force) to 1Coil or HalfStep