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 https://github.com/mwilliams03/BerryIMU.git
The code for this guide can be found under the compass_tutorial01_basics directory.
Overview of a 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”
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.
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
In this post I’ll show how to connect and use a 16 x 2 LCD on a Raspberry Pi.
The LCD I am using is a Blue Character OLED(LCD) 16×2 from adafruit.
This display has ultra-high contrast and any-angle readability. It has the best display I have seen on any LCD.
This LCD uses the HD44780 controller which is present in almost all LCDs.
LCDs that use this controller usually have 14 or 16 pins. This LCD has 16, numbered from 0 to 16 as shown below.
Sometimes these pins are present, but not used. Eg, pins 15 & 16 are for back-light and they are not used on this LCD. It depends on the manufacture.
|HD44780 pins in Detail
||Supply Voltage for OLED and logic
||Is usually connected to a potentiometer to control the contrast of the display.
||The register select signal (RS) determines whether the Data Bit values are interpreted as a command (E.g. clear screen) or data (aka: a character to display).
||Is the Read/Write pin. In read mode, this pin is used to get feedback from the LCD to work out if the LCD can accept commands or to indicate it is too busy.
We don’t need this function as we can wait the maximum time for a command to be written (200us) before sending the next command.
If read is enabled and Pin4 on the LCD is connected to a pin on your Raspberry Pi, there is a chance that you can destroy your Pi. We only ever want to write to the LCD, we never want to read from it. So this should always be connected to ground.
||The enable pin (E)functions as the command/data latching signal for the LCD. The LCD will latch in whatever is on the Data Bits and process it on the falling edge of the E signal
Meaning, when this pin goes low, the LCD will take the input from the data pins at this time.
|Pins 7 to 14
||Are the data pins. In 4 pin mode, only pins 11 to 14 are used.
|Pins 15 & 16
||Are used for the backlight if present.
Wiring the LCD up
Below shows how to wire up the LCD to the Raspberry Pi. We will be using 4 pin mode, so there is no need to connect pins 7 to 10. This LCD doesn’t use the backlight pins, pins 15 and 16. It also doesn’t use the contrast pin, pin 3.
Continue reading Interfacing a 16 x 2 LCD with the Raspberry Pi
If you want to control the brightness of a LED, the speed of a DC motor or the direction of a servo, you will need PWM.
The video shows PWM being used to control the brightness of some LEDs.
Pulse-width modulation (PWM) is used to control the amount of power supplied to electrical devices, especially to DC motors, servos and LEDs.
PWM is able to achieve this by quickly turning off and on the power to the device. The measurement for this is duty cycle.
Duty cycles describes the proportion of ‘on’; a low duty cycle corresponds to low power, because the power is off for most of the time. A high duty cycle corresponds to high power, because the power is on most of the time.
Duty cycle is expressed in percent, 50% is when the power is on half the time and 100% being fully on.
Continue reading Software PWM on a Raspberry Pi
In this post I show how to control the GPIO on a Raspberry Pi using a touchscreen.
This is a follow up on my previous post Programming for a Touchscreen on the Raspberry Pi
The TFT doesn’t come up too well in the above video. The picture below gives a better idea of how it looks. Click to enlarge
Link to the code;
In the above code touchbuttons.c creates three buttons on the TFT which will be used to turn on/off three LEDs.
This can easily be changed to add more buttons.
touchbuttons.c also requires WiringPI and needs to be compiled with
gcc -g -o buttonExample buttonExample.c -l wiringPi
Continue reading Controlling the GPIO on a Raspberry Pi with a Touchscreen
To accept input from a touchscreen we have to use the event interface of the Linux input system. We use the ioctl capabilities of the event interface, in addition to the normal read and write calls to get information from the touchscreen. This blog post explains how to use the touchscreen within your own programs using C as well as writing directly to the framebuffer.
Images of my TFT from a previous post;
Continue reading Programming for a Touchscreen on the Raspberry Pi
I have spend the last month creating a new version of PiBBOT (Pi Balancing roBOT) , PiBBOT V2.
This version has a sturdier frame and a LCD display. I replaced the 1.8″ TFT with a LCD as the TFT was causing delays in the main loop timing. I also added a very slim battery for the Raspberry Pi.
- RGB backlight LCD 20×4 which shows gyro, accelerometer and complimentary filter angle.
- Volt Meter to view condition of battery used for motors.
- RF Receiver RF M4 Receiver – 315MHz. Used to tune PID and then control direction.
- 1×4 Keypad to turn motors on/off and to reset the gyro.
- Motors; 9.7:1 Metal Gearmotor 25Dx48L mm with 48 CPR Encoder
- Wheels; Pololu Wheel 90x10mm
- IMU; BerryIMU – An accelerometer, gyroscope, magnetometer and barometric/altitude sensor
- Battery for Motors – 7.2V Tenergy 3800mAh Flat NiMH High Power (38A Drain Rate)
- Battery for Rasperry Pi – Anker Astro Slim2 4500mAh Ultra-Slim Portable External Battery Charger Power Bank
Continue reading PiBBOT V2 – Balancing Robot using a Raspberry Pi