Your submission was sent successfully! Close

Common Sense – using the Raspberry Pi Sense HAT on Ubuntu Impish Indri

Oliver Smith

on 19 October 2021

Dave Jones from the Canonical Raspberry Pi team has put together a helpful guide for those getting started with the Raspberry Pi Sense HAT on Ubuntu Impish Indri. We’ve reproduced an edited version below, or you can read the full post on his blog along with other great Raspberry Pi tips!

For more information on Ubuntu Impish Indri check out the release notes.

One of the main new features in Impish Indri for the Raspberry Pi is the addition of packages to support the Raspberry Pi Sense HAT. Specifically:

  • sense-hat — The main Sense HAT package which depends on all the other bits you’ll need (like the Python library detailed below) and a basic configuration with some reasonable calibration figures.
  • python3-sense-hat — The official Python library for interfacing to the Sense HAT. (Note: there’s no corresponding python-sense-hat library for Python 2.x on Ubuntu because Python 2.x is no longer supported.)
  • sense-emu-tools — The Sense HAT desktop emulator package, which includes the GUI desktop emulator and the sense_rec and sense_play tools.

How to set up the Sense HAT desktop emulator

We’ll start with the Sense HAT desktop emulator package for a couple of reasons (full disclosure: these reasons have nothing whatsoever to do with my having written it … no sir!). Firstly, you can play with it even if you don’t have a Sense HAT. Secondly, if you do have a Sense HAT, then you can use all the demo scripts that come with it!

Let’s get started with installing the HAT. This is my Sense HAT, mounted on a Pimoroni Black HAT Hacker board (because it makes it far easier to wiggle around without worrying about HDMI cables and such):

When you switch on the Pi, you should see a rainbow image on the Sense HAT (after an initially blinding flash, if you happen to be on a Pi 4 — don’t worry, this is normal!). Once the kernel starts, the rainbow on the Sense HAT will go blank.

Once you’re logged in and at a terminal prompt, install the packages we want:

$ sudo apt install sense-emu-tools sense-hat

Next, install a code editor. For this demo I’m using geany:

$ sudo apt install geany

Next, start the desktop emulator, which you should find under Gnome’s application menu (or you can hit the Super key and just type “Sense”):

Finally, configure it to use “geany” (or your-choice-of-editor-here) because the default, “thonny”, isn’t in the Ubuntu archive … yet:

While you’re at it, I’d suggest activating the “Simulate environment sensors” and “Simulate inertial measurement unit” options too, as these make the emulation of these sensors more “realistic”.N

Note

The Sense HAT emulator runs on any machine, not just the Raspberry Pi. But the emulator’s demo scripts can be used with the real Sense HAT too.

The Sense HAT emulator runs on any machine, not just the Raspberry Pi. But the emulator’s demo scripts can be used with the real Sense HAT too.

Let’s open up the humidity.py demo script. Go to File > Open example > Simple > humidity.py in the menus. This should fire up your chosen editor with a copy of this script, written to your home directory with a timestamp appended so that you can edit it as much as you’d like. Subsequent selections of that entry will generate a fresh copy of the original:

If you’re using Geany (as suggested), there’s one more thing we need to do. Geany still defaults to trying to call python rather than python3 when running Python scripts. Since Ubuntu no longer ships Python 2.x, the only interpreter is python3, so we need to tell it to use this instead. Select Build > Set Build Commands from the Geany menu and change the Execute command to python3:

Finally, we can run our demo script. Select Build > Execute from the menu, or click the cogs icon in the toolbar. You should see the display on the emulated Sense HAT turn partly white and partly green. The number of green elements indicates the current humidity. If you drag the humidity slider up and down you should see the number of green elements change accordingly.

Note

If you activated environment simulation earlier, there may be some delay between you dragging the slider and the display updating. This is because the real sensor typically displays a certain delay in responding to changing humidity which the simulation emulates.

When you want to stop the script, press CTRL+C in the terminal window that appeared when you ran the script (that terminal contains the Python interpreter running your script). Feel free to modify the script and see what difference it makes when you run it. A simple change could be modifying the colours used. A more complex one could change the orientation or appearance of the “graph”.

How to use the Raspberry Pi Sense HAT

What about running our humidity script on the real Sense HAT though? Firstly, edit the script to import from sense_hat instead of sense_emu. This is the only code change that’s necessary:

Next, we need to ensure our script starts as “root”. This is because the I2C bus that the Sense HAT uses is only accessible to root by default under Ubuntu currently. Bring up Geany’s Build > Set Build Commands dialog, and change the Execute command to include a “sudo” prefix (i.e. it should read sudo python3 “%f”:

Once again, select Build > Execute from the menu. This time, the terminal that appears will prompt for your password (which sudo needs to run things as root). Once entered, you should see the script running on the actual Sense HAT on your Pi:

In the image above, the reading is quite high as I’ve just breathed on the HAT. You can try the same and watch the reading spike up before falling back down.

At this point, I’d suggest having a play with the other demo scripts available in the emulator. There’s plenty in there that deal with the various environmental sensors, the display, the joystick, and the IMU on the HAT. Have fun!

Advanced tip – going rootless

If you want to use the Sense HAT without resorting to sudo or root, you need to add some udev rules to permit access to regular users. Add the following content to a file named /etc/udev/rules.d/99-user-sense.rules (you will need to be root to create/edit this file):

SUBSYSTEM=="i2c-dev", KERNEL=="i2c-[0123456]", GROUP="plugdev", MODE="0660"

SUBSYSTEM=="input", ENV{LIBINPUT_DEVICE_GROUP}=="*:rpi-sense-joy", GROUP="plugdev", MODE="0660"

SUBSYSTEM=="graphics", ENV{ID_PATH}=="*-rpi-sense-fb", GROUP="plugdev", MODE="0660"

Note
If you’re typing this instead of copying and pasting, pay close attention to the subtle distinctions between “==” and “=”. The first entries on a line use “==” as they’re matching events; the latter entries use “=” as they’re assigning values to the device.

After creating/editing this file, reboot the machine and you should find that you can access your Sense HAT without “sudo”.

Click here to find out more about getting started with Ubuntu 21.10 on the Raspberry Pi.

rapsberry pi logo

Ubuntu Desktop for Raspberry Pi

Watch the live event of the 20.10 launch the and find out all the news about the new Ubuntu Desktop image for Raspberry Pi.

Discover more ›

Newsletter signup

Select topics you're
interested in

In submitting this form, I confirm that I have read and agree to Canonical's Privacy Notice and Privacy Policy.

Related posts

5 Things to Check Out in Ubuntu Impish Indri

For Linux desktop users, Ubuntu Impish Indri contains a number of new features plus a preview or two ahead of our next LTS release. Here are our top 5.

Raspberry Pi 400 with Ubuntu support

The Raspberry Pi Foundation has a new product — the Raspberry Pi 400. The flagship Raspberry Pi 4 was released in June 2019. Since, they added an 8GB model,...

Build a Raspberry Pi Desktop with an Ubuntu heart

On the 22nd October 2020, Canonical released an Ubuntu Desktop image optimised for the Raspberry Pi. The Raspberry Pi Foundation’s 4GB and 8GB boards work out...