2

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.

4 Answers 4

6

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.

4
  • 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.
    – Stealing
    Sep 10, 2020 at 22:35
  • 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.
    – MvZ
    Sep 11, 2020 at 8:48
  • 1
    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
    – thomas
    Oct 5, 2020 at 5:48
  • Thanks Thomas, I wouldn't have managed without you linking Alexander's article. I'll add a step-by-step guide to my answer.
    – MvZ
    Oct 7, 2020 at 14:27
3

In my experience with Ubuntu 20.04 server on RPI4, both I2C and SPI are enabled by default. Check the file /boot/firmware/syscfg.txt and see if you have the following lines:

dtparam=i2c_arm=on
dtparam=spi=on

If not, maybe you can append them on /boot/firmware/usercfg.txt and reboot, and hopefully, that works. If that doesn't work, maybe do sudo apt update && sudo full-upgrade -y and see if there are any distro updates needed.

2
  • I don't even have syscfg.txt or usercfg.txt in my /boot/firmware folder. There's a config.txt file, but it already has these parameters set to on.
    – mvmn
    Nov 28, 2023 at 11:57
  • P.S. In my case the command that wanted I2C access just needed a sudo
    – mvmn
    Nov 28, 2023 at 12:32
1

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.

0

Working Solution:

I faced the same issue when I tried to interface BMX160 (address 0x68) sensor with my Raspberry Pi 4 which has Ubuntu 22.04 running on it. I searched over Google for several minutes, tried various others commands now then I found the perfect solution for the bus.SMBus(1): PermissionError: [Errno 13] Permission denied problem. Here's the solution that worked for me, described step-by-step:

  1. Make sure you have python3-smbus installed. If not install it by running the command sudo apt install python3-smbus
  2. Run the command, sudo i2cdetect -y 1 (or 0, maybe) This should show a matrix where the address (for me 0x68) is under the respective row and column.
  3. Run the command, sudo groupadd i2c (i2c group might already exist and that's okay)
  4. Run the command, sudo chown :i2c /dev/i2c-1 (or /dev/i2c-0)
  5. Run the command, sudo chmod g+rw /dev/i2c-1 (or /dev/i2c-0)
  6. Run the command, sdo usermod -aG <insert_your_username>
  7. Now close the terminal and logout of the system and login back again
  8. Now when will you finally run i2cdetect -y 1 (or 0), you will be able to see the matrix without any root access and that indicates that now you will be able to access your i2c device with the current user itself!

This is my first answer in all of StackExchange! Hope it actually helps you find the right solution.

Thank You!!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .