Is it possible to speed up execution time of a c++ program in raspberry pi solely by increasing the i2c baudrate and increasing the sampling frequency of the sensors?

I have the issue of sudden jerkiness of my quadcopter and found the culprit which is the frequency at which my loop excecutes which is only about 14Hz. The minimum requirement for a quadcopter is 100-200hz. It is similar to the issue he faces here https://robotics.stackexchange.com/questions/6720/raspberry-pi-quadcopter-thrashes-at-high-speeds

He said that he was able to increase his sampling rate from 66hz to 200hz by increasing the i2c baudrate. I am confused on how that is done.

In the wiring pi library, it says that we can set the baudrate using this command:

gpio load i2c 1000

will set the baud rate to 1000Kbps – ie. 1,000,000 bps. (K here is times 1000)

What I am curious about is how to set this baudrate to achieve my desired sampling rate?

I plan on optimizing it further to achieve at least a 100Hz sampling rate

As of now, the execution time of each loop in my quadcopter program is at 0.07ms or 14Hz.

It takes 0.01ms to 0.02ms to obtain data from the complementary filter.

I have already adjusted the registers of my sensors to output readings at 190Hz (Gyroscope L3GD20H) and 200Hz (Accelerometer LSM303) and 220Hz (Magnetometer LSM303).

  • Without knowing the architecture of your current solution it's hard to say. 14 iterations per second seems poor. I've seen higher claimed for Python.
    – joan
    Commented Jan 17, 2016 at 11:56
  • I am now able to achieve 50Hz sampling rate thanks to this though I doubt that would be enough for the quadcopter. I increased the baudrate to 400kbps. However I noticed that the execution time of the loop is not consistent as it varies from 0.007s to 0.02s Commented Jan 18, 2016 at 1:53
  • Now i can get 250hz sampling rate :) Commented Jan 18, 2016 at 6:49
  • There are limits depending on the devices and the length of the I2C bus. Longer buses only work at lower rates and slave devices can hold the bus to accommodate their timing needs (I believe there was initial problems for this on the RPi as it didn't handle such clock stretching properly). That being said I thought the RPi was limited tp the 100KHz (standard-mode) anyhow.
    – SlySven
    Commented Jan 18, 2016 at 11:08

1 Answer 1


To set the I2C bus 1 baud rate add the following command to /boot/config.txt


Where x is the desired baud rate.

See /boot/overlays/README for help.

  • Will increasing the i2c baudrate decrease the time it takes for me to obtain data from the sensor given that I just increased their sampling frequency through their registers? Commented Jan 17, 2016 at 12:01
  • Also how can I relate the baudrate to my desired sampling frequency :) Commented Jan 17, 2016 at 12:02

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.