How ToIoT HardwaresRaspberry PiSensorsTutorials/DIY

Getting Started with The Sense HAT – Raspberry Pi

The Raspberry Pi Sense HAT is a versatile interface board designed for the Raspberry Pi, offering a range of sensors and an LED display. It allows users to measure temperature, relative humidity, pressure, acceleration, and orientation using an accelerometer, gyroscope, and magnetometer. Additionally, it features a full-color 8×8 LED matrix display.

To use the Sense HAT, a Raspberry Pi with a 40-pin GPIO header is required, along with some software installations.

What You Will Need

Hardware:

  • Raspberry Pi (any model with a 40-pin GPIO header)
  • Sense HAT

Software:

You will need the latest version of Raspberry Pi OS (formerly Raspbian), which already includes the following software packages:

  • Python 3
  • Sense HAT library for Python 3

If for any reason you need to install the package manually, follow these instructions.

Installing the Sense HAT Python Library

Install the Sense HAT Python library by running the following command:

sudo apt-get install sense-hat

This installer will automatically enable I2C, so manual I2C setup is not required.

Sense HAT’s display uses a graphics library called Python Image Library (PIL), which must be installed using:

sudo pip3 install pillow

After installation, reboot your Raspberry Pi if I2C was not already enabled.

Testing the Sense HAT

To check if everything is working, open the Python console:

sudo python3

Now enter these commands:

from sense_hat import SenseHat
hat = SenseHat()
hat.show_message('RPi Tutorials by IoTbyHVM')

The LED matrix should display the message, scrolling it across the screen.

Measuring Temperature, Humidity, and Pressure with the Sense HAT

Now, let’s measure temperature, humidity, and pressure using the Sense HAT. Create a new Python script using any text editor or IDLE and paste the following code. Save it as sense_thp.py.

# Program to measure temperature, humidity, and pressure using Sense HAT
from sense_hat import SenseHat
import time

hat = SenseHat()

while True:
    t = hat.get_temperature()
    h = hat.get_humidity()
    p = hat.get_pressure()
    print('Temp C:{:.2f} Humidity:{:.0f}% Pressure:{:.0f} mbar'.format(t, h, p))
    time.sleep(1)

Running the Script

Run the script using:

sudo python3 sense_thp.py

Example Output:

Temp C:27.12 Humidity:45% Pressure:1012 mbar
Temp C:27.45 Humidity:44% Pressure:1011 mbar

Here, the temperature is in degrees Celsius, humidity is in percentage, and atmospheric pressure is in millibars.

Sensing a Magnet with the Sense HAT

The Sense HAT includes a magnetometer, which can detect magnetic fields. Use the following script to sense a magnet.

# Sense a magnet using the Sense HAT
from sense_hat import SenseHat
import time

hat = SenseHat()
fill = (255, 0, 0)  # Red color

while True:
    reading = int(hat.get_compass_raw()['z'])
    if reading > 200:
        hat.clear(fill)
        time.sleep(0.2)
    else:
        hat.clear()

Running the Script

Run the script using:

sudo python3 sense_magnet.py

When a magnet is brought close to the Sense HAT, the LED matrix will turn red for 0.2 seconds.

Finding Magnetic North with the Sense HAT

The Sense HAT can also be used as a compass to find magnetic north.

# Finding magnetic north using the Sense HAT
from sense_hat import SenseHat
import time

hat = SenseHat()

while True:
    bearing = hat.get_compass()
    print('Bearing: {:.0f} degrees to north'.format(bearing))
    time.sleep(1)

Running the Script

Run the script using:

sudo python3 sense_compass.py

Example Output:

Bearing: 138 degrees to north
Bearing: 140 degrees to north

Controlling LED with Raspberry Pi

Control LED with GPIO connector. Visit these articles given below.

The Raspberry Pi Sense HAT is a powerful add-on that enables environmental sensing, motion detection, and interactive displays. Whether you’re experimenting with weather data, building a digital compass, or detecting magnetic fields, the Sense HAT provides a versatile platform for various IoT and educational projects.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

9 thoughts on “Getting Started with The Sense HAT – Raspberry Pi

Leave a Reply

Your email address will not be published. Required fields are marked *