DEV Community

Cover image for Setup i2c on Raspberry Pi Zero W using Arch Linux
Thomas Brittain
Thomas Brittain

Posted on • Originally published at ladvien.com

Setup i2c on Raspberry Pi Zero W using Arch Linux

This article builds on the previous, where I ran us through setting up Arch Linux for the Raspberry Pi Zero W.

Let's not stop, let's get I2C going so we can interact with some cool hardware.

1. Installing sudo

If you've followed my previous guide on installing Arch Linux on a Raspberry Pi then you'll have ended up with a bare bones system, which is good. No unneeded fat. But sometimes fat is needed, it's what gives us curves, and curves are beautiful....I feel this metaphor is breaking down. In short, we need extra packages to get work done.

The first package we need is sudo

It will allow us to easily manage file permissions.

First, make sure your system is up to date. To do this we are going to login as the root user. You can do this by typing su followed by the root user's password, which for a barebone Arch Linux installation is root.

$ su
Password: root
Enter fullscreen mode Exit fullscreen mode

Oh, and if you haven't figured out yet, alarm stands for Arch Linux ARM.

Next we need to update the package libraries and system.

pacman -Syu
Enter fullscreen mode Exit fullscreen mode

After hitting enter, it should look like this:

root@alarmpi alarm]# pacman -Syu
:: Synchronizing package databases...
 core                                                        179.0 KiB   448K/s 00:00 [#################################################] 100%
 extra                                                      1982.8 KiB  1279K/s 00:02 [#################################################] 100%
 community                                                     4.0 MiB  1689K/s 00:02 [#################################################] 100%
 alarm                                                        35.0 KiB   583K/s 00:00 [#################################################] 100%
 aur                                                           6.0 KiB  0.00B/s 00:00 [#################################################] 100%
:: Starting full system upgrade...
resolving dependencies...
looking for conflicting packages...
Enter fullscreen mode Exit fullscreen mode

It should give you a list of packages with update and upgrade candidates and prompting you to confirm the updates. Go ahead and say yes.

Now we should be good to install sudo

$ pacman -S sudo
Enter fullscreen mode Exit fullscreen mode

Even after sudo is installed, we still need to add the main user, which is alarm to the sudo'er group. This in effect gives the alarm user the superpowers of the root user.

Now, the way sudo works is by adding a user to a special Linux group. Anyone added to this group will be given root superpowers. To get a list of those currently in the sudo group:

sudo -ll
Enter fullscreen mode Exit fullscreen mode

You should get something like

User root may run the following commands on alarmpi:

Sudoers entry:
    RunAsUsers: ALL
    Commands:
Enter fullscreen mode Exit fullscreen mode

Ok, let's get the alarm user added to the sudoer group.

Type

EDITOR=nano visudo
Enter fullscreen mode Exit fullscreen mode

This should allow you to edit the visudo file and add alarmpi to sudoers. Oh, the write permissions for the visudo file are limited to root, so if you have switched back from the root user to alarmpi you will need to run su again and log back in as root before editing this file.

Let's find the entry for adding users to the sudo'er group.

Find the part which looks like this:

##
## User privilege specification
##
root ALL=(ALL) ALL
Enter fullscreen mode Exit fullscreen mode

And add alarm ALL=(ALL) ALL right below the root entry. It should look like this after editing.

##
## User privilege specification
##
root ALL=(ALL) ALL
alarm ALL=(ALL) ALL
Enter fullscreen mode Exit fullscreen mode

Then hit CTRL+O to write the changes and CTRL+X to exit.

Before we can check the changes took, we will need to exit our root session.

exit
Enter fullscreen mode Exit fullscreen mode

This should land you back at your alarm session. To see you the alarm user is now added to the sudoer group type

sudo -ll
Enter fullscreen mode Exit fullscreen mode

And if all went well, you'll get this output

User alarm may run the following commands on alarmpi:

Sudoers entry:
    RunAsUsers: ALL
    Commands:
        ALL
Enter fullscreen mode Exit fullscreen mode

Notice, we now have access to ALL commands. Booyah!

We can do a hard test by typing:

sudo ls
Enter fullscreen mode Exit fullscreen mode

We should get

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for alarm:
Enter fullscreen mode Exit fullscreen mode

Type the alarm user password (which is alarm, if you haven't changed it).

2. Install needed packages

pacman -S git python2 i2c-tools base-devel python2-distribute python2-pip
Enter fullscreen mode Exit fullscreen mode

Use Python's Package Index (pip) to install Raspberry Pi GPIO support

sudo pip2 install RPi.GPIO
Enter fullscreen mode Exit fullscreen mode

3. Install raspi-config

sudo pacman -S xorg-xrandr libnewt
git clone https://aur.archlinux.org/raspi-config.git
cd raspi-config
makepkg -i
Enter fullscreen mode Exit fullscreen mode

Use the Raspi-Config tool to enable I2C

sudo raspi-config
Enter fullscreen mode Exit fullscreen mode

Select "Interfacing Options" and enable I2C.

Note: Going back through these instructions I did notice when I started raspi-config I received this warning:

/usr/bin/raspi-config: line 997: warning: command substitution: ignored null byte in input

And when I attempted to enable I2C it gave this error.

* Failed to read active DTB

But it still seemed to do the job. I'll investigate more when I've time.

Now, we need to reboot the system for everything to register.

4. Test the I2C Setup

We should be all setup. Try running

sudo i2cdetect -y 1
Enter fullscreen mode Exit fullscreen mode

If all has went well then you should get

[alarm@alarmpi ~]$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Enter fullscreen mode Exit fullscreen mode

Now, we just need to connect an I2C device to the bus and we should the hex address of where the device may be found.

Top comments (0)