Step 5: Example 1: CMPS03 Compass Module

We now have everything ready to start using I2C! 

To use the CMPS03 compass module, connect the power to V+ and 0V, from the Pi. I used the 5V line, which they recommend not doing because it might damage your pi, It worked for me, and has caused now damage, but I am not responsible if your's fries. 

Then, connect the SDA and SCL lines to the Pi SDA and SCL, and you are ready to roll. The wiring diagram is shown at http://www.robot-electronics.co.uk/htm/cmps3tech.htm. 

When you have connected it, run the command "i2cdetect -y 0". In my case, this returned: 

       0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

This shows that the module is on address 0x60.  You then need the following python file:

import smbus
import time
bus = smbus.SMBus(0)
address = 0x60

def bearing255():
        bear = bus.read_byte_data(address, 1)
        return bear

def bearing3599():
        bear1 = bus.read_byte_data(address, 2)
        bear2 = bus.read_byte_data(address, 3)
        bear = (bear1 << 8) + bear2
        bear = bear/10.0
        return bear

while True:
        bearing = bearing3599()     #this returns the value to 1 decimal place in degrees. 
        bear255 = bearing255()      #this returns the value as a byte between 0 and 255. 
        print bearing
        print bear255


This program should be saved as anything, but add ".py" on the end. Then, run the command with sudo python whateveryoucalledit.p and you should get values written to your screen in a long list. 
<p>Now here is a question from a newb who wants to get into I2C on the Pi.</p><p>The provided page (http://www.robot-electronics.co.uk/htm/srf08tech.html) says that this comes with default address 0xE0 and it is modifiable to 0xFE with increments of 0x02. But the pi only supports up to 0x78. How does that work?</p>
<p>(Step 6 sensor)</p>
<p>Oh man, Thank you so much for this instructable! Your tutorial was short and to the point, while remaining sufficient for my applications.<br><br>Thank you so much!</p>
<p>I am just learning i2c, thanks a lot for your info, but for the price of the i2c rangefinders I think I will stick with HC-SR04 Ultrasound Wave Detector Range Ultrasonic Sensor Distance Module for just over $1 each and use i2c for the i2c sensors.</p>
<p>thanks, very usefull. just wonder if i'm able to communicate with a pcf8574 or mcp23017 i/o expander now.</p>
<p>How did you get your nano that cool green color?</p>
<p>Where do I find the SDA and SCL lines on my Pi</p>
<p>you can find them on pin 3 and 5, 3 is the SDA and the SCL is the 5th pin</p>
<p>For the 512mb of ram USE </p><p>i2cdetect -y 1</p><p>instead of the other.</p>
<p>Thank you! I've been trouble shooting for almost an hour and was about to give up when i saw your post. I thought I had killed my BMP180 while soldering it or that it was broke for some other reason. =)</p>
<p>Same goes for the Pi 2, which is also a B+.</p>
<p>A good ible - another great example would be connecting MCP23008 or MCP23017 chips and controlling them either via python-smbus (by sending bytes to registers) or via wiringpi2 (high level driver providing setup routines and an abstraction layer).</p>
<p>Please forgive a complete noob commenting here but, as I read more and more <br>on I2C, the noobier I'm getting. </p><p>What's got me confused is how to connect, for example, several SRF08 Range Sensors. <br>Presumably they all have the same address (0x70) so confusion reigns, right? I <br>suspect you can only connect devices that have different addresses which, if <br>I'm right, severely limits the usefulness of I2C. </p><p>Am I right and is there a cost-effective way around this limitation?</p><p>P.S. I'm relatively new to all this hardware stuff. I'm retired now but was a mainframe programmer who has since discovered Python and loves it.</p>
<p>You can configure the address that the SRF08 uses, there a 16 possibles, <br> meaning you could use up to 16 SRF08's on one bus. Scroll down on this <br> page to see how http://www.robot-electronics.co.uk/htm/srf08tech.shtml</p>
<p>There is a very simplai I2C level shifter that will save your port. </p><p>see for example http://playground.arduino.cc/Main/I2CBi-directionalLevelShifter, you will probably have the pull up resistors on the boards anyway so you probaly just need the two FETs. </p>
<p>If I use another sensor for example IMU breakout-LSM9DSO, What is the change in the python file?</p>
<p>Great bit of info got my almost totally unrelated project working as you had all the important stuff</p>
<p>very easy explanation... I easily get confused with Rpi tutorials on web but this one is an exception....</p><p> thank you sir... @</p><p><a href="http://www.instructables.com/member/AntMan232/" rel="nofollow">AntMan232</a></p>
<p>as a lurker who is considering a pi for all sorts of nefarious purposes, I find this very helpful!</p>
What purpose does the write(0x51) provide? Is it just activating the module on the i2c bus so values can be read from it?
<p>0x51 is the write register to command data returned in centimeter vales. 0x50 commands values in inches and 0x52 commands values in microseconds (flight time.</p><p>See <a href="http://spaces.atmel.com/gf/download/frsrelease/228/1344/SRF08%20Ultra%20sonic%20range%20finder.pdf" rel="nofollow">http://spaces.atmel.com/gf/download/frsrelease/228/1344/SRF08%20Ultra%20sonic%20range%20finder.pdf</a></p>
<p>TNX for the great instruction but 1 question </p><p>i didn't understand if the 5V i2c device has damaged your device or not , </p>
When you do: <br> <br> bear2 = bus.read_byte_data(address, 3) <br> bear = (bear1 &lt;&lt; 8) + bear2 <br> bear = bear/10.0 <br> return bear.... <br>why do you divide by 10? I am assuming you have a signed 15 bit number and you are getting rid of the last 0? Why not use &gt;&gt; 1?
I could only run i2cdetect with the command &quot;sudo i2cdetect -y 0&quot; <br>Otherwise it would not run..
Hey, AntMan. Should you be so inclined, a similar article on how to get i2s (much different than i2c functionally) working on Raspberry Pi would be enormously appreciated. I'd like to attach a Wolfson audio codec but getting up to the configuration point where I could basically do that and begin talking to it is daunting to say the least.
Thank You!
To make this 'bile better, you might explain in step 1 what I2C is for.
Ah, whoops, thanks!<br>
Would the external GPS cable for a generic Android tablet or an old XM radio possibly work? I have a couple of those that I might be able to part with if they could be made to work.
As a supplement, I have a similar write-up that I buried perhaps a little too deep on the eLinux wiki: <br> <br>http://elinux.org/RPi_Tutorial_EGHS:Communicating_With_Other_Micro-controllers <br> <br>Includes: the official (probably overkill) way of interfacing 3.3V and 5V I2C applications, software for a few languages, and kernel considerations. <br> <br>Range sensor sounds fun, I'll give that a try!
Thanks for a very straight forward step by step explanation!
all we need now is gps and radar. and viola we are ai nav.. kewl. let me know when you get GPS up and running.. <br> <br>
If you give me a GPS module, I will gladly oblige. :D If only I had the money...
Thanks for sharing this,it means i could use my temperature sensors
Thanks guys, it's nice to be able to help! It geniunely took me about a week to realise that the Adafruit code was overkill, and that it only took one line to read over i2c...
Nice and clean examples! You have a new follower.
Thanks for sharing this. This will surely come in handy.

About This Instructable




More by AntMan232:PICAXE Raspberry Pi ADCRaspberry Pi I2C (Python)Arduino stepper motor and servos shield - AW GCSE Board
Add instructable to: