A new version of Scratch for Raspberry Pi: now with added GPIO

There are many excellent things to be found in last week’s release of Raspbian Jessie and we’ve been keeping one of the best ones tucked under our big Raspberry Pi-shaped hat. In the Programming menu on the desktop you’ll find a new version of Scratch, our favourite programming language for beginners.

Breadboard and Scratch on Raspberry Pi

Connect buttons, sensors, cameras, LEDS, goblin sticks and other gubbins to your Pi using Scratch

Tim Rowledge, who has been “vigorously wrangling Scratch into shape over the last couple of years” (thanks Eben), tells us what’s new:


 

Along with the new Raspbian release we are including the latest Scratch system update. It might have seemed a bit quiet on the Scratch front since March, but lots has happened here in the rainforests of Vancouver Island. There are two primary changes you will notice:

  • a significant increase in speed
  • the addition of a built-in GPIO server.

Speedier Scratch

One of the big projects last year was to modernize the Scratch code to run in a current Squeak Smalltalk system rather than the very old original version; this improved performance a fair bit all on its own, since the newer Squeak benefited from a lot of work over the years. [The Scratch world is created using Squeak, a dialect of the Smalltalk programming language, and Squeak itself runs on a software emulation of a computer system called a virtual machine -Ed.] It also built us a Scratch that could run on the very latest Squeak virtual machines that have dynamic code translation, generating machine code at run-time. Along with work to improve the Squeak code that implements Scratch, we then had a noticeably faster system.

A major project this year has been building such a virtual machine for the Pi; until now, only x86 machines have been able to run this version. With a very generous amount of support from Eliot Miranda – the original author of the Cog virtual machine and all-round software deity – the ARM Cog VM has been whirring away since around June.

Benchmarks are always a nastily slippery subject, but we feel that Squeak performance is typically between 3 and 10 times faster, obviously depending on what exactly one is measuring. Things will get even faster in the future as we iron out wrinkles in the code generation, and soon we hope to start benefiting from another project that does code optimization on the fly; early hints suggest at least a doubling of performance. Since Scratch uses a lot of graphics and UI code it doesn’t always speed up so much; but we already did a lot of graphics speed improvements for prior releases.

Our favourite “scary big demo” is Andrew Oliver’s implementation of Pac-Man. The original release of Scratch on the Raspberry Pi Model B could manage almost one frame per second, at best. The same Model B with the latest Scratch system can manage about 12-15 frames per second, and on a Raspberry Pi 2 we can get a bit over 30, making a very playable Pac-Man.

GPIO

The new GPIO server for Pi Scratch is a first pass at a new and hopefully simpler way for users to connect Scratch to the Raspberry Pi’s GPIO pins or to add-on boards plugged into them. It is modelled on the mesh/network server and uses the same internal API so that either or both can be used at any time – indeed, you can have both working and use a Pi as a sort of GPIO server or data source. We have not introduced any new blocks at this point.

The server also allows access to the Pi camera, IP address and date and time and allows complex functionality. For example, the following scripts (along with a suitably configured breadboard) provide the ability to turn LEDs on and off according to a button, to take a photo with a countdown provided by a progressively brightening LED, and ways to check the time etc.

Examples of using Scratch to control the camera module, control LEDs connected to the Raspberry Pi's GPIO pins, and check the time

Examples of using Scratch to control the camera module, control LEDs connected to the Raspberry Pi’s GPIO pins, and check the time

Add-On Hardware

We can also plug in Pi add-on cards such as the Sense HAT, Pibrella, Explorer HAT, PiFace, PiLite and Ryanteck motor board.

Each card has its own set of commands layered on top of the basic GPIO facilities described above.

Demo project scripts

In the Scratch Examples directory (found via the File-->Open dialogue and then the Examples shortcut) you will find a Sensors and Motors directory; several new GPIO scripts are included, including the one above.


 

Closing notes from Clive

We’re really pleased that GPIO is now built in to the Pi version of Scratch. It means that users can use access the GPIO pins “out of the box,” and so get into physical computing that much more easily. We’ve also introduced the GPIO pin numbering system also known as BCM numbering for consistency across our resources, and having our own version of GPIO support gives us finer control over functionality and support for add-on boards in future.

