Getting Started with the Sense HAT

Difficulty
Easy
Steps
8

Previously, you might have tried your hands with programming the Sense HAT emulator

In this guide, you will get started with using an actual Sense HAT with the Raspberry Pi 3 Model B+. 

 After completing this guide, you will have gathered real-world data using the Sense HAT.

Step 1  Attach Sense HAT to Raspberry Pi

Attach the Sense HAT to the Raspberry Pi by aligning and inserting the 40-pin header as shown.

Insert the NOOBS microSD card into the Raspberry Pi, and then power it up with the official power supply. 

Step 2  Install Sense HAT software

First, to make sure the package lists are up to date:

sudo apt-get update

 Install the Sense HAT software with the following commands:

sudo apt-get install sense-hat

Reboot the Raspberry Pi:

sudo reboot

Step 3  Open IDLE using sudo


              sudo idle3 &
            

We will control the Sense HAT by first programming it in Python on the Raspberry Pi. 

To do so, we need administrative privileges to execute scripts. So open up a terminal window and run IDLE with the following command:

Step 4  Create a sense.py file

  1. Click on the Raspberry Pi icon on the top left hand corner to access the main menu. 
  2. Click on Programming > Python 3 (IDLE). 
  3. Now, create a new file by clicking File > New File. 
  4. Next, save the file by clicking File > Save, and naming it sense.py

Step 5  The Python code


              from sense_hat import SenseHat

sense = SenseHat()
            

First, we will import the relevant Python modules. 

Then we will place it in a variable, called "sense".

Step 6  Environmental sensors


              from sense_hat import SenseHat

sense = SenseHat()

temp = sense.get_temperature()
pres = sense.get_pressure()
hum = sense.get_humidity()

            

Using two of the Sense HAT's sensors, let's measure:

  • Temperature in degree celsius
  • Pressure in millibars
  • Humidity as a percentage

We will place them in variables. 

Step 7  Print the measurements


              from sense_hat import SenseHat

sense = SenseHat()

temp = sense.get_temperature()
pres = sense.get_pressure()
hum = sense.get_humidity()

print(temp)
print(pres)
print(hum)
            

Add the following lines to your code to print out the temperature, pressure and humidity readings from the Sense HAT:

Step 8  Run the module

Now click on Run > Run Module or press F5 on your keyboard to run the program.

You should see three measurements being printed.