Quello che viene descritto di seguito è stato estrapolato dai siti:
- http://www.raspberrypi-spy.co.uk/2014/11/enabling-the-i2c-interface-on-the-raspberry-pi/
- http://think-bowl.com/raspberry-pi/installing-the-think-bowl-i2c-libraries-for-python/
- http://quick2wire.com/articles/how-to-add-quick2wire-as-a-raspbian-software-source/
- http://skpang.co.uk/blog/archives/575
- http://think-bowl.com/raspberry-pi/i2c-python-library-3-axis-digital-compass-hmc5883l-with-the-raspberry-pi/
Riunire in un'unica guida la configurazione dell'I2C sul Raspberry PI agevolerà i lettori evitando di far trascorrere pomeriggi davanti al computer alla ricerca sul web di disparate soluzioni.
Passo 1: Enable i2c using raspi-config utility
From the command line type : sudo raspi-config
This will launch the raspi-config utility.
Now complete the following steps :
- Select “Advanced Options”
- Select “I2C”
- Select “Yes”
The screen will ask if you want the interface to be enabled :
- Select “Yes”
- Select “Ok”
The screen will ask if you want the module to be loaded by default :
- Select “Yes”
The screen will state the module will be loaded by default :
- Select “Ok”
- Select “Finish” to return to the command line
When you next reboot the I2C module will be loaded.
Passo 2: Installing the Think Bowl I2C Libraries for Python
These steps assume your Pi is connected to the internet
To keep everything nice and tiddy, create a folder for your project and change directory to it.
To create one in your home folder:
mkdir ~/myproject
cd ~/myproject
Install the Quick2Wire libraries and setup the environment. The second two steps will have to be done every time you login:
git clone https://github.com/quick2wire/quick2wire-python-api.git
export QUICK2WIRE_API_HOME=~/myproject/quick2wire-python-api
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
To easily setup the environment every time you login. Put the two export lines above in a file (quick2wire.env) using an editor such as vi or nano and then source the environment by running:
. ./quick2wire.env
git clone https://bitbucket.org/thinkbowl/i2clibraries.git
The Think Bowl I2CLibraries require python3. If python3 is not installed, perform the following
sudo apt-get install python3
Nel codice python inserire all’inizio:
#!/usr/bin/python3
import sys
# appends to PYTHONPATH the location of the example codes
sys.path.append("/home/pi/myproject/quick2wire-python-api/")
Make sure your Raspberry Pi is connected to the internet when installing the drivers.
The new Raspbian distro already have the I2C driver installed but they are disabled by default. To enable it all you need to do is comment out a line by putting # in front. At the prompt type.
sudo nano /etc/modprobe.d/raspi-blacklist.conf
then add a # on the 3rd line.
#blacklist i2c-bcm2708
Aprire l’editor nano per modules:
sudo nano /etc/modules
e aggiungere se non presenti le seguenti righe:
i2c-dev
i2c-bcm2708
salvare e reboot.
Now install the i2c-tools package by:
sudo apt-get install i2c-tools
If you get a 404 error do an update first:
sudo apt-get update
then run the install the i2c-tools again. Note : The installation could take a few minutes to do, depend on how busy the server is. Now add a new user to the i2c group:
sudo adduser pi i2c
Reboot the machine by:
sudo shutdown -r now
After the reboot test to see any device connected by:
sudo i2cdetect -y 0
If your board is the Rev 2 type this:
sudo i2cdetect -y 1
Next install the python-smbus python module:
sudo apt-get install python-smbus
Passo 3 (forse non necessario): How to Add Quick2Wire as a Raspbian Software Source
Ensure your software is completely up to date:
sudo apt-get upgrade
Both of those are fine. If there is a problem it will print an error message.
Add the address of our package repository to your system’s package manager, so it can download our software and check for updates. Edit the file /etc/apt/sources.list with the following command:
sudo nano /etc/apt/sources.list
Add the following lines to the bottom of the file:
# Quick2Wire Software
deb http://dist.quick2wire.com/raspbian wheezy main
deb-src http://dist.quick2wire.com/raspbian wheezy main
Save the file by typing Control-O and exit the editor by typing Control-X.
Update the package manager’s list of available software packages:
sudo apt-get update
You can now install Quick2Wire software packages. For example to install the quick2wire-gpio-admin package:
sudo apt-get install quick2wire-gpio-admin
N.B.: il file python del codice dovrà essere salvato, seguendo la guida, nella cartella myproject. Altrimenti non riuscirà a richiamare le librerie.
Con il dispositivo collegato ai pin del raspberry (porte SDA e SCL), si può testare il funzionamento della i2c digitando da terminale (nel mio caso la porta è 1, in altre versioni è 0):
sudo i2cdetect –y 1