New i2c libraries with 'softi2c'
Moderators: adafruit_support_bill, adafruit

Re: New i2c libraries with 'softi2c'

by jer291113 on Sat Aug 07, 2010 3:21 pm

I really aprreciate the work you have done so no apology is necessary and surprised there isn't more interest.

I'm a retired EE and wanted to play with the Arduino environment, I don't quite understand why the pin-out defs don't match the schematic but that's the way it is.
I'll try your advice -> "If it is on the silkscreen try it. If it is an analog pin or there are problems read the code."

By the way I did find the pin_definitions...

Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

by schamp on Sun Oct 31, 2010 10:03 pm

I think this is a huge improvement over the provided "Wire" library. For one, it makes it easier to match an I2C interchange as described in a datasheet. What is the license? Have you considered contributing it to the Arduino core?
Posts: 9
Joined: Mon Jul 05, 2010 7:41 pm
Location: St. Louis

Re: New i2c libraries with 'softi2c'

by stevekamerman on Thu Dec 30, 2010 3:57 am

Hi guys, I'm not sure where this is all going at this point - I've seen lots of different libraries and other hacks for using i2c / TWI over the non-hardware ports. I looked around at quite a few libraries and decided to roll my own. This lib is specifically for the DS1307 and is class-compatible with the Arduino Time class, so you can simply specify the pins you want to use for SCL and SDA, and you're good to go!

Here's an example sketch that I'm testing with my new homebrew breadboard RTC:

Code: Select all | TOGGLE FULL SIZE

 * TimeRTCSet.pde
 * example code illustrating Time library with Real Time Clock.
 * RTC clock is set in response to serial port time message
 * A Processing example sketch to set the time is inclided in the download

#include <Time.h>
#include <TwoWireBase.h>
#include <SoftI2cMaster.h>
#include <SoftDS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

#define SDA_PIN 10
#define SCL_PIN 11

