2

I am attempting to increase the frequency at which I read samples from the I2C dedicated pins. I have attempted this through the wiringPi library (C code), from which I reached just about 900Hz. As well as with python, I was able to reach roughly the same figures. I did some research and was lead to multi-byte reads via ioctl and read(), write() functions which as far as I understand are more low level, direct access of the I2C bus as opposed to the wiringPi library and any other python library I could've used prior. Though after some code testing, I am stagnating at just about 1.1KHz for a 14 byte read (9 seconds for 10K samples). I am unsure what I am missing here or how I can improve this as I would like to hit around 2KHz.

Reason being is I would like to thread in between and around the I2C communication. Also if anyone has any pointers as how this should be approached that would be appreciated.

Code for reading data, file printing adds 2 seconds, without any of the commented out code the time for 10000 samples is roughly 7 seconds.

while(counter<10000){
    buf[0] = 0x3B;
    write(fd,buf,1);
    read(fd,buf,14);

    // if ((write(fd, buf, 1)) != 1){
    // // Send the register to read from
    // fprintf(stderr, "Error writing to MPU6050\n");
    // }

    // if (read(fd, buf, 14) != 14){
    // fprintf(stderr, "Error reading from MPU6050\n");
    // }else{}

    // for(int i=0;i<14;i++){
    //   fprintf(new_file,"%.0f,",(float)buf[i]);
    // }
    // fprintf(new_file, "%s\n","");
    counter++;
  }
theeddible
  • 23
  • 3

1 Answers1

2

I can see no way to speed up the code. You are already doing the minimum number of transactions: one to select the register, one to read the data.

To transfer 15 bytes of data (1 write, 14 read) would take 153 (17 bytes + ack bits) bits.

That gives a maximum of 400000/153 or 2614 readings per second. In practice you have found the limit.

You could try to increase the bus speed beyond 400 kbps but I reckon any improvement will be marginal.

joan
  • 71,852
  • 5
  • 76
  • 108