i2c working!
![]() |
i2cdetect discovering my compass and gyro/accelerometer devices. (The device at UU is related to the Pi) |
Cheap i2c devices from eBay
![]() |
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.