Scratch GPIO Version 2 – Using Motors

Scratch Controlling the GPIO Pins on a Raspberry Pi
(Part 2 of 4)

Motor Control

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. motor_pinout 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.

e.g To make MotorA go at half speed use  ma50

To make MotorA switch off use ma0

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


Part 3 – using Stepper Motors

37 thoughts on “Scratch GPIO Version 2 – Using Motors

      • NO!
        A motor can very easily become a voltage generator and DESTROY your Pi.
        UNDER NO CIRCUMSTANCES should a motor be directly be connected to a Pi.
        You can use other circuitry but the ULN chip is about the cheapest ways to do this 🙂

      • As well as protecting your Pi from being destroyed, the ULN chip also easily allows you to provide power from a seperate battery supply which can be quite useful as motors take a lot of current at times and batteries are an ideal source of power for this

  1. Hi Si.
    This is a brilliant way to teach computer programming in a school (or to anyone really). The flow chart programming takes away lots of silly nagging problems that can arise when learning to code, such as syntax errors, etc. This should remove a lot of frustration for beginning programmers and get them seeing results quickly. I think it’s great, bring on more of it! I’ve just written a module about teaching this for a university assignment. Very viable in my opinion.
    Thanks from South Australia! Great work.

  2. Hi there,

    I am guessing that this motor control function uses PWM on the Pi’s GPIOs.

    I want to use a modified servo for my Pi robot wheels. Does gpio2 support this? Sorry, I haven’t looked at the source code yet.

    I know I can do this with the servoblaster code. Is it easy to get Scratch to write a value to file on the Pi, to control servoblaster?


  3. Very nice!
    Would it also be possible to connect a tilt sensor and a distance sensor?

    • If your an expert, you can play around with how I call the PiZyPWM class (line 448) Search for PiZyPWM to find where I call it from motor and power variables. If your not an expert and need me to breakout the parameters – get back to me 🙂

  4. Hi
    We are 3 dads trying with very limited knowledge to help our children build a telepresence type robot and were overjoyed to discover scratch gpio

    We need to run 2 motors both forward and backwards with variable power and as you do not yet have servo support we are thinking of using a stepper motor for tilt, however we can only get motor11 and motor12 to act as a variable all the other pins just seem to be on or off

    Have you any suggestions as to what we could be doing wrong?


    • I’ll look into that for you as I intended all pins should be addressable as motor pins but since I’ve only used 11 and 12 maybe there is a mistake somewhere


      • I’ve had a quick look at the code (on gIthub as all my RPi are packed for tomrrows RaspberryJam at Manchester MadLab) and the code shouldn’t restrict any pin from responding to a MotorXX command – I’ll try it tomorrow


    • Got my brain in gear a bit more now 🙂
      A bit of history – ScratchGPIO was designed to to simple stuff like on/off control of GPIO pins and then PWM control added to vary the brightness of LEDS or control the speed of a DC motor via a buffer chip.
      And I added in the UltraSonic code as it was a great value for money winner.

      Then I added in Stepper Motors as they were fantastic value for money and are bi-directional (but a bit slow but ideal for desktop use in class )

      But to control motors backs and forwards requires external H-Bridges and the only configuration currently supported for that is my mate Jason’s #MotorPiTx board

      If you could use the same Pin outputs for your motors (or buy a board from Jason) then you wouldn’t have to use a Stepper – you could go back to using servos as the MotorPiTX uses them 🙂


      • Have been looking at the MotorPiTX, at the moment we are trying to build from things we have scavenged from old appliances etc and the pi of course but it probably will solve a lot of our problems
        Thanks for trying out the pins we must have done something wrong we were very excited to get it working as it was

  5. Hi,
    I’m really keen to follow your tutorials, but I am a noob at all this wiring etc. Also I’m not sure what hardware I need to buy too :/

    It would be much appreciated if you could get back to me with some help..

    I can’t wait to try the motors and LEDs

  6. I understand the connecting RPI to UNL.. but I dont understand connecting the motors and battery pack to the UNL.. it says pin 16 goes to motor 1, is this the pin named COMAND IS 15 GND and if so what is pin 9. On GPIO it’s counting across. But labels on UNL seem to be different .

      • It certainly is useful:) So you can use MotorA or MotorB to control pins 11 or 12 (just a default and to make it easy for young Scratchers) or you can use Motor 3..26 to do it on any pin or you can use alternative Power 3..26 which is more appropirate language when controlling brightness of an LED for instance

        PS You’ll get flickering at Power values of less than 30 or so as the PWM software process can’t guarantee that your RPi might decide to go and make a quick cuppa in the middle of things 🙂


    • Basically if you switch the pins on and off fast enough, and vary how much time is spent compared to how much times spent off then you effectively vary the power going out on the pins – its called PWM (Pulse Width Modulation). The RPi is only set to d othis one one pins but clever people (not me) have come up with Python libries (RPi.GPIO) that I can tap into and use to make it do it on any pin you like 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s