Skip to content
systemcoding.dev
Kernels

Enable I2C and SPI on a Raspberry Pi With dtparam

Turn on the Raspberry Pi I2C and SPI buses with dtparam lines in config.txt or raspi-config, confirm the device nodes appear, and change bus speeds.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Raspberry Pi, Linux and NXP logos.

A fresh Raspberry Pi OS install has the I2C and SPI controllers in its device tree, and both are switched off. The kernel sees nodes with status = "disabled", creates no devices for them, and the files /dev/i2c-1 and /dev/spidev0.0 do not exist. Turning them on takes one line each in the boot configuration, using the firmware's dtparam mechanism, or the equivalent raspi-config menu. This post shows both, what each line changes in the device tree, how to verify the result, how to raise the bus speed, and which pins you get. It applies to Raspberry Pi OS Bookworm, Linux 6.12 and a Raspberry Pi 4 or 5 style board; file locations are those of Bookworm. The Raspberry Pi documentation on device trees, overlays and parameters is the primary reference.

How the Pi loads a device tree

The Raspberry Pi's firmware, not a bootloader like U-Boot, reads config.txt from the boot partition, selects a base device tree for the board model, applies overlays and parameters it finds, and hands the merged result to the kernel. This is why settings that on other boards require editing source and recompiling are one-line changes here. A parameter, set with dtparam=, changes a property in the base tree. An overlay, set with dtoverlay=, merges a fragment such as the ones described in the overlay post of this cluster.

On Raspberry Pi OS Bookworm and later the boot partition is mounted at /boot/firmware. On Bullseye and older it was /boot. Check yours:

Shell
ls /boot/firmware/config.txt
Text
/boot/firmware/config.txt

Enable with dtparam

Edit the configuration file as root:

Shell
sudo nano /boot/firmware/config.txt

Add or uncomment these lines in the main section, which is the part before any [...] filter heading:

Text
dtparam=i2c_arm=on
dtparam=spi=on

i2c_arm=on enables the I2C controller on the GPIO header, bus 1 on most Pi models. The name i2c_arm refers to the controller the ARM cores use, as opposed to the one the firmware reserves for HAT identification. spi=on enables SPI controller 0 with its two chip selects. Reboot to apply:

Shell
sudo reboot

Enable with raspi-config instead

The same changes are available interactively, and the menu also loads the matching kernel modules on older images:

Shell
sudo raspi-config nonint do_i2c 0
sudo raspi-config nonint do_spi 0

In nonint mode, a value of 0 means enable and 1 means disable, which is the reverse of what you might expect. The tool edits config.txt for you, and it returns silently on success. Check the result with grep:

Shell
grep -E '^dtparam=(i2c_arm|spi)' /boot/firmware/config.txt
Text
dtparam=i2c_arm=on
dtparam=spi=on

Verify after the reboot

Three checks confirm each bus works. First, the device nodes:

Shell
ls /dev/i2c-* /dev/spidev*
Text
/dev/i2c-1  /dev/spidev0.0  /dev/spidev0.1

Second, the kernel's view in sysfs and the loaded modules:

Shell
lsmod | grep -E 'i2c_dev|spi_bcm2835|i2c_bcm2835|spidev'
Text
spidev                 20480  0
spi_bcm2835            20480  0
i2c_dev                16384  0
i2c_bcm2835            16384  0

The sizes and use counts will differ. spidev and i2c_dev provide the user-space nodes, and the bcm2835 modules are the controller drivers. On some images i2c_dev is not loaded automatically; add i2c-dev to /etc/modules to make it persistent.

Third, the live device tree shows the nodes are enabled:

Shell
cat /proc/device-tree/soc/i2c@7e804000/status; echo
cat /proc/device-tree/soc/spi@7e204000/status; echo
Text
okay
okay

The addresses are for the BCM2835 family names used across many Pi models, and a Pi 5 uses different peripheral addresses, so use ls /proc/device-tree/soc* or i2cdetect -l to find the exact nodes on your model.

Change the bus speed

The default I2C speed is 100 kHz. Raise it with another parameter on the same line or a separate one:

Text
dtparam=i2c_arm=on,i2c_arm_baudrate=400000

The parameters can be comma-separated on one dtparam line. 400000 gives the fast mode rate most sensors accept. Do not set a speed the slowest device on the bus cannot meet. Clock stretching on the Pi's hardware controller can misbehave with some chips, and the symptom is occasional corrupted reads from slow-responding sensors. If you see it, drop the speed or use a bit-banged bus.

SPI speed is not a boot parameter. Each user sets it per transfer, with SPI_IOC_WR_MAX_SPEED_HZ, up to the limit given by the controller's core clock divided by an even divisor.

Which pins you get

The enabled controllers appear on fixed pins of the 40-pin header, using GPIO function multiplexing that the firmware sets up when the parameter is on:

BusSignalGPIOHeader pin
I2C1SDAGPIO23
I2C1SCLGPIO35
SPI0MOSIGPIO1019
SPI0MISOGPIO921
SPI0SCLKGPIO1123
SPI0CE0GPIO824
SPI0CE1GPIO726

The I2C1 lines have 1.8 kilohm pull-ups to 3.3 V on the board, so a sensor breakout with its own pull-ups still works, but many in parallel can pull the lines too hard. Always use 3.3 V logic: the Pi's GPIO pins are not 5 V tolerant.

Scan to confirm

With a device connected, the scan from the user-space post should show it:

Shell
sudo apt install -y i2c-tools
i2cdetect -y 1
Text
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 

A device at 0x48 replied. The rest of the table is blank rows, omitted here. For SPI, the loopback test described in the spidev post confirms the controller.

Other buses on the same header

The Pi has more controllers than the default two. Additional I2C buses (i2c3, i2c4, i2c5, i2c6 on the Pi 4 and later) and SPI buses are enabled with overlays, such as dtoverlay=i2c3,pins_4_5, naming the pins to use. The soft I2C overlay i2c-gpio creates a bit-banged bus on any pair of pins, which is useful for chips that need clock stretching the hardware controller cannot handle. These are covered by the overlay posts.

Boundaries

dtparam settings apply at boot, so there is no way to toggle them on a running system, except by loading an overlay with dtoverlay from the command line on images that support it. Settings in config.txt belong to the Pi's firmware: they do not exist on other boards, where the same job is done by editing the board's device tree or applying an overlay in U-Boot. Also keep a copy of config.txt before editing, since a syntax error here can leave the board unbootable without access to the SD card.

Checking what the firmware actually applied

If the nodes do not appear after a reboot, ask the firmware what it did. The Raspberry Pi tools can print the merged result and the parameters that were applied:

Shell
sudo vcdbg log msg 2>&1 | grep -iE 'dtparam|dtoverlay|i2c|spi' | head -5
Text
Loaded overlay 'i2c-rtc'
dtparam: i2c_arm=on
dtparam: spi=on

The lines show each parameter the firmware processed, and a typo such as i2c_arm=yes would appear with a warning that the parameter was not recognized. If the log is clean and the nodes are still disabled, check that the lines are not under a conditional heading such as [pi4] or [cm4] that does not match your board, since lines under a filter apply only to the matching model. The same log is also where an overlay that failed to load reports why, usually a missing file in /boot/firmware/overlays.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.