An S.PORT Compass for FrSky Radio Control Telemetry.

by blopa1961 in Circuits > Arduino

108 Views, 1 Favorites, 0 Comments

An S.PORT Compass for FrSky Radio Control Telemetry.

ATtinyCompass.jpg


Target audience:

Radio control hobbysts who would like to add Compass telemetry. It can be used with iNav LUA in EdgeTX radios.

Disclaimer:

No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.

In other words: use at YOUR OWN RISK.

License:

Attribution Non-commercial (by-nc)

Supplies

Nano.png
Back.jpg
Qmc5883L.jpg

for the Arduino Nano version:

  • Arduino Nano or Arduino Pro Micro
  • QMC5883L Magnetometer board
  • Dupont Female to Female cables
  • Dupont cable with a Futaba connector on one end and 3 female pins on the other

for the ATtiny 85 version (which requires a custom PCB):

  • ATtiny85 (SOP8)
  • Half height 7.3728MHz crystal
  • (2) 22pF 0805 capacitors
  • (3) 10K 0805 resistors
  • (1) 100nF capacitor
  • (1) FR4 single sided PCB (and etching materials)
  • (5) Dupont PCB male pins (may come included with the QMC5883)
  • (4) 90° Dupont PCB male pins
  • (1) short piece of cable
  • (1) Futaba male to male cable
  • Arduino UNO to be used as an arduino as ISP programmer

Warning: the QMC5883L chip is 3.6V max, so, if you build this project use a sensor like the one in the photos (which has a voltage regulator and works in 5V)

Arduino Nano Connections:

Nano QMC5883L

GND GND

5V VCC

A4 SDA

A5 SCL

Nano S.PORT

GND GND

VIN VCC

D2 Signal


CODE:

/*********************************************************
  @file       FrSkyMagnetometer.ino
  @brief      Simple S.PORT Compass telemetry for FrSky's receivers
  @author     Pablo Montoreano
  @copyright  2024 Pablo Montoreano
  @version    1.21 - 18/Apr/24

  Board Manager for ATtiny: ATtinyCore by Spence Konde
  Clock: 16MHz for Nano and Pro Micro, 7.3728MHz for ATtiny85
  libraries:
    S.PORT sensor library for FrSky by Herman Kruisman (https://github.com/RealTadango/FrSky)
    QMC5883LCompass library by mprograms (https://github.com/mprograms/QMC5883LCompass)

    Do NOT install the QMC5883L library, I'm mentioning it here to give the author due mention, but
    you will need to put the trimmed version included with this sketch in the same sketch folder
    Please notice that the #include for the library is between "" and not between <> (it's a subtle difference)

Connections for Arduino Nano
Nano  QMC5883L
---   --------
GND   GND
5V    VCC
A4    SDA
A5    SCL

Nano  S.PORT
----  ------
GND   GND
VIN   VCC
D2    Signal

Connections for ATtiny85 (untested)
Compile with NO BOOTLOADER (no room)
ATtiny85      QMC5883L
--------      --------
GND           GND
5V            VCC
PB0 (pin 5)   SDA
PB2 (pin 7)   SCL

ATtiny85      S.PORT
--------      ------
GND           GND
5V            VCC
PB1 (pin 6)   Signal
*********************************************************/

#include <SPort.h>
#include "QMC5883LCompass.h"

#if (defined(__AVR_ATmega328P__) || defined(__AVR_ATmega32U4__))
SPortHub hub(0x12, 2);    // Hardware ID 0x12, Software serial pin 2 for Arduino Nano and Pro Micro
#endif

#ifdef  __AVR_ATtiny85__
SPortHub hub(0x12, PB1);  // Hardware ID 0x12, Software serial pin PB1 for ATtiny
#endif

SimpleSPortSensor sensor(0x0840);   // Sensor with ID 0x840 "Hdg" expects positive values 0 to 35999, an unsigned int (last 2 digits are decimals)
QMC5883LCompass compass;            // QMC5883L Magnetometer

// replace myNorth by 180, 270, 360 or 450 for different sensor positions
static const unsigned int myNorth= 360;   // using 360 the radio connector points South when Hdg is 0
static const unsigned long pollInterval= 200;  // poll Mag every 200mS
static const unsigned long longInterval= 600;
static unsigned long lastPoll;   // last time Magnetometer was polled
static unsigned long milliLoop;  // millis value for current loop
static unsigned int Hdg= 0;

void setup() {
  compass.init();
  hub.registerSensor(sensor);       // add Hdg sensor to the hub
  lastPoll= millis() - longInterval;  // force an update on first loop
  hub.begin();
}

void loop() {
  milliLoop= millis();
  if ((milliLoop - lastPoll) >= pollInterval) { // update sensor every pollInterval
    lastPoll= milliLoop;
    compass.read();
    Hdg= compass.getAzimuth() + myNorth; // Azimuth returns values -180 to 180, adjust to positive in one of the 4 possible positions
    if (Hdg >= 360) Hdg-= 360;
    Hdg*= 100;  // 2 decimals in Hdg telemetry
    sensor.value = Hdg;
  }
  hub.handle();
}


The code will work as it is with the unmodified QMC library if you use an Arduino Nano, but you will need copy the attached trimmed version in the sketch folder if you use the ATtiny85 version (do NOT install the QMC library if using an ATtiny85).

If you would like to improve precision, I suggest you analyze the full QMC library, do a calibration and save the obtained data hardcoded in the source code as there is no room for the calibration routine in the ATtiny. You should also add the declination for your location to the code.


Libraries used:

"S.Port sensor library for FrSky" by Herman Kruisman and "QMC5883LCompass" by MPrograms

To compile the ATtiny85 version you will also need to install the ATTinyCore (by Spence Konde) in Boards Manager



Schematic.jpg
TinyCompass.jpg
Components.jpg
TinyDevice.jpg

These photos contain the Schematic, PCB (top view), component mount (bottom view), and prototype for the ATtiny85 version.

Please notice there's no room in the ATtiny85 for a bootloader. The lonely pin in the center is connected to the ATtiny's /RST pin and used for the Arduino UNO as a programmer.

ATtiny connections:

ATtiny85 QMC5883L

GND GND

5V VCC

PB0 (pin 5) SDA

PB2 (pin 7) SCL

ATtiny85 S.PORT

GND GND

5V VCC

PB1 (pin 6) Signal