How to connect HMC5883 digital compass to Arduino

We consider the connection module GY-273 with triaxial digital compass HMC5883L the Honeywell company. This chip can be used for magnetometric measurements in the navigation, if you do not want large precision measurements (with an accuracy of 1…2 degrees and the possibility of calibration). The device connects via I2C.

Цифровой компас HMC5883

Related articles:

The question «what to buy a tablet?» — 3 answer

You will need

  • — Digital compass HMC5883;
  • — Arduino;
  • — breadboard and connecting wires;
  • computer.


1. These are the main characteristics of a magnetic compass:
— 3-axis magnetically sensitive sensor;
— 12-bit ADC with a resolution of 2 MGs (milligauss);
— built-in self test.
— low operating voltage and low consumption;
digital I2C interface;
— high sample rate – up to 160 times per second (time of one measurement is approximately 6 MS);
— the accuracy of determining the direction 1°…2°;
— can be used in strong magnetic fields (up to ±8 Gauss).
Wiring diagram for magnetic sensor HMC5883L to the Arduino are shown in Fig. It is very compact and simple because the two-wire I2C interface is good because it requires a small number of links. You can use the development Board.

Подключение цифрового компаса HMC5883 к Arduino

2. You should get something like in the photo. I’m still connected to the tire SCL and SDA logic analyzer to monitor the exchange of information between the Arduino and the module HMC5883. It’s not necessary.

Цифровой компас HMC5883 подключён к Arduino на макетной плате

3. Let’s as a first acquaintance try to read identification registers 10 (0xA), 11 (0xB) 12 (0xC) digital compass HMC5883 and write here such a sketch as in the figure. It comes with a detailed review.

Скетч, считывающий идентификационные регистры HMC5883

4. The signal obtained using the logic analyzer, going to be the same in the illustration.
What does it mean? The first byte is the I2C address that we (the master, Arduino) interfacing (upper 7 bits 0x1E) and recording mode (LSB – 0x0); get the number 0x3C. The second byte is the number 0xA, which we recorded at 0x1E and an acknowledge bit from the sensor HMC5883L, which is a slave. This is the register number from which we begin to read the data. At this first transaction has ended. The next one begins. The third byte is the read request of the slave (upper 7 bit address of 0x1E, 8th bit – read operation is set to 0x1; the number of turns 0x3D). 3 the last three bytes are the response of the slave HMC5883L registers 0xA, 0xB and 0xC, respectively.
Digital compass HMC5883L with continuous readout moves through the registers yourself. I.e. to specify the register is not required (but not prohibited). For example, if we instead would write 0xA 0x3 and 10 times believed it would have received the values of 10 registers starting from 3rd to 12th.
And what is this three numbers – 0x48, 0x34, 0x33? Again taking advantage of the passport to the digital compass HMC5883L, we will see that the default values for the three identification registers.

Временная диаграмма обмена по I2C с цифровым компасом HMC5883

5. To get the data from the digital compass in the magnetic field, it is necessary to read the registers from 3rd to 8th as we read the ID registers. The only difference is that the data for each of the three axes X, Y and Z represented in the form of two byte numbers. Convert it to a decimal number, we will receive direction for each of the three axes.

Регистры данных цифрового компаса HMC5883