All of our resources using Scratch will use this version from now on, and existing resources will be rewritten. Tim’s reference guide details all of the commands and functionality, and there will be a simplified beginner’s tutorial along this week.

Last of all, there’s no way I can end this post without taking the opportunity to thank our community who have supported (and continue to support) GPIO in Scratch on the Pi. In particular, a big thanks to Simon Walters, aka @cymplecy, for all of his work on Scratch GPIO over the last few years.

31 comments

Avatar

This is truly brilliant work and I cannot wait to have a play and use it at workshops and in schools. This could revolutionise the way physical computing is introduced to children.

You’ve chosen a few, great boards to have built-in support, but how do we add our own boards/kits/products/libraries. I understand you cannot support every resource and kit built-in, but how can we make it so people can download an extra library that supports our own product? Not that I’m hinting that we’re working on anything ;-)

Avatar

It’s something we’re discussing at the moment, it would be great to have a system that allowed plugins / “libraries” to support add-ons. In the meantime the best thing to do is get in touch :)

Avatar

This is great news – it’s going to make it much simpler to get started with physical computing without having to start a separate application to access the GPIO.

One limitation I see is for more complex add-ons. I am currently working on connecting to Scratch to NeoPixels, IR devices, a robot arm and DCC using the Gertbot, all of which I currently control using Python.

To make this possible I’ve created a Python 3 version of the scratchpy module (most of the code is in Python3 as is the GertBot API) and was looking at working with that. Do you think I may be better implementing these as a Scratch add-on instead of using scratchpy?

If so how can that be done can I get the details of the API? Does the add-on need to be created in Squeak? I don’t have the time to learn smalltalk at the moment, but if this can be coded is something else then I’d be happy to give it a go.

Avatar

The socket based extensions still can be used. There are the same process communcation patterns used in socket based extensions as in the plugin, as there are global variable output/sensor value input and broadcast events out/in.
So there exists a path from small scale, local extension board with spezialized code (socket based in python, flexible) up to a large scale, global, general available board supported by plugin (possibly not so flexible).
For your application, you could consider using scratchClient, which is a modular, extensible framework to connect python adapters for all kind of hardware with scratch. Includes logging, a web interface for monitoring the framework/adapters and a well defined configuration in xml.

Avatar

Thanks
I had assumed that the socket service would still be available, but as I’m still at a fairly early stage I’d rather put my time into making them work with the new GPIO server if that was simple enough.

One of the things that I didn’t like about the socket service is that “allow remote connections” is hidden and counter-intuitive. Getting it to work in presentation mode was a complete nightmare, whereas it sounds like it’s a simple config change for this new GPIO service.

I had looked at ScratchClient before, but was put off as it looks quite complicated for simple projects. In particular I was looking at creating some projects that children could follow and understand the code and telling them they need to start writing XML as well as the coding was adding an extra level of complexity.

I would like to learn smalltalk, but I’m currently studying operating systems and writing code in c using pointers to pointers; trying to learn a new programming language that uses the complete opposite approach at the same time would probably blow my mind :-)

Thanks for the explanation it gives me something to think about. I’m really excited about these changes to Scratch – I think they will make teaching physical computing with Scratch much, much easier.

Avatar

I’m flattered :-)
The add on hardware code has to be written in Squeak/Smalltalk right now and there is the beginnings of a drag/drop mechanism for adding new drivers. That will get expanded for the next release.
Don’t forget that the original socket based network interface is still there and all of the excellent stuff done by SimpleSi, GHP, Pridopia and others will still work. You should even be able to use both systems together.
Learning Smalltalk is always a Good Idea and I commend it to you.

Avatar

Second the thanks to Simon Walters for Scratch GPIO.
Made creating inspirational projects for Primary Schools possible for me.

Avatar

And me – My thanks to not-so-SimpleSi. ScratchGPIO is something that much of my whole “Raspberry Pi” world rotates around. I prototype in Scratch to see what’s possible and it’s great for demoing to people at Raspberry Jams.

I’m going to have to get used to the whole BCM numbering scheme, as are a whole load of students that I’ve worked with. Now… off to re-write those resources!

