Well, except for declination error and any nearby metal objects, and within the resolution of the stepper motor…
Ingredients:
- Adafruit 9-degree-of-freedom accelerometer/gyro/magnetometer, wired through a
- slip ring to an
- Arduino clone with lots of spare 5-volt power, driving
- one of those cheap unipolar tiny-stepper drivers, connected to
- a 48-step baby gear-shaft stepper converted from 6 pins to 5 with a stupid custom plug, meshing with
- a 3D-printed gear with bore sized to grip the top half of the slip ring.
The design is pretty much a matter of one questionable decision leading to another. At first, I wanted the sensor firmly mounted on the hat, but then I would have needed a foolproof way of measuring the angle of the needle (because skipped steps and unknown position at power-up). That was too hard, so the sensor had to move. And that in turn meant a slip ring, because the compass needle can rotate an arbitrary number of turns. And a slip ring meant the gear-shaft stepper, because the much-more-accurate BYJ-48 couldn’t drive anything on the far side of a slip ring without being geared itself, and why print and mesh two gears instead of one? Oh, and I could have used a motor-driver chip and driven the stepper with only 4 wires, but this is Halloween, so another set of blinking lights is all to the good.
This turned out to be a pretty good Halloween piece, because it’s subtle. Nothing happens until I turn my head. (In fact, when I first went out to supervise the kids trick-or-treating, someone stopped me to say “Hey, something’s wrong with your hat, it’s not spinning!”) It generally takes people a moment to figure out what’s going on, and then they like it.
The whole thing took longer to make work than I thought, mostly because I was confused about my directions: when the compass heading is positive, I have to tell the stepper to move clockwise to bring it back to north, and vice versa when it’s negative. So for a day it tried its best to point south while overcorrecting terribly in the wrong direction. Also, averaging angles (which you have to do because the measurements are noisy) is hard unless the angles are all in one quadrant, so finally I gave up and just put in a calibration spin at startup. And if I had it to do over again, I would definitely make a better hat. This one had no internal support and really wanted to slice through my ears by the end of the night.