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.
e.g To make MotorA go at half speed use
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
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.
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?
I’ve not added servo support since its not in the default Raspian image, as soon as its in the default image then I’ll add it in
Would it also be possible to connect a tilt sensor and a distance sensor?
Well it already hanldes an Ultrasonic Distance Sensor – I don’t have a tilt sensor – which one were your thinking about?
Fine that it supports already an Ultrasonic Distance Sensor. Can I find a documentation somewhere? For the tilt sensor, I was thinking about a simple one like the one, which is part of the Lego WeDo Set. I think it is of the type which is described here:
http://wp.me/p2C0q1-6T for ultrasonics
Here is another simple 4-way tilt sensor:
I think anhting that just provides a switched output will work fine connected to one of the input pins – might just need a few resistors
Is there some way of setting the motor PWM frequency?
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 🙂
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 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 🙂
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
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
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
Are you ChadRpi from the forums?
Indeed I am,
Sorry about all the question.
I’m just really looking forward to using motors and LED’s and your project here looks like the sort of thing that I’d be able to do, once I manage to set it all up..
My main problem is the hardware setup. I’m fine with the coding but hardware not so much,
Would this be enough for the setup?
Also does your project require any soldering as this is a no go area for me to be honest.
That’s a good kit to start playing around with and needs no soldering – it’s what I started off with 🙂
Soldering starts to be needed when you want to DIY with motors as they don’t normally come pre-wired although you can get away with twisting some wire to connect them if your prepared for them not to be too reliable 🙂
Just note to NEVER connect a motor direct to an RPi-you need buffer electronics 🙂
Ok. I’ll order that kit shortly. What motor would you recommend I buy ? What ones are you using when you talk about then using scratch .. are they ones that you can just twist wires with ?
The kit I’m going to order. Has all the essentials to use the LED and motors has it ?
You need to buy a ULN2003A from CPC and a 6V geared motor from 4tronix. Twisting wires is just a bodge to allow you to play and learn. 🙂
Please remember that certain types of mistakes in wiring WILL damage your RPi. 🙁
So 6v geared motor with dual shaft at £2.50 from 4tronix..
And also that eBay kit..
Is that all I need to follow your tutorials ?and I can simply just twist the wires instead of soldering …
Thanks for your help
The twisting of wires IS NOT the right way of doing it but given your budget and no soldering then it’s pretty much the only way.
Its a workaround, not a recommendation 🙂
Ok thank you, that has helped a lot.
I don’t suppose you have an actual image of the hardware setup ? Apart from the digital images.
I notice there is an image at the top of the page .. is that for any of the one of the setups ?
My second article should help you with that http://cymplecy.wordpress.com/2013/04/22/scratchgpio2-using-motors/
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 .
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.
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 🙂
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
Sorry – there was a mistake in the title- this refers to using ScratchGPIO (based on Scratch 1.4) – not Scratch 2
Is there a way to conncet the motors without the ULN2003?
and what is the ULN2003 function?
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