HMC5883L Magnetometer

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
daemeon
Posts: 10
Joined: Wed May 01, 2019 3:05 pm

HMC5883L Magnetometer

Post by daemeon » Fri May 03, 2019 3:03 am

Can someone help me get this compass working? I am using code from https://github.com/gvalkov/micropython- ... mc5883l.py

The main program is this (full module):

from hmc5883l import HMC5883L

sensor = HMC5883L(scl=4, sda=5)

x, y, z = sensor.read()
print(sensor.format_result(x, y, z))


The line from the hmc5883l driver that gives the error is this (including the line before):

Line 20: def __init__(self, scl=4, sda=5, address=30, gauss='1.3', declination=(0, 0)):
Line 21: self.i2c = i2c = machine.I2C(scl=machine.Pin(scl), sda=machine.Pin(sda), freq=100000)

There is the error:

Traceback (most recent call last):
File "main.py", line 3, in <module>
File "hmc5883l.py", line 21, in __init__
TypeError: can't convert 'int' object to str implicitly

I don't see where an int is being converted to string. Can anyone help me? I hope I have included all relevant information. Thanks.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: HMC5883L Magnetometer

Post by Roberthh » Fri May 03, 2019 5:45 am

Which hardware module and which firmware version are you using?
Edit: I just loaded your script to an ESP8266. It does not raise the error you post. But since the sensor is not attached, it fails at Line 24.
Firmware/Module version: MicroPython v1.10-315-ge70c438c7-dirty on 2019-05-01; ESP module with ESP8266
Edit 2: When using a PyBoard, the Pin names are different, like "Y9" and "Y10" or "X9" and "X10".

daemeon
Posts: 10
Joined: Wed May 01, 2019 3:05 pm

Re: HMC5883L Magnetometer

Post by daemeon » Fri May 03, 2019 6:55 am

Roberthh wrote:
Fri May 03, 2019 5:45 am
Which hardware module and which firmware version are you using?
Edit: I just loaded your script to an ESP8266. It does not raise the error you post. But since the sensor is not attached, it fails at Line 24.
Firmware/Module version: MicroPython v1.10-315-ge70c438c7-dirty on 2019-05-01; ESP module with ESP8266
Edit 2: When using a PyBoard, the Pin names are different, like "Y9" and "Y10" or "X9" and "X10".
It's a pyboard 1.1. The sensor is just marked hmc5883L.

daemeon
Posts: 10
Joined: Wed May 01, 2019 3:05 pm

Re: HMC5883L Magnetometer

Post by daemeon » Fri May 03, 2019 7:24 am

Ok. Thanks. It was a) the pins needed to be set as Y9 and Y10, and b) the address of the sensor is 13 and not 30 as most of the documentation says.

HOWEVER: the program is now only returning values X: 0.0000, Y: 0.0000, Z: 0.0000, Heading: 0° 0′

Any idea why?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: HMC5883L Magnetometer

Post by Roberthh » Fri May 03, 2019 8:35 am

I have a board here, and it contained a QMC5883 with address 13, instead of a HMC5883 at address 30. Maybe that's what happens in your case too.
What confused me with my board was, that is was connected to a MPU6050 as slave device, and I had to enable that slave port first. But until then, it was not visible on the i2c bus.

daemeon
Posts: 10
Joined: Wed May 01, 2019 3:05 pm

Re: HMC5883L Magnetometer

Post by daemeon » Sat May 04, 2019 4:54 am

I read that the HMC code does not work with the Chinese clone QMC code. I will look for code for QMC.

WRR
Posts: 7
Joined: Wed Jan 09, 2019 8:33 pm

Re: HMC5883L Magnetometer

Post by WRR » Mon May 06, 2019 10:19 pm

If your sensor acknowledges the I2C transaction but still returns all zeros for the data, it's also possible that the sensor is damaged.

Especially with cheap modules from "no-name" sources, you can sometimes get parts from a bad batch. I've received accelerometer/gyroscope sensors which were able to communicate and read/write to their status registers, but their output registers always returned 0s. I guess it's possible that they were counterfeit rather than damaged given the provenance, but it seems more likely that they came from lots that didn't pass QA or something like that. When I tried a more expensive board from a more reputable vendor, the same code ended up working.

So if you aren't sure about where the parts came from, you might try checking whether your sensor returns valid readings if you use a different platform such as an Arduino example sketch. Or you could see if your MicroPython code works with an HMC5883L sensor from a different vendor.
Remember, the "S" in "IoT" stands for "Security".

johnnyhansen
Posts: 2
Joined: Sun Oct 20, 2019 10:39 am

Re: HMC5883L Magnetometer

Post by johnnyhansen » Sun Oct 20, 2019 10:45 am

Hi There.

Im quite new to MicroPython but got it running on a ESP8266 D1 board. When I load the script in this thread I get a strange error

>>> sensor = HMC5883L(scl=4, sda=5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "hmc5883l.py", line 30, in __init__
OSError: [Errno 19] ENODEV

What is wrong here:-)?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: HMC5883L Magnetometer

Post by Roberthh » Sun Oct 20, 2019 10:56 am

The code does not find the sensor. Maybe it is a problem with labeling. According to you code, you connect to GPIO4 and 5. On the Wemos D1 device these are Pins D2 (GPIO4) and D1 (GPIO5). Maybe you device has a different labeling. so please check.

johnnyhansen
Posts: 2
Joined: Sun Oct 20, 2019 10:39 am

Re: HMC5883L Magnetometer

Post by johnnyhansen » Fri Oct 25, 2019 6:36 pm

Thanks for your reply. I found the issue. In the __init__ I2C speed was set for 10000 pbs so reducing it to 1000 it solved the problem:-)
Thanks for your support and reply

Post Reply