Tuesday, 11 July 2017

Interfacing the Explorer HAT Pro with Scratch

Looking for a way to get started with younger children in interfacing simple electronic circuits to the Raspberry Pi with Scratch, I learned that the Explorer HAT Pro from Pimoroni is supported in Scratch. Once everything is set up and configured this provides a great beginners platform for driving LEDs, buzzers and motors from Scratch programs, and enabling sensors and inputs to be read from within Scratch. Perfect for little coders in my school code club and protecting the Pi from damage due to circuit design errors. But getting this working in the first place was not straight forward, so here is my guide to setting it all up and getting started with some Scratch examples.

This post is very much a work in progress as I learn more and uncover new information. So I plan to regularly update it as I learn more. But I am publishing it now because I think it already brings together a lot of information that I have found was not easy to search for online.

Scratch 1.4 (Nu Scratch) in Raspbian Jessie can control the built in LEDs, the 5V inputs and outputs, and the motor drivers on the Explorer HAT Pro. But it cannot detect the capacitive touch inputs. The HAT was also a bit temperamental in starting to work with Scratch. I had to open and run the example from Motors and Sensors first before it started behaving. I am not sure that example contains anything my template does not, or whether it just needed a project closing and reopening to sort it out. More testing needed. I started a Vanilla Scratch project template to show how to do everything. One problem is that the ways to switch on the outputs and LEDs do not all appear to work. I can switch LEDs on and off using variables, but not the outputs (which only appear to respond to broadcast messages).

The instructions which cover how to use Scratch 1.4 with GPIO and various supported add on boards can be found here: https://www.raspberrypi.org/documentation/usage/scratch/gpio/README.md

I have uploaded my first Scratch 1.4 template project if you are interested in using it:
ExplorerHAT Vanilla template1.sb

The capacitive touch inputs are supported by Scratch GPIO Plus 8, which can be installed from:
http://simplesi.net/scratchgpio/scratch-raspberrypi-gpio/

This appears to use a slightly different approach to controlling things (setting specially named variables values to turn things on and off). I still need to experiment more to understand how they play together. Some examples can be seen here: http://simplesi.net/explorerhat/
I had to reboot the Pi after installing Scratch GPIO in order to get them to work. I found it temperamental and did not work after I shut down Scratch and reopened it.

Camera support is covered here: https://www.mattvenn.net/2014/12/16/using-a-camera-with-scratch-on-the-raspberry-pi/

Support for capacitive touch sensors is also covered in this article: http://www.pawfal.org/dave/blog/tag/scratch/

Worksheets from NI Raspberry Jam covering Scratch ExplorerHAT Pro here: https://github.com/NIRaspberryJam/Raspberry-Jam-Resources/blob/master/Sections/Explorer-HAT-Getting-started-Level1/

New! Scratch 2,0 offline support on Pi! This example and tutorial covers how to add custom blocks into Scratch to interface to JavaScript, which in turn calls Python code. This should enable support for any add-on boards to be written using their Python libraries.

Following a query I posted on the Scratch Forum I was pointed to http://heppg.de/ikg/wordpress/?page_id=6 ScratchClient, and implementation of a Scratch 1.4 and 2 interface to Python, with lots of adaptors already written for add on boards, and detailed documentation on how to extend this with your own adaptors. Lots more learning ahead!