1 month ago

Mathematica and the Sense HAT

Ever since the partnership between the Raspberry Pi Foundation and Wolfram Research began, people have been excited to discover – and are often surprised by – the power and ease of using the Wolfram Language on a Raspberry Pi.

The full article can be found in The MagPi 59 and was written by Brett Haines.

The Wolfram Language is particularly useful in partnership with the Sense HAT, which features an LED array and a collection of environmental and movement sensors. These give users the ability to read in data from the physical world and display or manipulate it in the Wolfram Language with simple, one-line functions. With the release of Mathematica 11, Wolfram Research has been working hard to refine functions that connect to the Sense HAT, allowing Mathematica to communicate directly with the device.

Programming the Sense HAT

The Sense HAT’s functionality is built on Wolfram’s Device Driver Framework (magpi.cc/2sl5Q5u), so connecting to the device is incredibly simple. To start, use the DeviceOpen function to establish a connection. This will return a DeviceObject, which we will use later to tell Mathematica which device we want to read from or write to.

In the case of the Sense HAT, there are three on-board sensors that Mathematica can read from. To access the data from these sensors, simply call DeviceRead with the name of the measurement wanted. For instance:

A total of seven measurements can be read from the Sense HAT: temperature, humidity, air pressure, acceleration, rotation, magnetic field, and orientation. All the readings are returned with appropriate units, making it easy to convert the values to other formats if necessary.

The other physical component of the Sense HAT is the 8×8 LED array. You can use the DeviceWrite function to send an image or a string to the array. For strings, the text scrolls sideways across the device. You can set the speed and colour of the scrolling text.

Alternatively, the Sense HAT can receive an 8×8 list of RGB values to be displayed on the LED array. Using this method, it is possible to display small images. Download and run the code from GitHub to see a display like the one in Figure 1, above.

Practical uses

Using these functions, you can write Mathematica programs that process the data received from the sensors on the Sense HAT. For example, at the Wolfram Technology Conference in October 2016, the Sense HAT was demonstrated by reading the temperature, humidity and air pressure around the Pi every five minutes, and pushing that data to the Wolfram Data Drop.

This function generates a new databin to store data, but what does that data look like once it has been recorded? Let’s look at the recordings made at the Wolfram Technology Conference (Figure 2).
That data was available to be downloaded into Mathematica by anyone, at any time after the conference, to show the changes in atmospheric conditions using DateListPlot.

In Figure 2 (below), you can see the rise in air pressure inside the conference centre as more people gathered to see the demos from Wolfram employees, followed by a drop as the conference ended.

Figure 2: Air pressure readings taken by a Sense HAT at the Wolfram Technology Conference 2016

Another demo run at the conference made use of DeviceWrite. Using the Wolfram Language’s financial database, the Sense HAT was transformed into a miniature stock ticker. This demo downloads the current stock market data from Wolfram’s servers, then picks a random stock from the list and shows the stock’s name and price on the Sense HAT’s LED array.

Let’s play a game

The final demo run at the conference used the Sense HAT’s LED array to run Conway’s Game of Life, a famous cellular automaton. For those unfamiliar with the game, imagine each lit LED is a cell in a Petri dish. If a cell has too few or too many neighbours, it dies out. If an empty space has exactly three living neighbours, a new cell appears there. When these rules have been applied to all of the spaces on the grid, a new ‘generation’ begins, and the rules are reapplied. This pattern can continue indefinitely, given the right conditions. In the demo, a random array of lit and unlit LEDs constitutes the starting pattern. The automaton then runs for a given number of iterations.

Check out the code download above to see how this was done. The rounds, pause, and color parameters can all be modified to change the way the automaton is displayed, and to adjust how long Mathematica waits before displaying the next iteration.

What next?

This is only a taste of what is possible with Mathematica and the Sense HAT. Visit magpi.cc/2slJu3N to discover more ideas for using them together. Once you’ve tried some of the ideas, why not share your work on the Raspberry Pi Wolfram group to inspire other users? We’ll see you there!