- Pull-up resistor values for an I2C circuit are bus speed dependent
- MaxBotix Inc., sensors require clock stretching for bus speeds above 50kHz
- Sensor addresses must be properly set and polled for proper operation
What is I2C?
Integrating a number of devices together into one circuit and individually commanding each device allows you to greatly simplify complex circuits. The I2C (inter-integrated circuit) protocol is one option that allows you to place multiple devices on one circuit and independently control each device.
The primary components of an I2C circuit consist of a lead device, the master device, and multiple peripheral or slave devices. The role of the master device is to set a bus speed on the clock line and command the peripheral devices. At each high pulse of the clock line, the status of the data line is read. The data line sends the communication between the master and slave devices. Reading the data line allows you to string out binary messages being sent to the peripheral, slave, devices and your I2C master device.
What Needs to Happen
In order to have successful I2C communication, the following requirements need to be met. The wiring must be done correctly. The bus speed must be set to a usable speed. The sensor datasheets list the supported clock speeds. The proper pull-up resistors must be present. (This is system and device dependent.) The master device must send the proper commands to the sensor. When all of this happens, the slave device must respond with an acknowledge bit. At this point, successful I2C communication can happen. Verifying that each requirement from this list is being met is an excellent method for I2C troubleshooting.
Addresses are key to the success of an I2C circuit. Each slave device on a circuit should have a unique address. To communicate with a specific slave device, the master device announces the unique address of that device. Each slave device should be given an individual address to prevent conflicts between multiple slave devices.
Setting the I2C address requires that you initiate a write at the sensor’s current address. The default sensor address will be listed in the datasheet. Next, you will write three bytes to the sensor starting with the addr_unlock_1 command as the first byte. Write the addr_unlock_2 command as the second byte. Then write the new sensor address as the third byte. We strongly recommend that a separate circuit is used to give each sensor its own address one at a time before placing the sensors in any larger circuits with multiple devices.
If you forget the address of a sensor, you can reset it to its default address. Pin 1 is internally pulled high in the sensor. On power-up, the state of this pin is checked; if left high or disconnected, the sensor will use the address stored in memory for I2C communications. If pulled low, the sensor will use its default address for the current power cycle.
While our sensors do not accept 14 bit addressing they can work in a circuit with 14-bit addresses. Whenever a 14-bit address is sent our sensors will not respond. Our sensors will only respond to their 7(8 bit when counting the read-write bit) address while ignoring any and all 14-bit addresses.
Gaining a better understanding of the I2C interface is one of the best places to start troubleshooting. The I2C interface can prove troublesome at moments. If you find yourself unable to get your circuit working, the I2C forum community is an excellent place to ask questions and familiarize yourself with the finer points of the I2C interface. Raspberry Pi, Texas Instruments, and Arduino, as well as numerous other companies, host active I2C forums.
Once you are comfortable, and while you are still researching, you may wish to check the following common I2C missteps.
Pull-up Resistor Value: Among the most common missteps is the use of improper pull-up values. The strength of the pull-up resistors will depend on the clock speed being used as well as the size of the circuit. The I2C specification recommends a resistance value of 4.7 kΩ for 20-100kHz interfaces with good low inductance routing. However, these specifications are for communication between chips on a single PCB. If you have longer cable lengths, it is best to use lower value resistor, such as 1kΩ, and also to use properly shielded cables.
I2C Bus Speed: The bus speed itself can be an issue. Our sensors support I2C clock frequencies up to 400kHz provided clock stretching is supported by the master device. Without clock stretching the sensors can run at speeds up to 50kHz. If you are using a bus speed above 50kHz, you need to verify that your master device supports clock stretching.
Sensor Address: The addressing of the sensor bears repeating. If you are trying to communicate with the sensor, but you are not polling the right address, the sensor will not respond. Errors in setting and polling the sensor address prevent proper communication. We always recommend that you get one sensor working followed by sequentially adding more sensors.
Check the Lines: If available, you may want to view the clock and data lines with an oscilloscope. The following image displays what the “Take a range reading” command looks like on the clock and data lines. There is an issue with your I2C setup if your oscilloscope does not report a similar output. You may wish to verify all of your wiring and coding if your oscilloscope readings do not match. The oscilloscope readout of your clock and data lines are one of the most useful troubleshooting tools for I2C sensors.
Some Helpful Tools: There are alternatives to using an oscilloscope to process the I2C communications. Tools such as an Aardvark I2C/SPI Host Adapter and a Beagle I2C/SPI Protocol Analyzer also work. If the sensor works with these devices and does not work in your system, try looking at the pull-up resistor values for the system as described earlier in the article.
Contact our technical support team if you have any additional questions or you need help with your I2C setup. We are here to help you succeed.