Sense Hat (announcement by raspbery pi foundation) is an add on board for PI2, which supports environmental sensors for temperature, pressure and humidity. There is an IMU-sensor included. And a fancy 8×8 RGB-LED matrix. And a small joystick which provides cursor keys and the enter key for the center button.
The software to drive this board is wrapped in a python API. This APi is handy to be used for scratchClient and connect it to scratch.
The adapter is adapter.senseHat_adapter.SenseHat_Adapter.
Events are ‘clear’, setPixel_xy’ and ‘clearPixel_xy’.
‘clear’ is pretty straightforward and clears the matrix to blank.
The pixel operations need variables to be set before the setPixel can be performed.
x and y position needs to be set and the color needed. The event sense_led_xy_on is mapped to setPixel_xy by scratchClient. This mapping is done in the configuration for this adapter (scratchClient/scratch/senseHat/config_senseHat.xml).
<input name= 'clear'> <broadcast name='sense_led_clear'/> </input> <input name= 'setPixel_xy'> <broadcast name='sense_led_xy_on'/> </input> <input name= 'clearPixel_xy'> <broadcast name='sense_led_xy_off'/> </input>
The commands available in scratch are the <broadcast/> elements. These all have the prefix ‘sense_’.
Color supports basic colors in plain english (red, green, white, blue, and some more) and ‘#aabbcc’ which is hex notation in red, green blue. In the scratchClient documentation are the details, look for the ‘colorType’-definition in chapter ‘Adapter Data Types’ .
In my experiments, I found that small rgb-values for the LED, e.g. #030303, which is red=3, green=3, blue=3 are not displayed, but leave the LED dark. Obviously the API is doing some gamma correction and discards small values. Try higher values if you run into this problem. #303030 seem to be smallest values available.
For the clearPixel call, no color needs to be set.
There is a scratch sample application scratchClient/scratch/senseHat/sense_hat_led.sb which allows to set/clear pixel in a (hopefully) intuitive way. Move the blue cursor mark with cursor keys and toggle the LED by blank key.
The joystick on senseHat can be used to move the cursor around. Unfortunately the center button sends ‘enter’, which is the ‘green flag event’ in scratch and restarts the code. Thus the toggle LED is mapped to the blank bar on keyboard for this sample.
Took me a while to identify the problem in the second photo: the exposure time was 1/100 sec, so the refresh rate of 80Hz for the LED produces fake ‘missing LED’ on the sense hat.
The humidity, pressure and temperature values are sent at a limited frequency to scratch. And only when values change. For this adapter, the precision of values sent is limited to one decimal digit. This reduces communication effort and system load by 10 percent.
The temperature value and dependent from this the humidity sensor suffers from heating of the hat board by the primary processor. On my desktop, the temperatures reported have been prox 10 degree too high. There are reports of people who separated the Pi and hat by a ribbon cable and got better results.
The gyro, accel and compass sensor contribute to the orientation signals, which are sent as orientation_yaw, orientation_roll and orientation_pitch to scratch. These values are quite stable. These values also are sent only at a limited frequency and reduced precision to one digit number. But 1/10 degree is good for most purposes.
Installation of prerequisite software
The board needs software to be installed. See the installation instructions on the API side
cd ~/scratchClient sudo python src/scratchClient.py -c scratch/senseHat/config_senseHat.xml