Arduino library · Communication

SoftWire: an example that runs

Yes. You can use the SoftWire library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Accelerometer and gyroscope, MPU6050). The example below, "Wire calls on two other pins, with the sketch’s own buffers", runs on the Arduino Uno R3.

Built in on Mokxi: #include <SoftWire.h> and it builds, with nothing to install. Free, and no account needed.

What SoftWire does

A software I2C master on any two pins with the Wire calls (beginTransmission(), requestFrom()) and the low-level llStart(), llWrite() and llRead(). That is what SoftWire, by Steve Marple, covers. The sketch below uses it on the Arduino Uno: “Wire calls on two other pins, with the sketch’s own buffers”.

Steve Marple's SoftWire library's interface, a software I2C master on any two pins with the Wire calls.

As upstream, the sketch owns the buffers: without setTxBuffer() a write() returns 0, and without setRxBuffer() requestFrom() reads nothing. The low-level calls (llStart(), llRepeatedStart(), llWrite(), llRead(), stop()) take the 8-bit address byte and return ack, nack or timedOut.

The calls follow release 2.0.10 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameSoftWire
AuthorSteve Marple
Include#include <SoftWire.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAccelerometer and gyroscope, MPU6050

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <SoftWire.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "SoftWire" by Steve Marple from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add SoftWire to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.10, so a sketch written here builds there too.

What to know in the simulator: The sketch supplies the buffers with setTxBuffer() and setRxBuffer(), as upstream. No part stretches the clock, so a timeout never happens.

The calls you will use

In the order a sketch meets them.

SoftWire sw(2, 3)

Makes sw, the SoftWire object the calls below are made on.

sw.setTxBuffer(swTxBuffer, sizeof(swTxBuffer))

Sets the tx buffer to the value you pass in. Arguments: tx buffer, tx buffer size.

sw.setRxBuffer(swRxBuffer, sizeof(swRxBuffer))

Sets the rx buffer to the value you pass in. Arguments: rx buffer, rx buffer size.

sw.setDelay_us(5)

Sets the delay us to the value you pass in. Arguments: delay us.

sw.setTimeout(1000)

Sets the timeout to the value you pass in. Arguments: timeout.

sw.begin()

Starts it up. Call it once in setup().

sw.requestFrom(0x68, 1)

Requests the from and returns a whole number. Arguments: address, quantity, send stop.

Example: Wire calls on two other pins, with the sketch’s own buffers

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <SoftWire.h>

SoftWire sw(2, 3);  // SDA, SCL
char swTxBuffer[16];
char swRxBuffer[16];

void setup() {
  Serial.begin(9600);
  sw.setTxBuffer(swTxBuffer, sizeof(swTxBuffer));
  sw.setRxBuffer(swRxBuffer, sizeof(swRxBuffer));
  sw.setDelay_us(5);
  sw.setTimeout(1000);
  sw.begin();

  sw.beginTransmission(0x68);
  sw.write(0x75);  // WHO_AM_I
  uint8_t status = sw.endTransmission(false);
  Serial.print("endTransmission: ");
  Serial.println(status);
  sw.requestFrom(0x68, 1);
  Serial.print("WHO_AM_I: 0x");
  Serial.println(sw.read(), HEX);

  SoftWire::result_t r = sw.llStart((0x50 << 1) | SoftWire::writeMode);
  sw.stop();
  Serial.print("0x50: ");
  Serial.println(r == SoftWire::ack ? "ack" : "nack");
}

void loop() {}

Wiring

  • Arduino Uno R3 2 to Accelerometer and gyroscope, MPU6050 SDA
  • Arduino Uno R3 3 to Accelerometer and gyroscope, MPU6050 SCL
  • Arduino Uno R3 5V to Accelerometer and gyroscope, MPU6050 VCC
  • Arduino Uno R3 GND to Accelerometer and gyroscope, MPU6050 GND

Open it in the editor, wired and ready to run

Try SoftWire without installing anything

Open the example, change the sketch, and keep your version in a free account.