All posts by Mark Williams

Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi

This guide covers how to use an Inertial Measurement Unit (IMU) with a Raspberry Pi . This is an updated guide and improves on the old one found here.

I will explain how to get readings from the IMU and convert these raw readings into usable angles. I will also show how to read some of the information in the datasheets for these devices.

This guide focuses on the BerryIMU. However, the theory and principals below can be applied to any digital IMU, just some minor modifications need to be made.

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone


The code for this guide can be found under the gyro_accelerometer_tutorial01_angles directory.

A note about Gyros and Accelerometers

When using the IMU to calculate angles, readings from both the gyro and accelerometer are needed which are then combined. This is because using either on their own will result in inaccurate readings. And a special note about yaw.

Gyros – A gyro measures the rate of rotation, which has to be tracked over time to calculate the current angle. This tracking causes the gyro to drift. However, gyros are good at measuring quick sharp movements.

Accelerometers – Accelerometers are used to sense both static (e.g. gravity) and dynamic (e.g. sudden starts/stops) acceleration. They don’t need to be tracked like a gyro and can measure the current angle at any given time. Accelerometers however are very noisy and are only useful for tracking angles over a long period of time.

Accelerometers cannot measure yaw.   To explain it simply, yaw is when the accelerometer is on a flat level surface and it is rotated clockwise or anticlockwise.  As the Z-Axis readings will not change, we cannot measure yaw.   A gyro and a magnetometer can help you measure yaw. This will be covered in a future guide.


Setting up the IMU and I2C

The IMU used for this guide  is a BerryIMUv3  which uses a LSM6DSL,
that consists of a 3-axis gyroscope plus a 3-axis accelerometer and a LIS3MDL which is a 3-axis magnetometer.

  • LSM6DSL – Accelerometer and gyroscope datasheet can be found here.
  • LIS3MDL – Magnetometer datasheet can be found here


This IMU communicates via the I2C interface.

The image below shows how to connect the BerryIMU to a Raspberry Pi

BerryIMUv3 and Raspberry Pi I2C hook up


The BerryIMUv3 can also be connected using the Raspberry Pi QWIIC connector and cable;

QWIIC connector Raspberry Pi
QWIIC connector for the Raspberry Pi


Continue reading Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi

Create a Digital Compass with the Raspberry Pi – Part 1 – “The Basics”

This will be a multipart series on how to use a digital compass(magnetometer) with your Raspberry Pi.

The magnetometer used in these tutorials is a LSM9DS0 which is on a BerryIMU. We will also point out where some of the information can be found in the Datasheet for the LSM9DS0. This will help you understand how the LSM9DS0 works.

The math and logic in this series can also be used with other magnetometers or IMUs.

We will also go over how to do some basic communication on the i2c bus. As well as using SDL to display the compass heading as traditional compass as shown in the video above.

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone

The code for this guide can be found under the compass_tutorial01_basics directory. 

Overview of a Compass

Raspberry Pi Compass
A traditional Magnetic compass (as opposed to a gyroscopic compass) consists of a small, lightweight magnet balanced on a nearly frictionless pivot point. The magnet is generally called a needle. The Earth’s Magnetic field will cause the needle to point to the North Pole.

To be more accurate, the needle points to the Magnetic North. The angle difference between true North and the Magnetic North is called declination. Declination is different in different locations. This angle varies depending on position on the Earth’s surface, and changes over time.

The strength of the earth’s magnetic field is about 0.5 to 0.6 gauss .

Continue reading Create a Digital Compass with the Raspberry Pi – Part 1 – “The Basics”

Matchbox-Desktop on the Raspberry Pi

Matchbox-desktop is a lightweight windows manager for embedded systems. It works great on a Raspberry Pi with one of the smaller TFTs attached. E.g. PiScreen, PiTFT , etc…

Matchbox-desktop is “finger friendly” and makes it easy to navigate while just using your fingers on the touchscreen.

Mathcbox-Desktop needs to be installed from source and via apt-get to get it going correctly, in this order;

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install autoconf autogen intltool libtool libx11-dev libxext-dev libxft-dev libpng-dev libgconf2-dev libgtk2.0-dev libstartup-notification0-dev libdbus-glib-1-dev -y
pi@raspberrypi ~ $ git clone
pi@raspberrypi ~ $ git clone
pi@raspberrypi ~ $ git clone git://
pi@raspberrypi ~ $ cd libmatchbox
pi@raspberrypi ~ $ ./
pi@raspberrypi ~ $ make
pi@raspberrypi ~ $ sudo make install
pi@raspberrypi ~ $ cd ../matchbox-common
pi@raspberrypi ~ $ ./
pi@raspberrypi ~ $ make
pi@raspberrypi ~ $ sudo make install
pi@raspberrypi ~ $ cd ../matchbox-desktop
pi@raspberrypi ~ $ ./
pi@raspberrypi ~ $ make
pi@raspberrypi ~ $ sudo make install
pi@raspberrypi ~ $ sudo apt-get install matchbox-desktop

To start matchbox-desktop

