« Close

Datasheets and User Guides

App Notes

Software & Driver

 

13.2 I2C

I2C Overview

T-series devices support master-mode Inter-Integrated Circuit (I²C or I2C) communication. I²C is a two-wire synchronous serial protocol typically used to send data between chips. The two wires are clock (SCL) and data (SDA). Any T-series digital I/O line can be as SDA or SCL. Note that the I²C bus generally requires pull-up resistors of perhaps 4.7 kΩ from SDA to Vs and SCL to Vs.

EIO and CIO digital I/O lines should be used, instead of the FIO lines, due to the extra lower output impedances on those lines. Look at the Digital I/O Specifications page for more details.

Lua scripting is often convenient for serial applications.  For example, you might write a script that runs the serial communication at a specified interval, and saves that reading in a USER_RAM register.  This puts the complications of serial communication in a script running on the T7 itself, and then the host software can collect results using a simple read of the USER_RAM register.  There are many serial examples available for Lua scripting.

This serial link is not an alternative to the USB connection. Rather, the host application will write/read data to/from the LabJack, and the LabJack will communicate with some other device using I2C. This serial protocol is considered an advanced topic. A good knowledge of the protocol is recommended, and a logic analyzer or oscilloscope might be needed for troubleshooting.

Register Listing


I2C Registers
Name Start Address Type Access
I2C_SDA_DIONUM 5100 UINT16 R/W
I2C_SCL_DIONUM 5101 UINT16 R/W
I2C_SPEED_THROTTLE 5102 UINT16 R/W
I2C_SLAVE_ADDRESS 5104 UINT16 R/W
I2C_NUM_BYTES_TX 5108 UINT16 R/W
I2C_NUM_BYTES_RX 5109 UINT16 R/W
I2C_OPTIONS 5103 UINT16 R/W
I2C_GO 5110 UINT16 R/W
I2C_ACKS 5114 UINT32 R/W
I2C_DATA_TX 5120 BYTE W
I2C_DATA_RX 5160 BYTE R
I2C_SDA_DIONUM
- Address: 5100
The number of the DIO line to be used as the I2C data line. Ex: Writing 0 will force FIO0 to become the I2C-SDA line.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_SCL_DIONUM
- Address: 5101
The number of the DIO line to be used as the I2C clock line. Ex: Writing 1 will force FIO1 to become the I2C-SCL line.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_SPEED_THROTTLE
- Address: 5102
This value controls the I2C clock frequency. Pass 0-65535. Default=0 corresponds to 65536 internally which results in ~450 kHz. 1 results in ~40 Hz, 65516 is ~100 kHz.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_SLAVE_ADDRESS
- Address: 5104
The 7-bit address of the slave device. Value is shifted left by firmware to allow room for the I2C R/W bit.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_NUM_BYTES_TX
- Address: 5108
The number of data bytes to transmit. Zero is valid and will result in a read-only I2C operation.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_NUM_BYTES_RX
- Address: 5109
The number of data bytes to read. Zero is valid and will result in a write-only I2C operation.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_OPTIONS
- Address: 5103
Advanced. Controls details of the I2C protocol to improve device compatibility. bit 0: 1 = Reset the I2C bus before attempting communication. bit 1: 0 = Restarts will use a stop and a start, 1 = Restarts will not use a stop. bit 2: 1 = disable clock stretching.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
I2C_GO
- Address: 5110
Writing to this register will instruct the LabJack to perform an I2C transaction.
  • Data type: UINT16  (type index = 0)
  • Readable and writable
  • Default value: 0
  • T7:
I2C_ACKS
- Address: 5114
An array of bits used to observe ACKs from the slave device.
  • Data type: UINT32  (type index = 1)
  • Readable and writable
  • Default value: 0
  • T7:
I2C_DATA_TX
- Address: 5120
Data that will be written to the I2C bus. This register is a buffer.
  • Data type: BYTE  (type index = 99)
  • Write-only
  • Default value: 0
  • This register is a Buffer Register
I2C_DATA_RX
- Address: 5160
Data that has been read from the I2C bus. This register is a buffer. Underrun behavior - fill with zeros.
  • Data type: BYTE  (type index = 99)
  • Read-only
  • Default value: 0
  • This register is a Buffer Register

 

Example:

This demonstrates I²C communications with an LJTick-DAC connected to FIO0/FIO1.

First, configure the I²C settings:

