Tag Archives: Smartphone

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

By: Robert Walsh

Engage

¿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:

Explore

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.

Explain

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.

Extend

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?

Evaluate

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?

References

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

Lesson: Exploring the Gyroscope using the Raspberry Pi, the Sense HAT module, and Scratch

By: Robert Walsh

Engage

Have you ever played a video game where all you had to do was tilt the controller to direct the on-screen action? The Nintendo Wii was one of the first gaming systems to employ this technology when it was released in 20061, and smartphone and tablet games like Temple Run and Subway Surfers use it too. Did you stop playing the game long enough to wonder how this works?

In this lesson, we will use a Raspberry Pi with the Sense HAT module to create a simple scene where you just need to tilt the Raspberry Pi to control a rocket ship. To complete this activity, you will need:

Explore

The video below will walk you through creating a Scratch program that allows you to control a rocket ship in space using a Raspberry Pi with a Sense HAT module. This is the same technology used in smartphones and video game controllers.

If you need help getting your program working, try these downloads:

Note: The complete project download is a ZIP file and will have to be expanded to get to the Scratch program. Scratch programs have an .SB3 file extension.

Explain

The Sense HAT contains an inertial monitoring unit (IMU) that is able to detect motion2. One component of the IMU is a gyroscope that detects rotation. When the Sense HAT is tilted forward or backward, a value called roll changes. When it is tilted left or right, the pitch changes. The Sense HAT can also detect a twisting rotation (like if you left the Raspberry Pi flat on a surface and rotated it to the left or right without tilting it). This is called yaw. Our program did not use the yaw value.

Airplanes, spacecraft, and other flying objects use pitch, roll, and yaw to describe their motion3. Pitch generally indicates whether the object is climbing or diving, while roll typically the tilt to the left or the right. (The Sense HAT extension for Scratch, though, uses forward and backward for roll and left or right for pitch.). Yaw shows where the nose is pointed.

This project also taught us about the XY coordinate plane. We used XY coordinates to position our sprite and to indicate which of the Sense HAT’s LEDs we wanted lit. For the sprite, the origin (the center or the point whose X and Y coordinates are both zero) was located in the center of the stage. X values greater than zero were to the right of center, while those with values less than zero were to the left. Y values greater than zero were above the center, and those less than zero were below. To move the rocket to the right, we increased the X coordinate, and to move to the left, we decreased the X. Likewise, to go up, we increased the Y, and to go down, we decreased it.

The Sense HAT LED matrix uses XY coordinates, too, but the origin is not in the center. It is in the upper left corner. As we move to the right, the X coordinate gets larger. However, as we move down, the Y coordinate gets larger. This is the opposite of the coordinate system for the sprite where larger Y values were higher on the screen. Another important point about the coordinates for the LEDs on the Sense HAT is that the numbering starts at zero, not one. For example, an LED in the first column has an X coordinate of 0 and one in the last column has an X coordinate of 7 even though there are 8 columns. The same is true for the Y coordinates, but instead of columns, the Y indicates the row.

Extend

In addition to game controllers and flying crafts, gyroscopes and IMUs are used in other devices, too. Segways, innovative personal transportation devices sometimes used by police officers and security guards in places where other vehicles are impractical, also use internal gyroscopes to help keep the Segway upright and to detect which way the rider is leaning4.

There are many ways that we could extend the rocket ship program. For example, we could make the ship move faster in a given direction based on how long the Sense HAT is tilted. This would make the movement more realistic. We could also use the yaw value to turn the ship rather than the left or right tilt. This might be a more appropriate control system game with a car or even a person. A game viewed in the first-person where the player sees what the character sees could use both tilt and yaw. One could turn the character, while the other could just turn the head to look in a different direction.

Another change could involve adding obstacles that the player must avoid. Or the player could be expected to collect the other objects on the screen rather than to avoid them.

What other adaptations could you think to make?

Evaluate

What we learned in this lesson:

  • How to interact with the Sense HAT module using Scratch 3 on a Raspberry Pi
  • How the gyroscope component of the IMU detects motion and reports pitch and roll
  • How to locate an object on an XY coordinate plane

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?

References

1Romero, J. (2006, December 18). How do motion-sensing video game controllers work? ScienceLine. https://scienceline.org/2006/12/motioncontrollers/

2Raspberry Pi Foundation. (n.d.). Getting started with the Sense HAT. https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/8

3SoftwarePole. (2015, March 25). Airplane control – roll, pitch, yaw [YouTube video]. https://www.youtube.com/watch?v=pQ24NtnaLl8

4Harris, T. (n.d.). How Segways work. How Stuff Works. https://science.howstuffworks.com/transport/engines-equipment/ginger.htm

Technological fluency, not computer literacy

By: Robert Walsh

I graduated high school in 1988, and personal computers were only just beginning to invade the home. Phones still had cords that tethered them to the wall, and many had rotary dials instead of push buttons. A digital watch was the height of mobile technology, particularly if it had alarms, could keep time in multiple time zones, or sported a mini calculator. My generation was on the leading edge of the transition from a world where computers were used only for specialized purposes to one where they have become a ubiquitous part of everyday life. As such, it was important that people in that time learned how to use computers to help with routine tasks. It was less important that they knew much about how they worked. Knowing how to use a word processor or a spreadsheet provided an advantage over others who did not have those skills.

It is no longer 1988, however, and computers are everywhere. More than 50% of children in the United States have their own smartphones by the time they are 11 years old, and over 80% of teenagers have them1. They know how to use them, too. I’ve seen kids record a video, edit it, and publish it to YouTube, Instagram, Facebook, or some other social media platform using only their cell phones. In this age, typewriters and calculators have been rendered almost obsolete, and being able to use a word processor or a spreadsheet is a life skill, not a competitive advantage.

Marina Bers describes this as the difference between being computer literate and technologically fluent2. She explains that computer literacy is the ability to use a computer and the programs running on it to accomplish specific tasks. Technological fluency, though, is the ability to use a computer “in a creative and personally meaningful way” (p. 14) in the same way that someone fluent in a foreign language can communicate “effortlessly and smoothly” (p. 14).

As we work to provide STEM education opportunities, we must strive to create technological fluency, not simply computer literacy. Students need to understand how technology works, not just how to use it. One who knows only how to use computers to do things others have made possible will always be dependent, while those who grasp the inner workings will be able to extend that which exists and innovate to create that which will be needed.

1Kamenz, A. (2019, October 31). It’s a smartphone life: More than half of U.S. children now have one. NPR. https://www.npr.org/2019/10/31/774838891/its-a-smartphone-life-more-than-half-of-u-s-children-now-have-one

2Bers M. U. (2010). Beyond computer literacy: supporting youth’s positive development through technology. New directions for youth development2010(128), 13–23. https://doi.org/10.1002/yd.371