LEGO MINDSTORMS Robot Inventor
This simple project uses the hub's internal gyro sensor to imitate a handheld magnetic compass. The gyro can't tell which direction is actually north, but it starts by defining north to be the direction that the model is pointed when you run the program. Then the red pointer will always point north even when you rotate the model around.
You can press one of the hub arrow buttons to reset the current direction to north at any time, and the display also shows "N", "S", "E", or "W" if the model is within 10 degrees of pointing that direction relative to its idea of north.