Example Code for Arduino-DC Motor Drive

FireBeetle Covers-DC Motor&Stepper Driver can control up to 4-channel DC motors simultaneously. There are 4 marked motor connectors: M1A&M1B, M2A&M2B, M3A&M3B and M4A&M4B. We’ll show you how to control a DC motor with a Firebeetle or Arduino board. Program Function: the first group rotate in C.W.(Clockwise) for 2s and rotate in anti-clockwise for 2s, then stop for 2s and followed by recycling.

Hardware Preparation

  • 1 x ESP32/ESP8266 Board
  • 1 x FireBeetle Covers-DC Motor&Stepper Driver
  • 1 x DC Motor

Software Preparation

Wiring Diagram

Connect the DC motor to M1A&M1B port, M1A to anode and M1B to cathode. Generally, you need to connect a motor power (4~12V) to Motor PWR port, and connect a logic power (3.3~5V) to VCC & GND pins. If you use FireBeetle, you can plug the cover on the board, and connect the external power (4~12V).

dc motor

Other Preparation Work

Connect the DC motor to M1A&M1B port, M1A to anode and M1B to cathode. Generally, you need to connect a motor power (4~12V) to Motor PWR port, and connect a logic power (3.3~5V) to VCC & GND pins. If you use FireBeetle, you can plug the cover on the board, and connect the external power (4~12V).

Sample Code

/*!
 * @file Motor.ino
 * @brief DFRobot's Motor Drive
 * @n The example demonstrates four groups D.C motors work together
      at the same time, contains operations such as rotation, speed
      adjustment, brakes and so on.

 * @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*****************/
//M1--->motor_Group_1--->[M1A(+),M1B(-)]
//M2--->motor_Group_2--->[M2A(+),M1B(-)]
//M3--->motor_Group_3--->[M3A(+),M3B(-)]
//M4--->motor_Group_4--->[M4A(+),M4B(-)]
//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(uint8_t dir)
  //*This function can be used to start the motor
  //*dir: Set Orientation       CW or CCW
//void speed(uint16_t val)
  //*This function is used to set the motor speed
  //*val: Set the speed       0<=val<=4096
//void stop()
  //*This function is used to stop the motor turning

DFRobot_Motor motor1(M1,A0);


void setup() {
  //initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
  //join i2c bus (address optional for master)
  Wire.begin();
  //Initialize D.C motor drive chip (Group1)
  motor1.init();
}

void loop()
{
  //Setting initial velocity(Min:0  Max:4096)
  motor1.speed(4096);
  //Motor1 rotate in positive direction
  motor1.start(CW);
  delay(2000);

  //All motors rotate in reverse
  motor1.start(CCW);
  delay(2000);

  //All motors brake and stop rotating
  motor1.stop();
  delay(2000);
}

Result

The first group rotate in C.W.(Clockwise) for 2s and rotate in anti-clockwise for 2s, then stop for 2s and followed by recycling.

Additional Information

Functions:

Create a motor object

DFRobot_Motor motor1(M1)  M1 represents the first group DC motor M1A(+),M1B(-)

Initiate the motor drive, read Product ID and Version ID, and print to the serial port.

 init()

Set speed

 speed(val)  0<=val<=4096(the maximum speed is 4096)

Set direction and start to rotate

start(dir)  dir=CW: rotate in C.W.(Clockwise) dir=CCW: rotate in anti-clockwise

Was this article helpful?

TOP