Faster I2C

I'm using MCP23017 I2C IO Expanders.

They supports: - 100 kHz - 400 kHz - 1.7MHz

I looked in wire.cpp and wire.h and didn't see any obvious place for changing the source to use a higher I2C speed.

Is it possible to use a faster I2C clock speed than 100 kHz on the arduino?

In the Wire subdirectory named 'utility', look at twi.c. At about line 85 you will see code relating to the clock frequency of the TWI.

Hi, the atmega-hardware can do 400 KHz, but you have to tweak the Wire-library in file hardware/libraries/Wire/utility/twi.h. Near the top of the file you see :

#ifndef TWI_FREQ
#define TWI_FREQ 100000L
#endif

If you change that to

#ifndef TWI_FREQ
#define TWI_FREQ 400000L
#endif

The I²C bus should run at 400kHz But you also have to the delete the files hardware/libraries/Wire/Wire.o hardware/libraries/Wire/utility/twi.o because the library must be re-compiled before it uses the new speed. (This is done automaticaly when you open the Arduino-IDE)

Eberhard

It worked. Thanks for the advice.

thanks a lot! it worked fine with a max 6956.

How do slave devices know the speed of the i2c bus?

I have a Duemilanove working perfectly at 100kHz with a Devantech LCD03. I want to add a Nunchuck to the bus. Various postings say that the Nunchuck operates at 400kHz. If I increase the speed in the wire library will the LCD03 and the Nunchuck work together?

Hi, can't find a datasheet for the LCD, but you can't break anything. If you don't have any serious performance problems there is no need to increase the speed anyway. The NunChuck is working fine at 100KHz.

Eberhard

So slave devices such as a Nunchuck automagically operate at the speed of the host? Why do several of the Nunchuck articles say that the speed in TWI.h must be changed?

How do slave devices know the speed of the i2c bus?

They operate at the speed of SCL, provided by the master.

Any idea why articles such as http://www.windmeadow.com/node/42 say "Since the nunchuck uses "Fast" I2C, we will need to change the default speed: #define TWI_FREQ 400000L."? I guess that higher speeds may be slightly more responsive but it seems strange to imply that 400kHz operation is required.

Hi KenS,

Any idea why articles such as http://www.windmeadow.com/node/42 say "Since the nunchuck uses "Fast" I2C, we will need to change the default speed"?

The reason is, that it will not work with the default speed for all devices because you are out of spec. It may work with one specific Nunchuck, but not with all others. My Wireless Nunchuck (from Blazepro) requires 400k TWI speed.

MikeT

So SCL determines the speed at which data is sent/received but some devices are not tolerant of a slower than expected clock?

I don't have the funny triangular screwdriver so I can't get the unit apart without risking destruction. >:( What chip does the Nunchuck use?

Hi KenS,

So SCL determines the speed at which data is sent/received but some devices are not tolerant of a slower than expected clock?

Exactly!

What chip does the Nunchuck use?

There is not something like "the Nunchuck". Multiple vendors are using different hardware to build their own Nunchuck and all follow the interface definition.

You should always design against the defined interface and not against a specific implementation (e.g. the original Nunchuck from Sony).

When you want to avoid changing the standard library files, you can just set the TWI speed using the following code (copied from my posting at http://www.windmeadow.com/node/42#comment-28 ):

#define TWI_FREQ_NUNCHUCK 400000L
TWBR = ((CPU_FREQ / TWI_FREQ_NUNCHUCK) - 16) / 2;

Is there any reason, why you want to avoid changing the speed? When there are other TWI devices which need lower TWI speeds you could try to switch the speed before accessing each of the devices.

MikeT

Thanks for the additional information! I had missed the suggestion to set the speed without changing the library. Yes, I have several other devices on the i2c bus (LCD/keypad, motor controller) so I didn't want to change the speed and things worked fine without the speed change.

I certainly appreciate the value of working to the standard and would like to if at all possible. Is a description of the Nunchuck standard interface available online?

The most comprehensive information about the Wii remote can be found at http://wiibrew.org/wiki/Wiimote and specifically for the Nunchuck under http://wiibrew.org/wiki/Wiimote/Extension_Controllers

MikeT

That is some excellent material that I completely missed in Google. Thanks!

Has anyone used wire with the arduino mega? I don't see if conditions for the atmega1280 in the wire utility twi.h file...

  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
    // activate internal pull-ups for twi
    // as per note from atmega8 manual pg167
    sbi(PORTC, 4);
    sbi(PORTC, 5);
  #else
    // activate internal pull-ups for twi
    // as per note from atmega128 manual pg204
    sbi(PORTD, 0);
    sbi(PORTD, 1);
  #endif