For a more in-depth guide, we've created a detailed tutorial hosted on GitHub. In this tutorial, you'll find step-by-step instructions, code examples, and additional resources to help you better understand and implement the concepts discussed here. Whether you're a beginner or looking to deepen your knowledge, this comprehensive guide covers everything you need.
You can access the GitHub repository with the full code here: https://github.com/System-Electronics/astrial_tutorials/tree/main/03-sense_hat
IntroductionIn this guide, we will walk you through setting up and using the Sense HAT with the Astrial board. The Sense HAT is a multifunctional add-on board that includes an array of sensors and an LED matrix, making it perfect for educational projects, environmental monitoring, and interactive applications. This tutorial will help you connect and program the Sense HAT, enabling you to harness its full potential.
Hardware SetupBegin by setting up the hardware components:
- Connect the Sense HAT to the GPIO Header of the CM4 Board: attach the Sense HAT to the GPIO header on the CM4 board. Since the Sense HAT will occupy all GPIO pins, you will need to power the Astrial board via the 5V and GND pins on the USB header, as illustrated in the provided schematic.
Once the hardware is connected, proceed with the software configuration:
- Use a Custom Device Tree Blob (DTB): to enable the Sense HAT functionalities, you must upload a custom Device Tree to your Astrial board. This DTB configuration is necessary to activate the specific GPIO and I2C functionalities required by the Sense HAT. You can find the custom DTB in the resources folder of this tutorial's repository. If you are not sure of how to use a custom DTB, please refer to our GitHub guide.
- Install RTIMULib: the Sense HAT includes an IMU (Inertial Measurement Unit) sensor suite, for which you need the RTIMULib library. Download the RTIMULib on your host device and copy it to your Astrial:
git clone https://github.com/RPi-Distro/RTIMULib.git
scp -r RTIMULib root@<ip_astrial>:/home/root
To build and install the library execute the following commands in the Astrial's terminal:
cd RTIMULib/Linux/python
python3 setup.py build
sudo python3 setup.py install
- Install Python packages: The smbus2 library is required for I2C communication. Install it using the following command:
pip3 install smbus2==0.4.3
- Copy the custom sense_hat library: we provide a custom version of the sense_hat package, named sense_hat_astrial, tailored for compatibility with the Astrial board. Download it from the GitHub repository of this tutorial and copy it into the Astrial.
scp -r sense_hat_astrial root@<ip_astrial>:/home/root/
- Create the Python script: create a Python script that implements the code provided in the Code section of this article. This script will allow you to display some text on the Sense HAT's LED matrix.
- Run the Python script: with everything set up, you can now execute the example script:
python3 sense_hat_example.py
To deepen your understanding and capabilities with the Sense HAT, explore additional examples and tutorials available in the GitHub repository. These examples include reading IMU sensor data, using the LED matrix for visual feedback, and implementing joystick controls.
When working with the IMU sensor on the Sense HAT, you may need to adjust the configuration to ensure compatibility with the Astrial board's I2C setup.
The first time you run the imu_example.py
script, you might encounter an error message that looks like this:
OSError: Humidity Init Failed
This error occurs because the Sense HAT uses I2C bus 1 by default, whereas the CM4 carrier board uses I2C bus 4. To resolve this issue, you must modify the RTIMULib configuration file.
Go to /home/root/.config/sense_hat
and open the RTIMULib.ini
file in a text editor of your choice. Locate the line containing the I2CBus
setting, and change its value from 1
to 4
to match the Astrial board's I2C bus:
I2CBus=4
Now, run imu_example.py
again and try out the sensors.
Comments