Is there support for servos using something like Servod? I don’t see it there.

Avatar

I can’t wait until my kids are old enough to start playing with this.

Avatar

This is brilliant. Well done! the new Scratch is horrendous, so to see the 1.x series continue development is really exciting. I am buying two more Pi’s to show my enthusiastic support!

Avatar

This is all great news and the speed increases are very impressive. I have some queries about the motor control however. It looks as if this is only provided through one of the supported add-on boards? I have been using Simon’s ScratchGPIO for a good while now and there are two features I hope can be implemented easily in the new Scratch.
The first is stepper motor control which is very useful. I drive a stepper motor through a ULN2803 chip directly from the GPIO pins. It would be great if I could do this in the new Scratch.
The second pint is more technical. PWM is often fixed at 100Hz or higher and this is fine for things like dimming LEDs but when you want to control DC motors the frequency becomes important; at low power factors such motors simply stall. The solution is to run them at more like 10Hz so that the motor is getting decent bursts of power and it is possible to control down to a few revs per second. I contacted Simon about this last year and he re-wrote the interface to reduce the frequency when power is below 20%.
Is it possible to do these in the new Scratch or if not is it possible they will be impelmented sometime? I think they are both very useful for anyone wanting to control models or robots.

Avatar

micheal, please make sure to add you concerns in a thread on the Scratch forum; that is a much safer place to put requests and suggestions. Better yet, make a github account and open an issue, mark it as a bug or enhancement or whatever and then whenever I get a chance we can discuss it and maybe work out a solution. That goes for everyone, of course.

I’d love to add support for everything we can think of, but it takes thought and effort to work out what is needed. All contributions are welcomed.

Avatar

This is GREAT news! I had been forced to use Lego Mindstorm NXT kits at a high school with very limited resources, and while NXTs are fine for middle school kids, they’re not appropriate for high school students (in the early 1990s, I spent time at the MIT Media Lab with some folks who developed the original prototypes for what became Lego RCX and similar products, and middle school was their target audience even then). The top-end students get to build and compete FIRST Robotics Competition robots (FRC – FIRST is Segway/medical-stent developer Dean Kamen’s For Inspiration and Respect for Science and Technology non-profit STEM education promoting foundation). FRC is aimed at high school students, while FIRST Lego League (FLL) is for middle school age kids, but FRC requires learning Java, C++, or LabView for software control, downloaded from a Windows laptop into a National Instruments RoboRIO industrial microcontroller (actually a giant Field Programmable Gate Array software configured in flash RAM as a dual ARM processor system – pretty slick). The jump from Lego NXT’s visual programming paradigm to Java, C++, or even the GUI-oriented LabView is just too great for most kids to make in one swell foop. I discovered the wonderful Enchanting extended version of Scratch, which adds NXT sensor and motor I/O to Scratch (albeit as yet-another version of Scratch, not a plug-in/add-on-library). When the Pii arrived, I was hoping that exactly this sort of sensor/motor interface functionality via GPIO would be built into Scratch at some point so that I wouldn’t also need to teach the kids (and myself, a not-insignificant effort given everything else on my plate and advancing senility :) how to integrate GPIO into Scratch via yet-more software dangly thingies (that’s the technical term ;)

That the new Scratch is significantly closer to the velocity of a speeding bullet, on a par in power to a steam locomotive, and able to leap tall buildings in a single bound (or three :) even on an Original Flavor Model B is icing on the cherry on top of the whipped cream smothering a bananananana (I know how to spell it, I just don’t know when to stop :) split positioned strategically on a cake made of alternating layers of German chocolate, Boston cream pie, and raspberry tart filling! Is anyone getting as famished as me at this point???

Bravo Zulu, as we say in the Navy, to all who have contributed to this improvement! Bigger! Faster!! Higher!!! To Infinity … and Beyond! :D

Avatar

Jim, step *away from the chocolate*. The nice folk with one of those ever so comfy jackets are even now speeding towards you.

