Sense HAT Scratch control
Les Pounder shows how to obtain data and control sprites using the Sense HAT add-on board with Scratch 3.
Les Pounder shows how we can obtain data and control sprites using the Sense HAT add-on board with Scratch 3.
This month we’ll use the scientific powerhouse Sense HAT board with Scratch 3 to create four quick projects to demonstrate the power of Scratch 3 and the Sense HAT.
The Sense HAT is a remarkable board. Released in 2015, it works with Python, Node-RED and for this tutorial it works with Scratch 3 thanks to a special palette of blocks that enable anyone to use the sensors, display and joystick with very little coding knowledge.
With the power off, connect the Sense HAT to all 40 pins of the GPIO, ensuring that the board fits neatly over the Pi. Use the brass standoff, provided with the Sense HAT to fix the board firmly in place. Connect your peripherals and then power up the Raspberry Pi to the desktop.
Scratch 3 should come pre-installed on your Raspberry Pi OS image, but just in case it’s missing it can be installed from the main menu, under Preferences>Recommended Software. Scratch 3 is found in the Programming category: place a tick in the box and click Apply to install. Once installed, Scratch 3 can be found in the main menu under Programming.
Open Scratch 3 (on first start Scratch may take a little while to open). We’re going to assume that you have an understanding of how to code with Scratch, but if not we covered the basics in previous issues. Before we write any new code we need to click the blue folder icon in the bottom left of the screen to load the Extensions menu. From there select Raspberry Pi Sense HAT and a palette of new blocks are added to our code.
Project 1: Spin me right round
The Sense HAT has a sensor that can detect our orientation using an accelerometer. This sensor can measure our pitch, yaw and roll, in other words if we pick up the Raspberry Pi and tilt it backwards, forwards, left or right it can measure the orientation. We’re going to use the pitch value to spin a sprite. Click the New Sprite icon (blue cat icon in the bottom right) and select the Bat. Then delete the Cat icon using the trashcan icon next to it. From Events, drag When Green Flag Clicked into the coding area.
Now from Control drag forever to create a neverending loop. From the Raspberry Pi Sense HAT palette drag display sprite and pitch . Place display sprite inside the loop. From Motion drag turn clockwise 15 degrees and place that inside the loop, under display sprite . Drag pitch and drop it on top of 15 . Click the green flag and look at the Sense HAT on your Pi. There’s the sprite! Move it around and watch the sprite rotate.
Project 2: Shaken, not stirred
We can also use the accelerometer to register gestures, for example shaking the Raspberry Pi. From the Raspberry Pi Sense HAT palette drag when shaken
and start a new section of blocks. From Control drag two repeat 10 loops, place one under the previous block, then place the second loop inside the other. Change the second loop to repeat 4 .
Inside the second loop drag from the Raspberry Pi Sense HAT display Sprite and then from Looks drag next costume . Finally from Control drag wait 1 seconds
and place it inside the second loop, under next costume
. So now when we shake the Pi, the Bat will cycle
through all four of its costumes, giving us a basic animation. It does this 10 times. Shake the Pi and watch the bat on the screen and the Sense HAT.
Project 3: Hot stuff
We can use specific actions to trigger our code. Here we’ll tilt the Raspberry Pi forward and then change the colour of the bat’s nose using the current temperature. The bat will then tell us the temperature.
From Raspberry Pi Sense HAT palette drag when tilted forward and start a new section of blocks. From Control drag a repeat 10 loop and connect it to the previous block. From Looks drag change color effect by 25 . To use the current temperature as a value to change the colour effect we need to drag temperature from Raspberry Pi Sense HAT and drop it on top of 25 . Outside of the loop from Looks place say hello for 2 seconds and then drag another temperature block and drop it on top of hello . So now tilt the Raspberry Pi forward and the bat will change colour and tell us the current temperature in Celsius.
Project 4: Disco balls!
The 8x8 (64 pixels) RGB LED matrix of the Sense HAT is awesome, so let’s use it to make a random disco dance floor. From Events drag when space key is pressed and start a new section of blocks. From Raspberry Pi Sense HAT drag clear display and place the block under the previous. From Control drag repeat 10 and place it under the previous blocks. Change the 10 to 100.
Go to variables and create two new variables for all sprites. The first variable is called x and the second is y. These two variables will store the x- and y-coordinate for a randomly chosen pixel on the matrix. Drag two set my variable to 0 from Variables and place them inside the repeat 100 loop. Change the variables from my variable to x and y, respectively. From Operators drag two pick random 1 to 10 and place them over the 0 of each set x / set y to . Change the values to read 0 to 7 for both variables.
The next block is a little tricky. It sets the value of a specific pixel using x- and y-coordinates, to a randomly chosen colour. For this we need to drag set pixel 0, 0 to color from the Raspberry Pi Sense HAT and place it under the previous blocks, inside the repeat 100 loop.
Now from Variables drag the x and y variables and drop them over the x 0 and y 0 of the set pixel block. To set the random colour grab two join apple banana blocks from Operators. Drop the second join apple banana block on top of banana in the first block. Drag a pick random 1 to 10 from Operators and drop it over the first apple then change the values to 0 to 100. Now change apple and banana to 81 and 50. These set the saturation and brightness of the pixels.
The final block is found in Control and it’s wait 1 seconds . Place this under the previous block and change the value to 0.1 seconds. Now press the Space key and watch the LED matrix spring into life.
So there we have it – four short projects that show how to use the Sense HAT with Scratch 3. All of these projects can be mashed together to create unique and fun projects. We can control sprites with the Sense HAT, perhaps even our own version of Flappy Bird.