How to use MS-A (azimuthal magnetic sensor) in your Arduino antenna rotator control projects

The MS-A sensor is a commerical product (Trademark 4O3A Signature) used for the 4O3A rotator genius antenna rotator. It might also be used standalone with Windows app. Then it´s connected to the computer using a USB-RS485 converter and showing the heading on a compass rose. However if you just want to use the magnetic sensor in your own project I´ll describe way to do so.

I´m working on a antenna rotator system using the wonderfull K3NG code with stepper motors to turn big antenna system. Although the K3NG code offers a lot of possibilities for sensors including magnetic sensors (magnetic compass) I gave the MS-A sensor a try. For homebrewing it´s a relatively expensive solution especially considering the quality of the housing. The advantage is that it´s using RS-485 instead of I2C which is only good for short cable length without additional relay drivers.

As there is no detailled information available the challenge was to access the sensor get out the heading information for further usage.

Here is what you have to do:

  1. Get a low cost UART – RS485 converter with DE/RE control. I recommend devices for 3.3 to 5V usage. Connect VCC, GND and DI (Pin 11), RO (Pin 10) and shortcut DE/RE and connect to Pin 3.
  2. Connect the terminals A/B at the converter to the to the A/B terminals at the MS-A sensor (no crossing). The sensor needs a ground connection to both the RS485 converter and a power supply delivering 12V to the sensor. Preferably use twisted wire (like CAT5) for this connection over long distances. That´s all hardware you need for this part of the project..

The attached code is polling the sensor about 3 times per second to retrieve the azimuth direction. The sensor needs a reguest string and replies with 5 bytes. The code is analyzing the sensors answer and converts it into a heading in degrees.The heading is also available in the serial monitor window for testing. Use the ‚degree‘ variable for integration in other software like K3NG.

Have fun! Hope this code becomes part of the K3NG distribution. (I´m not a coder – as you can easily see from the code. but its tested and works! )

Joe, DF9LJ

/* HOW TO USE 4O3A Rotator Genius (Tradenmark 4O3A) Sensor MS-A in 
 *  your Arduino environment
 *  Code uses basic elements from public code
   - Connect your Arduino (tested with Mega 2560) and cheap RS485 converter with DE/RE control
   - Code uses Pins 10, 11, Gnd for connection to RS485 converter
   - Pin 3 used for RS485 direction control (Pin assignments can be changed)
   - Output with 3Hz on Serial Monitor saying: "Grad: " which means degree in german
   - for usage in other code like K3NG just use the variable "degrees"
   - tested with 100ft non CAT wire. twisted CAT wires prefered
   - I´m not a coder. Any improvements or more elegant solutions are welcome
   73, Joe, DF9LJ
*/

/*----- Import needed libraries -----*/
#include <SoftwareSerial.h>
/*----- Declare Constants and Pin Numbers ----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin
#define SSerialTxControl 3   //RS485 Direction control

#define RS485Transmit    HIGH
#define RS485Receive     LOW

/*----- Declare objects -----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived[40];   // input buffer 
String req = "?0";      // string for polling magnetic sensor
int diff;               // difference between  byte 3 and byte 4
uint16_t degree;
int n=0;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Only for testing: Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("Testing magnetic sensor:");
  pinMode(SSerialTxControl, OUTPUT);
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver   
  // Start the software serial port, for RS485 connection
  RS485Serial.begin(4800);   // data rate of magnetic sensor 
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit   
    RS485Serial.print(req);          // request for data to magnetic sensor
    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit       
    delay(300);                       // wait for 300ms
  int i=0;
  while(RS485Serial.available()>0)  //Look for data from other Arduino
   {
      byteReceived[i] = RS485Serial.read();    // Read received byte
//      bb=byteReceived[i];
      if (i==5) {
         diff=abs((byteReceived[3])-(byteReceived[4]));
         if (diff==255) {
            diff=1;}
         if (diff <2){
            if (diff==1) {
                degree=(0x100+(byteReceived[3])); }
            else {
                degree=(0x00+(byteReceived[3])); }  // generating degrees > 255 degrees
            Serial.print("Grad:  "); 
            Serial.println(degree, DEC);  
           }
      }           // i==5 loop
      i++;
   }              // While Serial.RS484 Schleife
}//----- end main loop / end sketch-----

Just copy the ino file:

/* HOW TO USE 4O3A Rotator Genius (Tradenmark 4O3A) Sensor MS-A in 
 *  your Arduino environment
 *  Code uses basic elements from public code
   - Connect your Arduino (tested with Mega 2560) and cheap RS485 converter with DE/RE control
   - Code uses Pins 10, 11, Gnd for connection to RS485 converter
   - Pin 3 used for RS485 direction control (Pin assignments can be changed)
   - Output with 3Hz on Serial Monitor saying: "Grad: " which means degree in german
   - for usage in other code like K3NG just use the variable "degrees"
   - tested with 100ft non CAT wire. twisted CAT wires prefered
   - I´m not a coder. Any improvements or more elegant solutions are welcome
   73, Joe, DF9LJ
*/

/*----- Import needed libraries -----*/
#include <SoftwareSerial.h>
/*----- Declare Constants and Pin Numbers ----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin
#define SSerialTxControl 3   //RS485 Direction control

#define RS485Transmit    HIGH
#define RS485Receive     LOW

/*----- Declare objects -----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived[40];   // input buffer 
String req = "?0";      // string for polling magnetic sensor
int diff;               // difference between  byte 3 and byte 4
uint16_t degree;
int n=0;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Only for testing: Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("Testing magnetic sensor:");
  pinMode(SSerialTxControl, OUTPUT);
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver   
  // Start the software serial port, for RS485 connection
  RS485Serial.begin(4800);   // data rate of magnetic sensor 
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit   
    RS485Serial.print(req);          // request for data to magnetic sensor
    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit       
    delay(300);                       // wait for 300ms
  int i=0;
  while(RS485Serial.available()>0)  //Look for data from other Arduino
   {
      byteReceived[i] = RS485Serial.read();    // Read received byte
//      bb=byteReceived[i];
      if (i==5) {
         diff=abs((byteReceived[3])-(byteReceived[4]));
         if (diff==255) {
            diff=1;}
         if (diff <2){
            if (diff==1) {
                degree=(0x100+(byteReceived[3])); }
            else {
                degree=(0x00+(byteReceived[3])); }  // generating degrees > 255 degrees
            Serial.print("Grad:  "); 
            Serial.println(degree, DEC);  
           }
      }           // i==5 loop
      i++;
   }              // While Serial.RS484 Schleife
}//----- end main loop / end sketch-----

Links:

  • https://github.com/k3ng/k3ng_rotator_controller
  • https://www.electrodragon.com/product/max485-rs-485-module-ttl-to-rs-485/
  • https://4o3a.com/azimuth-magnetic-sensor


Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert