Introduction: Raspberry Pi Loves Sensors and LEDs

About: DIY, electronics...and loves getting creative...

This tutorial teaches you how to talk to I2C devices on a raspberry pi, this is based on the ADAfruit tutorials but i have made the code more generic and you can easily modify them to work with any sensors.

This tutorial uses the distro provided by adafruit, if you dont have the distro up an running re-visit one of my old tutorial on raspberry pi.

Step 1: Parts

i am using a Mblink and PMODtemp (from digilent) for this tutorial, this approach would work for most I2C devices. connect the I2C sensor to the I2C pins refer the connection diagram from here, make sure you connect the a 3.3v I2C sensor.

connect 
SCL>>> SCL
SDA >>> SDA
GND >>>GND
VCC >>>VCC


the diagram below is from http://elinux.org/RPi_Low-level_peripherals

Step 2: Tools

make sure the tools are installed... to test if the tool are installed run the following command..

sudo i2cdetect -y 0

this will scan all devices connected to the I2C bus. it should look like the pic below. if you see that i2ctools are not installed 
run the following comand

sudo apt-get install i2c-tools

also install 

sudo apt-get install python-smbus

sudo apt-get install python-pip

sudo pip install rpi.gpio


sudo /home/pi/Desktop

wget -O geekman-python-eeml.tar.gz https://github.com/geekman/python-eeml/tarball/master

tar zxvf geekman-python-eeml.tar.gz

cd geekman-python-eeml*

sudo python setup.py install

note down the address of the I2C device that you have connected. in this case i have connected a temp. sensor PMODTEMP2 from digilent.we use the sumbus function for I2C communcation.






Step 3: Temp. on a Site

now you can setup your COSM account for receiving the sensor data... note the key and feed ID down, you can also refer this tutorial for more detailed steps

to make things easier you can download the git code to home folder. doing the following...

cd /home/pi/Desktop


run the code
git clone https://github.com/sajingeo/raspberry-pi-saj.git

to get a copy of my repository on your desktop.

cd raspberry-pi-saj

cd cosm-temp-loger


here you will see two files, open the  i2cpy.py using nano

nano i2cpy.py

read the code for comments, the Adafruit library that is included in the files is very good for I2C operations and should be used to avoid common I2C mistakes...thanks to ADAFRUIT...

you should be able to now embed the temperature graph on your website, and host it on the raspberry pi  refer this tutorial.


Step 4: Let Raspberry Pi Check My Email

this uses raspberry pi to check my email and control Mblink to notify me of a new email...

connect the Mblink as shown in step 1. and find the address as shown in step 2...


go to /mail-checker folder in my git repo and open i2cLED.py

change the user name and password for your mail account and set the MAILSNOW to your current inbox unread mails number and MAILCHECK as the time duration for checking mails. (make sure you are connected to the internet)

run the code and you will see that the mail LED goes green when you have now new emails and RED when you GOT MAIL...


Step 5: More Fun With Raspberry Pi

Do check out other projects in the GIT repo, that includes a MP3 player and  a CPU usage logger(2 types), the README file in the folders should explain on how to get them running...happy hacking with raspberry pi... you can check out my raspberry pi website here 

Make It Glow

Participated in the
Make It Glow