pi@raspberrypi ~ $ FRAMEBUFFER=/dev/fb1 xinit /usr/local/bin/matchbox-desktop

UPDATE: Matchbox-desktop doesn’t work with  newer Raspbian images. The last confirmed image it works on is 2014-06-20-wheezy-raspbian.img

To get matchbox-desktop working;
1. Burn the above img.
2. Follow the instructions above and matchbox-desktop will compile correctly.
3.Perform updates

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo rpi-update
pi@raspberrypi ~ $ sudo apt-get upgrade
pi@raspberrypi ~ $ sudo apt-get dist-upgrade
pi@raspberrypi ~ $ sudo apt-get install raspberrypi-ui-mods

4. Remove SPI from black-list
Open raspi-blacklist.conf

pi@raspberrypi ~ $ sudo nano /etc/modprobe.d/raspi-blacklist.conf

And place a hash ‘#’ in front of ‘blacklist spi-bcm2708’.

5.Configure drivers for any attached TFT.
6. Disable framebuffer driver option for /dev/fb0.
Open up the framebuffer conf file;

pi@raspberrypi ~ $ sudo nano /usr/share/X11/xorg.conf.d/99-fbturbo.conf

Comment out the line of text below;
Option "fbdev" "/dev/fb0"
This can be done by adding a hash (#) at the beginning of line.
#Option "fbdev" "/dev/fb0"

7. Now startmatchbox-desktop.

pi@raspberrypi ~ $ FRAMEBUFFER=/dev/fb1 xinit /usr/local/bin/matchbox-desktop


New Kickstarter Launched – BerryIMU

An accelerometer, gyroscope and magnetometer which is specifically designed for the Raspberry Pi.


What is it?

BerryIMU is an inertial measurement unit, or IMU, that measures and reports on velocity, orientation and gravitational forces, using a combination of an accelerometer, gyroscope and a magnetometer.

IMUs can be found in devices like, quad copters, smart phones, segways, Wii Remote etc.. and are used to sense movement and orientation.

BerryIMU for the raspberry pi

Adding BerryIMU to your Pi will open up a whole new world of possibilities.

BerryIMU is specifically designed for the Raspberry Pi and is designed to take some of the complexity out of IMUs and to try and make them more enjoyable to use. BerryIMU includes the latest and greatest sensor ICs.

Head on over to to check it out;

Virtual Keyboard for the Raspberry Pi

Matchbox-keyboard is an on screen ‘virtual’ or ‘software’ keyboard which works well for small touchscreen TFTs connected to a Raspberry Pi.

Matchbox-keyboard also uses XML files to specify the layout of the keys, which makes it highly customizable.

The touchscreen used in the above video is a PiScreen.
Continue reading Virtual Keyboard for the Raspberry Pi

Raspberry Pi with stroke and Gesture Recognition

If you are using a touchscreen with your Raspberry Pi, then this guide is for you.

Xstroke (Gesture recognition) is the perfect tool to help you get more out of your touhscreen. I am using a PiScreen

Gesture recognition allows you to draw “strokes” on your touchscreen, which get interpreted as commands or text. Xstroke allows these to be entered anywhere on the screen. In some cases, Xstroke can be used instead of a keyboard.

Continue reading Raspberry Pi with stroke and Gesture Recognition

PiScreen – 3.5″ TFT(480×320) with touch control for the Raspberry Pi – Kickstarter launched

We have launched a Kickstarter campaign for a 3.5″ TFT (480×320) with touch control, we have given it the name of PiScreen.

PiScreen includes a 3.5″ TFT with touchscreen control, PCB and components needed to add this display to a Raspberry Pi.

PiScreen connects right on top of the Raspberry Pi GPIO pins

PiScreen can now be purchased directory from

PiScreen is compatible with all models of Raspberry Pi, including Pi 2.


The TFT used is the best quality we have seen so far.


Some specifics;
Touch Screen Resistive
Display construction 480 * 320 DOTS
LCD Controller ILI9481
Touch Screen ControllerXPT2046
Display Color Multicolor, 65536 colors

Head on over and check it out.


How to Control the GPIO on a Raspberry Pi with an IR Remote

In this post I demonstrate how to use an infrared remote to control the GPIO on a Raspberry Pi.

Normally a remote would be used to control a TV card or XMBC, however they also provide a good interface to control the GPIO on a Raspberry Pi.
Adafruit has a Mini Remote and IR sensor which are perfect for this.

IR ReceiverMini Remote

In this example we will use the remote to control a number of LEDs connected to some GPIOs on a Raspberry Pi.

Connect the IR Sensor to the Raspberry Pi

Connecting the IR sensor to a Raspberry Pi is very easy as there are only 3 pins on the sensor, GND, 3v and Output. We will connect the output to GPIO 18. You can choose another pin, just take note of it as you will need to specify this pin when installing LIRC.
We will also connected up three LEDs to GPIOs 23, 24 & 25, and a 270Ω on the GND sound of each LED.


Here is my setup;


Continue reading How to Control the GPIO on a Raspberry Pi with an IR Remote