I have considered to use a combination of an Arduino Uno and RPi for my Home Automation Project.
After vigorous research(Googling of cause) on how I am to interface the two, I came to a conclusion that using I2C was the best option as compared to using UART(which required Logic Level Switching as the RPi runs on 3V3 and Arduino on 5V), or as compared to USB communication.
The idea is to have the Arduino doing all Analogue detections and calculations such as a temp sensor, LDR, IR and etc. And also including extra GPIO's for additional controls.
Now back to the I2C, in this case the RPi will function as the Master and Arduino as the Slave.
Hint: The RPI is running on 3V3 while the Arduino is running at 5V. I read some tutorials where they used a level shifter for the I2c communication. This is NOT needed IF the RPI is running as master and the Arduino is running as slave device. You can use the I2C bus without level shifter as only the master will push the data line high!
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.
We need to download additional drivers and enable I2C.
[sourcecode language="bash"]
sudo apt-get update
sudo apt-get install i2c-tools
[/sourcecode]
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:
[sourcecode language="bash"]
sudo adduser pi i2c
[/sourcecode]
Install the python-smbus python module:
[sourcecode language="bash"]
sudo apt-get install python-smbus
[/sourcecode]
Now you are ready to use the i2c with python
[sourcecode language="bash"]
sudo cp /etc/modprobe.d/raspi-blacklist.conf /etc/modprobe.d/raspi-blacklist.conf.bak
sudo cat /etc/modprobe.d/raspi-blacklist.conf
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804
[sourcecode language="bash"]
sudo nano /etc/modprobe.d/raspi-blacklist.conf
[/sourcecode]
Remove I2C from the Blacklist:
[sourcecode language="bash"]
# blacklist spi and i2c by default (many users don't need them)
#blacklist spi-bcm2708
#blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804
[/sourcecode]
Press CTRL X then y to save and exit.
Next edit the modules file by:
[sourcecode language="bash"]
sudo nano /etc/modules
[/sourcecode]
Add the following to a new line.
[sourcecode language="bash"]
i2c-dev
i2c-bcm2708
[/sourcecode]
Press CTRL X then y to save and exit.
Run command to activate i2c-dev:
[sourcecode language="bash"]
sudo modprobe i2c-dev
[/sourcecode]
Reboot the Pi by:
[sourcecode language="bash"]
sudo shutdown -r now
[/sourcecode]
After the reboot test to see any device connected by:
[sourcecode language="bash"]
ls -l /dev/i2c*
[/sourcecode]
You should get something like this:
[sourcecode language="bash"]
crw-rw---T 1 root i2c 89, 1 Dec 19 09:30 /dev/i2c-1
[/sourcecode]
Now we run a simple test, scan the i2c bus:
[sourcecode language="bash"]
sudo i2cdetect -y 0
[/sourcecode]
If your board is the Rev 2 type this:
[sourcecode language="bash"]
sudo i2cdetect -y 1
[/sourcecode]
You should see something like this:
[sourcecode language="shell"]
sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
[/sourcecode]
Reboot the Pi by:
[sourcecode language="bash"]
sudo shutdown -r now
[/sourcecode]
Hint: if you’re using the first revision of the RPI board, use “-y 0″ as parameter. The i2c bus address changed between those two revisions.