Using CyberAtom AHRS with Arduino

This tutoral describes how-to connect and program Arduino UNO with CyberAtom AHRS device using I2C interface. We will also use Arduino IDE to compile and upload program.

Equipment

  • Arduino UNO (other types will do too)
  • CyberAtom X-200 device (X-201 or others with I2C interface will do too)
  • Cables to connect the boards together.

Connectivity

The diagram below demonstrates how-to connect two boards together using I2C bus and power line.

Arduino off course needs to be connected to PC (USB port).

Once Arduino board is powered ON and CyberAtom is connected as above, the LED indicator on the AHRS device should regularly blink indicating normal operation mode.

Software

We will use GET_QUAT_DATA message (as in User Manual for CyberAtom device) over I2C interface and process retrieved float data, then send it to serial port and observe on Arduino's Serial Monitor.

in setup part we need to enable Wire and Serial interfaces.

void setup() {
  Wire.begin();         // Arduino will act as a master device
  Serial.begin(115200); // used for logging
}

In the main loop we will repeatedly do the following sequence:

  • send GET_QUAT_DATA request message to CyberAtom device
  • retrieve sensor's orientation data represented by a quaternion (of floats)
  • send results to PC using Serial interface

Before we fill `loop()` with code, we can define some useful constants:

// CyberAtom has address of 0x30 by default, 
// Arduino uses 7-bit addresses so we need 
// right-shift that value by 1 bit.
const char cyberAtomDevice = 0x18; 
 
// this is message id as in User Manual
const char GET_QUAT_DATA = 0x02;    

// response is 16 bytes (4 x sizeof(float))
const char QUAT_DATA_SIZE = 16;   

Please note that Arduino uses 7-bit addresses for I2C devices, so it that convention, default I2C address of CyberAtom AHRS device is 0x18.

The first thing would be like this:

    ...
    Wire.beginTransmission(cyberAtomDevice);   
    Wire.write(GET_QUAT_DATA);               
    Wire.endTransmission();
    ...

The we will read AHRS' response bytes:

    ...
    Wire.requestFrom(cyberAtomDevice, QUAT_DATA_SIZE);
    int i=0;
    char response[16];
    while (Wire.available()) {
        response[i++] = Wire.read();
    }
    ...

The complete code can be [downloaded from here].

Once we're done with receiving data, we can display the results:

    ...

    float* quaternion = (float*)response;
    Serial.print("Q: ");
    Serial.print(quaternion[0]); Serial.print(", ");
    Serial.print(quaternion[1]); Serial.print(", ");
    Serial.print(quaternion[2]); Serial.print(", ");
    Serial.print(quaternion[3]); Serial.println();
    ...

Observing Results

We can now verify and upload the code to the Arduino board.

To observe the orientation results, we can open Serial Monitor window (with 115200 bauds). We can see quaternion components (rounded values):

Q: 1.0, 0.01, 0.02, -0.01
Q: 1.0, 0.02, 0.02, -0.01
Q: 1.0, 0.02, 0.01, -0.02
Q: 1.0, 0.01, 0.01, -0.02
...

Summary

CyberAtom AHRS devices offer simple I2C interface that can be used with Arduino to retrieve sensor's orientation data. With Wire library, this will take literally couple of lines of code. Following the pattern described in this article, it's possible to easily receive other output data, e.g. Euler angles by switching to GET_EULER_DATA message for CyberAtom device.