-1

I am trying to use the magnetometer on the sense hat to act as a compass however I also want to utilise the GPIO pins for other components. I've successfully wired up the sense hat to the GPIO pins on my Raspberry Pi 4 Model B and am having no issues with connectivity or outputs from the sense hat itself. The issue I am having is that because the sense hat is "loose" and I have it sitting next to the Raspberry Pi, whenever I run the following code to get the sense hat bearing:

from sense_hat import SenseHat

sense = SenseHat()
while True:
    print(sense.compass)

the position of the Raspberry Pi affects the results. If I rearrange the Raspberry Pi to be sitting effectively underneath the sense hat, the output is 0 or 360 when pointing to north (as expected). When I move the Raspberry Pi off to the side without changing the orientation or position of the sense hat, the output changes be nearly 10-20 degrees.

I know that I could account for this and have some extra calibration in my code but I want to avoid this for two reasons. One is I want to actually put the effort in to fix it instead of applying a fudge factor and two is my project incorporates some movement via servos and I want to make sure I can reduce the interference as much as possible.

I've played around with trying to shield the sense hat but have had little success.

Does anyone have any suggestions as to how to reduce or reject this interference? I am assuming that it is the current flowing through the wires connecting the sense hat to the GPIO pins that is affecting the magnetic field. Is there some way to shield the wires? I would have thought the current would only affect the magnetic field close to the magnetometer when actually moving through the field and wouldn't have an affect on the output when static (in different positions).

Any input would be greatly appreciated I haven't seen too much out there in terms of interference on the sense hat magnetometer.

1

Your question may be considered "off-topic" for RPi SE - but of course you wouldn't necessarily know that since you're new to RPi, and possibly to magnetometers in general. Consequently, my proposed answer won't dwell on RPi-based solutions or your software. However, you shouldn't take that as any sort of absolution for your hardware - that is your RPi, or your HAT. They (either or both of them) may be partially or completely responsible for your errant readings.

You've not identified the make and model of your Sense Hat - but even if you had, it may not lead to a better answer. As a first step, you should consider contacting the manufacturer/vendor to check for any recommendations. For example, do they counsel a certain orientation or mounting location? The vendor/manufacturer should almost always be your "first stop" in troubleshooting issues such as this. After all, they designed it, they built it, and presumably they have tested it - who would have more information than they? If the manufacturer/vendor has nothing substantial to offer, read on.

Unless you have an instrument capable of measuring magnetic fields, your solution will have to proceed largely along the lines of trial-and-error. You needn't spend a lot of money to get a useful measurements; there are a variety of methods available: 1. 2, 3, 4.

"Careless" wiring practices can create magnetic fields that will swamp the ambient magnetic field you are trying to measure. By "careless" wiring, I only mean wiring that does not take resultant generation of magnetic fields into account. If you need some background on the characteristics of magnetic fields and magnetometers, these Wikipedia articles may help. Here's a video that shows the potential significance of "careless" wiring, offers some tips on remediation, and points out that the wiring errors may not even be yours!

The presence of a ferrous metal in the vicinity of your experiments may also interfere with your attempted ambient measurements. They may perturb the ambient field enough to give erroneous results, but this may require a significant mass & fairly close proximity to make a significant difference. If you suspect an object, simply move it further away and note any difference in your measurements.

Hope this helps. Please know that it's entirely possible that your issue is not the result of a stray magnetic field at all. However, magnetometers are sensitive devices that measure a low-level ambient field, and should be included in your list of suspects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.