Example Code for Arduino-DC and Stepper Motor Drive
FireBeetle Covers-DC Motor&Stepper Driver can control a stepper motor and 2 DC motors simultaneously and let them work independent to each other. The test explains the way to control both stepper motor and DC motors at the same time. Program Function: group A stepper motors (M1A(A+), M1B(A-), M2A(B+), M2B(B-)) reverse in every 3s and group 4 DC motor (M4A(+), M4B(-))reverse in every 1.5s.
Hardware Preparation
- 1 x ESP32/ESP8266 Board
- 1 x FireBeetle Covers-DC Motor&Stepper Driver
- 1 x Hybrid Stepper Motor for 3D Printer (3.5kg)
- 1 x DC Motor
Software Preparation
- Arduino IDE V1.6.x Click to Download Arduino IDE from Arduino®
- Please download and install FireBeetle Covers-DC Motor&Stepper Driver library files.
- About Library installation.
Wiring Diagram
Connect the stepper motor to STEPPER1 and DC motor to M4, shown as below.

Other Preparation Work
Connect the stepper motor to STEPPER1 and DC motor to M4.
Sample Code
*!
* @file Motor_Stepper.ino
* @brief DFRobot's Motor Drive
* @n The example demonstrates one group of stepping motor and 2 groups
of D.C motors work together at the same time, contains direction control
of D.C motors and stepping motor.
* @copyright [DFRobot](https://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
* @author [LiXin]
* @version V1.0
* @date 2017-7-31
* @https://github.com/DFRobot/DFRobot_MotorStepper
*/
#include "Arduino.h"
#include "Wire.h"
#include "DFRobot_MotorStepper.h"
/*****************Keywords instruction*****************/
//M3--->motor_Group_3--->[M3A(+),M3B(-)]
//M4--->motor_Group_4--->[M4A(+),M4B(-)]
//SA--->Stepper_Group_A--->[M1A(A+),M1B(A-),M2A(B+),M2B(B-)]
//CW: rotate in positive direction
//CCW: rotate in reverse
//A0: Chip Selection Address 1
//A1: Chip Selection Address 2
//A2: Chip Selection Address 3
//A3: Chip Selection Address 4
/*****************Function instruction*****************/
//void start(float angle, uint16_t speed, uint8_t dir)
//*This function can be used to start the motor
//*angle: Set the Angle Min:0°(If the Angle is equal to 0°,The motor will not stop spinning)
//*speed: Set the speed Min:8
//*dir: Set Orientation CW or CCW
//void getDir()
//*This function can get the current rotation direction of the motor
DFRobot_Stepper stepperA(SA,A0);
DFRobot_Motor motor4(M4,A0);
void reverse_4()
{
motor4.start(!motor4.getDir());
}
void reverse_SA()
{
stepperA.start(0, 12, !stepperA.getDir());
}
void setup() {
//initialize serial communication at 9600 bits per second:
Serial.begin(115200);
//join i2c bus (address optional for master)
Wire.begin();
//Initialize motor drive chip of stepping motor (Group A) and ¾ group of D.C motor
stepperA.init();
motor4.init();
//Set the initial direction
motor4.start(CW);
stepperA.start(0, 12, CW);
delay(1500);
}
void loop()
{
static int i=0;
//motor4 reverse in every 1.5 seconds
reverse_4();
//Stepping motor (Group A) reverse in every 3 seconds
if((++i%2) == 0) {
reverse_SA();
i=0;
}
delay(1500);
}
Result
Group A stepper motors (M1A(A+), M1B(A-), M2A(B+), M2B(B-)) reverse in every 3s and group 4 DC motor (M4A(+), M4B(-))reverse in every 1.5s.
Additional Information
Functions:
Create a stepper motor object.
DFRobot_Stepper stepperA(SA): SA represents group A stepper motors (M1A (A+), M1B (A-), M2A (B+), M2B(B-))
Create a DC motor object.
DFRobot_Motor (M4): M4 represents group 4 DC motors (M4A (+), M4B (-))
Initiate the motor drive, read Product ID and Version ID, and print to the serial port.
init()
Set the rotation direction of the DC motor and start to rotate.
start(dir) dir=CW: rotate in C.W.(Clockwise) dir=CCW: rotate in anti-clockwise
Set angle, speed, direction of the stepper motor rotation and start to rotate.
angle: set the rotation resolution. The maximum resolution is 0.9°, which means the minimum rotate angle is 0.9°.
So that the motor won’t rotate if the resolution set to be 0.1° and only rotate to 0.9° if the resolution set to be 1°.
Min: 0° (if the rotate angel set to be 0, the motor will keep rotating).
speed: set speed. The speed range is 0~1023. Set it to 0 and the rotate speed is 0r/min; set it to maximum 1023 and the rotate speed is 200r/min.
dir: set direction. dir=CW: rotate in C.W.(Clockwise) ; dir=CCW: rotate in anti-clockwise.
The reverse function of group 4 DC motor.
reverse_4()
The reverse function of group A stepper motor.
reverse_SA()
Was this article helpful?
