edit

# Arduino Software I2C user guide

The standard I2C library for the Arduino is the Wire library. While this library is sufficient most of the time, there are situations when it cannot be used:

• the I2C pins A4/A5 (or SDA/SCL) are in use already for other purposes
• same I2C addresses devices are used

So we write the SoftwareI2C library to use digit port and analog port to enable multiple same I2C addresses devices work on Arduino.

## Example#1: Scan I2C device address¶

### Connection¶

Here we will show you how this works via a simple demo. First of all, you need to prepare the below stuffs:

Seeeduino V4 Grove - OLED Display 1.12 Base Shield
Get ONE Now Get ONE Now Get ONE Now

This is an easy-to-use module, what you need to do is connect the module to D2 port of a Base Shield.

Arduino Pin OLED Pin
Digit 3 SDA
Digit 2 SCL
VCC VCC
GND GND

### Software¶

• Open the code directly by the path: File -> Example ->Arduino_Software_I2C-master->SoftwareI2C_Scan.
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include "SoftwareI2C.h" SoftwareI2C softwarei2c; void setup() { Serial.begin(115200); softwarei2c.begin(3, 2); // sda, scl Serial.println("begin to scan..."); } void loop() { for(unsigned char i=1; i<=127; i++) { if(softwarei2c.beginTransmission(i)) { Serial.print("0x"); Serial.println(i, HEX); while(1); } softwarei2c.endTransmission(); } Serial.println("find nothing"); while(1); } 
• Upload the code to arduino.
• Please configure serial port baud rate as 115200.
• We can see I2C address from serial port.

## Example#2: Display different information on 2 Grove - OLED Display 1.12¶

### Connection¶

Here we will show you how this works via a simple demo. First of all, you need to prepare the below stuffs:

Seeeduino V4 Grove - OLED Display 1.12 Base Shield
Get ONE Now Get ONE Now Get ONE Now
• Connect one Grove - OLED Display 1.12 to D2 port and other to D4 port.

### Software¶

• Copy SeeedGrayOLED.cpp and SeeedGrayOLED.h to Arduino_Software_I2C-master folder
• Edit SeeedGrayOLED.cpp

• Step1: Change the library from Wire.h to SoftwareI2C.h
 1 2 3 4 From #include "Wire.h" To #include 
• Step2: Add initSoftwareI2C function, we have to change the class name for different products.
 1 2 3 4 5 void SeeedGrayOLED::initSoftwareI2C(SoftwareI2C *w, int __sda, int __scl) { Wire = w; Wire->begin(__sda, __scl); } 
• Step3: Replace all Wire. to Wire-> For example, change Wire.endTransmission() to Wire->endTransmission().
 1 2 3 4 From Wire.endTransmission(); To Wire->endTransmission(); 
• Edit SeeedGrayOLED.h

• Step1: Change the library Wire.h to SoftwareI2C.h
 1 2 3 4 From #include "Wire.h" To #include 
• Step2: Add initSoftwareI2C function into public class
 1 void initSoftwareI2C(SoftwareI2C *w, int __sda, int __scl); 
• Step3: Add SoftwareI2C *Wire into private class
 1 SoftwareI2C *Wire; 
• Open the code directly by the path: File -> Example ->Arduino_Software_I2C-master->OLED_Display.

• We have to define SoftwareI2C objects as well as SeeedGrayOLED objects.

  1 2 3 4 5 6 7 8 9 10 //define 2 SoftwareI2C objects #include "SoftwareI2C.h" SoftwareI2C WireS1; SoftwareI2C WireS2; //define 2 SeeedGrayOLED objects #include "SeeedGrayOLED.h" #include SeeedGrayOLED SeeedGrayOled1; SeeedGrayOLED SeeedGrayOled2; 
- We use initSoftwareI2C instead of Wire.begin during setup.

 1  SeeedGrayOled1.initSoftwareI2C(&WireS1, 3, 2); // initSoftwareI2C, sda, scl 

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 //define 2 SoftwareI2C objects #include "SoftwareI2C.h" SoftwareI2C WireS1; SoftwareI2C WireS2; //define 2 SeeedGrayOLED objects #include "SeeedGrayOLED.h" #include SeeedGrayOLED SeeedGrayOled1; SeeedGrayOLED SeeedGrayOled2; void setup() { SeeedGrayOled1.initSoftwareI2C(&WireS1, 3, 2); // initSoftwareI2C, sda, scl SeeedGrayOled1.init(SSD1327); SeeedGrayOled1.clearDisplay(); //Clear Display. SeeedGrayOled1.setNormalDisplay(); //Set Normal Display Mode SeeedGrayOled1.setVerticalMode(); // Set to vertical mode for displaying text for(char i=0; i < 12 ; i++) { SeeedGrayOled1.setTextXY(i,0); //set Cursor to ith line, 0th column SeeedGrayOled1.setGrayLevel(i); //Set Grayscale level. Any number between 0 - 15. SeeedGrayOled1.putString("11111111"); //Print 11111111 } SeeedGrayOled2.initSoftwareI2C(&WireS2, 5, 4); // initSoftwareI2C, sda, scl SeeedGrayOled2.init(SSD1327); //initialize SEEED OLED display SeeedGrayOled2.clearDisplay(); //Clear Display. SeeedGrayOled2.setNormalDisplay(); //Set Normal Display Mode SeeedGrayOled2.setVerticalMode(); // Set to vertical mode for displaying text for(char i=0; i < 12 ; i++) { SeeedGrayOled2.setTextXY(i,0); //set Cursor to ith line, 0th column SeeedGrayOled2.setGrayLevel(i); //Set Grayscale level. Any number between 0 - 15. SeeedGrayOled2.putString("00000000"); //Print 00000000 } } void loop() { } 
- Upload to Sketch. - We will see 11111111 display on one screen while 00000000 is on other.

## APIs of the library¶

• begin() function: SoftwareI2C.begin() must first be called to start any software I2C communication using the SoftwareI2C library.
 1 SoftwareI2C::begin(int Sda, int Scl) 
• beginTransmission function: Used when the ATmega is acting as an I2C master. Sets internal variables in the SoftwareI2C library in preparation for transmitting to the given address.
 1 SoftwareI2C.beginTransmission(uchar addr) 
• endTransmission() function: end for transmitting to the given address.

 1 SoftwareI2C.endTransmission() 
- write function: Populate the send data buffer with the data found in the argument list. there are 2 functions. One is sending a byte and other is array.

 1 SoftwareI2C.write(uchar dta) 
 1 SoftwareI2C::write(uchar len, uchar *dta) 
 1 SoftwareI2C.read()