I hope to have the chance to integrate quite a lot of ideas from Scratch variants like Enchanting, Snap, BYOB, YMMV, etc. The more people express their ideas and requests in places like the forum and github issues the more likely it is to happen. Convince the relevant Mighty Overlords that your idea is important and they will issue appropriate orders to have me flogged until it is implemented.

Avatar

Yet another great development from the foundation, Scratch working with GPIO ‘out of the box’ AND with AddOns and 3rd party HATs is wonderful news. This combined with the new Jessie distro makes the RaspberryPi – regardless of model a powerful development board and an educational resource. Big up to Clive for honouring community support AND Simon Walters work on ScratchGPIO – that might even warm Si’s cockles :P

Avatar

Thanks for all of the comments and queries. As with all the stuff we make, we love feedback and suggestions and community input — the best place for this is our Scratch forums so pop along for a chat!

Avatar

Great news. Can Scratch be upgraded on Wheezy without going to Jessie? We have all our RPi installations on 4gb cards which are too small for the full upgrade to Jessie, but would like to have the latest gpio ready version of Scratch. Any details of how to install it would be appreciated.

Avatar

It should be no problem since it was all developed on wheezy; in fact I hadn’t tried it in jessie until very recently.

To the best of my knowledge a plain old `apt-get install scratch ` ought to do it but I’m no expert in that area. There may be other things you need to install – wiringPi and a new alsalib for example – and I don’t know if all that is currently automagic.

Avatar

I’ve already tried ‘sudo apt-get install scratch’ which reports that I already have the newest version, when I don’t (there’s no start gpioserver option in the Edit drop down menu and no gpio examples in the examples folder). Are there any other ways to install this new gpio-ready version of Scratch without installing Jessie please?

Avatar

Anyone?

Avatar

Looks like we’ll be sticking with Simon’s Scratch GPIO then.

James Robinson

This can be done with:

sudo apt-get update
sudo apt-get upgrade

Sorry your comment went unanswered

Avatar

When will Scratch on the Pi catch up the version that runs in the browser? Obviously this is indeed a good thing, but ideally we’d want one that matches the one on their site.

Avatar

Can you tell me the version number?
Is it 1.4?
It looks that, so far, version 2 is not going to run (soon?) on raspberry…
SdP

Avatar

So awesome I just made a photo of myself. I am running on kano in my piclub but with this I am switching to jesse!

Avatar

Awesome to have GPIO support in Scratch out-of-the-box! BIG THANKS for making this happen!

One question, if I may: Is the source code for this accessible somewhere?
(Background: We have been using SimpleSi’s awesome solution so far. Hacked for special selfmade hardware, like e. g. extensions to the Pi2Go. …)

I full-heartedly support the idea of a simple interface for adding self-made plugins or addons, as mentioned earlier in this thread. This would make tinkering/making with Scratch an even greater fun experience :-))

Assuming, though, that this is a mid- or maybe even longer-term development exercise, here’s my question: Where/how can I start hacking in the meantime? :-)

Thanks to everyone involved,
troubadixx.

Avatar

Hi,
I really like the new GPIO support in Scratch.
One question: is there any support for LCD like Hitachi HD44780 in Scratch?
Pi_Scratch is supportig these displays, but it is much less comfortable than the new out-of-the-box GPIO support.

Avatar

I have a raspberry pi 2b, did apt-get update and upgrade and don’t see evidence of scratch having gpio support. how do i verify this? i was able to install scratchgpio and have some luck with the led’s but not with the pushbuttons. I’m trying to help a classroom get on their feet with this so any help is much appreciated, thank you!

Avatar

You’re replying to an old post that isn’t monitored – could you please ask your question in the forums?

Avatar

I was just wondering whether you could input audio using a usb mike and work with that to make a Personal assistant app with the scratch which could use gpio at the same time of a reply which would look like those old star trek computers,

Avatar

Hi, I have a new Jessie installation on RPi3 and have done the upgrade and update stuff. I am trying to use the Astro Pi SenseHAT in Scratch without success.

I am logged on as ‘pi’ and I can open Scratch and play the demo games, but when I try to run the SenseHAT examples, Scratch closes. I can run the python demos code examples quite happily in Idle 3 so it doesn’t seem to be a hardware problem

Leave a Comment

Comments are closed