Practise coding for the Sense HAT hardware using the Sense HAT emulator built into Raspbian
The Sense HAT emulator was developed by Dave Jones. It is intended for people who own a Raspberry Pi, but not a Sense HAT.
The Sense HAT is one of the most important pieces of Raspberry Pi hardware. The board was developed to travel aboard the International Space Station (ISS) as part of the Astro Pi mission. It was also made available to buy, and schoolkids around the world use it to develop code – some of which runs in space as part of a series of competitions.
The Sense HAT adds various sensors to the Raspberry Pi: gyroscope, accelerometer, magnetometer, temperature, barometric pressure, and humidity.
You develop code for the Sense HAT and run it in the emulator. A visual representation of the Sense HAT hardware appears, and a range of sliders and buttons can be used to emulate the Sense HAT’s features.
The sliders are used to change the values reported by the sensors while your code is running. You can increase the pressure and humidity that the Sense HAT hardware would detect, and check that your system responds accordingly.
The Sense HAT emulator is a great option for somebody who wants to develop code for the Astro Pi mission, but doesn’t have access to the Sense HAT hardware. It’s also a great environment for testing code, because you can manually adjust the values reported via the sensors.
You can access the Sense HAT emulator from the Raspbian desktop menu, under Programming. The emulator closely simulates the experience of attaching the Sense HAT hardware to your Pi. You can read from the sensors or write to the LED matrix using multiple Python processes.
Open IDLE (Programming > Python 3) and choose File > New. Enter this code:
from sense_emu import SenseHat
sense = SenseHat()
green = (0,255,0)
white = (255,255,255)
humidity = sense.humidity
humidity_value = 64 * humidity / 100
pixels = [green if i < humidity_value
else white for i in range(64)]
This program adjusts the number of green and white pixels displayed on the LED, depending on the detected humidity.
Run the program in IDLE (Run > Run Module) and the Sense HAT image will appear and display some green LEDs. Adjust the humidity slider and watch the number of green LEDs change to match the new readings.
There are some preferences that you can adjust to change the behaviour of the emulator. Choose Edit > Preferences. Increase the Screen updates value to provide a more realistic experience of the behaviour of the hardware sensors. You’ll see that the values being returned in your code drift according to the known error tolerances of the physical sensors used on the Sense HAT.
If you’re new to the Sense HAT, you can copy and paste a range of example code from the Raspberry Pi educational resources page. Projects include a getting started guide and a random number program. You will also find lots of examples under File > Open Example. These will be written to your home directory.
If you want to port your emulator code to a physical Sense HAT, you just need to change:
Reverse this if you’re porting a physical Sense HAT program to the emulator.
Click here to download our free Experiment with the Sense HAT guide.