Skip to content

Instantly share code, notes, and snippets.

@ribasco
Last active December 4, 2022 07:45
Show Gist options
  • Save ribasco/c22ab6b791e681800df47dd0a46c7c3a to your computer and use it in GitHub Desktop.
Save ribasco/c22ab6b791e681800df47dd0a46c7c3a to your computer and use it in GitHub Desktop.
How to change the I2C Frequency/Speed for Raspberry Pi 2/3

How to change the I2C Frequency/Speed for Raspberry Pi 2/3

Note: Steps 5-8 are only for verification purposes, you can skip them

  1. Open /boot/config.txt file

    sudo nano /boot/config.txt

  2. Find the line containing dtparam=i2c_arm=on

  3. Add i2c_arm_baudrate=<new speed> (Separate with a Comma)

    dtparam=i2c_arm=on,i2c_arm_baudrate=400000

  4. Reboot Raspberry Pi

  5. Create a simple test script to verify the speed

  • Switch to home directory cd ~
  • Create shell script nano i2cspeed.sh
  • Copy and paste the following lines of code
#!/bin/bash
var="$(xxd /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency | awk -F': ' '{print $2}')"
var=${var//[[:blank:].\}]/}
printf "%d\n" 0x$var
  1. Change file permissions

chmod +x i2cspeed.sh

  1. Execute test script

./i2cspeed.sh

  1. Verify that the value is the correct setting
pi@rpi-dev:~ $ ./i2cspeed.sh 
400000

Source: Raspberry Pi Forums

@aziya21c
Copy link

aziya21c commented Jan 3, 2019

It will be better, if the file 'i2cspeed.sh' is changed to the followings,

#!/bin/bash
var="$(xxd -ps /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency)"
var=${var//[[:blank:].\}]/}
printf "%d\n" 0x$var

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment