ScratchGPIO – Add on Modules/Boards

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

Ultrasonic Sensor Boards ultrasonic1pinSingle Pin Ultrasonics – if you connect a cheap 4 pin Ultrasonic Module as per this diagram, then you only need one spare GPIO pin to trigger it and receive the returned pulse

So now you simply use it (assuming connected to pin 23)

Use Sonar23  followed by a wait 1 and then just use the sensor item sonar23 to get the distance measured in cm.  Any pin can be used for this purpose and if you had several of them you could broadcast each of them in turn.

Ultra8Alternatively, you can use this syntax which will tell your Raspberry Pi to continuously do a distance check every 1 second and update sensor item Ultra8 with the results without any further need to do another broadcast

Gordon’s Ladder Board
This cheap and useful educational board (link) is an ideal introduction to getting kids to turns things on and off and saves having to wire up a breadboard to get going.  It does need someone to solder it together and if you don’t have that skill or tools – jsut give a shout out at a RaspberryJam and I’m sure someone will help you out 🙂

LadderAdOnIf you’ve got one of these then just create a variable called AddOn and set it to Ladder as the first line in your Green Flag event. This tells the system that your using one and then you can use broadcasts such as:

ld2This script will simply turn all the LEDs out and then just switch on every other one.

ld3To vary the brightness of a LED, you can use a Power variable like this example:

The state of the switches can be accessed through the sensing blocks.ld4 Scroll down to the bottom and click on the arrow next to slider and you should see switch1 – 4 (they will only appear after you have  run a Green Flag event as above)

MotorPiTx

This board makes it very easy to control Motors and Servos without any extra hardware as well as giving you the ability to switch your Raspberry Pi on and off and power it from AA batteries.

PiGlow

See https://cymplecy.wordpress.com/2013/08/12/scratch-gpio-piglow-support/ for full support info

Compass

CompassYou can plug in a HMC5883L based I2C device and access the magnetic heading value it outputs.  Simply set AddOn to Compass and then use the heading sensor value which is updated 4 times every second

BerryClip
https://www.modmypi.com/berry-clip-raspberry-pi-add-on-board?filter_name=berryclip

This really cheap and useful board is an alternative to @drogon LadderBoard.

It has 6 LEDs, a switch and a buzzer

To use it just set the AddOn variable to Berry and then you have easy methods of accessing it

Broadcasts available
[Led1..6] [On/Off/High/Low] e.g broadcast Led1On will make first red led light up

[All] [On/Off/High/Low] e.g broadcast AllOn will make all LEDs and the buzzer come on.

[Buzzer] ]On/Off/High/Low] e.g broadcast BuzzerOn

Variables
You can use variables instead e.g Set Led1 On will turn on 1st LED
And also use Power1..6 (values 0-100) variables to vary the brightness as

Sensor
There is only one sensor value returned and that is called switch (1 if not pressed – 0 if pressed)

PiRoCon

This board is a complete robotics controller for your Raspberry Pi based mobile robot.

Scratch GPIO Version 2 – Using Stepper Motors

Stepper Motors

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

Stepper Motors Battery_bb

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)

stepperIn your Scratch program, you need to say that you are using Stepper motors instead of ordinary motors this is simply done by broadcasting Stepper in your Green Flag code block.

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.

sma-100

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

Position Control
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.

e.g. To turn 500 steps (approx 180 degrees on my motors) use posa

Note the use of change and not set

Part 4 – Direct Support for External Modules/Boards

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

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)

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

ScratchGPIO – Introduction for Beginners

This post refers to an older version of ScratchGPIO
Please use the new Version 5

This post is being left here as some books refer to the older version

Scratch Controlling the GPIO Pins on a Raspberry Pi
Part 1 of 4
(Version 4 – 20 Feb 2014)

This post is intended to make it as Simple as Pi to get up and running and make your Raspberry Pi control some lights and small motors and to respond to switches and sensors. Minimum Requirements – a Raspberry Pi with Raspbian installed (a working internet connection is very handy but not required)  a breadboard, some Light Emitting Diodes (LEDs), some resistors and some wire connectors. Total cost £5-£10. Blue italics are extra information for those who like to understand things a bit more – Pink italics are for the more advanced users and  can be completely ignored by normal users.

How to get a Raspberry Pi to control the GPIO Pins using Scratch —————————————————————
Your Raspberry Pi needs to be connected to the internet to install the software but not needed to run ScratchGPIO. Copy the text below ( left click just before the s of sudo and drag right until all the text in the line as been selected) then right-click and select copy. Open up an LX Terminal window and select Edit and the Paste that into an LX Terminal window and run it to download the installer.

sudo wget http://goo.gl/dANpKr -O isgh4.sh

Once the installer has been downloaded then just type (or copy and paste the text below as before)

sudo bash isgh4.sh

(If logged in as a different user to standard pi, then  type sudo bash isgh.sh yourusername) This will install all the necessary extra software and some simple examples. (If you do not have internet on your Pi then, put your SD card into a card reader and try using your browser to right-click and save the script direct to your SD card and then put it back into you Pi and run the second instruction) Connecting Components Up ———————————————————————

EXTREME care should be taken when connecting hardware to the GPIO pins. You can cause your Pi to die by connecting the wrong things together – only do this if your confident of your ability to follow instructions correctly 🙂

AT A MINIMUM – get a breadboard and use some female-male 0.1 leads (available from lots of on-line suppliers or your local Maplin shop) Check out GPIO pin guides to make sure you know what pins are what. Wire up Pin 1 (3.3V) to (at least) a 330ohm resistor – connect that resistor to the long lead of an LED and then connect other end of LED to Pin 6 (0V). It should light up. If it doesn’t try reversing your LED. Now move the lead from Pin 1 to Pin 11. Using ScratchGPIO Run the special Scratch icon (Scratch GPIO) on your desktop. (It is actually a completely normal version of Scratch, it just runs a little Python background program as well that handles communications between Scratch and the GPIO and automatically enables Scratch’s Remote Sensor Connections(RSC)) To test out control from Scratch, click on File then Open and then click on the My Projects button and select blink11 and click on OK. Once the project opens, just click on the OK to enable Remote Sensor Connections. To run the script just click on the Green Flag.

blink11Your LED should now blink on for 1 second and off for 2 seconds – see trouble shooting if this doesn’t happen.

What more can I do with Scratch and the GPIO
As it comes, you can control six pins as outputs (Pins 11,12,13,15,16 and 18) and treat all the rest as simple inputs (22,7,3,5,24,26,19,21,23,8 and 10) (GPIO pin numbers/ordering do not follow anything that makes sense to most people so you just have to go with the seemingly random numbering arrangement)

As you can see in the blink11 script , you can simply use a broadcast message telling Pins to go on or off (Up to 3.3V and down to 0V) The valid messages are pinson along with the corresponding pin off messages. pinsoff

You can also say allonoff And you can replace the word  on with high and replace off with low if you want to talk in pure logic levels.

You can combine message together to make a single broadcast so to turn Pin11 and Pin13 on and all others off you can sayjoined oroff11on13on

Alternatively you can use the pinpattern broadcast to achieve the same result e.g:bpinpattern2This will also set just pins 11 and 13 on.

Inputs To check an input, you should go into the Sensing block and click on the word “slider” at the bottom and you’ll notice that you have pins 22,7,3,5,24,26,19,21,23,8 and 10. inputs If you connect a switch to one of these pins (through a resistor don’t forget) to OV, then you can detect when the switch is open or closed. The inputs will normally read 1 and go to 0 when they are connected (through a resistor) to ground. Click on the checkbox next to pin7 and try it out.

Using variables instead of broadcasts
For more advanced Scratchers, you can use variables instead (or as well as broadcast messages) .

For example: create a global variable called pin11 To make pin11 go on or off  use vonoff On can be replaced with high or 1 and off can be replaced with low or 0 so that you can use whatever logic scheme you’d like.

vallonoffTo set all outputs to on or off use

To use a “bit-pattern” to set/unset multiple outputs simultaneously usevpp (this will set Pin 11 , Pin 13,  Pin 16  and Pin 18 on and Pins 12 and 15  off)

vbug3Note – currently there is an unfortunate “bug” in Scratch in that it remembers variable states and only sends changes out.    Even when you press the Green Flag, it will not send the state of all the variables out, it will only send them when a variable changes. I recommend (nay insist even!) setting any gpio variables to an invalid value – say a full-stop and then to their initial state in a Green Flag start-up script.

Need more Pins as outputs?
If you need more than 6 pins to be outputs  then, for example, you can use broadcast pin7on and pin7 will change from an input to an output.

If for some reason,  you need more input pins you can use broadcast config11in to change pin 11 from an output to an input

Further documentation

Part 2 – using Motors and varying brightness of LEDs

Troubleshooting

To test if the software necessary to control the GPIO is correctly installed open a LXTerminal session and type

sudo python blink11.py

If this doesn’t give an error but doesn’t make a LED on Pin 11 blink then we have real problems Houston 😦 Try connecting the lead going to Pin 11 back to Pin 1 to make sure the LED lights up then just in case you have a loose connection.