1

I am using an arduino to sample sound at 44.1 kHz and sends those values as a long through I2C to a Raspi for processing. The desired rate is 44.1k*4bytes = 177 kb/second.
Currently the raspi only reads about 300 values per second, much slower than the desired 44.1k. The raspi code is written in python - I'm wondering if this is limiting the speed? Can the raspi and/or I2C handle this speed?
Baud rate on the raspi is set to 500k.

Here is our basic test code, writing simple numbers:
Arduino code:

    #include <Wire.h>
    #define SLAVE_ADDRESS 0x04
    long number = 0;
    int state = 0;

    void setup() {
      //pinMode(13, OUTPUT);
      Serial.begin(9600); // start serial for output
      // initialize i2c as slave
      Wire.begin(SLAVE_ADDRESS);

      // define callbacks for i2c communication
      Wire.onReceive(receiveData);
      Wire.onRequest(sendData);

      Serial.println("ready");
    }

    void loop() {
      delay(10);
    }

    // callback for sending data
    void sendData() {
      number++;
      //number = 123456;
      Wire.write((const uint8_t*)&number, sizeof(long));
      //Wire.write(number, long);
    }


raspi code (python):

    import struct
    import smbus
    import time
    # for RPI version 1, use "bus = smbus.SMBus(0)"
    bus = smbus.SMBus(1)
    nn = 100
    # This is the address we setup in the Arduino Program
    address = 0x04

    def writeNumber(value):
            bus.write_byte(address, value)
            # bus.write_byte_data(address, 0, value)
            return -1

    def readNumber():
            block = bus.read_i2c_block_data(address, 0)
            n = struct.unpack('<l', ''.join([chr(i) for i in block[:4]]))[0]
            return n

    while True:
            number = readNumber()
            if number%nn == 0:
                    print number
  • The Arduino code does not appear to implement the read I2C block data command, it appears to simply return 4 bytes when the Arduino is addressed. The read I2C block data command adds an overhead of 3 bytes per transfer, so a total of 7 bytes or 56 bits are needed per sample. – joan Oct 29 '17 at 20:52
  • Is the read function needed in the arduino code? The arduino simply sends values on request. – Joey Costello Oct 30 '17 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.