Enabling and checking I2C on the Raspberry Pi using the command line for your own scripts
Most people know that you can enable the I2C port using raspi-config.
raspi-config also offers a non-interactive mode, where you can drive it’s functionality using command line parameters to it.
Reading the current state of the I2C port on the Raspberry Pi
Here is how to read the current state of the I2C port:
sudo raspi-config nonint get_i2c
This command will return:
- 1 if the port is disabled
- 0 if the port is enabled
Attention: in this case the logic is bash scripting! “0 means true or no error, and all other numbers mean an error code”. This got me at first!
Note: nonint stands for “non-interactive”
Enabling I2C on the Raspberry Pi using one simple command
Again using raspi-config we can set the state of the I2C port. Using raspi-config has the advantage that it takes care of many additional things for you, and should be upwards compatible. Behind the scenes, raspi-config will edit /boot/config.txt for you, for example, and also update the device tree.
Here is how to enable the I2C port:
sudo raspi-config nonint do_i2c 0
Again, note that 0 means true / on here!
Here is how to disable the I2C port:
sudo raspi-config nonint do_i2c 1
1 means “false” or off for this purpose.
Checking that I2C is enabled
raspi-config in non-interactive mode
as mentioned above, you can read the state of the I2C port with
sudo raspi-config nonint get_i2c
The screenshot shows the output when the I2C port is enabled (0 = true)
I2C device node
When I2C is enabled on the Raspberry Pi, you will also see a /dev node for it:
ls /dev/i2*
/dev/i2c-1
Note: on the first Raspberry Pi models the I2C port exposed to users was I2C 0, so one would expect /dev/i2c-0
The screenshot shows that the i2c-1 node exists in /dev
gpio readall
Here is the output of
sudo gpio readall
For BCM 2 and 3, you see “SDA1” (I2C data) and “SCL1” (I2C clock) under Name. Also the mode is ALT0, which corresponds to the I2C function on these particular pins (GPIO2, GPIO3).
Note that BCM 2 and 3 is the Broadcom numbering, not the GPIO pinout. The GPIO pin position is the first column in “Physical” (pins 3 and 5).
(You can refer to this document, page 102, for an overview of the different GPIO pin modes if you are interested)
lsmod
lsmod will show you I2C modules which are loaded:
lsmod | grep i2c
i2c_bcm2835 16384 0
i2c_dev 20480 0
so no difference to I2C being disabled.
i2cdetect:
A final test is to run i2cdetect to scan the address range. For modern Pis, you will need to specify 1 as the port:
sudo i2cdetect -y 1
this will output a table of the currently detected I2C devices.
note: as, in my case, no I2C devices were attached, no I2C devices show up – this output is expected.
Checking that I2C is disabled
raspi-config in non-interactive mode
as mentioned above, you can read the state of the I2C port with
sudo raspi-config nonint get_i2c
The screenshot shows the output when the I2C port is disabled (1 = false)
I2C device node
When I2C is disabled on the Raspberry Pi, you will not see a /dev node for it:
ls /dev/i2*
ls: cannot access ‘/dev/i2*’: No such file or directory
The screenshot shows that no i2c nodes exist in /dev.
gpio readall
Here is the output of
sudo gpio readall
For BCM 2 and 3, you see “SDA1” (I2C data) and “SCL1” (I2C clock) under Name.
In contrast to the enabled version, however (see above), the mode is IN, the pin is now a GPIO input.
Note that BCM 2 and 3 is the Broadcom numbering, not the GPIO pinout.
(You can refer to this document, page 102, for an overview of the different GPIO pin modes if you are interested)
lsmod
lsmod will show you I2C modules which are loaded:
lsmod | grep i2c
i2c_bcm2835 16384 0
i2c_dev 20480 0
so no difference to I2C being enabled – you can’t use this as a test.
i2cdetect:
A final test is to run i2cdetect to scan the address range. For modern Pis, you will need to specify 1 as the port:
sudo i2cdetect -y 1
if I2C is disabled, this will show an error:
Error: Could not open file `/dev/i2c-1′ or `/dev/i2c/1′: No such file or directory
Pitfalls & Problems with the I2C port, FAQ
is a reboot necessary to enable the I2C port?
From my perspective a reboot is not necessary. I have looked at the code of raspi-config, and they ask for reboot for other ports (e.g. UART), but not for the I2C port.
Also, when dynamically switching the I2C on/off state, you will be able to verify that the device node is created and the I2C range can be scanned using i2cdetect without a reboot.
If all else fails, however, go ahead and try to reboot – it might help with some other conditions.
are there more possibilities to drive raspi-config, for e.g. the UART and SPI ports, for SSH, etc.?
Sure – raspi-config supports a wide range of different options in the non-interactive mode.
all these commands are run using
raspi-config nonint <command> <optionally parameters>
You can have a look at the raspi-config source code, or refer to this useful post in the Raspberry Pi forum.
again, note the (for some developers confusing) convention that 0 = true / on and 1 = false / off
i2cdetect not able to scan
If you get the following error:
Error: Could not open file `/dev/i2c-1′ or `/dev/i2c/1′: No such file or directory
I2C is most likely disabled. Try to enable it, using the how-to in this article or using raspi-config, and then try to scan the I2C port again.
You can use the methods presented in this article to verify the state of the I2C port on your Raspberry Pi.
gpio readall unable to determine board type
if you get this message when trying to run gpio readall on a Raspberry Pi 4:
13:01:19 pi@Avalon:/dev $ sudo gpio readall
Oops – unable to determine board type… model: 17
You have to install a newer version of wiringpi from the author (it is not in the upstream repositories yet). This article describes how to do it.
Here’s the short version:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg –i wiringpi-latest.deb