Scratch GPIO – 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)

Power
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

spower11

Part 3 – using Stepper Motors

You may also like...

39 Responses

  1. Andy Waller says:

    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. Andrew Errington says:

    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?

    Thanks!

  3. Johann Stockinger says:

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

  4. John says:

    Is there some way of setting the motor PWM frequency?

    • cymplecy says:

      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 🙂

  5. Shane says:

    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?

    Thanks

    • cymplecy says:

      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

      Simon

      • cymplecy says:

        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

        Simon

    • cymplecy says:

      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 http://www.boeeerb.co.uk/motorpitx/

      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 🙂

      Simon

    • cymplecy says:

      In direct answer to the question _ i’ve just tried using Motor19 to control the brightness of a LED and it seems to work as expected

      Simon

      • Shane says:

        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

  6. chad says:

    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

  7. chad says:

    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 .

  8. Hi
    I’m a little bit confused. I thought the GPIO pins were only digital and could only be 1 or 0. So how do you control speed as they are not analogue?

    • I don’t know how it does it – but it does it.

      That is clever and rather useful.

      • cymplecy says:

        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 🙂

        Simon

    • cymplecy says:

      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 🙂

      Simon

  9. Nathan Garpiya says:

    Can you Please send me the complete code from when the Flag is clicked? I am using Scratch2 and a bit lost at the new GPIO assignment

    • cymplecy says:

      Sorry – there was a mistake in the title- this refers to using ScratchGPIO (based on Scratch 1.4) – not Scratch 2

  10. Pat says:

    Hello

    Is there a way to conncet the motors without the ULN2003?
    and what is the ULN2003 function?

    Thank You

  11. cymplecy says:

    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 🙂
    Simon

  12. cymplecy says:

    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. 26th April 2013

    […] David Ferguson shows a very cheap way of getting 2 motors connected safely to your Raspberry Pi. […]

Leave a Reply to cymplecy Cancel reply

Your email address will not be published. Required fields are marked *