Category Archives: Linux

Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi

A lot of people have asked how I got the readings from the Gyro, Accelerometer, and Compass inertial measurement unit(IMU) which is used to keep PiBBOT upright.

UPDATED And updated guide has been published here, with cleaner and simpler code.
BerryIMU Raspberry Pi Gyroscope Accelerometer

In this guide 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. The theory and principals below can be applied to any digital IMU, just some minor modifications need to be made. Eg register values, sensitivity level…

The code can be found here;
https://github.com/mwilliams03/Raspberry-Gyro-Acc

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.

Here is why;
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.

Here is an excellent tutorial about accelerometers and gyros;
http://www.instructables.com/id/Accelerometer-Gyro-Tutorial/?ALLSTEPS

Setting up the IMU and I2C

The IMU I use is a MinIMU-9 v2 Gyro, Accelerometer, and Compass which uses a L3GD20 3-axis gyroscope and a LSM303DLHC 3-axis accelerometer and 3-axis magnetometer.
The datasheets are needed if you want to use these devices;
L3GD20 datasheet
LSM303DLHC datasheet

This IMU communicates via the I2C interface.

From the images below, you can see how to connect it to the Raspberry Pi. You can also see the orientation of the X, Y and Z axis.

Temp 12 hour Graph Graph

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

Expand the Number of GPIO Pins on the Raspberry PI

As of revision 2 boards, there is space to add some header pins to get access to eight more GPIO pins. These are found on the Header 5. Highlighted below.

Raspberry Pi P5 header

Raspberry Pi P5 header

 

 

Here are their functions;
Raspberry Pi P5 header GPIO P5

  • P5-Pin1 – 5v0
  • P5-Pin2 – 3.3v
  • P5-Pin3 – GPIO28
  • P5-Pin4 – GPIO30
  • P5-Pin5 – GPIO29
  • P5-Pin6 – GPIO31
  • P5-Pin7 – GND
  • P5-Pin8 – GND

I needed some more pins for PiBBOT as I want to change my TFT to a character OLED which needed more pins then I had spare, so I decided to make use of these extra pins.

Here are the results, I soldered the head onto the underside of the Pi so I could plug in an IDC cable.

Continue reading Expand the Number of GPIO Pins on the Raspberry PI

Success with a Balancing Robot using a Raspberry Pi

I have had success with getting PiBBOT to balance.

 

 

 

When I saw my first two wheel balancing robot I was very fascinated.  And after receiving my Raspberry Pi, I decided to build one myself.
PiBBOT (Pi Balancing roBOT) is my first successful balancing robot. And it has room for improvement and extra functionality.

When building PiBBOT, I had a few roadblocks I needed to overcome;

  1. I originally had the Anker as the power source for both Pi and the motors, however the amperage was too low.
  2. Original H-Bridge not powerful enough for my motors
  3. Gyro calculation off by 15 degrees


The TFT displays the angles from the accelerometer, gyro, complementary filter and power to the motors.
The buttons are to turn the motors on and off and to reset the gyro.

BerryIMU Raspberry Pi Gyroscope Accelerometer

What next?

  • Incorporate the wheel encoders for better balance
  • Build a sturdier frame
  • Implement direction control
  • The able to self right itself after falling over
  • Room mapping
  • Retrieve a can of beer from the fridge
PiBBOT PiBBOT TFT Motor Controller
PiBBOT TFT PiBBOT PiBBOT TFT


PiBBOT consists of these components;
Adafruit:
TFT; 2.2″ 18-bit color TFT LCD display
RF Receiver :RF M4 Receiver – 315MHz

Pololu;
Motors;  9.7:1 Metal Gearmotor 25Dx48L mm with 48 CPR Encoder
Wheels; Pololu Wheel 90x10mm
IMU; MinIMU-9 v2 Gyro, Accelerometer, and Compass (L3GD20 and LSM303DLHC).

Anker;
Battery; Anker® Astro3 10000mAh  5V / 9V / 12V 2A Dual USB Output External Battery

Tenergy;
Battery; 7.2V Tenergy 3800mAh Flat NiMH High Power


Continue reading Success with a Balancing Robot using a Raspberry Pi

Raspberry Pi with a 1.8″ TFT shield

I’m using a shield from Texy to attach my TFT to my Pi.

TFT Shield TFT shield

This is soooo much easier then connecting up the cables individually, which requires me to always refer to the wiring diagram… and it always takes me two or three goes to get it right.

This just requires a quick snap onto the top of the GPIO pins. It also includes wiring to use 3 buttons on the GPIO pins not being used by the TFT.

Look here to find out where you can get one;
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674







Continue reading Raspberry Pi with a 1.8″ TFT shield

Automatic login between two Raspberry Pi hosts

If you have multiple Raspberry’s or even multiple Unix hosts , you will start to get frustrated at constantly having to enter a password every time you copy files between them or access these devices from another Unix host.


