DC Motor Control Class for Arduino

One of the most rewarding aspects about building robots is understanding and designing individuals components, getting them to work and finally putting everything together into a more complex machine.

DC motor control under test

DC motor control under test

One of the key design aspects I am trying to learn is component re-usability. I don’t know what the technical term for this is, but in essence it means that I need to build components that are generic and can be reused in different robots with little or no modifications needed. This concept is very similar to Classes in Object Oriented Programming, something that I am trying to learn as well.

So, to build a balancing robot, I first needed to develop a motor control mechanism that would balance the robot in an upright position using only two wheels. After a little experimenting, I became quite comfortable with the L293N motor driver IC, and made a short video on how this device can be used to control 2 DC motors. Now I needed to design the software that would control the motors. The motors themselves would just be rotating based on electrical impulses and would not know or care whether they were balancing a glass of milk or carting vegetables to market.

The specs I wanted for my software that would control a pair of DC motors mounted on opposite sides of a chassis:-

DC Motor Control. These are the generic controls I needed to implement.

DC Motor Control. These are the generic controls I needed to implement.

1. Move forward/ backward
2. Turn left and right on the spot
3. Do a gradual turn while moving forward/ backward
4. Accelerate/Decelerate  to a given speed in a given time
5. Stop

Only 1 and 5 are needed for a balancing application, the rest add to the “generic” functionality of the class in motor control.

These have been implemented in a c++ class I wrote that will work for Arduino devices using the L293N or L293D motor driver ICs or any other similar setup that uses 2 input pins and 1 enable pin. Using this class, any number of motor pairs can be created and controlled. A total of 2 digital pins and 1 PWM pin are required for each motor. The digital pins are used to control direction of rotation and the PWM pin is used for speed control. I made this example circuit using Fritzing. Zoom in for a more detailed view. Then entire schematic can be downloaded here.

Fritzing Schematic for DC Motor Pair Controlled by L293N and Arduino UNO

Fritzing Schematic for DC Motor Pair Controlled by L293N and Arduino UNO

The member functions of my c++ class are:-

BOMotorPair(int, int, int, int ,int, int);
void SetMoveDirection(int);
void SetMoveSpeed(int);
void SetTurnSpeed(int, int);
void SetWheelSpeedDifferential(int, int);
void Move();
void Move(int);
void Stop();
void Accelerate(int s1, int s2, int accel);
void Accelerate(int finalSpeed, int delayRate);

Most of the member functions of this class are self-explanatory, the only thing that perhaps needs to be explained a little more are the arguments to the Constructor


These are :-

a and b – digital input pins for motor1
c – PWM pin for motor1

d and e – digital input pins for motor2
f – PWM pin for motor2

My BOMotorPair class can be downloaded from here. To use this, just extract the contents and copy the BOMotorPair folder into your /arduino/libraries folder. You can then include it into your Arduino sketches and control a pair of DC motors! If you like it and find it useful, please share it with your friends. Obviously there is still a lot of functionality that will be added to this class, and I will update it as soon as I make any changes.


4 thoughts on “DC Motor Control Class for Arduino

  1. Pingback: Arduino with L293N Motor Driver | Bayesian Adventures

  2. Pingback: Balance Good, Everything Good | Bayesian Adventures

  3. Pingback: DIY Servo Motor: Cheap and with Plenty Muscle! | Bayesian Adventures

  4. Pingback: Experimental Robotic Platform ERP1 : Part 2 – Electronics Integration | Bayesian Adventures

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s