3

I want to change the I2C frequency of my Arduino Mega 2560. I did find two methods. One method is to change the TWBR variable after Wire.begin(). The other one is to use Wire.setClock() after Wire.begin().

Whats the difference between the 2 methods? which one is better? Can I set any I2C frequency I want?

3

Whats the difference between the 2 methods?

The difference is that one is more portable than the other. The "direct register set" will not work very well on an Arduino Due.

There is no speed or foot-print advantage with the "direct register set" as this is often only done once in the setup of the sketch.

Which one is better?

If better is portable, maintainable, readable, etc then the answer is simple - use the API and not "direct register set".

Can I set any I2C frequency I want?

No - obviously there are some restrictions. First, the CPU frequency is scaled so it cannot generate all possible frequencies. Second, the I2C device(s) and bus will set restrictions on the maximum frequency. Longer wires and several devices will add capacity and require lower frequency. Also the device with the lowest frequency puts an upper limit. Always check the data sheets for the I2C devices.

Cheers!

1

They are both one and the same. One is easier to remember though (I'll let you work out which).

Wire.setClock(frequency) just (ultimately) does:

TWBR = ((F_CPU / frequency) - 16) / 2;

You can set any frequency that ends up with that formula generating an integer between 0 and 255.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.