There is an easy way to disable this with RSA key pairs.

  1. On each host, enter ssh-keygen
  2. Hit enter to keep the default directory.
  3. Passphrase.  Leave blank and hit enter
  4. Your keys are created under .ssh/

Now you need to copy over your key to all the other hosts with;
ssh-copy-id -i .ssh/id_rsa.pub username@serverip



Once this is done, you should now be able to copy files or access each device without having to use passwords.

You still need to enter a password when trying to connect from a host that hasn’t got a key.


Continue reading Automatic login between two Raspberry Pi hosts

Raspberry Pi with a 2.2″ TFT

 

Notro has done some great work with developing drivers for different models of TFT displays for the Pi.
He successfully created one for the Adafruit 2.2″ TFT . This is a great little display and is very clear.

Notro’s Wiki with instructions can be found here.

As with the 1.8″ TFT from Adafruit, this device can be used to display video, images and your own data with easily using pygame.

A couple of images from my Pi running the 2.2 TFT;

photo2.2TFT

PiTFT22-1

 

 
Continue reading Raspberry Pi with a 2.2″ TFT

PiBBot – Robotic Project – Phase 2 – Prototype

Here is my first attempt at getting PiBBot balanced.

This is a very early prototype, confirming that all the hardware works and the I can get the time of the main loop down to 20ms, which was easy.
This prototype also helped me get my head around the code and math need to balance.

 

 

I am waiting on the delivery of some hardware to build my final and larger version.  Which will have a battery pack placed up high, this will help a lot with the balancing.

1stBalance2

 

The main components used to balance this prototype;

  • MinIMU-9 v2 Gyro, Accelerometer, and Compass (L3GD20 and LSM303DLHC Carrier)
  • Solarbotics GM6 120:1 Mini Gear Motor Offset Output
  • Dual H-Bridge Motor Driver
  • 315MHz RF M4 Receiver
  • 12v battery

The receiver allowed my to change the PID values in real time.

There is also a lot of other components on the breadboard, but they are not needed to balance. Eg TFT, 8x8Matrix, Temp monitor…



Continue reading PiBBot – Robotic Project – Phase 2 – Prototype

Setting up VNC on Raspberry Pi

I run my Pi headless and every now and than I need to access the GUI.
Rather than connecting up a monitor, keyboard and mouse, I use TightVNC.

Below are some quick instructions on how to get this working.



– Install TightVNC

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install tightvncserver


-To start VNC. When running it for the first time you will be asked for a password.

pi@raspberrypi ~ $ vncserver :1 -geometry 1200x768 -depth 24



-To connect to your Pi from another PC, use TightVNC viewer.
VNC viewer






-To stop TightVNC on your Pi.

pi@raspberrypi ~ $ vncserver -kill :1

Continue reading Setting up VNC on Raspberry Pi

PiBBot – Robotic Project – Phase 1 – Testing Components

This is the first phase on my Balancing Robot project.
Which I think I will call PiBBot. (Pi balancing Bot)

I want to test all the components first before I put them in their final position on the frame for my balancing robot.

Components2
Components1

Hardware list;

  • Raspberry Pi rev 2
  • Two breadboards
  • 1.8″ TFT, connected via SPI
  • Three 8×8 led matrix’s connected via I2C
  • One MinIMU  (Accelerometer, Gyro & Compass) all connected via I2C
  • H-Bridge to control the motors
  • Temperature sensor connected via I2C
  • 315Mhz RF Receiver
  • One LED

Click the image below to see the components named on the breadboard;
Breadboard with Labels

Continue reading PiBBot – Robotic Project – Phase 1 – Testing Components

Raspberry Pi and an 8×8 LED Matrix, using C.

Below is some information on how to get an 8×8 led matrix working on your Raspberry Pi using C.
I have also included the code needed to get text scrolling.

The matrix I am using is this one from Adafruit. This matrix uses a HT16K33 controller chip and communicates with the Pi via the i2c bus.


Adafruit has very good and detailed tutorials on how to solder it up and get i2c working between your Pi and the Matrix.
http://learn.adafruit.com/adafruit-led-backpack/
http://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi/configuring-your-pi-for-i2c


In brief;

  1. Scan the i2c bus for your device.
  2. Download the code needed.
  3. Compile.
  4. Run.



1. Scan i2c bus

Adafruit have some great instructions in the links above on how to do this.
When using i2cdetect to scan my bus, 0x70 was returned for the address of my matrix.
I am using a Rev B board and my bus is 1. If you get nothing back, check bus 0 with i2cdetect -y 0.

pi@raspberrypi ~ $sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — —
70: 70 — — — — — — —
pi@raspberrypi ~ $




Continue reading Raspberry Pi and an 8×8 LED Matrix, using C.