hmc5883l Compass Tutorial
hmc5883l Compass Tutorial
1. THE SENSOR
1.1. The HMC5883L Compass is a type of sensor called a magnetometer. This
sensor measures the magnetic field strength (in microTesla) of the Earth and
provides its component values (x, y, z). It outputs those three measurements and
calculates your orientation with respect to true north.
1.2. Limitation: The sensor must be calibrated in order to provide useful information.
2. HARDWARE CONNECTIONS
3. SUPPORTING PROGRAMS
3.1. The packages youll need are: I2C tools, python smbus
First, I2C automatic loading must be enabled through the Pis
configuration tool. Enter the configuration tool in the terminal by entering
sudo raspi-config. Then choose option 8, then option A7.
When youre returned to the terminal, run an update and upgrade before
downloading the packages. This is done by entering the following two
commands:
sudo apt-get update
sudo apt-get upgrade
Next download the packages with the following two commands:
sudo apt-get install i2c-tools
sudo apt-get install python-smbus
3.2. Libraries: Quick2Wire Python API, sensor library
The libraries can also be downloaded straight from your terminal.
Open the terminal and navigate to your projects directory.
Download libraries by entering git clone [library url] like this:
5. SAMPLE PROGRAM
5.1. Line 1: Imports sensor functions from I2C library.
5.2. Line 2: Selects I2C port.
5.3. Line 3: Specifies that sensor should continuously make readings.
5.4. Line 4: Passes the magnetic declination of your location as the argument. The
magnetic declination is the angle between magnetic north and true north at your
location.
5.5. Line 5: Prints the magnetic field strength measurements and calculated compass
heading.