# How To Use EPMC With Arduino

<figure><img src="/files/6S43J3OUVGrs3GPghpWN" alt=""><figcaption></figcaption></figure>

### EPMC Module I2C Connection Overview

When using the **EPMC** with I2C, you'll need to power the EPMC module via the power port (7v to 12v), while the **5V3** pin of the **EPMC** I2C port will be connected to the Arduino-based microcontroller power for reference during I2C communication.

<figure><img src="/files/ajuqibw7hf7PjTkFcTDl" alt=""><figcaption></figcaption></figure>

For example, if it is connected to an ESP32 microcontroller, the **5V3** pin of the **EPMC** I2C port will be connected to the ESP32 3.3v power (while the EPMC module is powered via the power port (7v to 12v)).

<figure><img src="/files/S9TXTnkAfo1pTTYEivRN" alt=""><figcaption></figcaption></figure>

Also, if it is connected to an Arduino UNO, MEGA, or NANO, the **5V3** pin of the **EPMC** I2C port will be connected to the Arduino microcontroller’s 5V power (while the EPMC module is powered via the power port (7v to 12v)).

<figure><img src="/files/uEOrRmHk6cvYFshOlUio" alt=""><figcaption></figcaption></figure>

Don’t forget to ensure the **EPMC** module and the Arduino microcontroller are connected to a common ground.

### EPMC Arduino Test Code - PID Motor Control

* Ensure you have your **Geared DC motors** connected to the **EPMC Module** and the motor **PID gains** already set up. If not, follow the instructions here \[[link to the post](https://robocre8.gitbook.io/robocre8/eimu-tutorials/how-to-calibrate-and-setup-the-eimu)]
* Download the **epmc\_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 **`epmc_arduino`**
* Move the downloaded library file - **`epmc_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 **EPMC** Module (feel free to modify it to your taste).

<mark style="color:purple;">**pid\_motor\_control.ino**</mark>

```cpp
/*
 * Basic example code on how to control via I2C your geared DC motor with quadrature
 * encoder which is already connected to the Easy PID Motor Controller module and have already
 * succesfully set up their velocity PID control using the epmc_setup_application
 *
 * The code basically sends a low target velocity (in rad/s), waits for some time and then
 * sends a high target velocity (in rad/s). it also prints out the motors' angular positions
 * (in rad) and angular velocities (in rad/s).
 *
 * you can copy the code and use it in your project as you will.
 */

// Easy PID Motor Control i2c communication library
#include <epmc.h>

int epmcAddress = 1; // set this address to the same address you have during setup via the GUI app
EPMC motor(epmcAddress);

///////// my sepcial delay function ///////////////
void delayMs(int ms)
{
  for (int i = 0; i < ms; i += 1)
  {
    delayMicroseconds(1000);
  }
}
//////////////////////////////////////////////////

bool isSuccessful;
float angPosA, angPosB; // motorA, motorB (in rad)
float angVelA, angVelB; // motorA, motorB (in rad/sec)

float lowTargetVel = -3.142;  // rad/sec
float highTargetVel = 3.142; // rad/sec
bool sendHigh = true;

long prevTime;
long sampleTime = 100; // millisec

long ctrlPrevTime;
long ctrlSampleTime = 5000; // millisec

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

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

  // wait for the driver module to fully setup (5 to 10 sec)
  for (int i = 0; i <= 6; i += 1)
  {
    delayMs(1000);
    Serial.println(i);
  }
  motor.sendTargetVel(0.00, 0.00); // targetA, targetB
  // int cmd_vel_timeout = 2000; // 0 to deactivate.
  // motor.setCmdTimeout(cmd_vel_timeout); // set motor command velocity timeout
  // motor.getCmdTimeout(cmd_vel_timeout); // get the stored command velocity timeout
  // Serial.print("motor command vel timeout in ms: ");
  // Serial.println(cmd_vel_timeout);

  motor.sendTargetVel(lowTargetVel, lowTargetVel); // targetA, targetB
  sendHigh = true;

  prevTime = millis();
  ctrlPrevTime = millis();
}

void loop()
{
  if ((millis() - ctrlPrevTime) >= ctrlSampleTime)
  {
    if (sendHigh)
    {
      motor.sendTargetVel(highTargetVel, highTargetVel); // targetA, targetB
      sendHigh = false;
    }
    else
    {
      motor.sendTargetVel(lowTargetVel, lowTargetVel); // targetA, targetB
      sendHigh = true;
    }
    ctrlPrevTime = millis();
  }

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

    motor.getMotorsPos(angPosA, angPosB); // gets angPosA, angPosB
    motor.getMotorsVel(angVelA, angVelB); // gets angVelA, angVelB

    Serial.print(angPosA, 3);
    Serial.print(", ");
    Serial.println(angVelA, 3);

    Serial.print(angPosB, 3);
    Serial.print(", ");
    Serial.println(angVelB, 3);

    Serial.println();

    prevTime = millis();
  }
}
```

* watch the motors controlled precisely
* read output from serial monitor.
* adjust the code to your taste and use.

<figure><img src="/files/UDbrAGTh65QACGFuMku4" 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-epmc-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.
