How does an iphone directional compass work?

My possibly wrong conception of magnetism is that it is a force that can and does interact with physical materials such as a compass needle or particles in the upper atmosphere, etc. If that’s essentially correct, I’m curious about how the compass on my phone works. After all, it’s an app that was downloaded and simply consists of lines of code. How does that translate into a sensitivity to the magnetic field of the earth? What is actually being acted upon by the earth’s magnetism?

Your phone (and most smartphones) include solid-state compass hardware to measure Earth’s magnetic field. Using multiple detectors at different orientations allows the software to convert the magnetic field strength measurements into an accurate computed magnetic bearing (i.e., figure out where “north” is).

I got curious and put a magnet close to mine, to see if I could rotate the display. It balked and put up a warning to move away from the strong magnetic field. Then I had to go through the recalibration motions before it removed the warning and began working again. (Android, if it matters)

So compass apps on phones use magnetic field detection? Huh. I had assumed that they used GPS. Interesting.

They use a whole lot of things. But GPS, by itself, can’t tell which direction you’re oriented, if you’re not moving.

A phone’s location services are a combination of data from the magnetometers, accelerometers, GPS, WiFi, cell coverage, and maps. Using all of these in combination results in far better data than any one of them alone.

You can get around this limitation by using two or more GPS receivers to make a satellite compass. Explanation in this short PDF:

Short version: if you can tell where the bow and stern of the ship are, you can work out what direction the ship is pointed, regardless of whether it’s moving or not. Since it doesn’t depend on magnetism, it works anywhere in the world where it can see GPS satellites, and it’s immune to nearby magnetic disturbances.

Phones won’t do this because a second GPS receiver would add considerable weight/bulk/cost to your phone, and also because you wouldn’t be able to get enough separation distance between the two receivers to be able to tell one end of the phone from the other and figure out which way it was pointed.

I used ‘a’ compass on my iPhone, just to see it work. It stopped working after a few times, and said I had to buy the app. I think there’s another compass on there that I don’t have to pay for, but I haven’t looked. I think the Compass icon on the screen is the pay-to-play one, and I think the free compass might be in one of the menus.

I assume the phone uses GPS location to correct for magnetic declination?

I have a (completely free) app on my phone called Physics Toolbox Suite, which provides direct access to all of the sensors on the phone, and allows logging data from them. I’ve used it most for the accelerometers, but it also has the magnetometers in it.

At least in my version of iOS, the default compass app installed as part of a standard iOS install, it’s just called Compass.

FWIW, the compass in an iPhone is very accurate.
When I was shooting the 2017 eclipse, I needed to align my tracker to due North. Since it was during the day, I couldn’t use the North Star. So, I used my iPhone and corrected for the local magnetic declination. The tracker had already been set for the correct latitude. after doing this, I centered the Sun in the frame (I was using a 400mmm lens on a cropped sensor, 600mm equivalent for 35mm), and the sun stayed in the frame for a full two hours.

Depends on the size of the ship, I suppose. Phones regularly get 3 meter accuracy, so if you have a big enough ship, you can probably get a pretty good idea of what direction you’re going. And your GPS accuracy slop will be minimized as well, because of hte

But that’s still only as accurate as your GPS receiver. Most receivers are only good to about 3-5 meters with regular old single-frequency GPS, if you’re not using some sort of differential assistance. I’m not feeling froggy enough to work out the exact amount, but you could be off by however much about 3 meters at either end translates to for a ship of whatever size. On a small boat, that could be very significant.

My latest phone is my first to have a barometer.

A few years back I put my phone in a sealed bag that I then compressed it to see if the phone’s altimetric reading was based on air pressure or GPS. I was surprised that it was air pressure.

Re-read @Machine_Elf 's post. They were talking about a phone not being big enough, even though a ship is.

GPS doesn’t give very good accuracy for the vertical axis. I’d expect most phones mostly just use the X and Y position together with an electronic topographic map, and assume that you’re at ground level. This assumption can of course cause issues: For a good while, Google thought that one of my bike commutes involved a steep 100’ descent followed near-immediately by a similar 100’ rise, because it didn’t realize I was crossing the valley on a bridge, not at ground level.

Straight GPS is better than a non-weather-corrected pressure-to-altitude mapping, though, yet my phone uses air pressure all the same. Hence my surprise. I think barometry is just part of the commodity sensor chips these days.

Topology maps might be a trick used by some online routing tools, but for a standalone altimeter it’s not viable, and I can’t believe any altimeter (or compass+altimeter) app does that. For one, it wouldn’t work in the backcountry where there’s no Internet available. But even in civilization, I’d expect an altimeter to work in many scenarios where topology can’t be known well or known at all.

It’s both. Air pressure is more accurate in the short term, but is affected by the weather. GPS gives an absolute value but with less accuracy. They use a filter to get the best of both worlds.

Same thing for position. The accelerometer and gyroscope is used to increase short-term accuracy, but drifts over time. GPS positioning is used to correct that drift.

Huh. It wasn’t on my old cheap phone, and so I just sort of forgot about it. But checking the app right now, it is on my new cheap phone. Which suggests that you’re right, that an all-the-sensors chip is just a standard commodity now, and so it’d be more expensive to make a phone without it than with it.

So far, I haven’t seen chips with barometer and the other ones integrated. 9 DoF chips are extremely common–that is, 3 axes for each of acceleration/gyroscope/magnetometer. But the barometer is separate. They’re all very cheap, regardless.