I2C read/write to serial sketch?

Does anyone came across some sort of I2C debug via serial sketch? I need something to manually write/read address+register on i2c bus from serial console. Pretty simple sketch but I don't want to reinvent the wheel.

read : https://www.arduino.cc/en/Tutorial/MasterReader

write would be a little bit more tricky.... :slight_smile:

How does this link are related to my question? I don't see anything "tricky" in writing to I2C bus as a master.
I know how to communicate on the I2C bus. All I need a ready sketch to read/write a single byte at once on i2c bus controlled from serial comm.

NStorm:
How does this link are related to my question? I don't see anything "tricky" in writing to I2C bus as a master.
I know how to communicate on the I2C bus. All I need a ready sketch to read/write a single byte at once on i2c bus controlled from serial comm.

Seems like you can write it up alright then in the time you are spending searching for one!

@OP

Are you looking for something like this:

The DS3231's (RTC Chip) slaveAddress (104 = 0x68), registerAddress(14 = 0x0E), and registerData(32 = 0x20) will be enetered fromm the InputBox of the Serial Monitor with 'Newline' option enabled. In response, the measured temperature will be shown on the Serial Monitor.

#include<Wire.h>                //this file comes with IDE; it is needed for TWI Bus operation
//#define deviceAddress 0b1101000 //address of the DS3231 RTC Chip = 0x68
int slaveAddress;
int registerAddress;
int registerData;
char myData[20] = "";
byte x;                         //global variable

void setup()
{
  Serial.begin(9600);
  Wire.begin();       //TWI (aka I2C Bus) Bus is formed
  Serial.println("In InputBox, enter 104(slaveAddress),14(regsiterAddress),32(registerData) and then click on Send button");
}

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    Serial.readBytesUntil('\n', myData, 20);
    //Serial.print(myData);
    myData[3] = 0x00; //append NULL-byte
    int slaveAddress = atoi(myData);
    //-------------------
    myData[6] = 0x00; //append NULL-byte
    int registerAddress = atoi(myData + 4);
    //-------------------
    int registerData = atoi(myData + 7);
    //---------------------------------
    //Serial.print(slaveAddress, HEX);
    //Serial.print(registerAddress, HEX);
    //Serial.println(registerData, HEX);
    //-------------------------------

    Wire.beginTransmission(slaveAddress); //START, deviceAdr, data direction (write) are queued
    Wire.write(registerAddress);//(0x0E);                     //Control Register Address is queued
    Wire.write(registerData);//(0x20);                     //Start Temperature Conversion command byte is queued
    Wire.endTransmission();               //queued information are transferred on ACK

    do
    {
      Wire.requestFrom(slaveAddress, 1);   //command to the slave to send 1-byte data
      x = Wire.read();                      //data is read from FIFO buffer
    }
    while (bitRead(x, 5) != LOW);           //bit-5 of Control Register is LOW indicates end-of-conversion

    //--- read Temperature Signal----------------
    Wire.beginTransmission(slaveAddress);  //
    Wire.write(0x11);                       //point higher byte of Temperature Register; queued
    Wire.endTransmission();                 //transfer the above queued information

    Wire.requestFrom(slaveAddress, 2);       //request to send 2-byte data of Temperature Register
    float tempUpper = (float)Wire.read();    //integer part is saved as: XX.00000…….
    byte tempLower = Wire.read();            //fractional part as: XX000000

    float tempLowerx = (float) ((tempLower >> 7) * 0.5 + ((tempLower >> 6) & 0x01) * 0.250);
    float temp = tempLowerx + tempUpper;
    Serial.print("Showing DS3231's Measured Temperature: ");
    Serial.print(temp, 2);
    Serial.println(" degC");
    Serial.println();
    Serial.println("In InputBox, enter 104(slaveAddress),14(regsiterAddress),32(registerData) and then click on Send button");
  }
}

Yes, looking for something like this but more generic for any i2c slave. Need only to read/write 1 byte at once and just return raw HEX value on read.

You can modify the codes of Post#4 to suit your specific needs.

Yes, I know. Thank you for sharing!
I was just hoping there would be a "turnkey" solution. That sounded like a pretty common thing for me. Devices like PICkit Serial Analyzer, Buspirate. etc exists and I was hoping that someone did a simpler opensource alternative like this.

I could not find a turnkey solution.
There is this one, but I suggest to adapt the example by GolamMostafa.

Maybe I could help to adapt that, but you need to specify what you want (repeated start ? writing register address before reading data ? What is the format that is transmitted/received via the serial port).

The problem is that a common sketch requires a never ending bunch of code when interfacing with every possible sensor. Not every sensor is compliant with the I2C standard.

Some sensors must have a repeated start, other sensors do not allow a repeated start.
Some sensors detach themself from the I2C bus when they are busy.
Some sensors require a delay before the data is ready.
Some sensors don’t have registers.
and so on.

The Bus Pirate is open source, and that should work in (almost) every situation.

Koepel:
There is this one

Thanks. This is pretty close to something I was looking for.

Koepel:
Maybe I could help to adapt that, but you need to specify what you want (repeated start ? writing register address before reading data ? What is the format that is transmitted/received via the serial port).

The problem is that a common sketch requires a never ending bunch of code when interfacing with every possible sensor. Not every sensor is compliant with the I2C standard.

No need for repeated start and/or auto-increment read. But thanks, I think I'll adapt the sketch above myself. Most of the sensors I was working with was able to communicated in that mode. I'll post it once done.

What is on the other side ?
Just you, with the Arduino serial monitor ? or a script ? Python ? Bash ?
I think it will be very hard if you manually send commands from a serial console. After 15 minutes or so, you probably want to put that in a script.

Just me. This is only for manual tests. Need to see how a slave responds with raw values. Not something for a regular usage. I'd rather use/write a library/functions/scripts for a regular usage ofc. But before doing that I need to "play" with a sensor to check how it responds and which values are common for that. Sometimes datasheet aren't enough and you have to check how it responds in real environment to know how it works.