none

Antenna Angle Sensor



Introduction

I have an antenna in my attic to receive digital HD signals.  It is mounted on a rotor for 360 degree pointing.  When I bought the system, I expected some kind of remote panel that allows me to know the antenna angle.  But the control was super simple and only single channel.  Pressing the button causes the antenna to pan left-right, reversing direction only when the end of travel is hit.  I was essentially running the system blind with only the image quality as the indicator.  Since we use over a dozen over-the-air channels, it would be nice to know the pointing angle.


Antenna in my attic that is rotor mounted.  Cost was about $25.  Power to
the motor is transferred via the coax up the mast, and there is a amplifier
box in the attic that inserts the motor power and boosts the signal.

Sensor using a three axis magnetometer

I tried to figure out several ways to sense the pointing angle, and thought of a potentiometer somehow, or some kind of optical sensor.  I finally decided to use a three-axis magnetometer.  These are based on the HMC5883 sensor, and can be commonly found as the GY-271 or the GY-273.  These interface via I2C.  Since my favorite microcontroller (Microchip PIC) and C compiler (PCM) has support for this, it was a good fit.

As for the hardware, I decided to retrofit an original MPLAB-ICD programmer as the starting point.  I added a programming header, and converted the small linear voltage regulator for 5V output.  I then reused the RJ12 connector as the main connector and could use some unused phone cable (4 conductor) to connect to the sensor.  Two wires would be for power, and two for RS-232 comm.  The MPLAB-ICD board already has a RS-232 translator, so almost all of the board's hardware was reused.

This was my first I2C project in a long time, and I learned each device has an address and returns an ACK.  My initial interface attempts were not very successful, so I decided to purchase a small logic analyzer for assistance.  I have my large HP unit, but it is old, and does not decode serial protocols.  I decided to obtain the USB 8-channel unit from Sparkfun for less than $30.  With this tool, I could see that there was a problem with the 'stop' on the I2C interface.  For some reason, the magnetometer was pulling the SDA line low at the end of a read.  I was never able to resolve that, so I decided instead to power cycle the sensor at the end of each data read cycle.


Logic Analyzer display on Pulseview.

The above shows how handy it is to have the serial interface be decoded automatically.  On the left, we can see the S symbol for I2C Start (SCK goes low after SDA goes low).  We then see the next 8 bits for the Address and the Write command (small W).  The magnetometer acknowledges with a good ACK (small A).  There is then a write of four data bytes and their ACKs.  This is concluded with a good Stop (P).  We can see both SCK and SDA going high for the Stop.


Compass sensor mounted on the MPLAB-ICD board.

Hard to see in the image above, but the GY-271 was glued to the RJ12 jack at the bottom (small green board).  As a result, the X and Z axis of the sensor see the changing magnetic field of the Earth as the board is rotated along the long axis of the tube that the board is fastened to.


Graph of the X and X axis outputs when the sensor is rotated 360 degrees.
The Y axis hardly changes and is not shown.
Horizontal axis is degrees rotation.  Vertical are raw units of the sensor.

I observed that the Z-axis peaks when the top of the entire board faces North, and the X-axis peaks when it is facing East.  The software on the PIC can be commanded via the serial line, and when an 's' is sent, the code will continuously scan and hold the max and min values of the X and Z axes.  During this time, the antenna is slewed left-right over its full range.  Upon getting a 'w', it will then save these values in on-chip EEPROM.  These are retrieved on power up.  While in use, the code continuously polls the magnetometer, and then compares the values read to the saved max/mins to calculate the X and Z coordinate (scale of -1 to +1).  I then wrote my own atan2 function that is lean and efficient (compared to the normal atan2), to calculate the four quadrant antenna heading.  Once powered up, or when commanded, the angle is streamed continuously on the RS-232 line as an ASCII printable string of bytes.  The value 0.0 is North, and the value increases as the antenna rotates to the East.  A full turn produces the value of 360.

About 75% of the code space of the PIC16F876 is used with the code.  RAM utilization is low at about 15%.  Floating point is used for the calculations, a nice feature of the PCM compiler.

Sensor Installed

I installed some additional 2x4 beams into the attic beams to allow easy access to the top of the antenna.  The sensor was then clipped onto the back of the array with the top of the board facing the pointy end of the Yagi array.


I clipped the sensor on the back of the antenna array.  Due to poor lighting,
it is difficult to see the sensor board.

I intended to run a long phone cable to my office from the sensor, but changed my mind once I got into the attic.  I decided instead to use Bluetooth Low Energy, specifically Adafruit's LE Friend.  The BLE transceiver was put in the attic and powered from there.  I then loaded the Bluefruit Connect app onto my phone, and could communicate with the compass sensor anywhere in my house.  See here for my blog on wireless comm for microprocessors.

The future use of this device will be for me to do a survey of the optimal orientation for each of our normally viewed stations, and to see if there is a direction that would allow more or other stations.  If we then need to view those for a particular event, I can jog the antenna to the temporary direction as needed.  It may be possible for me to replace the simple button with a controller that automatically goes to a preset direction upon command.  Having all the TV signals on one coax and in HD suits our lifestyle.  This way any TV in the house can see different HD sources and not pay individual HD box subscriptions.

Project Log

  • May 2020 - Attic antenna installed with rotor.  Purchased on Ebay.
  • October 2025 - Installed compass sensor.

Back Home

(c) 2025 Edward Cheung, all rights reserved.