Raspberry Pi B.Tech/Diploma Beginner

Interfacing HMC5883L Magnetometer with Raspberry Pi

Interfacing HMC5883L Magnetometer with Raspberry Pi 4 using Python Code to read Magnetic field value in uT & find bearing in degress

  • Suitable forB.Tech/Diploma Raspberry Pi students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

Overview
In this tutorial, we will explore how to connect the HMC5883L Triple Axis Digital Magnetometer to a Raspberry Pi and harness its capabilities using Python. The HMC5883L stands out as an optimal choice for digital compass applications, thanks to its three-dimensional magnetometer features that allow for precise measurements of magnetic field strength and orientation. This makes it an invaluable tool for pinpointing the Earth’s magnetic north.
Upon integrating the HMC5883L magnetometer with the Raspberry Pi, we will be able to fetch compass readings and ascertain the magnetic field across the X, Y, and Z axes in terms of microteslas (µT). From this data, determining the heading in degrees becomes a straightforward computation. We’ll be guiding you through every step of the process, ensuring you can seamlessly execute these tasks with Python on your Raspberry Pi.
Bill of Materials
We need following components to work on this project.
S.N.
Components
Quantity
Purchase Link

Raspberry Pi 4

Amazon
AliExpress
SunFounder

HMC5883L Magnetometer

Amazon
AliExpress
SunFounder

Jumper Wires

Amazon
AliExpress
SunFounder

Breadboard

Amazon
AliExpress
SunFounder
HMC5883L 3-Axis Compass/Magnetometer
The HMC5883L is a digital compass conceived by Honeywell. It is a surface-mount, multi-chip module engineered specifically for the detection of low magnetic fields. It is particularly adept at supplying directional data, making it a common choice for navigation and positioning systems.
The design of the HMC5883L revolves around anisotropic magnetoresistive (AMR) technology, which facilitates the precise measurement of magnetic fields. It includes three-axis sensors, with field capacities spanning from ±1.3 to ±8.1 Gauss. This denotes its capability to measure magnetic fields across three distinct axes (X, Y, and Z), allowing it to sense the direction of the Earth’s magnetic field in a three-dimensional context.
The HMC5883L is equipped with numerous inherent features that simplify its incorporation into a variety of systems. Among these are an integrated ADC (Analog-to-Digital Converter) offering 12-bit data output, and an I2C serial bus interface. This interface is utilized for device communication, enabling it to transfer data to a microcontroller or another processing unit.
The HMC5883L Magnetometer Module is comprised of an HMC5883L Magnetometer IC, a Voltage Regulator IC, resistors, and capacitors, all consolidated within an integrated circuit. It uses a voltage regulator IC
XC6206P332MR
(662K).
Refer to HMC5883L datsheets to learn more:
Download HMC5883L Datasheet
Specifications of HMC5883L
Operating Voltage: 3V to 6V DC
Operating Current: 100-130 μA
I2C interface
1-2 degree heading accuracy
Integrated 12-bit ADC
160Hz max data rate
Range of -8 to +8 Gauss
Data Output Rate: 0.75, 1.5, 3, 7.5, 15 Hz
Pinout of HMC5883L
The HMC5883L module has five pins as shown in this image below:
Here is a basic description of the typical pinout for an HMC5883L module.
VCC
: This is the power supply pin. It requires a DC voltage, typically 3V -6V.
GND
: This is the ground pin.
SCL
: This is the clock line for the I2C interface.
SDA
: This is the data line for the I2C interface.
DRDY
: This is Data Ready pin which goes low when new data is available. It’s optional and isn’t always used in basic applications.
Register Mapping & I2C Communication
Register Address
Access

Configuration Register A
Read/Write

Configuration Register B
Read/Write

Mode Register
Read/Write

Data Output X MSB Register
Read

Data Output X LSB Register
Read

Data Output Z MSB Register
Read

Data Output Z LSB Register
Read

Data Output Y MSB Register
Read

Data Output Y LSB Register
Read

Status Register
Read

Identification Register A
Read

Identification Register B
Read

