The Start


Hi, What and Why

Compare to Arduino

What do you need to buy ?

It's plugged in, what's first ?

How to install packages.

Recommended packages.

Essential Linux Commands.

How to use the vi editor.

The next step


Set up I2C.

My first program - blink.

VNC - Use your computer to control your Pi.

Samba - copy files.

Setting up the I2C interface

Later note: Raspbian in January 2016: found to work better than described below.

I am using the 2015 model 2. Previous models apparently did not have a 5V output, and a logic level converter was needed. This Pi has a 5V output, so you can directly connect 5V components.

I am going to start with a 20x4 LCD with an I2C interface. The connections are simple, so I should be able to focus my attention on the programming.

First, however, I have to set my pi up to use I2C. I found this to be a nuisance, and I was surprised that it wasn't enabled by default or at least a fully working option in raspi-config.

Have you enabled I2C with raspi-config ?

Run raspi-config and under the advanced menu, select the option to enable I2C and install the kernel module. This installs the i2c kernel module .

Update: fyi: the raspi-config program is a lovely shell script under /usr/bin. Reading it is a great way to learn shell scripting. When you choose the option 'Enable I2C', it writes the line dtparam=i2c_arm=on to /boot/config.txt. It is this that ensures the kernel module i2c_bcm2708 is loaded each time you boot the pi.

However, the raspi-config program does not install the other kernel module or the software packages for i2c. This has to be done manually.

Have you installed the other kernel module ?

First, you should have some I2C device files. All device files in linux are stored under the aptly named directory /dev. If you haven't got a file starting with i2c in the /dev directory, then you haven't installed the I2C kernel module.

$
$ ls /dev/i2c*
/dev/i2c-1
 

Run the above command, ls /dev. You should see a file like /dev/i2c-1. If it's not there, then install the kernel module (below) and reboot.

Installing the i2c kernel module

You need to add a line to the file /etc/modules and reboot. You'll know this has worked successfully when you see a file like i2c-1 in the /dev directory (see above).

$
$ sudo bash
# cp /etc/modules /etc/modules.original
# echo "i2c-dev" >> /etc/modules
 
( note : that will append the line to the file. You _must_ use >> to append. If you use > you will accidentally overwrite the file. The cp command will copy the original file so that you have a backup.)

Have you installed the i2c commands and programming packages ?

Given that you have a device file (above: /dev/i2c-1) then you should be able to run the i2c detect program to check the status of the I2C bus.

$
$ locate i2cdetect
/usr/sbin/i2cdetect
/usr/share/man/man8/i2cdetect.8.gz
 

A few things to note.
- If you haven't installed the locate command yet, then do so - see here.
- If i2cdetect is not installed yet, then see below.
- If it is installed, it is in the /usr/sbin directory; meaning that only root can run it.

Installing i2cdetect

$
$ sudo bash
apt-get install i2c-tools
 

You can then check that i2cdetect is installed, using the locate command (above).

You know it works when

You know that you can write a program to use the i2c bus, when you get a successful result from the i2cdetect command.

$
$ sudo bash
# i2cdetect -F 1
Functionalities implemented by /dev/i2c-1:
I2C                              yes
SMBus Quick Command              yes
SMBus Send Byte                  yes
SMBus Receive Byte               yes
SMBus Write Byte                 yes
SMBus Read Byte                  yes
SMBus Write Word                 yes
SMBus Read Word                  yes
SMBus Process Call               yes
SMBus Block Write                yes
SMBus Block Read                 no
SMBus Block Process Call         no
SMBus PEC                        yes
I2C Block Write                  yes
I2C Block Read                   yes
 
module i2c_bcm2708 is installed okay. (lsmod) however, no devices /dev/i2c exist ... Even after enabling device tree in raspi-config and a reboot, no devices ...