Wednesday, June 8, 2016

DIY Aviation Altimeter with QNH setting

Aviation use altimeters are extremely sensitive mechanical devices which use a bellows to convert pressure to displacement, and then use a clockwork mechanism to convert the displacement to dial position on the display. Since the overall displacement is quite small (a few millimeters of bellows compression for the ~20,000 ft. altitude range of the device, the mechanism must be fairly complex, precise and maintained periodically. Accurate and sensitive MEMS pressure sensors have been available since around 2005, which are a perfect replacement for the mechanical altimeters. However the early versions were analog (simple Wheatstone bridges) which meant that they were susceptible to drifts, required temperature compensation and fairly complex electronics to read the output voltage with sufficient precision to allow use as an aviation altimeter. 

DIY altimeter.


Technology to the rescue, recently these problems were solved with tiny pressure sensors which include temperature compensation, low drift, and high precision analog digital converters on-board. Simple communication protocol is sufficient to interface with them, and it is only a matter of following the datasheet to implement up to 24 bit pressure and temperature readout. Quite incredible, where the tecnology has come!

One such notable pressure sensor is Measurement Specialities MS5611 series. They have 24 bit precision and high accuracy, with factory calibrated coefficients permanently stored on-chip, which are used to convert raw measurement values to pressure reading. It is a dream come true; such a precision and accuracy is difficult to come by at such low prices, of about $8~10. Better, they come installed in many drone flight controllers. This is great because for about $25, you get a STM32F103 microcontroller with many other sensors, clock, regulator, all broken out to .1" pin headers. A great platform to experiment and develop with (see my blog on using Naze32 for development).

In this project, a full function barometric altimeter for aviation use was implemented using a Flip32 (Naze32 clone) flight controller board with installed MS5611 pressure sensor, an SSD1306 graphic OLED screen on I2C, and a rotary volume encoder. A single LiPo cell powers the system, with about 40mA current draw.

QNH adjustment is also implemented. You can learn the local QNH value of a nearby airport, dial it into the device, and obtain the MSL altitude in feet. In exactly the same way as a conventional altimeter is used. It was compared during flight with the altimeter of a Cessna 172, and the altitude is always within about 50ft, so it works quite well.




It is quite easy if you want to build one! You only need to buy a Naze32 or Flip32, or similar flight controller with a STM32F1, connect the I2C display (only 4 wires), connect the volume encoder (again 4 wires), burn the binary, and you are ready to go! It can be powered off a single LiPo cell. A small 240mAh cell completely hides behind the display, and will power the altimeter for about 6 hours.


Operation: At power up, an information screen is briefly displayed. Then it returns to normal operation where altitude in ft. is displayed in the center of the screen, measured local pressure in hPa and temperature in Deg. C. at the top, and set QNH at the bottom. To adjust QNH, press and turn the dial. The QNH set screen appears. At the center QNH value is displayed and at the top are the MSL altitude in feet and meters. If you know QNH, you can dial it while keeping the dial pressed in, or if you know the MSL altitude, you can dial that, just as in conventional altimeters. Upon releasing the dial, the device returns to the altitude display screen. Double click takes you into the information screen and back out of it.


Information screen at power up.
QNH set screen:  Enter either QNH of nearby airport or known airfield altitude. Turning the knob changes both.

Firmware: The source code of the project can be downloaded here. It uses u8glib graphics library ARM port, and was compiled with GCC ARM Embedded toolchain, easily downloadable from Launchpad. A binary is also provided, if you do not want to compile from source, for direct burning into a Naze32, Flip 32 or similar. Electrical connections are very simple: OLED display connects to the provided I2C terminals (I2C2), encoder to GPIOA: A signal to PA1, B signal to PA3, click signal to PA0. Internal pull ups of STM32 are used for simplicity so no pull up resistors are needed, no smoothing capacitors are needed either, since debounce is done in the software by a digital filter. See my blog posts for u8glib on STM32F103, and on connecting a volume encoder to the STM32F103.

The pressure altitude calculation formula can be found at several places in the Internet. There are a few sets of coefficients with a slight variation. However, the ones used here have been tested aboard a C172, and compare very well with its installed (mechanical) altimeter.

I wrote the pressure sensor communication and pressure calculation routines from the device datasheet.  I also wrote the bulk of u8g_arm.c and u8g_arm.h files using templates from u8g and several examples from around the web. One critical component is the setup of TIM2 for the delays.

So there you have it. Please build one for yourself, and make suggestions on how it should be. I have several ideas in my mind of how to make it better:
  • Implement a menu to adjust parameters (AGL or QNH display, feet or meters etc.).
  • Battery backup or   EEPROM backup for set parameters. Unfortunately flight controllers typically have the battery backup pin of the processor connected to upply voltage. Also, STM32F103 does have self programmable flash but again unfortunately, with a very small cycle count for this application of about 10,000 writes.
  • Upon power up it should default to the latest recorded altitude for best convenience.
  • I have other versions for larger STN LCD displays, for DOGM128W, and for ST7920, both connected over the SPI interface. DOGM128W is nice because it has a small form factor; only the glass panel. But it is somewhat difficult to find. ST7920 is bulkier, but abundant; it is used in the graphical interface of the Reprap Ramps 3D printer user interface, complete with a volume encoder! STN LCD is best in direct sunlight applications. Future blog posts will detail altimeters with those screens.  
Check back here for updates.

Disclaimer: Although this is an altimeter, those who build or use one accept the associated risks and are solely responsible for safe operation. Do not trust your life on it! It is not intended to be a replacement for certified or any other instrument for aircraft (but perhaps a coal stove).