Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.
This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.
Hardware Required
- Arduino Uno or Mega
- USB Cable for Arduino.
- PHPoC Shield R2 or PHPoC WiFi Shield R2
- DC Motor Controller - PES-2604
- A DC motor with encoder
- Power supply for DC motors (DC Adapter 4 ~ 18V)
- Power supply for Arduino (USB or DC adapter)
Note that: we can use DC motor controller to control:
- DC motor with two-phase encoder.
- DC motor with one-phase encoder. However, in this case, we cannot get the rotating direction of DC motor.
- DC motor without encoder. However, in this case, we cannot get position, speed, and rotating direction of DC motor.
Note that: Power supply for Arduino and power supply for DC motor should be separate. In case of DC motor requires very low current and low voltage, it can be use the same power with Arduino.
Wiring
- Stack PHPoC Shield or PHPoC WiFi Shield on Arduino
- Stack DC motor controller PES-2604 on PHPoC Shield or PHPoC WiFi Shield
- Connect the terminal block of DC motor controller to DC motor and power supply for DC motor as follows:
- Terminal Block - VM <----> (+) wire - power supply for DC motor
- Terminal Block - GND <----> (-) wire - power supply for DC motor
- Terminal Block - M1+ <----> (+) wire - DC motor
- Terminal Block - M1- <----> (-) wire - DC motor
- (Optional) Connect the encoder port of DC motor controller to encoder pin of DC motor as follows:
- Encoder 1 - 5V <----> Vcc - Encoder of DC motor
- Encoder 1 - GND <----> GND - Encoder of DC motor
- Encoder 1 - 1A <----> A phase - Encoder of DC motor
- Encoder 1 - 1B <----> B phase - Encoder of DC motor
Encoder 1 is located on the upper row of encoder port. More detail is here

The DC motor controller can connect and control/monitor two DC motors simultaneously. This tutorial present the use of one DC motor. Adding the another DC motor is similar.
Install Arduino IDE
If you have not install Arduino IDE yet, please download and install Arduino IDE .
Install Library
- Run Arduino IDE.
- Navigate to Sketch > Include Library > Manage Libraries
- Search "Phpoc Expansion" on search bar of the Library Manager.
- Select the PhpocExpansion library and press the [Install] button.
- Restart Arduino IDE for the next step.
Source Code
- Connect Arduino to PC via usb cable
- Open Arduino IDE
- Open "ExpansionDCMotor" example
- Compile the example code and uploads to Arduino by clicking "Upload" button on Arduino IDE
- Open "Serial Monitor" tool on Arduino IDE to see the output log:
(position, period and speed depend on motor, encoder and power supply )
Code Explanation
At the beginning
we need to create and DC motor object:
Code:
ExpansionDCMotor dcm1(expansionId, 1);
You can change it and map it to the below to know the value.
Note that: DC Motor Controller is one of the expansion boards. List of expansion for arduino is available here
In setup() function
- Phpoc.begin() and Expansion.begin() functions must be used to initial PHPoC [WiFi] Shield and expansion board, repectively.
- Serial.println(dcm1.getName()) function is used optionally to get name of expansion board and print it to serial.
- There are some functions has been commented.
Code://dcm1.setPolarity(-1); //dcm1.setDirection(-1); //dcm1.setDecay(1); //dcm1.setEncoderPolarity(-1); //dcm1.setEncoderPosition(100000); //dcm1.setEncoderPSR(32); //dcm1.setFilterFrequency(5000); //dcm1.setFilterPNC(0);
- dcm1.setPeriod(10000); function is used to set PWM period.
In loop() function
There are some functions are used.
- dcm1.setWidth(width): set duration of the high-voltage level in a PWM cycle to control DC motor. If width is 0, DC motor stops rotating.
Duty cycle of PWM = width in setWidth() / period in setPeriod(). - dcm1.getEncoderPosition(): get encoder position, unit is pulse
- dcm1.getEncoderPeriod(): get encoder period, unit is microsecond per pulse.
when encoder period is known, speed of DC motor can be calculated as follows:
speed = 1000000 / period
Unit is pulse per second.
See Also
- Arduino - Relay Expansion Board - PES-2601
- Arduino - Digital Input Expansion Board - PES-2602
- Arduino - DC Motor Controller - PES-2604
- Arduino - Stepper Motor Controller - PES-2605
- Arduino - RS-232 Expansion Board - PES-2606
- Arduino - RS-422 Expansion Board - PES-2607
- Arduino - RS-485 Expansion Board - PES-2607
References