Thursday, October 7, 2010

Add I2C to any Arduino pins

Ever wanted to use any pair of pins for I2C on Arduino, not just the dedicated pins on Analog 4 & 5? Me too, so I made a quick little Arduino library called “SoftI2CMaster”, available in the “blinkm-projects” Googlecode repository.

Get it here: SoftI2CMaster.h, SoftI2CMaster.cpp.

It’s still a work in progress, but it can write data pretty successfully and do it over longer cables than normal.

For the VIMBY/Scion Hackerspace Challenge, I created an array of BlinkM MaxM-powered accent lights for the device we made. Because the I2C cable was longer than a few feet, the normal Wire library that BlinkM_funcs.h uses to communicate with BlinkMs couldn’t be used. This is because the Wire library assumes a perfect bus. If there is any noise or other bus problems, the Wire library will currently lock up. For the SoftI2CMaster library, I wanted it to be very tolerant, even lazy, about bus problems and also have more tunable timing to let you slow the bus down. Of course, you still need pull-up resistors on the two lines. I’ve found using 2.2k resistors to be good.

The SoftI2CMaster API follows Wire’s API pretty closely:

  • SoftI2CMaster(sdaPin,sclPin) — create an new SoftI2CMaster for the two pins specified
  • beginTransmission(address) — begin sending data
  • send(data) — send some data (byte or byte arrays)
  • endTransmission() — stop sending data

In use it looks something like this:

#include "SoftI2CMaster.h" const byte sdaPin = 7; const byte sclPin = 6; SoftI2CMaster i2c = SoftI2CMaster( sdaPin,sclPin );  i2c.beginTransmission( 9 );  // send to address 9 i2c.send('c'); i2c.send(255); i2c.send(0); i2c.send(200); i2c.endTransmission(); 

There is a simple demo for BlinkMs that this library currently lives in. It’s called “BlinkMSoftI2CDemo” and shows off a simplified BlinkM_funcs called “BlinkM_funcs_soft.h“. The entirely of BlinkMSoftI2CDemo is shown below.

const byte sdaPin = 7;  // digital pin 7 wired to 'd' on BlinkM const byte sclPin = 6;  // digital pin 6 wired to 'c' on BlinkM  #include "SoftI2CMaster.h" SoftI2CMaster i2c = SoftI2CMaster( sdaPin,sclPin );  // must define "i2c" before including BlinkM_funcs_soft.h #include "BlinkM_funcs_soft.h"  byte blinkm_addr = 9;  // void setup() {   Serial.begin( 19200 );   Serial.println("BlinkMSoftI2CDemo");    BlinkM_off(0);    for( int i=0; i< r =" random(255);" g =" random(255);" b =" random(255);">
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following