## Introduction: LED Compass and Altimeter

Objects with LEDs always fascinates me. Therefore this project to combine the popular digital compass sensor HMC5883L with 48 LEDs. By placing the LEDs in a circle the led which is lighting is the direction you are heading. Every 7.5 degrees will drive a new LED which gives detailed results.

The GY-86 board also provides a MS5611 barometric pressure sensor. With help of this sensor it is possible to calculate the altitude. Because of the high resolution it is perfect for altimeters.

The MPU6050 sensor on the GY-86 board has both a 3-axis accelerometer and a 3-axis gyroscope. The gyroscope can measure the velocity of the angular position over time. The accelerometer can measure gravitational acceleration and by using trigonometry math it is possible to calculate the angle at which the sensor is positioned. By combining the accelerometer and gyroscope data it is possible to get information about the sensor orientation. This can be used for the tilt compensation for the HMC5883L compass (to do).

The short instruction videos in this instructable will explain in detail how it works. Calibration procedures are automated so success guaranteed. Temperature is available in Celsius (default) or Fahrenheit.

Have fun !!

## Step 1: Altimeter

The altimeter makes use of the MS5611 barometric pressure sensor. Altitude can be determined based on the measurement of atmospheric pressure. The greater the altitude, the lower the pressure. At startup, the altimeter uses the default sea-level pressure of 1013.25 mbar. By pressing the button at pin 21 the pressure at your location will be used as a reference. In this way it makes it possible to approximately measure what height something has (e.g. when driving uphill with a car).

The so called "Hypsometric formula" is used in this project. This formula makes use of the temperature to compensate the measurement.

`<p>float alt = ((powf(source / ((float) P / 100.0), 0.19022256) - 1.0) * ((float) TEMP / 100 + 273.15)) / 0.0065;</p>`

You can find more about the hypsometric formula here:

Hypsometric formula

Factory calibration data and sensor temperature are read from the MS5611 sensor and applied to code to get the most accurate measurements. During test I found that the MS5611 sensor is sensitive for air-flows and differences in light intensity. It must be possible to get better results than in this instruction video.

## Step 2: Parts

1 x Microchip 18f26k22 microcontroller 28-PIN PDIP

3 x MCP23017 16-Bit I/O Expander 28-pin SPDIP

48 x LED's 3mm

1 x GY-86 module with MS5611, HMC5883L and MPU6050 sensors

1 x SH1106 OLED 128x64 I2C

1 x Ceramic capacitor 100nF

1 x 100 Ohm resistor

## Step 3: Circuit Diagram and PCB

Everything fits on a single sided PCB. Find here the Eagle and Gerber files so you can make it yourself or ask a PCB manufactorer.

I use the LED Compass and Altimeter in my car and use the OBD2 interface as a power supply. The microcontroller fits perfect in the connector.

## Step 4: How to Perfectly Align LED's in a Circle in Seconds With Eagle PCB Design Software

You must see this really nice feature in Eagle PCB Design Software which saves you hours of work. With this Eagle feature you can perfectly align LEDs in a circle in seconds.

Just click on the "File" tab and then "Run ULP". From here click on "cmd-draw.ulp". Select "Move", "degree step" and "Circle". Fill in the name of the first LED in the field "name". Set the coordinates of the center of the circle on the grid at the fields "X center coord" and "Y center coord". In this project are 48 LEDs so 360 divided by 48 makes 7.5 for field "Angle step". The radius of this circle is 1.4 inch. Hit enter and you have a perfect circle of LEDs.

## Step 5: Compass Calibration Process

The HMC5883L includes a 12 bit ADC that enables 1 to 2 degree Celsius compass heading accuracy. But before it gives usable data it needs to be calibrated. In order to have this project up and running smoothly there is this calibration method which provides x- and y offset. It is not the most sofisticated method but it is sufficient for this project. This procedure will cost you only a few minutes and gives you nice results.

By loading and running this software you will be guided in this calibration process. The OLED display will tell you when the process will start and when it ends. This calibration process will ask you to turn the sensor 360 degrees while holding it absolutely flat (horizontal to the ground). Mount it on a tripod or something like that. Doing this by holding it in your hand does not work. At the end the offsets will be presented on the OLED. If you run this procedure several times you must see almost equal results.

Optionally, the collected data is also available via RS232 via pin 27 (9600 baud). Just use a terminal program like Putty and collect all the data in the log file. This data can be imported easily in Excel. From here you can see more easily how the offset of your HMC5883L looks like.

The offsets are put in the EEPROM of the microcontroller. These will be loaded at startup of the compass and altimeter software which you'll find in step 7.

## Step 6: Compensate the Magnetic Declination of Your Location

There is a magnetic North and a geographic North (North Pole). Your compass will follow the earth's magnetic field lines so point to the magnetic North. The difference between the magnetic North and geographic North is called the magnetic declination. At my location the declination is only 1 degree and 22 minutes so not worth compensating this. At other locations this declination can be up to 30 degrees.

Find the magnetic declination at your location

If you want to compensate this (is optional) you can add the declination (degrees and minutes) in the EEPROM of the microcontroller. At location 0x20 you can add the degrees in signed hexadecimal form. It is signed because it can also be a negative declination. At location 0x21 you can add the minutes also in hexadecimal form.

## Step 7: Compile the Code

Compile this source code and program your microcontroller. This code compiles correct with MPLABX IDE v5.20 and XC8 compiler v2.05 in C99 mode (so include the C99 directories). Also the hex file is available so you can skip the compilation procedure. Make sure that you uncheck the checkbox "EEPROM data enabled" to prevent calibration data (see step 5) to be overwritten. Set your programmer to 3.3 volt!

By connecting pin 27 to ground you get the temperature in Fahrenheit.

Thanks to Achim Döbler for his µGUI graphic library

Runner Up in the
Sensors Contest