Enable SPI and I2C on Ubuntu 20.04 Raspberry Pi

10,780

Solution 1

Update, a step-by-step guide to enabling I2C:

Last week my last parts came in, so I finished the hardware and configured Ubuntu 20.04. I was about to start tinkering with I2C, when Thomas commented on my original answer, linking this article written by Alexander Rüedlinger. With that missing piece in hand, getting I2C up was a breeze. The below should be adaptable to SPI as well.

  1. Navigate to raspi-config on archive.raspberrypi.org. Pick the latest version.

  2. Download that file to /tmp and try to install using dpkg.

    wget https://archive.raspberrypi.org/debian/pool/main/r/raspi-config/raspi-config_*INSERT DATE*_all.deb -P /tmp

    sudo dpkg -i /tmp/raspi-config_*INSERT DATE*_all.deb

  3. This results in an error, reporting missing dependencies. Read the output and install the dependencies:

    sudo apt-get install *INSERT DEPENDENCIES*

    This botched the install of alsa-utils, one of the dependencies. To fix, run:

    sudo apt-get -fy

  4. Now install raspi-config:

    sudo dpkg -i /tmp/raspi-config_*INSERT DATE*_all.deb

  5. Run raspi-config, navigate to 'Interfacing Options', enable I2C.

  6. Install relevant packages. You'll want i2c-tools at least, but I'd recommend getting libi2c-dev as well and python3-smbus if that's your language of choice.

  7. Try probing the bus as user and root:

    sudo i2cdetect -y 1 (or 0, I2C bus number is hardware-dependent)

    If this gives you an address matrix, I2C is on and working.

    i2cdetect -y 1 (or 0)

    If this gives you a permission error, perform the next step. Otherwise, skip it.

  8. Create an I2C usergroup, apply it to the bus and add your user to this group:

    sudo groupadd i2c (group may exist already)

    sudo chown :i2c /dev/i2c-1 (or i2c-0)

    sudo chmod g+rw /dev/i2c-1

    sudo usermod -aG i2c *INSERT YOUR USERNAME*

    Log out and log in. I was connected via VS code's SSH extension and needed to reboot. Then retry:

    i2cdetect -y 1

A few comments:

The instructions provided by anvoice in this question were essential, but the actual dependencies are a few years out of date. So I suggest you try step #1 and read the dpkg output to tailor dependencies to your version of raspi-config.

It appears that raspi-config may not be necessary, but you might only need to add the dtparam=i2c_arm=on flag to /boot/config.txt (see the official docs). I haven't tried (yet). If you want to try, sudo nano /boot/config.txt, add the flag on a new line, CTRL-X, confirm with Y, then start at step #6.

Original answer:

I've been looking at a similar issue over the past few days. There appears to be no official support, unofficial packages appear to be largely out-of-date.

Installing raspi-config

Given that both RasPi OS and Ubuntu are Debian-based, it could easily work. I have formulated the following approach for myself, but may not get to test it within the next few weeks:

  • I will attempt the approach suggested by anvoice in the question you linked.
  • Note that his answer links to this script by EmilGus on Github, it specifies a number of dependencies.
  • As suggested by nc4pk in this question, I'll grab the latest version of raspi-config from the official RasPi repo.
  • I think that's in this folder.

Permissions issue

So that is as far as the installation process, which you might have nailed already. From the error, I'd guess that the user running testwind.py needs additional permissions to access that interface - or that the interface must be accessed differently under Ubuntu. I hope someone can chime in on that.

Context

I am planning a project that implements SLAM on an 8GB RasPi 4. I'm using two Arduinos as additional GPIO and to run HW control loops: I.e. PID to control PWM, so that RPM is approximately constant even if the terrain differs. Using I2C with the RasPi as master; not looked into SPI yet.

Given that SLAM algorithms have a few more memory-intensive (but less compute-heavy) implementations, I opted for the new 8GB RasPi 4. The 3GB per-process memory limit could get in my way, so I'll run Ubuntu 20.04 LTS 64-bit server.

If I can't get it working, I'll test I2C under Raspberry Pi OS. Once I have a working test script, I'll move back to Ubuntu.

Solution 2

AFAIK and as Faiyaz Haider has already stated both SPI and I2C are already active on Ubuntu Server 20.04 on a RPi 4. Your issue seems much more related to a permission issue than a disabled SPI.

About SPI and permissions, check the spidevs permissions:

 ls -la /dev/spidev*

If you see such kind of permissions:

crw-------

then you have to change their permissions accordingly to make them rw for the user you want to be able to access SPI. You can do it in this way:

sudo groupadd spiuser
sudo usermod -aG spiuser <yourusername>
sudo chown :spiuser <put the spi device name here>
sudo chmod g+rw <put the spi device name here>

You have to repeat last two lines for every spi device, you can get spi device names listing them using the ls command above. This should be able to solve your permission issue.

Share:
10,780
Stealing
Author by

Stealing

Updated on September 18, 2022

Comments

  • Stealing
    Stealing over 1 year

    Using Rasbian there is the convenient tool rasi-config to enable/disable SPI and I2C on a raspberry pi. However there is no such tool for Ubuntu, and no straightforward way for enabling them (as far as I can google). There are patches like this where some have had success, but it does not seem to work for 20.04. I'm trying to enable SPI to follow this tutorial. When I run the python script I always get:

    Traceback (most recent call last):
      File "testwind.py", line 8, in <module>
        spi.open(0,0)
    PermissionError: [Errno 13] Permission denied
    

    Is there any official documentation for Ubuntu Server 20.04 for raspberry pi to enable SPI and I2C? Thanks in advance.

  • Stealing
    Stealing over 3 years
    I gave the script by EmilGus a try, but it didn't end up working. Maybe I'm still missing some dependencies. For now, I'm falling back to Raspbian OS, and will take a crack at Ubuntu later. It seems odd that Canonical went to all the trouble to create Ubuntu images for Pi 2, 3 and 4 (32 & 64 bit), but there's no standard way to enable SPI, I2C, GPIO, etc. Cool project, I'm making a weather station.
  • MvZ
    MvZ over 3 years
    Well, a lot of RasPi's are used as VPN, webhost, media center, kiosk - etc. None of which need GPIO much. I wonder if a lack of experience with Linux/Debian permissions and OS structure may be holding you back. I expect that that will be my main problem - it will take research and troubleshooting at every step. So I won't run EmilGus's script, but use it as an example and work out the installation process for 20.04 myself. I expect to run into issues that are obvious to experienced Ubuntu users, and I'll need to be specific to find the right help. It'll be slow. But I'll learn a lot.
  • thomas
    thomas over 3 years
    I followed instructions in this answer askubuntu.com/a/1130074/68826 except that I chose the latest version of raspi-config as mentioned in a comment. I then changed permissions as described here: lexruee.ch/setting-i2c-permissions-for-non-root-users.html This is on a Raspberry pi 4 4gb with ubuntu 20.04 64 bit
  • MvZ
    MvZ over 3 years
    Thanks Thomas, I wouldn't have managed without you linking Alexander's article. I'll add a step-by-step guide to my answer.