void setup()  {
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
     Serial.println("RTC has set the system time");     

void loop()
     time_t t = processSyncMessage();
     if(t >0)
        RTC.set(t);   // set the RTC and the system time to the received value

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  if(digits < 10)

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = ;
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c =;         
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number   
      return pctime;
  return 0;

Anyway, if you are interested in taking a further look into the code, check it out at my Github repo:
Posts: 1
Joined: Thu Dec 30, 2010 3:46 am

Re: New i2c libraries with 'softi2c'

by lemuroid on Fri Feb 11, 2011 1:17 pm

Woot! The software i2c works. if you are struggling to follow the above thread ( i got distracted by the discussion of pins 93 and schematics)

the simple answer is,

step 1: get the i2c libraries posted at the top of the thread and put them in the proper location for arduino to find them.
step 2: modify the softDS1307 code as follows:

#define USE_SOFT_I2C 1


// pins for DS1307 with software i2c
#define SCL_PIN 59
#define SDA_PIN 58
Posts: 14
Joined: Wed Feb 02, 2011 5:35 pm

Re: New i2c libraries with 'softi2c'

by jarcher on Tue Nov 08, 2011 8:26 pm

Can this library be used inside an ISR? In other words, does it use interrupts itself?
Posts: 40
Joined: Sun Mar 27, 2011 2:49 am

Re: New i2c libraries with 'softi2c'

by lemuroid on Thu Dec 08, 2011 4:30 pm

I had this working fine with version 022 but now it throws error messasges in version 1.0. Any ideas how to update the i2c library to work in 1.0?
Posts: 14
Joined: Wed Feb 02, 2011 5:35 pm

Re: New i2c libraries with 'softi2c'

by fat16lib on Sun Dec 18, 2011 5:42 pm

I added a new version,, in the original post that should work with Arduino 1.0.
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

by harvey11 on Wed Dec 21, 2011 1:22 am

Hi everyone, just wanted to share a bit of code to make your libraries compatible with Arduino 1.0 as well as keep them working on Arduino 22. You can make them work easily by simply changing “WProgram.h” to “Arduino.h”, but if they are shared, anyone who does not upgrade to the Arduino 1.0 IDE will lose the function of that library.
You will need to change the header file that ends in .h as well as the source file that ends in .cpp. As you might already know, the file “WProgram.h” has been replaced with “Arduino.h” in version 1.0 of the Arduino IDE.
You can make the library backwards compatible by replacing the line #include “WProgram.h” with the following code:

Code: Select all #if defined(ARDUINO) ampamp ARDUINO gt= 100
#include “Arduino.h”
#include “WProgram.h”

This simply checks for the version of Arduino and loads the correct file. When there is an upgrade to the Arduino IDE, the version number will be higher than 100, still keeping the function of the code listed above. I have tried this with some small libraries that I have made as well as the PCD8544 (Nokia 5110/3310 LCD from Adafruit) and it works well. Happy coding.
Posts: 3
Joined: Tue Dec 20, 2011 7:59 am

Re: New i2c libraries with 'softi2c'

by TheLandlord on Sun Dec 25, 2011 2:31 pm

I have downloaded this library and can run the scan demo successfully using an Arduino Uno board against the I2C port and digital pin (6,7) using TwiMaster and SoftI2CMaster respectively. I am trying to turn on/off an LED connected to a MCP23017 GPIO, the code runs successfully with TwiMaster, but fails when I run with it SoftI2cMaster using any pair of pins. Curiously enough I can scan the bus using the SoftI2cMaster and see the MCP23017.

The wiring, ports, etc are solid since it works with the TwiMaster. Scanning the bus with the SoftI2cMaster implementation identifies the MCP23017 and writing to the ports do not generate any errors. Any pointers on how to proceed to debug this problem?
Posts: 7
Joined: Fri Apr 15, 2011 8:37 pm

Re: New i2c libraries with 'softi2c'

by fat16lib on Mon Dec 26, 2011 8:36 am

There must be some timing difference between TWI and my soft I2C that causes the failure. You can never exactly emulate hardware I2C with a bit-bang implementation.

I happen to have a MCP23017 that I never used for a project.

Could you post an example that demonstrates the problem and I will look at it. Please try to isolate the problem so the example is as simple as possible.

I will look at it with a scope and logic analyzer and try to find a fix.

I found an MCP23017 library and modified it to use I2cMaster and verified your problem. I see a glitches on the SDA line for Soft I2C and am trying to find the reason.
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

by fat16lib on Mon Dec 26, 2011 5:18 pm

I have updated with corrections that should fix soft I2C problems with the MCP23017. Go to the original post at the beginning of this topic and download the new version.

I have attached the library and sketch that I used to debug the MCP23017 problem.
MCP23017 library with test sketch
(4.14 KiB) Downloaded 222 times
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

by TheLandlord on Mon Dec 26, 2011 5:46 pm

Thanks for the prompt response!!!!
The addition of the extra line to the write method worked as you described in your post. Enclosed is an extract of the code i use for testing. Now I can use SoftI2cMaster or TwiMaster to perform the same function. For my own education, how did you go about troubleshooting this problem? Good Kung-Fu!

SoftI2cMaster rtc(18,19);
//TwiMaster rtc(false);
uint8_t address = 0x24 << 1;
void setup()
Serial.println("Begin Demo");

for ( uint8_t i = 1; i < 126; ++i )
if ( rtc.start( (i << 1) | I2C_READ) )
Serial.print("Add read: ");
Serial.println(i, HEX);;
if (!rtc.start( address | I2C_WRITE) ) Serial.println("start failed");
if (!rtc.write(0x01) ) Serial.println("Write 1 failed");
if (!rtc.write(0x00) ) Serial.println("Write 2 failed");
if (!rtc.start( address | I2C_WRITE) ) Serial.println("start failed");
if (!rtc.write(0x13) ) Serial.println("Write 3 failed");
if (!rtc.write(0x01) ) Serial.println("Write 4 failed");
Posts: 7
Joined: Fri Apr 15, 2011 8:37 pm

Re: New i2c libraries with 'softi2c'

by fat16lib on Mon Dec 26, 2011 6:23 pm

I setup the MCP230177 with a scope on SCL (yellow) and SDA (blue) and looked at TWI I2C:

hardI2C.jpg (101.85 KiB) Viewed 6214 times

and software I2C:

softI2C.jpg (104.64 KiB) Viewed 6214 times

And noticed SDA was LOW after sending a byte with TWI and HIGH with software I2C.

I added the line to write and it seemed to work so I did the post with the added line.

I thought more about it and realized it was really stop() that had a problem. I did more mods, deleted the post with the added line, and posted a new version of

You should download the new version of which is posted in the beginning of this topic.
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

by Stan12 on Fri Jul 13, 2012 1:14 am

I am using
Windows XP
Arduino IDE 1.0
Adafruit Data Logger shield on Arduino Mega 2560

I2cMaster library installed into 'libraries' folder from

When I run softDS1307 sketch (from I have initially in Serial Monitor window:
Code: Select all | TOGGLE FULL SIZE
The current time is 07/13/2012 Thu 00:51:22
Control: 00

Options are:
(0) Display date and time
(1) Set time
(2) Set date
(3) Set Control
(4) Dump all
(5) Set NV RAM
Enter option:

time seems correct, but no options are working.

When I enter any number and hit 'send', I am always getting:
Code: Select all | TOGGLE FULL SIZE
Invalid entry

Invalid option

I made no modifications to softDS1307 code.
Results are the same in IDE 1.0 and 1.0.1

Is this normal or am I doing anything wrong?

i2cScanAddress sketch, when I changed to
Code: Select all | TOGGLE FULL SIZE
#define USE_SOFT_I2C 1

produces this output:
Code: Select all | TOGGLE FULL SIZE
Add read: D0
Add write: D0

with USE_SOFT_I2C 0
it only shows word 'done' which means it did not find anything.
Posts: 2
Joined: Fri Jul 13, 2012 12:54 am

Re: New i2c libraries with 'softi2c'

by fat16lib on Fri Jul 13, 2012 4:06 pm

I suspect you have an option other than "No line ending" enabled in the serial monitor.

This is at the bottom of the serial monitor near the baud rate selector.

The input function needs to be fixed to treat any combination of CR and LF as end-of-line.

I keep meaning to post a new version of the I2C stuff but have not found time to finish the new version. Sorry!
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm