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.
Pingback: Getting Started with The Sense HAT – Raspberry Pi — IoTbyHVM – Explore TechBytes – hashstacks
Pingback: How to set up Windows 10 IoT Core on the Raspberry Pi
Pingback: piCore (Tiny Core) Linux on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Create a Docker Container for Raspberry Pi to Blink an LED
Pingback: How to Boot Up Raspberry Pi 3 from External Hard Disk
Pingback: Flask SocketIO - Socket.IO integration for Flask applications.
Pingback: Running the TICK Stack on a Raspberry Pi | TICK Satck on Raspberry Pi
Pingback: Setup Docker on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi Introduction and Tutorials — OnionLinux