For educational purposes, I would like to write my own i2c protocol. Is it possible to write a custom i2c library to communicate using regular ports, or should I use the SCL and SDA ports? Can I mimic SDA port by using an external transistor with pull-up resistor connected to digital pin?

  • \$\begingroup\$ The NXP I2C specification was originally written before dedicated hardware was available. Bit-banging a GPIO is the original way it was done, \$\endgroup\$
    – RussellH
    Commented Sep 30, 2023 at 16:17
  • \$\begingroup\$ I was looking at a design where the I2C bus interface available to a FPGA was actually 2 output and 1 input pins, with a CPLD on the board connecting the 3 pins on the FPGA to the actual SCL and SDA signals connected to other I2C devices on the board. As per I2C SCL from CPLD is push-pull rather than open-collector this lead to some restrictions on use of I2C. \$\endgroup\$ Commented Sep 30, 2023 at 17:51
  • \$\begingroup\$ Continued. If it helps i2c_bit_banged.c is the source code for the bit-banged driver which was created for the above board, which runs under Linux on a x86-64 processor rather than an Arduino. Given the relatively fast processor, the driver inserts the delays from the I2C spec. \$\endgroup\$ Commented Sep 30, 2023 at 17:54

1 Answer 1


Is it possible, yes, and has been done many times already.

It can be done with GPIO ports.

You can mimic SDA port with an external transistor and a pull-up, but usually nobody does it because it can be done without the external transistor by simply using the GPIO ports. No external hardware at all, only the pull-up resistors that are needed anyway.

What you seem to be trying to do is just called software bit-banged I2C interface.

Since you tagged "Arduino", I would be very surprised if the Arduino framework does not already have a software based I2C implementation built-in you can simply use so you don't even have to write it yourself.

Edit: Just confirmed, Arduino comes with "SoftWire" library .

  • \$\begingroup\$ Thank you for the answer! Yes, I know that Arduino has native I2C library. I'd like to write a custom one for better understanding. Also I'm planing to program chips and to do that I'd like to try it first on Arduino board. \$\endgroup\$ Commented Sep 30, 2023 at 15:47
  • \$\begingroup\$ @AntonTsvayer Arduino has both a native I2C library that uses the I2C hardware and it also has a software bit-bang library which you want to replicate. Feel free to reinvent the wheel, you can use the SoftWire as a base to learn or write your own. \$\endgroup\$
    – Justme
    Commented Sep 30, 2023 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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