How To Connect Digital Compass HMC5883 To Arduino

Table of contents:

How To Connect Digital Compass HMC5883 To Arduino
How To Connect Digital Compass HMC5883 To Arduino

Video: How To Connect Digital Compass HMC5883 To Arduino

Video: How To Connect Digital Compass HMC5883 To Arduino
Video: hmc5883l arduino compass,magnetometer sensor,compass navigation"Arduino Project for beginners 2024, December
Anonim

Let's consider the connection of the GY-273 module with the Honeywell HMC5883L three-axis digital compass. This microcircuit can be used for magnetometric measurements, in navigation, if a high measurement accuracy is not required (with an error of 1 … 2 degrees and the possibility of calibration). The device is connected via the I2C interface.

Digital Compass HMC5883
Digital Compass HMC5883

Necessary

  • - Digital compass HMC5883;
  • - Arduino;
  • - prototype board and connecting wires;
  • - computer.

Instructions

Step 1

These are the main features 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 interface I2C;

- high polling rate - up to 160 times per second (time of one measurement is about 6 ms);

- the accuracy of determining the direction is 1 °… 2 °;

- can be used in strong magnetic fields (up to ± 8 Gauss).

The diagram for connecting the HMC5883L magnetic sensor to the Arduino is shown in the figure. It is very compact and simple, because The two-wire I2C interface is great because it requires few connections. You can use a breadboard.

Connecting the HMC5883 digital compass to the Arduino
Connecting the HMC5883 digital compass to the Arduino

Step 2

It should look something like the photo. I will also connect a logic analyzer to the SCL and SDA buses to monitor the information exchange between the Arduino and the HMC5883 module. It's not obligatory.

HMC5883 digital compass connected to Arduino on breadboard
HMC5883 digital compass connected to Arduino on breadboard

Step 3

As a first acquaintance, let's try to read the identification registers 10 (0xA), 11 (0xB) and 12 (0xC) of the digital compass HMC5883 and write such a sketch as in the figure. It is provided with detailed comments.

Sketch reading the identification registers of the HMC5883
Sketch reading the identification registers of the HMC5883

Step 4

The signal obtained with the logic analyzer will be as shown in the illustration.

What does it mean? The first byte is the I2C address with which we (the master device, Arduino) establish communication (high 7 bits 0x1E), and the write mode (low bit - 0x0); the number is 0x3C. The second byte is the number 0xA, which we wrote to address 0x1E and the confirmation bit from the HMC5883L sensor, which is the slave. This is the register number from which we will start reading data. This concludes the first transaction. The next one begins. The third byte is a read request from the slave (the most significant 7 bits are the address 0x1E, the 8th bit is the read operation 0x1; the resulting number is 0x3D). The last 3 bytes are the response from the HMC5883L slave from registers 0xA, 0xB, and 0xC, respectively.

The digital compass HMC5883L moves through the registers independently during continuous reading. Those. it is optional (but not prohibited) to specify the case each time. For example, if instead of 0xA we would write 0x3 and read 10 times, we would get values in 10 registers, starting from the 3rd to the 12th.

And what are these three numbers - 0x48, 0x34, 0x33? Using the data sheet for the HMC5883L digital compass again, we will see that these are the default values for the three identification registers.

Timing diagram of the I2C exchange with the HMC5883 digital compass
Timing diagram of the I2C exchange with the HMC5883 digital compass

Step 5

To get the digital compass data on the magnetic field, you need to read registers 3 through 8, just like we read the identification registers. The only difference is that the data for each of the three axes X, Y and Z are presented as double-byte numbers. Converting them to decimal numbers, we get directions along each of the three axes.

Recommended: