Lesson: Exploring the Magnetometer using the Raspberry Pi, the Sense HAT module, and Python

By: Robert Walsh


¿Que le dijo un imán a otro imán? Respuesta: Eres muy atractivo1

(What did one magnet tell another one? Answer: You are very attractive)

Does your smartphone or tablet have a compass app? Most do (or at least one is available if not installed by default). Have you ever used it? What kind of information does it provide? Have you ever wondered how it works?

Round grey and black compass

The compass app on your smartphone is intended to emulate (or pretend to be) a real compass like the one seen here. A compass uses a magnet to indicate which direction is north2. In this lesson, we will use a Raspberry Pi with the Sense HAT module to demonstrate how a compass works. To complete this activity, you will need:


The video below will walk you through creating a Python program that will point you in the right direction! Well, it will at least tell you which way is north. This is the same technology used in your smartphone to power the compass app.

Note: The download is a ZIP file and will have to be expanded to get to the Python source code. Python programs have an .py file extension.


The Sense HAT contains an inertial monitoring unit (IMU) that includes a component called a magnetometer3. “A magnetometer is a scientific instrument used to measure the strength and direction of the magnetic field in the vicinity of the instrument” (section 4.2.4, para. 1)4. The magnetometer translates its internal readings into a direction, so, just like a magnet, a magnetometer can tell us which way is north.

Evidence exists to suggest that humankind has understood the principles of magnetism since the very earliest recorded history. Compasses were first used to aid navigation beginning around 1000 or 1100 CE in China2. Today, compasses can be found in scouts’ backpacks as well as in airplanes and spacecraft. Some may be actual magnets while others use magnetometers just like the one in the Sense HAT module.

In our Python code, we queried the Sense HAT’s magnetometer, and it told us the direction of north measured in degrees. We then translated those degrees into XY coordinates to determine which pixels on the Sense HAT’s LED matrix to light up. As the Raspberry Pi and the Sense HAT are rotated, the magnetometer updates and our Python code redraws the display on the LED matrix so that the lit pixels are always point north.


Apple was perhaps the first company to incorporate the magnetometer into their smartphones in 2009. In October, 2020, Apple filed for a new patent describing how the magnetometer could be made to provide more accurate readings5.

Researchers have found other ways to improve the accuracy of magnetometers, particularly when they are used in environments that distort or interfere with the Earth’s natural magnetic fields. In one such innovation, multiple magnetometers are used, and their readings are combined using a complex mathematical formula. This technology is used in virtual reality video games and in a system used to assist patients when rehabilitating after certain kinds of injuries6.

The Python program could be extended to show the letters to indicate cardinal direction that the Raspberry Pi and Sense HAT module are facing. For example, If the magnetometer reads 45 degrees, then the device is facing north west. (In other words, the north is 45 degrees to the right or east of the direction the device is facing.). Because of the limited space available on the Sense HAT LED matrix, it might be necessary to cycle between the compass display and the cardinal direction. You could even add a third state that displays the compass heading in degrees.

The program could be further improved by introducing functions to encapsulate some of the lower-level steps. For example, coloring the pixel for north red actually involves also clearing the red color from the last pixel representing north. These two steps could be combined into a function called point_north.

How else could you think to use a magnetometer? What other changes might you make to the Python program we wrote in this lesson?


What we learned in this lesson:

  • How to interact with the Sense HAT module using Python 3 on a Raspberry Pi
  • How the magnetometer component of the IMU detects and reports which direction is north
  • How to locate an object on an XY coordinate plane
  • How to use Python lists (arrays), tuples, for loops, and while loops
  • How to use a proportion to convert from one set of units to another

How did you do with the lesson?

  • What parts were easy and what parts were confusing?
  • Were any parts a review of things you already knew?
  • What would you like to know more about?


1Arreguin-Anderson, M. G., & Ruiz-Escalante, J. A. (2018). Adivinanzas and dichos: Preparing prospective educators to teach science by incorporating culturally responsive tools. Journal of Latinos and Education, 17(1), 84-91. https://dx.doi.org/10.1080/15348431.2016.1257447

2National Geographic. (n.d.) Compass. https://www.nationalgeographic.org/encyclopedia/compass/

3Raspberry Pi Foundation. (n.d.). Sense HAT. https://www.raspberrypi.org/products/sense-hat/?resellerType=home

4Bai, Y., & Bai, Q. (2019). 4 – Subsea surveying, positioning, and foundation. In Y. Bai & Q. Bai (Eds.), Subsea Engineering Handbook (Second Edition) (pp. 81-121). Gulf Professional Publishing. https://doi.org/10.1016/B978-0-12-812622-6.00004-X

5Purcher, J. (2020, October 9). Apple reveals their new magnetometer architecture that will advance compass and mapping apps & future gaming. Patently Apple. https://www.patentlyapple.com/patently-apple/2020/10/apple-reveals-their-new-magnetometer-architecture-that-will-advance-compass-and-mapping-apps-future-gaming.html

6Whittmann, F., Lambercy, O., & Gassert, R. (2019). Magnetometer-based drift correction during rest in IMU arm motion tracking. Sensors, 19(6), 1312. https://dx.doi.org/10.3390%2Fs19061312

Leave a Reply

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