Identification Register C
Read
The HMC5883L magnetometer uses various internal registers to configure and receive data:
Configuration Register A (0x00):
Configures device operation, including output data rate and measurement configuration.
Configuration Register B (0x01):
Sets the device gain and sensitivity.
Mode Register (0x02):
Selects the operating mode, such as continuous measurement, single measurement, or idle mode.
Data Output X Registers (0x03 and 0x04):
Hold X-axis sensor readings.
Data Output Y Registers (0x05 and 0x06):
Hold Y-axis sensor readings.
Data Output Z Registers (0x07 and 0x08):
Hold Z-axis sensor readings.
Status Register (0x09):
Contains status flags about data output.
Identification Registers (0x0A, 0x0B, 0x0C):
Used to identify the device.
The HMC5883L employs the I2C communication protocol, functioning as a slave device. Its designated I2C device address is 0x1E. The corresponding addresses for its read and write operations are as follows:
For write operations (SLA+W), the address is 0x3C.
For read operations (SLA+R), the address is 0x3D.
Interfacing HMC5883L Magnetometer with Raspberry Pi Pico
Let us interface the HMC5883L Magnetometer with Raspberry Pi 4 using Python code and extract magnetometer readings for the X, Y, and Z axes, as well as the heading information.
The connection diagram is straightforward.
Connect the VCC & GND pin of the HMC5883L magnetometer to the 3.3V & GND pin of the Raspberry Pi. Likewise, join the SDA & SCL pins of the Magnetometer to the SDA (GPIO2) & SCL (GPIO3) pins of the Raspberry Pi respectively.
Setting Up the Raspberry Pi
Before moving directly to the Python programming part, we need to setup the Raspberry Pi.
1. Enable I2C on the Raspberry Pi
:
Open a terminal and type
sudo raspi-config
Navigate to
Interfacing Options
>
I2C
and enable it.
Reboot the Raspberry Pi.
2. Install the necessary Python packages
:

sudo
apt
get
update
sudo
apt
get
install
i2c
tools
python3
smbus
3. Detect the device
:
After connecting the HMC5883L, you can detect it using the following command:

i2cdetect
y

If everything is connected properly, you should see an address
1E
appear in the list, which is the default address for the HMC5883L.
Python Code for Interfacing HMC5883L with Raspberry Pi
Here is a Python Code to interface HC5883L Magnetometer with Raspberry Pi 4. Copy the following code and paste it on your Thonny IDE. Then Save it with any name.
This simple Python script will read data from the HMC5883L.

import
smbus
import
time
import
math
# HMC5883L register addresses
ADDRESS
=
0x1E
CONFIG_A
=
0x00
CONFIG_B
=
0x01
MODE
=
0x02
X_MSB
=
0x03
Z_MSB
=
0x05
Y_MSB
=
0x07
bus
=
smbus
SMBus
(

)
def
setup
(
)
:
bus
write_byte_data
(
ADDRESS
CONFIG_A
0x70
)
# Set to 8 samples @ 15Hz
bus
write_byte_data
(
ADDRESS
CONFIG_B
0x20
)
# 1.3 gain LSb / Gauss 1090 (default)
bus
write_byte_data
(
ADDRESS
MODE
0x00
)
# Continuous measurement mode
def
read_raw_data
(
addr
)
:
# Read raw 16-bit value
high
=
bus
read_byte_data
(
ADDRESS
addr
)
low
=
bus
read_byte_data
(
ADDRESS
addr
+

)
# Combine them to get a 16-bit value
value
=
(
high
<<

)
+
low
if
value
>
32768
:
# Adjust for 2's complement
value
=
value
65536
return
value
def
compute_heading
(
y
)
:
# Calculate heading in radians
heading_rad
=
math
atan2
(
y
)
# Adjust for declination angle (e.g. 0.22 for ~13 degrees)
declination_angle
=
0.22
heading_rad
+=
declination
_
angle
# Correct for when signs are reversed.
if
heading_rad
<

:
heading_rad
+=

*
math
pi
# Check for wrap due to addition of declination.
if
heading_rad
>

*
math
pi
:
heading_rad
-=

*
math
pi
# Convert radians to degrees for readability.
heading_deg
=
heading_rad *
(
180.0
/
math
pi
)
return
heading_deg
def
main
(
)
:
setup
(
)
while
True
:
=
read_raw_data
(
X_MSB
)
y
=
read_raw_data
(
Y_

Technologies used

Raspberry PiBLEPythonI2CSPIADC

Hardware components

  • Raspberry Pi

Software & tools

  • Thonny

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.