Scratch GPIO – Using Motors
Scratch Controlling the GPIO Pins on a Raspberry Pi
(Part 2 of 4)
Note: Connecting a motor directly to a GPIO pin WILL BREAK YOUR RASPBERRY PI!!! So don’t do it!
David Ferguson supplied the diagram below which shows a very cheap way of getting 2 motors connected safely to your Raspberry Pi. The chip used is a very cheap one(37 pence) called a ULN2003. Wire pin11 of RPi to Pin1 on ULN2003 and then RPi pin12 to ULN2003 pin2 and finally RPi Pin6 (Gnd) connects to ULN Pin8
Then ULN2003 pin16 goes to one motor, ULN2003 pin15 goes to other motor.
The other connections on each motor are both connected to ULN Pin 9.
Finally connect a separate motor power supply (I use a 4xAA battery pack to give 6V) and CAREFULLY connect +ve to the ULN2003 Pin 9 and -ve to ULN2003 pin 8)
Once you’ve got your motor(s) SAFELY connected up, to control a motor, it is usually required to be able to not just switch it on or off but to vary its speed.
To do this in Scratch you can create 2 special variables called MotorA and MotorB and then assign these values from 0 (off) to 100 (fully on)
MotorA controls pin11 and MotorB controls pin12.
Although MotorA controls pin 11 and MotorB controls pin12, you could have used variables called Motor11 and Motor12. In fact all the pins on the Raspberry Pi can be controlled in this way – to use a motor on pin 13 you would simply crate a variable called Motor13 and set that from 0-100 to control the speed
(Remember – pins on the Raspberry Pi must NEVER be connected directly to motors – always go through some sort of buffer electronics)
As well as using the term “Motor” you can use the word “Power” instead. Using “Power” makes more sense if you are just controlling the brightness of an LED. To make an LED go from off to full brightness try this