I2C_SDA_DIONUM = 1  //Set SDA pin number = 1 (FIO1)
I2C_SCL_DIONUM = 0  //Set SCL pin number = 0 (FIO0)
I2C_SPEED_THROTTLE = 0  //Set speed throttle = 0 (max)
I2C_OPTIONS = 0  //Set options = 0
I2C_SLAVE_ADDRESS = 80  //Set 7-bit slave address of the I2C chip = 80 (0x50)

Read from EEPROM bytes 0-3 in the user memory area. We need a single I2C transmission that writes the chip's memory pointer and then reads the data.

I2C_NUM_BYTES_TX = 1  //Set the number of bytes to transmit to 1
I2C_NUM_BYTES_RX = 4  //Set the number of bytes to receive to 4
I2C_DATA_TX = {0}  //Set the TX data. byte 0: Memory pointer = 0.
I2C_GO = 1  //Do the I2C communications.
I2C_DATA_RX = {?, ?, ?, ?}  //Get the RX data (4 bytes).

Write EEPROM bytes 0-3 in the user memory area, using the page write technique. Note that page writes are limited to 16 bytes max, and must be aligned with the 16-byte page intervals. For instance, if you start writing at address 14, you can only write two bytes because byte 16 is the start of a new page.

I2C_NUM_BYTES_TX = 5  //Set the number of bytes to transmit to 5
I2C_NUM_BYTES_RX = 0  //Set the number of bytes to receive to 0 (not receiving data)
I2C_DATA_TX = {0, 156, 26, 2, 201}  //Set the TX data. byte 0: Memory pointer = 0, bytes 1-4: EEPROM bytes 0-3.
I2C_GO = 1  //Do the I2C communications.

Note: When writing the TX and RX data, LJM functions eWriteNameArray and eReadNameArray functions are recommended for ease of use.

 

 

10 comments

Do you have any example projects that use the I2C or SPI interfaces? It would be nice to learn this functionality through example.  Could I use these interfaces to talk to the Maxim (Dallas) DS18B20 1-wire temperature sensors?

Currently we don't have any example projects for the T7 & modbus implementing serial protocols.  We are actively working on documenting the T7's I2C and SPI protocols.  Both are currently implemented in the latest version of firmware.  The I2C Simulator page shows what registers can be configured on a T7 and what the protocol looks like which should greatly help in picking out compatible IC's.
http://labjack.com/content/i2c-simulator

We have a limited number of examples if you are looking at using our other devices.
http://labjack.com/support/app-notes/serial-communication-examples

As for 1-Wire, the T7 will support it in the future but currently it is not available.   Our U3,U6, and UE9 support 1-wire and we have an app note for it:
http://labjack.com/support/app-notes/1-wire

How is the current status of the 1-Wire support? I would also like to use the DS18B20 with the T7.

1-Wire is up and running. Check out the 1-Wire section: http://labjack.com/support/datasheets/t7/digital-io/1-wire

 

Also, if the programming language you are using does not have a 1-Wire example shoot us an email at [email protected].

Could you please upload an Labview example for the DS18B20. I tried to follow the instructions but got an error message.

Check out the Serial Communication Examples in app notes.  These are geared towards the U3/U6/UE9, but we can help if you try to do similar with the T7 and get stuck.
1-wire, I2C, and SPI are all different.  We have not released 1-wire support for the T7 yet, but it is planned.  There are some excellent I2C temperature sensors out there, but if you want to use the DS18B20 with the T7 email [email protected] and we will prioritize 1-wire support for the T7.

We have a limited number of examples if you are looking at using our other devices.
http://labjack.com/support/app-notes/serial-communication-examples

Link is broken

I see ... from a previous comment below.  Thanks for letting us know.  Here is the new link:

http://labjack.com/support/app-notes/i2c-spi-1-wire-examples

I have been working with the Example I2C Utility Example EEPROM Read.vi in Labview.  My part is a bit larger than the Microchip 24C01C (16 byte page size) that the example is written for.  My part is a Microchip 2AA512 (128 byte page size).  I have adjusted I2C_NUM_BYTES_RX to 128, and it give me an error:  I2C_RX_SIZE_TOO_LARGE.  IS there any way to increase the page size? 

 

 

-LabJack T7 with Labview 14

In all of the current firmware versions the maximum number of bytes that can be read at once is 40 so you will need to read 32 bytes of data at a time.  We will increase this limit in the next beta firmware version so keep an eye on the T7 beta firmware page.