Installation
In order to work correctly, the Sense HAT requires an up-to-date kernel, I2C to be enabled, and a few libraries to get started.
Overview The Sense HAT (B) is specially designed for Raspberry Pi, integrates multi powerful sensors such as gyroscope, accelerometer, magnetometer, barometer, temperature and humidity sensor, etc. It is communicated via I2C interface, and allows to connect more external sensors if you need. All Raspberry Pi HATs & pHATs Look no further for plenty of fantastic options to expand the capabilities of the Raspberry Pi with this great variety of exciting HATs (and pHATs for the Pi Zero)! All these boards are fully compliant to official standards and offer every Maker the chance to apply their Pi to any one of a number of great bespoke. The Basics: DIY HATs. You may have noticed that your RaspPi doesn’t include a prototyping area. Jul 12, 2018 The answer is very simple; HATs are designed to work with standard Raspberry Pi computers (such as the Model B 3), while pHATs are designed to work with the Raspberry Pi Zero. If you are interested in recording both positioning and environmental data, the Sense HAT is a famous choice.
- Ensure your APT package list is up-to-date:
- Next, install the sense-hat package which will ensure the kernel is up-to-date, enable I2C, and install the necessary libraries and programs:
- Finally, a reboot may be required if I2C was disabled or the kernel was not up-to-date prior to the install:
Hardware
The schematics can be found here.
Software overview
After installation, example code can be found under
/usr/src/sense-hat/examples
.These can be copied to the user's home directory by running
cp /usr/src/sense-hat/examples ~/ -a
.The C/C++ examples can be compiled by running
make
in the appropriate directory.The RTIMULibDrive11 example comes pre-compiled to help ensure everything works as intended. It can be launched by running
RTIMULibDrive11
and closed by pressing Ctrl+c
.Python sense-hat
sense-hat
is the officially supported library for the Sense HAT; it provides access to all of the on-board sensors and the LED matrix.Complete documentation can be found at pythonhosted.org/sense-hat.
RTIMULib
RTIMULib is a C++ and Python library that makes it easy to use 9-dof and 10-dof IMUs with embedded Linux systems. A pre-calibrated settings file is provided in
/etc/RTIMULib.ini
, which is also copied and used by sense-hat
. The included examples look for RTIMULib.ini
in the current working directory, so you may wish to copy the file there to get more accurate data.Other
LED matrix
The LED matrix is an RGB565 framebuffer with the id 'RPi-Sense FB'. The appropriate device node can be written to as a standard file or mmap-ed. The included 'snake' example shows how to access the framebuffer.
Joystick
The joystick comes up as an input event device named 'Raspberry Pi Sense HAT Joystick', mapped to the arrow keys and
Enter
. It should be supported by any library which is capable of handling inputs, or directly through the evdev interface. Suitable libraries include SDL, pygame and python-evdev. The included 'snake' example shows how to access the joystick directly.Calibration
Taken from this forum post.
Raspberry Pi
Install the necessary software and run the calibration program as follows:
You will then see this menu:
Press lowercase
m
. The following message will then show; press any key to start.After it starts, you will see something similar to this scrolling up the screen:
Focus on the two lines at the very bottom of the screen, as these are the most recently posted measurements from the program.Now you have to move the Astro Pi around in every possible way you can think of. It helps if you unplug all non-essential cables to avoid clutter.
Try and get a complete circle in each of the pitch, roll and yaw axes. Take care not to accidentally eject the SD card while doing this. Spend a few minutes moving the Astro Pi, and stop when you find that the numbers are not changing anymore.
Now press lowercase
s
then lowercase x
to exit the program. If you run the ls
command now, you'll see a new RTIMULib.ini
file has been created.![Raspberry Raspberry](/uploads/1/1/9/7/119711421/153440351.png)
In addition to those steps, you can also do the ellipsoid fit by performing the steps above, but pressing
e
instead of m
.When you're done, copy the resulting
RTIMULib.ini
to /etc/ and remove the local copy in ~/.config/sense_hat/
:You are now done.
Updating the AVR firmware
...
Pi Zero Battery Hat
EEPROM data
These steps may not work on Raspberry Pi 2 Model B Rev 1.0 and Raspberry Pi 3 Model B boards. The firmware will take control of I2C0, causing the ID pins to be configured as inputs.
- Enable I2C0 and I2C1 by adding the following line to
/boot/config.txt
: - Enter the following command to reboot:
- Download and build the flash tool:
Reading
- EEPROM data can be read with the following command:
Writing
Please note that this operation is potentially dangerous, and is not needed for the everyday user. The steps below are provided for debugging purposes only. If an error occurs, the HAT may no longer be automatically detected.
- Download EEPROM settings and build the
.eep
binary: - Disable write protection:
- Write the EEPROM data:
- Re-enable write protection:
This article was written by Sean McManus and first appeared in The MagPi 86. Get a free Raspberry Pi with a 12-month subscription to the print edition of The MagPi magazine.
Scratch 3 is now on Raspberry Pi, with new blocks for the Sense HAT. In this game, you tilt your Raspberry Pi to turn left and right, and use the HAT’s up and down joystick controls as an accelerator. Going off-road or colliding with the computer’s racer damages your car. Can you complete three laps before your car is too beaten up to drive? If so, how fast is your qualifying time? This project will give you a tour through what’s new in Scratch 3, including computer speech, new sound effects, and the extensions. Rev up!
What you'll need
- Speakers to hear the engines roar
- Download the full code from magpi.cc/github86
Add extensions to Scratch
One of the best features of Scratch 3 is the extensions, which enable you to add new capabilities to Scratch. For example, you can add blocks for simple electronics, controlling the GPIO, video sensing, and using the Makey Makey input device. To simplify Scratch for first-timers, the Pen and Music blocks that used to be in the Blocks Palette have now been put into extensions too.
For this project, you need to add the extensions for Raspberry Pi Sense HAT, Pen, and Text to Speech. The menu to add extensions is in the bottom left.
Draw the road
Hover over the icon in the bottom right to open the backdrop menu. Click the option to paint a backdrop and draw a green box that fills the Stage. Add Listing 1 to Sprite1 (the cat). You’ll need to use the menu in the first block to set up a new message called ‘draw road’. Click the script to test it. You should see the road is drawn with two differently coloured checkpoints at the top and bottom.
Draw the cars
Hover over the icon at the bottom of the Sprite List to add a sprite and choose Paint. The vector editor is now the default. Draw an overhead view of a race car, driving right. Centre your car on the cross-hairs target on the canvas, to make sure it turns correctly. Right-click your car in the Sprite List and duplicate it. With vector images, you can edit the shapes and colours afterwards, so you can easily make the second car look different. Click each car in the Sprite List and use the boxes above them to rename them to ‘player car’ and ‘rival car’.
Start the rival car moving
The rival car is just an obstacle to avoid. Add the scripts in Listing 2 to it. You’ll need to create a new broadcast message for ‘start race’. Click the green flag to position the car, then click the longer script. You should see it looping around the road, without veering off. If needed, adjust the size of the car in Listing 2 (and Listing 3 later) to fit both cars comfortably on the road. You might need to tweak the positions in the glide blocks in Listing 2 too.
Create the variables
In Scratch 2, variables were created in the Data section of the Blocks Palette. Now, it’s called Variables again, as it was in Scratch 1.4. Every project now starts with a variable created called my variable to help newcomers experiment. You need to make these variables: half lap count, next checkpoint, qualifying time, roadworthiness, row to light, row to turn off, and speed. They can be ‘for all sprites’. Untick your variables in the Blocks Palette, except for roadworthiness, which we want to show on the Stage. Drag its box to the bottom middle of the Stage.
Set up the player’s car
Click the player’s car in the Sprite List and add Listing 3 to it. Sense HAT and Text to Speech are both new in Scratch 3. The display block enables you to set a pixel pattern to show on the LED matrix, using the light colours you’ve set up with set background and set colour blocks. At the start, the game shows a chequered flag on the Sense HAT and the computer says, “Ready, Get Set, Go!”
Add player movement controls
You’ll steer by holding your Raspberry Pi up, with the Sense HAT facing you, and tilting it left and right. You detect this axis of movement using the pitch block in the Sense HAT extension. Tilting left (up to 90 degrees) gives values of 0 to 90. Tilting right (up to 90 degrees) gives values of 360 to 270. Listing 4 goes on your player’s car. It turns your car if your Raspberry Pi is tilted left or right by more than 10 degrees, and uses the Sense HAT joystick to trigger the blocks to change your speed. To make the pink blocks, click My Blocks and use the button to make your own blocks, with the names you see in the listing. For the touching color blocks, click the colour box in the block and use the new, improved pipette to copy the checkpoint colours from the Stage.
Add sounds
There are fun new sounds to explore. For this game, we need the Cheer, Coin, Car Horn, Engine, and Skid effects. Click the Sounds tab on your player’s car, and use the button in the bottom left to add them. You can now search for them by name.
Add speed controls
Find the pink hat blocks for define increase speed and define decrease speed. They were created for you in the Code Area when you made those blocks in Step 7. Use them to build Listing 5. You’ll need to create new blocks (using My Blocks again) for ‘speed lights on’ and ‘speed lights off’.
The set pixel Sense HAT block enables you to light an individual LED using its x and y position (both numbered from 0 to 7, from the top left). We’re using it to turn the LED matrix into a speedometer, with a bar graph that grows with your speed, which can range from 0 to 8. Scratch 3 adds new sound effects blocks. We’re making the engine sounds higher when you speed up. Don’t confuse the sound pitch (how low or high it is) with the Sense HAT pitch (the angle of the device). Your colours in the ‘speed lights off’ script should match the Sense HAT background colour in Listing 3. Click the colour box in the block to check the numbers.
Add the lap counter
To confirm the car has done a lap of the track, we’re using checkpoints. The car has to visit the red one, then the yellow one, and repeat until it’s gone around three times. The next checkpoint variable remembers which checkpoint the car needs to visit next. The half lap count variable stores how many checkpoints have been successfully crossed. Add Listing 6 to your existing define lap counter block in the Code Area. Copy the red and yellow colours in the touching color blocks from the Stage.
Detect car damage
Your roadworthiness variable is like a health score. It goes down when you hit the grass or the other car. The two scripts in Listing 7 continuously check whether you’ve hit something. If you have, they reduce your score and then pause briefly to stop your health sapping too fast. Use the pipette to copy the exact grass colour and the main body colour of your rival car (pink in our case) into your scripts. Listing 7 goes on the player’s car.
Add game-over sequences
There are two ways for the game to end. Either your roadworthiness gets to zero, or you successfully complete three laps. The display text block scrolls an appropriate message across the Sense HAT’s LEDs. Attach Listing 8 to the end of Listing 4, the script with movement controls on the player’s car. Now you’re ready to play!
Top tip: Random gliding
We’re not using it here, but there’s a new block to glide to a random position, which you might find handy in your games.
Sean McManus is the author of Scratch Programming in Easy Steps, Cool Scratch Projects in Easy Steps, and Mission Python. Get free chapters at Sean’s website.
See also: