# How To Use EIMU With Arduino

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FpwFQUbZ91ydm4FRKFrhc%2Feimu_pic_arduino.png?alt=media&#x26;token=ad012398-7511-474f-aee6-8c18ddadb5db" alt=""><figcaption></figcaption></figure>

### EIMU Module I2C Connection Overview

When using the **EIMU** with I2C, you’ll need to power it from its 5V input port, while the **5V3** pin of the **EIMU** I2C port will be connected to the Arduino-based microcontroller power for reference during I2C communication.

For example, if it is connected to an ESP32 microcontroller, the **5V3** pin of the **EIMU** I2C port will be connected to the ESP32 3.3v power (while the EIMU power port will use 5V).

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FYdLqjWllbUUImRPrSZHq%2Feimu_esp_web_pic.png?alt=media&#x26;token=3e76703d-e849-494c-a6e5-4a0fc07f7bc5" alt=""><figcaption></figcaption></figure>

Also, if it is connected to an Arduino UNO, MEGA, or NANO, the **5V3** pin of the **EIMU** I2C port will be connected to the Arduino microcontroller’s 5V power (the EIMU power port will still use 5V, most likely also from the microcontroller also).

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FxZxRgtPiK5pJlU4y3Ghc%2Feimu_uno_web_pic.png?alt=media&#x26;token=1b20c466-57c9-4278-be84-8fa72ab8ef11" alt=""><figcaption></figcaption></figure>

Don’t forget to ensure the **EIMU** and the Arduino-based microcontroller are connected to a common ground.

### EIMU Arduino Test Code - Read RPY

* Ensure you have calibrated and setup the EIMU I2C address following this [tutorial](https://robocre8.gitbook.io/robocre8/eimu-tutorials/how-to-calibrate-and-setup-the-eimu).
* Download the **eimu\_arduino** I2C comm library from [here](https://github.com/robocre8/eimu_arduino) by clicking on the green Code button.

  > if you download it, extract it and change the folder name to **`eimu_arduino`**
* Move the downloaded library file - **`eimu_arduino`** - to your Arduino library folder

  > e.g on linux: ... home/Arduino/libraries/
  >
  > e.g on windows: ... Documents/Arduino/libraries/
  >
  > (or any where your arduino libraries are stored)
* restart your ArduinoIDE and copy this example code into your project to test the **EIMU** Module (feel free to modify it to your taste).

<mark style="color:purple;">**read\_rpy.ino**</mark>

```cpp
/*
 * Basic example code shows how to read orientation data from the MPU9250 EIMU Module
 * which have been succesfully calibrated with filter and covariances setup
 *
 * The code basically reads roll, pitch, and yaw values from the MPU9250 EIMU Module connected to it.
 * read printed values from serial monitor or serial plotter.
 *
 * you can copy the code and use it in your project as you will.
 */

// Easy IMU i2c communication library
#include <eimu.h>

// please update with the address with that which you set when doing
// calibration and filter setup with the eimu_setup_application
uint8_t imuAddress = 104; // i.e 0x68 in HEX
EIMU imu(imuAddress);

float toRad = 2 * PI / 360;
float toDeg = 1 / toRad;

float roll, pitch, yaw; // create variables to store orientations
String ref_frame;

long prevSampleTime;
long sampleTime = 100; // millisec

void setup()
{
  // start i2c communication
  Wire.begin();

  // setup serial communication to print result on serial minitor
  Serial.begin(115200);

  // wait for the imu module to fully setup
  for (int i = 1; i <= 6; i += 1)
  {
    delay(1000);
    Serial.println(i);
  }

  // change the reference frame to ENU frame (0 - NWU,  1 - ENU,  2 - NED)
  imu.setRefFrame(1);

  // check the refence frame the IMU is working in (0 - NWU,  1 - ENU,  2 - NED)
  int ref_frame_id;
  imu.getRefFrame(ref_frame_id);
  if (ref_frame_id == 0)
    Serial.println("Reference Frame is North-West-Up (NWU)");
  else if (ref_frame_id == 1)
    Serial.println("Reference Frame is East-North-Up (ENU)");
  else if (ref_frame_id == 2)
    Serial.println("Reference Frame is North-East-Down (NED)");

  // wait for the imu module to fully setup
  for (int i = 1; i <= 4; i += 1)
  {
    delay(1000);
    Serial.println(i);
  }

  prevSampleTime = millis();
}

void loop()
{

  if ((millis() - prevSampleTime) >= sampleTime)
  {
    /* CODE SHOULD GO IN HERE*/

    imu.getRPY(roll, pitch, yaw); // read roll, pitch, yaw in radians

    Serial.print(roll, 4);
    Serial.print(", ");
    Serial.print(pitch, 4);
    Serial.print(", ");
    Serial.println(yaw, 4);

    // Serial.print(roll * toDeg, 1);
    // Serial.print(", ");
    // Serial.print(pitch * toDeg, 1);
    // Serial.print(", ");
    // Serial.println(yaw * toDeg, 1);

    prevSampleTime = millis();
  }
}
```

* read output from serial monitor.
* adjust the code to your taste and use.

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FTBX3BwUu00D6MXt9zUnr%2Fsic_i2c_comm_arduino.gif?alt=media&#x26;token=a28811cc-6d2d-4179-87d9-552bfc67a46f" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://robocre8.gitbook.io/robocre8/tutorials/how-to-use-eimu-with-arduino.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
