Tutorial to Interface HMC5883L Compass Sensor With Arduino

58,058

73

10

Introduction: Tutorial to Interface HMC5883L Compass Sensor With Arduino

Description

HMC5883L is a 3-axis digital compass used for two general purposes: to measure the magnetization of a magnetic material like a ferromagnet, or to measure the strength and, in some cases, the direction of the magnetic field at a point in space. Communication with the HMC5883L is simple and all done through an I2C interface. There is an on board regulator. The breakout board includes the HMC5883L sensor and all filtering capacitors. The power and 2-wire interface pins are all broken out to a 0.1" pitch header. Uses famous HMC5883L magnetometer chip. Supports 3.0V to 5.0V IO levels on I2C SCL and SDA pins.

Specification

  • Power supply :3V - 5V
  • IO Voltage Level: 3V - 5V
  • Communication: Standard IIC communication protocol
  • Module Dimensions: 14.35 mm (L) x 13.16 mm (W) x 3.40 mm (H)
  • PCB thickness: 1.60 mm
  • Measuring range: ± 1.3-8 Gauss

Step 1: Materials Preparation

In this tutorial, you will need :

1. Arduino Uno Board and USB Cable.
2. HMC 5883L Compass Sensor
3. Jumper Wires
4. LCD 16X2
5. Breadboard
6. 10K Potentiometer
7. Arduino IDE Software

Step 2: Pin Description

VCC : Power supply

GND : GND power

SCL: I2C Clock Input

SDA : I2C Data IO

DRDY: Data Ready Output

Step 3: Pin Connection

Connect HMC5883L to Arduino Uno Board as following :

  • VCC to +5V
  • GND to GND
  • SCL tO A5
  • SDA to A4

Connect LCD to Arduino Uno Board as following :

  • VSS to +5V
  • VDD to GND
  • RS to 12
  • RW to GND
  • E to 11
  • D4 to 5
  • D5 to 4
  • D6 to 3
  • D7 to 2
  • A/VSS to +5V
  • K/VDD to GND

Connect 10K Potentiometer to LCD as following (refer image for potentiometer pinout) :

  • GND to GND
  • Data to v0
  • VCC to +5V

Step 4: Connect Arduino to Computer

After u have complete your circuit, connect your Arduino Uno Board to your computer via USB Cable. You can see your LCD is on.

Step 5: Sample Source Code

You may download this sample source code and open it on your Arduino IDE. Choose the correct board and port. Then, upload your Code into your Arduino Uno Board.

Step 6: Library

You have to download these libraries before uploading the sample source code into your Arduino IDE to allow Arduino to communicate with LCD and HMC5883L. Download the ZIP file below > Open Zip File > Extract to your Arduino Uno Library folder. Refer the image above for your references.

Step 7: Corresponding Output

As illustrated, twisting or turning the device will provide the corresponding outputs.

Step 8: Result : Serial Monitor

i. when device rotate around it's X-axis, X-axis remain the same while the other two axis changed.

ii. when device rotate around it's Y-axis, Y-axis remain the same while the other two axis changed.

iii. when device rotate around it's Z-axis, Z-axis remain the same while the other two axis changed.

Step 9: Video

This video shows HMC5883L Compass Module corresponding output when interfaced with Arduino Uno.

Step 10: News!

HMC5883L not Working!

Try download Library (Mecha_QMC5883L) and source code.

2 People Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

10 Discussions

0
George WinstonS
George WinstonS

4 years ago

I want to make a driveway alarm that will actuate if a vehicle passes but ignore animals. Do you think this would work as the sensor? Thanks

0
saxophoneguy1984chicago
saxophoneguy1984chicago

Reply 6 months ago

To detect cars...place a hose across the driveway. Seal one end and on the other, place a barometric pressure sensor (BME280) in a closed container. The tires driving over the hose will increase the value dramatically.
if sensorvalue > baseline then pin 8 goes high

0
azet1368
azet1368

1 year ago

hi thanks for your tutorial
I did all steps as you said.but the output always is like below picture . digits don't change.

compass.JPG
0
MarceloP80
MarceloP80

4 years ago

Hi, I made the tutorial as it says, however, my serial only prints 0 values for x, y and z. I didnt find the code well explained, so I don't know where could be the problem. But anyway, thanks for the tutorial!

0
MrSottong
MrSottong

Reply 1 year ago

your module might be qmc5883l not hmc5883l

0
MamunEdt
MamunEdt

Reply 3 years ago

active delay command

0
MamunEdt
MamunEdt

Reply 3 years ago

but i dont get acurate valu of it...

0
meharaj Kazi
meharaj Kazi

Question 1 year ago on Step 8

Sir we connect sensor with arduino and and checking readings on serial monitor but it shows HMC5883L not fount plz check wiring. Then we use logic level convertor between them then also it show same warning. Plz help me.

0
HaCo
HaCo

3 years ago

Thank you for the sample with the HMC5883L chip, this made be better understand how it works!

0
RobertoDuarte
RobertoDuarte

3 years ago

Dears. Probably there is a mistake in the conections of the LCD display.

A/VDD is 5V and K/VSS is GND.

Ok?

Thanks