Home / English Posts / Raspberry Pi Sense HAT

Raspberry Pi Sense HAT

The Sense HAT Expansion Card

(Reference La Scienza con il Raspberry pi – Marco Casolino)

One of the most interesting features of the Raspberry – which differentiates it from a simple low-cost computer – is that it can be connected to a series of sensors and actuators via the digital ports (GPIO, General Purpose Input/Output) with which it is equipped and with which can interact with the outside world. Given the price/performance ratio and ease of use, the Sense Hat is among the best expansion cards available. The Sense Hat is equipped with a series of sensors: temperature, pressure, accelerometer/gyroscope, magnetic field and humidity, as well as a 64-channel multicolor display and a small joystick with which you can interact without a keyboard to give simple commands. This card – which plugs into the GPIO connector – was developed to carry out various practical demonstrations on board the space station on the occasion of the flight of the British astronaut Tim Peak in 2015 and subsequently by other astronauts including Paolo Nespoli and Luca Parmitano (here Astro Pi ESA challenge).

The following image shows the board mounted on a Raspberry pi unit.

Temperature, Pressure and Humidity Sensors

The Sense HAT has an integrated temperature sensor, which returns the temperature in Celsius degrees. The Sense HAT has a humidity sensor that returns relative humidity as a percentage, it uses the data from the temperature sensor to provide a correct value. There is also a barometric sensor integrated on the board which returns a pressure value expressed in millibars.

Gyroscope, Accelerometer and Magnetometer

In the Sense HAT a small gyroscopic sensor is incorporated which can provide the values ​​of the rotations on the pitch, roll and yaw axes, always via Python library API. The result are values ​​that are returned as angles between 0 and 360 degrees. The figure below shows these axes.

An acceleration sensor and a magnetic field sensor are also integrated in the Sense Hat. The accelerometer has three axes: x, y, z and can be used both to establish the orientation of the card using the acceleration of gravity g, and to measure the acceleration of the card if it is moving. The magnetometer is also three-axis and provides the measure of the magnetic field expressed in μT.

Power the Raspberry with a battery

If with the Raspberry pi – equipped with Sense Hat – you want to record data on the move, for example acceleration and orientation, or even pressure and temperature, while the card is subjected to a movement, you must provide battery power. We have used the card shown in the following image which also includes a lithium battery, the battery ensures us an autonomy of 1 – 2 hours.

The image below shows the complete stack : battery unit, Raspberry pi and Sense Hat, positioned inside a container.

Data Logging Software

To control the RPi Sense Hat it is necessary to install python libraries, as described in the official documentation and to do this use the “apt-get” tool as described below.
The first step is, as always recommended, an update of the packages present on your distribution:

sudo apt-get update

at the end you can install the specific libraries:

sudo apt-get install sense-hat

The one below is the Python code for acquiring data from Sense HAT and for logging on file. The acquisition is carried out at regular time intervals.


from datetime import datetime
from csv import writer
import time
import sys

sense = SenseHat()
sense.set_imu_config(True, True, True)  # accelerometer, magnetometer , gyroscope

logging = False

def get_sense_data():
  sense_data = []
  temperature = round(sense.get_temperature(),2)
  pressure = round(sense.get_pressure(),2)
  humidity = round(sense.get_humidity(),2)
  mag = sense.get_compass_raw()
  mag_x = round(mag["x"],2)
  mag_y = round(mag["y"],2)
  mag_z = round(mag["z"],2)

  acc = sense.get_accelerometer_raw()
  acc_x = round(acc["x"],3)
  acc_y = round(acc["y"],3)
  acc_z = round(acc["z"],3)
  gyro = sense.get_orientation()
  pitch = round(gyro["pitch"],2)
  roll = round(gyro["roll"],2)
  yaw = round(gyro["yaw"],2)
  return sense_data

def pushed_up(event):
  global logging, timestart
  if event.action == ACTION_PRESSED:
    logging = True

def pushed_down(event):
  global logging
  if event.action != ACTION_PRESSED:
    logging = False

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down

timestamp = datetime.now()
timestart = datetime.now()
delay = 100 #milliseconds

with open('data.csv', 'w', newline='') as f:
  data_writer = writer(f)
  while True:
    if logging:  
      data = get_sense_data()
      dt = data[-1] - timestamp
      elapsed = data[-1] - timestart
      if int(dt.total_seconds()*1000) > delay:
        timestamp = datetime.now()


Free Fall

A simple experiment is to reproduce – in a small way – the microgravity condition that is created during a free fall. It is a matter of activating logging and letting the card fall from a few meters in height, obviously taking care that the fall arrest is appropriately mitigated.
The parameters that interest us are the accelerations. The graph presented below shows the acceleration values ​​measured by the Sense HAT during the fall. The parameter that interests us is the acceleration along the Z axis : before the fall, the value corresponds to g = 9.81 m/s2, during the fall it resets to zero as expected, at the moment of impact the backlashes due to the impact are noted and subsequently the value settles again on the value g.

Geomagnetic field measurement

The magnetic field sensor integrated in the Sense HAT does not have very high precision, however it allows us to evaluate the direction and intensity of the geomagnetic field with a good approximation. The test consists in reading the sensor value for a few seconds, in order to collect a high number of data and then average the values.
The obtained values are the following :

Mag X = -19,35 μT
Mag Y = -21,71 μT
Mag Z = 7,59 μT

Knowing the components according to the three axes, the direction and intensity of the magnetic vector can be obtained. The intensity, calculated by composing the three vectors, has a value of 30 μT, close to the expected value for the geomagnetic field at our latitude.

Orientation in Space

The Sense HAT board is equipped with an IMU (Inertial Measurement Unit). The main part of the IMU is the gyroscopic sensor. This sensor is able to give us the orientation of the board, expressed as an angle of rotation with respect to the three axes of roll, pitch and yaw. The angle can be expressed in degrees or radians. It is easy  to understand that this type of sensor is of paramount importance on aircraft and spacecraft.
To demonstrate its functioning, we recorded the data provided by the gyroscope during the movement of the board, the result is shown in the graph below. You can see how the range of values ​​goes from 0° to 360°.

If you liked this post you can share it on the “social” Facebook, Twitter or LinkedIn with the buttons below. This way you can help us! Thank you !


If you like this site and if you want to contribute to the development of the activities you can make a donation, thank you !

Check Also

Measurement of the Milky Way Rotation

PDF Abstract : In this post we want to continue the study of the structure …