Scratch controlling Shrimp/Arduino

Note 01Feb133 -V0.50 Radical code review and full supportfor 2 stepper motors

Following on from my worldwide (Well North-West England anyway) success with my Scratch GPIO package, I wanted to achieve the same end result with Scratch and a Shrimp/Arduino. This would give the same end result (Scratch interfacing with LEDs,switches, sensors etc.) but at much lower cost per classroom workstation.

Whereas an RPi setup requires quite a bit of extra hardware(PSU or battery, WiFi and either Screen/Kbd/mouse or less-satisfying VNC experience) the Shrimp/Arduino set-up only requires the Shrimp or Arduino to be plugged into an existing computer. And with a complete Shrimp costing £5, then financially is streets ahead.

There are existing Scratch modifications (S4A is best known) but they are “hard-coded” modifications of Scratch itself and I wanted to use standard Scratch 1.4 and get a background program coded in Python to handle communications between Scratch and a Shrimp/Arduino. There is an existing protocol called Firmata which allows for simple commands to be issued over a serial link and I’ve used the Pyfirmata library in Python to do the heavy lifting.

At the moment, the whole project is in alpha status and this is where I’ve got so far

First video of it in action

And Additional video of it running on a Win7 64bit school machine controlling a ShrimpingIt Shrimp 🙂

Video 3:  Scratch accepting analog input

(Current version has temp dropped analog input because I’m getting a few errors – prob  need to sample it a lower rate)

V0.35 now has PWM/Motor control 🙂

V0.36 now has servo control

Demo of a possible programming problem for pupils

How to setup your computer to do this

You need a Windows PC (tested on an XP machine – should work on Win7 – should work on Linux with minor mods but not yet coded or tested) and a Shrimp or Arduino Uno.

There area lot of steps to go through if you’ve not installed it before so please be patient 🙂

Install Main Arduino code and get your Arduino driver up and working (or your Shrimp Driver if using one instead)

First of all get Firmata uploaded and working on your Shrimp/Arduino

Start the standard Arduino program and upload the StandardFirmata sketch from Examples -> Firmata -> StandardFirmata.

Use the Firmata test program

to make sure your PC is talking to your Shrimp/Arduino OK.

Then you need to install Python 2.7 on your computer.

(No instructions for that as plenty out there on T’internet)

Then we need to install the PySerial package

This is from

Install pySerial as follows:

Download pySerial from – click on pyserial-2.6.tar.gz to download the library.

Download and install 7-Zip if you haven’t already got something to unpack .gz files

Run 7-Zip (Start -> All Programs -> 7-Zip -> 7-Zip File Manager).

Open the pyserial-2.6.tar.gz file with 7-Zip (File -> Open).

Double click the “dist” folder.

Single click on the “pyserial-2.6.tar” file and click the “Extract” button at the top of the window.  When asked where to put the file, specify c:\Python27\Lib\site-packages\pyserial-2.6

Exit from 7-Zip

Now that you have downloaded pySerial, install it:

Open a command window (Start -> All Programs -> Accessories -> Command Prompt) and type into the command line: cd c:\Python27\Lib\site-packages-\pyserial-2.6

Install pySerial by typing this command: c:\Python27\python.exe install

Now we need to install Pyfirmata as well

Currently, you’ll need to download a very slightly modified copy of PyFirmata but hopefully I can get the mod into the master source code shortly.

(Click on the Zip link button) then unzip. (as an example lets assume you unzip it to e:\pyFirmata-master)

Run a cmd window and change your path to the download location

cd pyFirmata-master

then add the library to your python install
c:\Python27\python.exe install

Download my Python Scratch_Arduino_Handler alpha script V0.46

We can now run it (I press F5 from IDLE myself)

The program will attempt to identify a working Arduino or Shrimp programmed with StandardFirmata and use that Com Port – please let me know if it doesn’t work for you (and you can edit the code to manually set the port for your system if needed)

Launch Scratch and enable Remote Sensor Connections by
right-clicking on the () Sensor Value block, found in the sensing category and then selecting the “Enable remote sensor connections” option.

You should now be able to blink the Arduino on-board LED (or connect a LED/Resistor to your Shrimp) and run the following simple Scratch script

When Green Flag is pressed
broadcast pin13on
wait 1
broadcast pin13off
wait 1

to get yourself a blinking LED

V0.35 now lets you vary the outputs of pins9,10 and 11 using PWM

1st off create a variable called motor11 then run the following Scratch script

When Green Flag is pressed
set motor11 to 0

When Spacebar pressed
repeat 99
change motor11 by 1
wait 0.1

You should see an LED on pin11 go from off to full brightness over a 6 secs interval (LED brightness is non-linear so effect most noticeable at low values)

You can control servos connected to Pins 7,8,12 and 13.
Simply create a variable called say, servo12 then run the following Scratch script

When Green Flag is pressed
set servo12 to 0

When Spacebar pressed
repeat 10
change servo12 by 10
wait 1

if you’ve connected the control wire for a servo to pin 12, then it should rotate to starting position and then on spacebar just turn 10 times

Stepper Motors
The setup supports up to two unipolar stepper motor3 (5 wire ones) using pins 6,7,89 for steppera and pins 10,11,12 and 13 for stepperb (Red common wire connects to Gnd)

When Green Flag is pressed
set stepperb to 0

When u key pressed
change stepperb by 10

When d key pressed
change stepperb by -10

This code will let you speed up and slow down a stepper connected to pins 10,11,12 and 13  (and go into reverse if stepperb is negative)

Advanced users:  the delay between each step is set to 0.005 secs.  You can adjust this by creating a Scratch variable called stepdelay and set its value to smaller than 0.005 to speed up your motor but beware that if you use too small a value, the motor will mis-step.

Good Luck 🙂

Version changes

Note 31Jan13 -V0.46 StepperB added and slow data input to 1 per 0.1 secs

Note 31Jan13 -V0.45 FIx PWM regression 🙂

Note 29Jan13 -V0.44 auto detect com port,steppers,servos etc 🙂