i2c working and noisy sensors

i2c working!

I had tried many things to get basic i2c working on my RasPi B+ and despite lots of help, failed miserably.  I bought an A+ for another project and got i2c working straight away.  There are lots of tutorials on i2c on the Pi and a really good script that I mentioned in my last post so I won’t be going into setting up i2c.
The test that shows whether you’ve been successful is running sudo i2cdetect -y 1 and having i2c components you have connected to the RasPi appear:

i2cdetect discovering my compass and gyro/accelerometer devices. (The device at UU is related to the Pi)
In the screenshot you can see that I am using the latest Raspbian image that was released last month.
I updated my RasPi B+ to the latest image and on a whim tried i2c on it and it worked!  I had thought I’d fried the i2c hardware, but I guess now that I had somehow broken something in the image, can’t think how or what, but anyway refreshing the Raspbian image fixed it.

Cheap i2c devices from eBay

So the two i2c devices I have connected to my Raspi are a Compass and Gyro/Accelerometer.  Both are really cheap units.  I got the Gyro from a UK supplier because I didn’t want to wait for it to come from China.
This text is what to search for if you wish to buy these and link to the actual items I bought on eBay
Both units can be bought for about a quid from China, which is amazing value for such interesting modules.

My new Raspi A+ hooked up to i2c Compass and Gyro/Accelerometer

The image above shows the units connected up, in both cases you simply need to connect v3.3, Ground, i2c Clock and i2c Data.  Nothing else to do, it couldn’t be easier. Both of these units work at v3.3.

There are wires and buttons at the back of the breadboard where I was playing with the internal pull up and pull down resistors on the RasPi GPIO.

Python and Tkinter interface

As a learning exercise I put together a Python and Tkinter interface to display the output from the sensors.  I found that to get the Tkinter to work well on the Pi I was better off using Python 2.7 which was really disappointing.

I originally wrote my own Python class to encapsulate the compass module but the results seemed iffy so I found a class that is better than my effort here: https://github.com/rm-hull/hmc5883l as it turns out, the problem is that the area of my desk where I am testing the compass is awash with magnetic interference, which I proved with my trusted Silva compass I use for hiking.  In fact the output from the compass sensor is just as iffy with the better code.

Looking at the interface, I have created a canvas in Tkinter and draw a yellow line for the compass heading and move red and green circles around for the gyro and accelerometer.

I only take notice of 2 out of the 3 data axis that the sensors give, this leads to inaccuracies in the compass if it is not kept horizontal, these inaccuracies can be fixed in code by doing the math in 3 dimensions.

The video shows just how noisy the data is.  To the point that it’s difficult to interpret if the Gyro is working properly at all.

My balancing robot has 3 accelerometers on it and I have been working on the Python code (the robot has a micro Python board in it) to get a very smooth and fast accelerometer reading of tilt (by measuring the acceleration due to earths gravity) and I need to work out how to clean that up when the robot is falling and there are accelerations due to actual movement.  I have moved the accelerometers as close as possible to the wheel axis to minimise the unwanted effects of movement but there are a few challenges.  The video shows nicely what we are up against using cheaper sensors for our small scale robotics.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s