How to code the Sense HAT for Raspberry Pi in your web browser
Look Ma, no hardware! Coding the Raspberry Pi in a web emulator
Now you can code the Sense HAT for Raspberry Pi in an emulator in your web browser, without any hardware.
You may be familiar with the Sense HAT, an add-on board for the Raspberry Pi which was made especially for a space mission with British ESA Astronaut Tim Peake for Astro Pi. It's a great piece of hardware, very handy for data logging, science experiments, environmental analysis, games and more. It comes with a Python library making it work out-of-the-box. (See Exploring the Raspberry Pi Sense HAT and Experimenting with the Raspberry Pi Sense HAT).
Enter the emulator
The Raspberry Pi Foundation teamed up with Trinket, who make brilliant online resources for learning Python (and more) in the browser. Trinket built a Sense HAT emulator which allows you to run the same Python code you use on a real Sense HAT on a Raspberry Pi, and it emulates the hardware's behaviour in a virtual graphic on screen.
The virtual Sense HAT has temperature, pressure and humidity sensors, and can change its behaviour according to the values they report. The Sense HAT emulator has sliders you can move to change these values, so you can test how your code responds to environmental variables.
The emulator features:
- LED display
- Environmental controls
- Joystick control (on-screen or keyboard)
- Full Python syntax highlighting
- Contextual auto-complete
- Intuitive error reporting and highlighting
You can also upload images, embed trinkets on your own website, share your project with a direct URL, or download your project as a ZIP archive so you can easily move it to your Raspberry Pi.
Previously, access to the joystick was limited to use of a third-party library like pygame or evdev, but since access to libraries is limited to what's implemented in Trinket, we decided to press on with an idea we were considering anyway—to implement a native joystick interface within the Sense HAT library. The latest Python code release features this, and it was implemented within Trinket too. Now you can access
sense.stick to process events and assign callbacks to actions. (See the joystick documentation for more information.)
At the end of 2015 we sent two Raspberry Pis to the International Space Station with Tim Peake. Tim completed his six-month mission and successfully ran student code on the Astro Pi units. The Pis are still aboard the space station and we now have ground access to them! This means we can upgrade the library, and upload more programs to them in future. We intend to run more competitions on a larger scale in the next year, and we thought this would be a great way to encourage more engagement, as you no longer need access to the hardware to take part.
Sense HAT in the classroom
The Sense HAT emulator is perfect for use in the classroom with a small number of Raspberry Pis with Sense HATs. In a PC suite everyone can write code for their virtual Sense HAT, and take turns running their code on a real one.
The emulator is also useful for testing code in extreme or unlikely conditions. If something is supposed to happen when the temperature is particularly high or low, it's difficult to test it works. The emulator lets you simulate these conditions without taking your Pi to the beach—or the North Pole!
Check out some Sense HAT resources on the Astro Pi site, and be sure to watch out for more science and coding competitions to have the chance to run your code in space!