Soft I2C and Programmable I2C address
While writing the mcp4728 library, one of most difficult thing was implementing programmable I2C address.
Single I2C bus can be used to connect multiple I2C devices, so addressing a specific device on the bus require a specific address for the device. Normal I2C devices have couple of pins for selectable address. Address of device is determined by whether address pins (ADx) is ground or VCC. There are some devices like mcp4728 has a advanced way of selectable address. Without physical pins, their address can be programmed and stored in EEPROM of chip. I love this feature of mcp4728, since it allow to stack the shield without physical jumpers.
Microchip’s implementation of programmable address is slightly non-standard, because it require another pin other than two I2C pins. While I understand why they did this way, it make a bit difficult to implement in arduino library. It is not because of requirement of third pin, but because of critical timing of pin latch. Let’s take a look at datasheet how programmable address works.
As you see, LDAC pin need to be latched between 8th and 9th clock. The problem was AVR’s hardware I2C don’t allow me to do it. AVR’s TWI register return it’s status after it receive the ACK from slave device. There was no way me to know when to latch, since TWI register only return status after 9th clock (ACK). So, I need to look for Software I2C library (bit-bang).
There are couple of software I2C library for arduino. I found that SoftI2C from fat16lib is most useful for this.
http://forums.adafruit.com/viewtopic.php?f=25&t=13722
Also, simple logic logger for arduino was quite handy for debugging.
http://jdesbonnet.blogspot.com/2010/05/using-arduino-duemilanove-as-quick-and.html
Dotted blue line show what I can do with wire library of arduino. Notice LDAC latch after ACK ( 9th clcok).
By modifying softI2C library, I was able to latch LDAC pin between 8th and 9th clock.
It is included in mcp4728 library and you can find modified SoftI2C library from google code.
http://code.google.com/p/neuroelec/source/browse/#svn%2Ftrunk%2FSoftI2cMaster
Arduino sketch for reading and writing address of mcp4728.


Hi ! I am new to I2C protocol. I am trying to read data from 3 K22 CO2 sensors (all of them have 0×68 address preprogrammed)
From the I2C manual these are the commands I have to submit:
First frame:
Start | 0xD0 | 0×22 | 0×00 | 0×08 | 0x2A | Stop
Second frame:
Start | 0xD1 | | Stop
I can read data from sensor using Wire library, but I am little bit confused with read method on SoftI2CMaster. It looks like for 4 bytes I have to issue read method 4 times. Am I correct ?
If you have more examples using SoftI2CMaster library, it will help me a lot. Thank you !