« Close

Datasheets and User Guides

App Notes

Software & Driver


HMC6352 Magnetometer - I2C (App Note)

This AppNote explains the operation and use of the I2C functionality of our LabJack devices with special regards to the Honeywell HMC6352 Magnetometer.


Overview of I2C

The basis of I2C with regards to our devices is a two wire serial interface that lets you communicate to sensors or embedded systems that require a host device to properly function. The host device is initially in transmit mode and sends a start bit followed by a 7-bit address of the slave it wishes to communicate with which is followed by a single bit representing whether it wishes to write(0) or read (1) from the slave. If the slave exists on the buss then it will respond with an ACK bit for that address and the master then continues in either transmit or receive mode and the slave continues in its complementary mode. If the master wishes to write to the slave then it repeatedly sends a byte of information with the slave sending an ACK bit in response. If the master wishes to read from the slave then it repeatedly receives a byte of information from the slave and sending back an ACK bit for every byte but the last one. Once communication is finished, the data transmission concludes with a stop bit. A quick look at what these waveforms will look like, you can reference page 4 and 9 of the datasheet for the HMC6352 that we will be using: HMC6352.pdf.

Using a LabJack as the Master

Before attempting communication with a I2C slave through a LabJack, insure the following conditions are met:

  • The slave must accept 3.3V logic as that is the voltage of our FIO, CIO, and EIO lines.
  • If connecting to VS on the device, make sure that the slave accepts 5V as a supply voltage, if it doesn't consider our EB37 Experiment Board as it supplies useful supply voltages: 9.5V, 5V, 3.3V, and 1.25V.
  • Make sure that the correct pull-up resistors are installed, refer to the device datasheet for appropriate conditions.

After the above conditions are met, it will be possible to initiate communication with I2C slaves using a LabJack as the master.

Using a LabJack as the Master

The low-level function for I2C requires that you can supply it with a valid address that needs to be communicated with, a list of bytes that need to be sent to that address. You can also specify how many bytes will be received, what pins to use as SCL and SDA and other attributes that can be read in section 5.2.19 of the labjack users guide (by selecting the low-level function link, information can also be found in sections 5.2.21 for U6 and 5.3.20 for UE9)


HMC6352 Magnetometer - I2C connected to LabJack U3-LV Multifunction USB DAQ Device

1. Overview

For this example a Honeywell HMC6352 digital magnetometer is used to demonstrate the use of I2C. The U3-LV running firmware v1.24 is configured as the master and the Magnetometer has been soldered to a break out board exposing the SCL SDA VS and GND pins. Above is a picture of this test setup.

2. Connections

Since 5V(VS) is readily available on the U3-LV and the digital Magnetometer is capable of using 5V as an input voltage and there are already pull-up resistors and a decoupling capacitor installed on the included break out board by SparkFun, all that is left to do is connect the device to the LabJack. As the picture above shows, the SCL line was connected to FIO7, the SDA line was connected to FIO6, VCC to VS, and GND to GND.

HMC6352 Magnetometer - I2C with a LabJack U3-LV USB DAQ Device HMC6352 Magnetometer - I2C with a LabJack U3-LV USB DAQ Device

3. Initiating Communication

The first step in communicating to the HMC6352 and other I2C devices is figuring out what I2C address the device will respond to. This information can all be found by looking at its datasheet:HMC6352.pdf. The specific information were looking for at this time is found on page 3, the address that we need to communicate with to write information to the device is 0x42 and to read it is 0x43.

4. The Command Byte

The second step in communicating to the HMC6352 is figuring out what information to send to the device. Most of this information can be found at the top of page 5 of the datasheet in table 1. The command byte, A(41) is what we will send the Magnetometer to get a response that pertains to what direction it is facing. To send the Magnetometer other requests, simply change the command byte to a different recognized command, and add bytes after (if needed) when building the list of bytes, shown later.

5. Predicting the response and interpretation

In order to get useful information from the Magnetometer the U3 needs to be told how many bytes of information it needs to receive to get a complete amount of information. This can be found by looking at the same table that was used to find a list of valid commands. Looking closely, the command 0x41 requires that we listen for a two byte response, Response 1, the MSB of data; and Response 2, the LSB of data. These groups of information can be combined into one long binary response that can be interpreted as tenths of degrees from zero to 3599 (page 7 of the datasheet, under the title Heading Mode).

6. Useful Code

The following download is a python example program that was created during testing of this HMC6352 Magnetometer and can be used as a reference to develop for additional I2Ccompatibleslave devices.

HMC6352.py_.txt(you will need to re-name the file to HMC6352.py in order to run the example)

7. Problems and Trouble Shooting

The first problem that was encountered was that the I2C command doesn't configure the device to digital output. I found this problem when trying to use the FIO4 and FIO5 lines instead of the default FIO6 and FIO7 lines. The device I was using had FIO4 configured to be a clock output line. To quickly fix this problem, the following line sets all FIO and EIO lines to digital lines:

d.configIO(FIOAnalog = 0, EIOAnalog = 0)

The biggest problem that I was encountering was making sure that the address that I was trying to communicate with was the correct address. In the python library, the address is shifted over one bit so if you tell it to write to the hex address 0x42, it writes to 0x84. I found this out by trying to communicate to the Magnetometer in the python command line and after opening the device, "d = u3.U3()" I made set the internal debug value to true, "d.debug = True" which tells the library to print out useful debug information. Using this information I looked at each of the bytes that was being sent and noticed that the 11th byte (the address byte as defined by 5.2.21 in the users guide) being sent was 0x84 instead of the proper 0x42. Looking deeper into the U3.py library it became clear that the address was being increased by one binary digit, so "0,0,1,0,1,0,1,0" was becoming "0,1,0,1,0,1,0,0". This can be easily avoided by either calling the address 0x21 instead of 0x42 or setting the AddressByte attribute of the I2C function to the address that you want to call.

Support - Further I2C Resources

Further reading and Examples for I2C

Some posts and more resources about using LabJack devices with I2C sensors can be found on the forums section of our website as well as in some external locations:

  • There are some I2C examples for LabVIEW, Matlab, and Lua for the UD devices as well as devices that support LJM on GitHub in the I2C-AppNotes repository. In particular, look at the LJM_I2C_Utils.m file for LJM compatible devices and the UD_I2C_Utils.m file for UD compatible devices.
  • There are some detailed descriptions of I2C on UD devices on DAQFactory's forum regarding the MCP23017 I2C chip on a forum topic titled I2C Problem (MCP23017)
  • The I2C Simulator is a quick visualization tool aimed to help users understand how the LJM I2C registers affect the resulting I/O data pattern that you would see on a logic analyzer.
  • The generic I2C AppNote describes some general properties about I2C and has some logic analyzer screen shots describing some of the I/O patterns.