Digital compass HMC5883L with python and the raspberry pi

I recently installed the hmc5883l which I ordered from amazon on my robot.
The plan is to do some simple navigation experiments.

The assembly was unproblematic. I connected the i2c bus to the adafruit servo controller which in turn is connected to the pi.

pi robot2 with hmc5883l

But I ran into a little bit of a problem, there is a very good library in python that allows easy access to the hmc5883l with python. But I already use the Adafruit I2C library for controlling the servo motors. Fortunately the code was very easy to adapt, which I did.

Here is my adapted code:

For this to work you first have to set your declination. You can get the declination for your current position here.

Example for reading the data:


This entry was posted in python, raspberry pi, robots. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *