Hello!!

This post is the 1st of a 3 part series on motors. Motors are a necessity for almost any mechanical project. By definition, an electric motor is a machine that converts electrical energy to mechanical energy. Which for a DIY project bascially translates to: You need a motor if you want anything to move!

This post is about the most basic motor you can use in a project, a simple DC motor.

Introduction

(Excerpt from Wikipedia)
A simple DC motor has a stationary set of magnets in the stator and an armature with one more windings of insulated wire wrapped around a soft iron core that concentrates the magnetic field. The windings usually have multiple turns around the core, and in large motors there can be several parallel current paths. The ends of the wire winding are connected to a commutator. The commutator allows each armature coil to be energized in turn and connects the rotating coils with the external power supply through brushes. (Brushless DC motors have electronics that switch the DC current to each coil on and off and have no brushes.)

In plain english, a motor consists of an electromagnet inside a set of stationary permanent magnets, when current is applied to a part of the electromagnet, it causes an opposing polarity to the stationary magnet, which repel each other and make the motor move. If you want more technical detail, their are hundreds of resources on the internet!

In the context of the motors we will be using, the motor will rotate when we apply a DC voltage to the it, the direction of rotation depends on the poloarity of the voltage applied to it.

Things needed

  • DC motor
  • MCU
  • A motor driver board(L298/L9110)

Motor Driver Board

A motor driver board is just a small board that already has a motor driver IC and some protection components installed. It usually has 2 inputs per motor, and depending on whether those inputs are high or low, drive the motors in different directions. The one I will be using is this one I got from DX. It is based on the L9110/HG7881. It is capable of driving upto 2 DC motors. It is also very convenient as a PWM Signal can be applied to one of the input pins to control the speed of the motor, and the other input pin can be used as the direction control. It has a truth table that looks like this:

Input Output
IA IB OA OB Description
L L L L No movement
H L H L Forward
L H L H Backward
H H H H No Movement

Here, a PWM Signal can be applied to the IA pin for speed control, and the IB pin will decide the direction.

Setup

We will be setting this up in a very basic way. We will connect one DC motor to the Motor A input of the board and connect the input pins of the board like this:

HG7881 Pin Arduino Pin Description
1 NC Motor B IA
2 NC Motor B IB
3 GND Ground
4 5V VCC
5 11 Motor A IA
6 10 Motor A IB

As we are only using one motor, we can leave the Motor B pins not connected. My setup looks like this:

Thats all the connections we need to do.

Code

To make the code simple I have first defined 2 functions, move_forward(speed) and move_backward(speed). All these functions do is set the correct level on the direction pin, and apply the PWM according to the speed value. They both use a helper function check_speed(speed) that makes sure that the speed value is within the 0-255 limits. In the loop I just start a pattern of movement.



// connections
#define MOTOR_A_PWM 10 // Motor A PWM Pin
#define MOTOR_A_DIR 11 // Motor A Direction Pin

void setup()
{
  Serial.begin( 9600 );
  pinMode( MOTOR_A_DIR, OUTPUT );
  pinMode( MOTOR_A_PWM, OUTPUT );
  digitalWrite( MOTOR_A_DIR, LOW );
  digitalWrite( MOTOR_A_PWM, LOW );
}

int check_speed(int speed) {
  if(speed > 255)
    return 255;
  if(speed < 0)
    return 0;
  return speed;
}

void move_forward(int speed) {
  speed = check_speed(speed);
  digitalWrite( MOTOR_A_DIR, HIGH );
  analogWrite( MOTOR_A_PWM, 255 - speed );
}

void move_backward(int speed) {
  speed = check_speed(speed);
  digitalWrite( MOTOR_A_DIR, LOW );
  analogWrite( MOTOR_A_PWM, speed);
}

void loop()
{
  move_forward(250);
  delay(3000);
  move_forward(80);
  delay(3000);
  move_backward(250);
  delay(3000);
  move_backward(80);
  delay(3000);
}

A quick point to note, is that 1 direction on the motor will always run on the inverted duty cycle which is why we have 255-speed as the speed in the move_forward function.

Conclusion

And thats it!! You have the motor working in different directions at different speeds. You can now try adding another motor and applying the same concepts.